12b271ae8fa3093ab7f2ab0deea46b3c8aee30dd
[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         // <summary>
1457         //   This represents a typecast in the source language.
1458         //
1459         //   FIXME: Cast expressions have an unusual set of parsing
1460         //   rules, we need to figure those out.
1461         // </summary>
1462         public class Cast : Expression {
1463                 string target_type;
1464                 Expression expr;
1465                         
1466                 public Cast (string cast_type, Expression expr)
1467                 {
1468                         this.target_type = cast_type;
1469                         this.expr = expr;
1470                 }
1471
1472                 public string TargetType {
1473                         get {
1474                                 return target_type;
1475                         }
1476                 }
1477
1478                 public Expression Expr {
1479                         get {
1480                                 return expr;
1481                         }
1482                         set {
1483                                 expr = value;
1484                         }
1485                 }
1486                 
1487                 public override Expression Resolve (TypeContainer tc)
1488                 {
1489                         type = tc.LookupType (target_type, false);
1490                         eclass = ExprClass.Value;
1491                         
1492                         if (type == null)
1493                                 return null;
1494
1495                         expr = ConvertExplicit (tc, expr, type);
1496                         
1497                         return expr;
1498                 }
1499
1500                 public override void Emit (EmitContext ec)
1501                 {
1502                         //
1503                         // This one will never happen
1504                         //
1505                         throw new Exception ("Should not happen");
1506                 }
1507         }
1508
1509         public class Binary : Expression {
1510                 public enum Operator {
1511                         Multiply, Division, Modulus,
1512                         Addition, Subtraction,
1513                         LeftShift, RightShift,
1514                         LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual, 
1515                         Equality, Inequality,
1516                         BitwiseAnd,
1517                         ExclusiveOr,
1518                         BitwiseOr,
1519                         LogicalAnd,
1520                         LogicalOr
1521                 }
1522
1523                 Operator oper;
1524                 Expression left, right;
1525                 MethodBase method;
1526                 ArrayList  Arguments;
1527                 Location   location;
1528                 
1529
1530                 public Binary (Operator oper, Expression left, Expression right, Location loc)
1531                 {
1532                         this.oper = oper;
1533                         this.left = left;
1534                         this.right = right;
1535                         this.location = loc;
1536                 }
1537
1538                 public Operator Oper {
1539                         get {
1540                                 return oper;
1541                         }
1542                         set {
1543                                 oper = value;
1544                         }
1545                 }
1546                 
1547                 public Expression Left {
1548                         get {
1549                                 return left;
1550                         }
1551                         set {
1552                                 left = value;
1553                         }
1554                 }
1555
1556                 public Expression Right {
1557                         get {
1558                                 return right;
1559                         }
1560                         set {
1561                                 right = value;
1562                         }
1563                 }
1564
1565
1566                 // <summary>
1567                 //   Returns a stringified representation of the Operator
1568                 // </summary>
1569                 string OperName ()
1570                 {
1571                         switch (oper){
1572                         case Operator.Multiply:
1573                                 return "*";
1574                         case Operator.Division:
1575                                 return "/";
1576                         case Operator.Modulus:
1577                                 return "%";
1578                         case Operator.Addition:
1579                                 return "+";
1580                         case Operator.Subtraction:
1581                                 return "-";
1582                         case Operator.LeftShift:
1583                                 return "<<";
1584                         case Operator.RightShift:
1585                                 return ">>";
1586                         case Operator.LessThan:
1587                                 return "<";
1588                         case Operator.GreaterThan:
1589                                 return ">";
1590                         case Operator.LessThanOrEqual:
1591                                 return "<=";
1592                         case Operator.GreaterThanOrEqual:
1593                                 return ">=";
1594                         case Operator.Equality:
1595                                 return "==";
1596                         case Operator.Inequality:
1597                                 return "!=";
1598                         case Operator.BitwiseAnd:
1599                                 return "&";
1600                         case Operator.BitwiseOr:
1601                                 return "|";
1602                         case Operator.ExclusiveOr:
1603                                 return "^";
1604                         case Operator.LogicalOr:
1605                                 return "||";
1606                         case Operator.LogicalAnd:
1607                                 return "&&";
1608                         }
1609
1610                         return oper.ToString ();
1611                 }
1612
1613                 Expression ForceConversion (TypeContainer tc, Expression expr, Type target_type)
1614                 {
1615                         if (expr.Type == target_type)
1616                                 return expr;
1617
1618                         return ConvertImplicit (tc, expr, target_type);
1619                 }
1620                 
1621                 //
1622                 // Note that handling the case l == Decimal || r == Decimal
1623                 // is taken care of by the Step 1 Operator Overload resolution.
1624                 //
1625                 void DoNumericPromotions (TypeContainer tc, Type l, Type r)
1626                 {
1627                         if (l == TypeManager.double_type || r == TypeManager.double_type){
1628                                 //
1629                                 // If either operand is of type double, the other operand is
1630                                 // conveted to type double.
1631                                 //
1632                                 if (r != TypeManager.double_type)
1633                                         right = ConvertImplicit (tc, right, TypeManager.double_type);
1634                                 if (l != TypeManager.double_type)
1635                                         left = ConvertImplicit (tc, left, TypeManager.double_type);
1636                                 
1637                                 type = TypeManager.double_type;
1638                         } else if (l == TypeManager.float_type || r == TypeManager.float_type){
1639                                 //
1640                                 // if either operand is of type float, th eother operand is
1641                                 // converd to type float.
1642                                 //
1643                                 if (r != TypeManager.double_type)
1644                                         right = ConvertImplicit (tc, right, TypeManager.float_type);
1645                                 if (l != TypeManager.double_type)
1646                                         left = ConvertImplicit (tc, left, TypeManager.float_type);
1647                                 type = TypeManager.float_type;
1648                         } else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){
1649                                 //
1650                                 // If either operand is of type ulong, the other operand is
1651                                 // converted to type ulong.  or an error ocurrs if the other
1652                                 // operand is of type sbyte, short, int or long
1653                                 //
1654                                 Type other = null;
1655                                 
1656                                 if (l == TypeManager.uint64_type)
1657                                         other = r;
1658                                 else if (r == TypeManager.uint64_type)
1659                                         other = l;
1660
1661                                 if ((other == TypeManager.sbyte_type) ||
1662                                     (other == TypeManager.short_type) ||
1663                                     (other == TypeManager.int32_type) ||
1664                                     (other == TypeManager.int64_type)){
1665                                         string oper = OperName ();
1666                                         
1667                                         tc.RootContext.Report.Error (34, "Operator `" + OperName ()
1668                                                                      + "' is ambiguous on operands of type `"
1669                                                                      + TypeManager.CSharpName (l) + "' "
1670                                                                      + "and `" + TypeManager.CSharpName (r)
1671                                                                      + "'");
1672                                 }
1673                                 type = TypeManager.uint64_type;
1674                         } else if (l == TypeManager.int64_type || r == TypeManager.int64_type){
1675                                 //
1676                                 // If either operand is of type long, the other operand is converted
1677                                 // to type long.
1678                                 //
1679                                 if (l != TypeManager.int64_type)
1680                                         left = ConvertImplicit (tc, left, TypeManager.int64_type);
1681                                 if (r != TypeManager.int64_type)
1682                                         right = ConvertImplicit (tc, right, TypeManager.int64_type);
1683
1684                                 type = TypeManager.int64_type;
1685                         } else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){
1686                                 //
1687                                 // If either operand is of type uint, and the other
1688                                 // operand is of type sbyte, short or int, othe operands are
1689                                 // converted to type long.
1690                                 //
1691                                 Type other = null;
1692                                 
1693                                 if (l == TypeManager.uint32_type)
1694                                         other = r;
1695                                 else if (r == TypeManager.uint32_type)
1696                                         other = l;
1697
1698                                 if ((other == TypeManager.sbyte_type) ||
1699                                     (other == TypeManager.short_type) ||
1700                                     (other == TypeManager.int32_type)){
1701                                         left = ForceConversion (tc, left, TypeManager.int64_type);
1702                                         right = ForceConversion (tc, right, TypeManager.int64_type);
1703                                         type = TypeManager.int64_type;
1704                                 } else {
1705                                         //
1706                                         // if either operand is of type uint, the other
1707                                         // operand is converd to type uint
1708                                         //
1709                                         left = ForceConversion (tc, left, TypeManager.uint32_type);
1710                                         right = ForceConversion (tc, left, TypeManager.uint32_type);
1711                                         type = TypeManager.uint32_type;
1712                                 } 
1713                         } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
1714                                 if (l != TypeManager.decimal_type)
1715                                         left = ConvertImplicit (tc, left, TypeManager.decimal_type);
1716                                 if (r != TypeManager.decimal_type)
1717                                         right = ConvertImplicit (tc, right, TypeManager.decimal_type);
1718
1719                                 type = TypeManager.decimal_type;
1720                         } else {
1721                                 left = ForceConversion (tc, left, TypeManager.int32_type);
1722                                 right = ForceConversion (tc, right, TypeManager.int32_type);
1723                                 type = TypeManager.int32_type;
1724                         }
1725                 }
1726
1727                 void error19 (TypeContainer tc)
1728                 {
1729                         tc.RootContext.Report.Error (
1730                                 19,
1731                                 "Operator " + OperName () + " cannot be applied to operands of type `" +
1732                                 TypeManager.CSharpName (left.Type) + "' and `" +
1733                                 TypeManager.CSharpName (right.Type) + "'");
1734                                                      
1735                 }
1736                 
1737                 Expression CheckShiftArguments (TypeContainer tc)
1738                 {
1739                         Expression e;
1740                         Type l = left.Type;
1741                         Type r = right.Type;
1742
1743                         e = ForceConversion (tc, right, TypeManager.int32_type);
1744                         if (e == null){
1745                                 error19 (tc);
1746                                 return null;
1747                         }
1748                         right = e;
1749
1750                         if (((e = ConvertImplicit (tc, left, TypeManager.int32_type)) != null) ||
1751                             ((e = ConvertImplicit (tc, left, TypeManager.uint32_type)) != null) ||
1752                             ((e = ConvertImplicit (tc, left, TypeManager.int64_type)) != null) ||
1753                             ((e = ConvertImplicit (tc, left, TypeManager.uint64_type)) != null)){
1754                                 left = e;
1755
1756                                 return this;
1757                         }
1758                         error19 (tc);
1759                         return null;
1760                 }
1761                 
1762                 Expression ResolveOperator (TypeContainer tc)
1763                 {
1764                         Type l = left.Type;
1765                         Type r = right.Type;
1766
1767                         //
1768                         // Step 1: Perform Operator Overload location
1769                         //
1770                         Expression left_expr, right_expr;
1771                         
1772                         string op = "op_" + oper;
1773
1774                         left_expr = MemberLookup (tc.RootContext, l, op, false);
1775
1776                         right_expr = MemberLookup (tc.RootContext, r, op, false);
1777
1778                         MethodGroupExpr union = Invocation.MakeUnionSet (left_expr, right_expr);
1779
1780                         Arguments = new ArrayList ();
1781                         Arguments.Add (new Argument (left, Argument.AType.Expression));
1782                         Arguments.Add (new Argument (right, Argument.AType.Expression));
1783                         
1784                         if (union != null) {
1785                                 method = Invocation.OverloadResolve (tc, union, Arguments, location);
1786                                 if (method != null) {
1787                                         MethodInfo mi = (MethodInfo) method;
1788                                         
1789                                         type = mi.ReturnType;
1790                                         return this;
1791                                 }
1792                         }       
1793                         
1794                         //
1795                         // Step 2: Default operations on CLI native types.
1796                         //
1797                         
1798                         // Only perform numeric promotions on:
1799                         // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
1800                         //
1801                         if (oper == Operator.LeftShift || oper == Operator.RightShift){
1802                                 return CheckShiftArguments (tc);
1803                         } else if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
1804
1805                                 if (l != TypeManager.bool_type || r != TypeManager.bool_type)
1806                                         error19 (tc);
1807                         } else
1808                                 DoNumericPromotions (tc, l, r);
1809
1810                         if (left == null || right == null)
1811                                 return null;
1812
1813                         if (oper == Operator.BitwiseAnd ||
1814                             oper == Operator.BitwiseOr ||
1815                             oper == Operator.ExclusiveOr){
1816                                 if (!((l == TypeManager.int32_type) ||
1817                                       (l == TypeManager.uint32_type) ||
1818                                       (l == TypeManager.int64_type) ||
1819                                       (l == TypeManager.uint64_type))){
1820                                         error19 (tc);
1821                                         return null;
1822                                 }
1823                         }
1824
1825                         if (oper == Operator.Equality ||
1826                             oper == Operator.Inequality ||
1827                             oper == Operator.LessThanOrEqual ||
1828                             oper == Operator.LessThan ||
1829                             oper == Operator.GreaterThanOrEqual ||
1830                             oper == Operator.GreaterThan){
1831                                 type = TypeManager.bool_type;
1832                         }
1833                         
1834                         return this;
1835                 }
1836                 
1837                 public override Expression Resolve (TypeContainer tc)
1838                 {
1839                         left = left.Resolve (tc);
1840                         right = right.Resolve (tc);
1841
1842                         if (left == null || right == null)
1843                                 return null;
1844
1845                         return ResolveOperator (tc);
1846                 }
1847
1848                 public bool IsBranchable ()
1849                 {
1850                         if (oper == Operator.Equality ||
1851                             oper == Operator.Inequality ||
1852                             oper == Operator.LessThan ||
1853                             oper == Operator.GreaterThan ||
1854                             oper == Operator.LessThanOrEqual ||
1855                             oper == Operator.GreaterThanOrEqual){
1856                                 return true;
1857                         } else
1858                                 return false;
1859                 }
1860
1861                 // <summary>
1862                 //   This entry point is used by routines that might want
1863                 //   to emit a brfalse/brtrue after an expression, and instead
1864                 //   they could use a more compact notation.
1865                 //
1866                 //   Typically the code would generate l.emit/r.emit, followed
1867                 //   by the comparission and then a brtrue/brfalse.  The comparissions
1868                 //   are sometimes inneficient (there are not as complete as the branches
1869                 //   look for the hacks in Emit using double ceqs).
1870                 //
1871                 //   So for those cases we provide EmitBranchable that can emit the
1872                 //   branch with the test
1873                 // </summary>
1874                 public void EmitBranchable (EmitContext ec, int target)
1875                 {
1876                         OpCode opcode;
1877                         bool close_target = false;
1878                         
1879                         left.Emit (ec);
1880                         right.Emit (ec);
1881                         
1882                         switch (oper){
1883                         case Operator.Equality:
1884                                 if (close_target)
1885                                         opcode = OpCodes.Beq_S;
1886                                 else
1887                                         opcode = OpCodes.Beq;
1888                                 break;
1889
1890                         case Operator.Inequality:
1891                                 if (close_target)
1892                                         opcode = OpCodes.Bne_Un_S;
1893                                 else
1894                                         opcode = OpCodes.Bne_Un;
1895                                 break;
1896
1897                         case Operator.LessThan:
1898                                 if (close_target)
1899                                         opcode = OpCodes.Blt_S;
1900                                 else
1901                                         opcode = OpCodes.Blt;
1902                                 break;
1903
1904                         case Operator.GreaterThan:
1905                                 if (close_target)
1906                                         opcode = OpCodes.Bgt_S;
1907                                 else
1908                                         opcode = OpCodes.Bgt;
1909                                 break;
1910
1911                         case Operator.LessThanOrEqual:
1912                                 if (close_target)
1913                                         opcode = OpCodes.Ble_S;
1914                                 else
1915                                         opcode = OpCodes.Ble;
1916                                 break;
1917
1918                         case Operator.GreaterThanOrEqual:
1919                                 if (close_target)
1920                                         opcode = OpCodes.Bge_S;
1921                                 else
1922                                         opcode = OpCodes.Ble;
1923                                 break;
1924
1925                         default:
1926                                 throw new Exception ("EmitBranchable called on non-EmitBranchable operator: "
1927                                                      + oper.ToString ());
1928                         }
1929
1930                         ec.ig.Emit (opcode, target);
1931                 }
1932                 
1933                 public override void Emit (EmitContext ec)
1934                 {
1935                         ILGenerator ig = ec.ig;
1936                         Type l = left.Type;
1937                         Type r = right.Type;
1938                         OpCode opcode;
1939
1940                         if (method != null) {
1941
1942                                 // Note that operators are static anyway
1943                                 
1944                                 if (Arguments != null) 
1945                                         Invocation.EmitArguments (ec, method, Arguments);
1946                                 
1947                                 if (method is MethodInfo)
1948                                         ig.Emit (OpCodes.Call, (MethodInfo) method);
1949                                 else
1950                                         ig.Emit (OpCodes.Call, (ConstructorInfo) method);
1951
1952                                 return;
1953                         }
1954                         
1955                         left.Emit (ec);
1956                         right.Emit (ec);
1957
1958                         switch (oper){
1959                         case Operator.Multiply:
1960                                 if (ec.CheckState){
1961                                         if (l == TypeManager.int32_type || l == TypeManager.int64_type)
1962                                                 opcode = OpCodes.Mul_Ovf;
1963                                         else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
1964                                                 opcode = OpCodes.Mul_Ovf_Un;
1965                                         else
1966                                                 opcode = OpCodes.Mul;
1967                                 } else
1968                                         opcode = OpCodes.Mul;
1969
1970                                 break;
1971
1972                         case Operator.Division:
1973                                 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
1974                                         opcode = OpCodes.Div_Un;
1975                                 else
1976                                         opcode = OpCodes.Div;
1977                                 break;
1978
1979                         case Operator.Modulus:
1980                                 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
1981                                         opcode = OpCodes.Rem_Un;
1982                                 else
1983                                         opcode = OpCodes.Rem;
1984                                 break;
1985
1986                         case Operator.Addition:
1987                                 if (ec.CheckState){
1988                                         if (l == TypeManager.int32_type || l == TypeManager.int64_type)
1989                                                 opcode = OpCodes.Add_Ovf;
1990                                         else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
1991                                                 opcode = OpCodes.Add_Ovf_Un;
1992                                         else
1993                                                 opcode = OpCodes.Mul;
1994                                 } else
1995                                         opcode = OpCodes.Add;
1996                                 break;
1997
1998                         case Operator.Subtraction:
1999                                 if (ec.CheckState){
2000                                         if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2001                                                 opcode = OpCodes.Sub_Ovf;
2002                                         else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2003                                                 opcode = OpCodes.Sub_Ovf_Un;
2004                                         else
2005                                                 opcode = OpCodes.Sub;
2006                                 } else
2007                                         opcode = OpCodes.Sub;
2008                                 break;
2009
2010                         case Operator.RightShift:
2011                                 opcode = OpCodes.Shr;
2012                                 break;
2013                                 
2014                         case Operator.LeftShift:
2015                                 opcode = OpCodes.Shl;
2016                                 break;
2017
2018                         case Operator.Equality:
2019                                 opcode = OpCodes.Ceq;
2020                                 break;
2021
2022                         case Operator.Inequality:
2023                                 ec.ig.Emit (OpCodes.Ceq);
2024                                 ec.ig.Emit (OpCodes.Ldc_I4_0);
2025                                 
2026                                 opcode = OpCodes.Ceq;
2027                                 break;
2028
2029                         case Operator.LessThan:
2030                                 opcode = OpCodes.Clt;
2031                                 break;
2032
2033                         case Operator.GreaterThan:
2034                                 opcode = OpCodes.Cgt;
2035                                 break;
2036
2037                         case Operator.LessThanOrEqual:
2038                                 ec.ig.Emit (OpCodes.Cgt);
2039                                 ec.ig.Emit (OpCodes.Ldc_I4_0);
2040                                 
2041                                 opcode = OpCodes.Ceq;
2042                                 break;
2043
2044                         case Operator.GreaterThanOrEqual:
2045                                 ec.ig.Emit (OpCodes.Clt);
2046                                 ec.ig.Emit (OpCodes.Ldc_I4_1);
2047                                 
2048                                 opcode = OpCodes.Sub;
2049                                 break;
2050
2051                         case Operator.LogicalOr:
2052                         case Operator.BitwiseOr:
2053                                 opcode = OpCodes.Or;
2054                                 break;
2055
2056                         case Operator.LogicalAnd:
2057                         case Operator.BitwiseAnd:
2058                                 opcode = OpCodes.And;
2059                                 break;
2060
2061                         case Operator.ExclusiveOr:
2062                                 opcode = OpCodes.Xor;
2063                                 break;
2064
2065                         default:
2066                                 throw new Exception ("This should not happen: Operator = "
2067                                                      + oper.ToString ());
2068                         }
2069
2070                         ig.Emit (opcode);
2071                 }
2072         }
2073
2074         public class Conditional : Expression {
2075                 Expression expr, trueExpr, falseExpr;
2076                 
2077                 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr)
2078                 {
2079                         this.expr = expr;
2080                         this.trueExpr = trueExpr;
2081                         this.falseExpr = falseExpr;
2082                 }
2083
2084                 public Expression Expr {
2085                         get {
2086                                 return expr;
2087                         }
2088                 }
2089
2090                 public Expression TrueExpr {
2091                         get {
2092                                 return trueExpr;
2093                         }
2094                 }
2095
2096                 public Expression FalseExpr {
2097                         get {
2098                                 return falseExpr;
2099                         }
2100                 }
2101
2102                 public override Expression Resolve (TypeContainer tc)
2103                 {
2104                         // FIXME: Implement;
2105                         throw new Exception ("Unimplemented");
2106                         // return this;
2107                 }
2108
2109                 public override void Emit (EmitContext ec)
2110                 {
2111                 }
2112         }
2113
2114         public class SimpleName : Expression {
2115                 public readonly string Name;
2116                 public readonly Location Location;
2117                 
2118                 public SimpleName (string name, Location l)
2119                 {
2120                         Name = name;
2121                         Location = l;
2122                 }
2123
2124                 //
2125                 // Checks whether we are trying to access an instance
2126                 // property, method or field from a static body.
2127                 //
2128                 Expression MemberStaticCheck (Report r, Expression e)
2129                 {
2130                         if (e is FieldExpr){
2131                                 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2132                                 
2133                                 if (!fi.IsStatic){
2134                                         r.Error (120,
2135                                                  "An object reference is required " +
2136                                                  "for the non-static field `"+Name+"'");
2137                                         return null;
2138                                 }
2139                         } else if (e is MethodGroupExpr){
2140                                 // FIXME: Pending reorganization of MemberLookup
2141                                 // Basically at this point we should have the
2142                                 // best match already selected for us, and
2143                                 // we should only have to check a *single*
2144                                 // Method for its static on/off bit.
2145                                 return e;
2146                         } else if (e is PropertyExpr){
2147                                 if (!((PropertyExpr) e).IsStatic){
2148                                         r.Error (120,
2149                                                  "An object reference is required " +
2150                                                  "for the non-static property access `"+
2151                                                  Name+"'");
2152                                         return null;
2153                                 }
2154                         }
2155
2156                         return e;
2157                 }
2158                 
2159                 //
2160                 // 7.5.2: Simple Names. 
2161                 //
2162                 // Local Variables and Parameters are handled at
2163                 // parse time, so they never occur as SimpleNames.
2164                 //
2165                 Expression ResolveSimpleName (TypeContainer tc)
2166                 {
2167                         Expression e;
2168                         Report r = tc.RootContext.Report;
2169
2170                         e = MemberLookup (tc.RootContext, tc.TypeBuilder, Name, true);
2171                         if (e != null){
2172                                 if (e is TypeExpr)
2173                                         return e;
2174                                 else if (e is FieldExpr){
2175                                         FieldExpr fe = (FieldExpr) e;
2176
2177                                         if (!fe.FieldInfo.IsStatic)
2178                                                 fe.Instance = new This ();
2179                                 }
2180                                 
2181                                 if ((tc.ModFlags & Modifiers.STATIC) != 0)
2182                                         return MemberStaticCheck (r, e);
2183                                 else
2184                                         return e;
2185                         }
2186
2187                         //
2188                         // Do step 3 of the Simple Name resolution.
2189                         //
2190                         // FIXME: implement me.
2191
2192                         r.Error (103, Location, "The name `" + Name + "' does not exist in the class `" +
2193                                  tc.Name + "'");
2194
2195                         return null;
2196                 }
2197                 
2198                 //
2199                 // SimpleName needs to handle a multitude of cases:
2200                 //
2201                 // simple_names and qualified_identifiers are placed on
2202                 // the tree equally.
2203                 //
2204                 public override Expression Resolve (TypeContainer tc)
2205                 {
2206                         if (Name.IndexOf (".") != -1)
2207                                 return ResolveMemberAccess (tc, Name);
2208                         else
2209                                 return ResolveSimpleName (tc);
2210                 }
2211
2212                 public override void Emit (EmitContext ec)
2213                 {
2214                         throw new Exception ("SimpleNames should be gone from the tree");
2215                 }
2216         }
2217
2218         // <summary>
2219         //   A simple interface that should be implemeneted by LValues
2220         // </summary>
2221         public interface LValue {
2222                 void Store (ILGenerator ig);
2223         }
2224         
2225         public class LocalVariableReference : Expression, LValue {
2226                 public readonly string Name;
2227                 public readonly Block Block;
2228                 
2229                 public LocalVariableReference (Block block, string name)
2230                 {
2231                         Block = block;
2232                         Name = name;
2233                         eclass = ExprClass.Variable;
2234                 }
2235
2236                 public VariableInfo VariableInfo {
2237                         get {
2238                                 return Block.GetVariableInfo (Name);
2239                         }
2240                 }
2241                 
2242                 public override Expression Resolve (TypeContainer tc)
2243                 {
2244                         VariableInfo vi = Block.GetVariableInfo (Name);
2245
2246                         type = vi.VariableType;
2247                         return this;
2248                 }
2249
2250                 public override void Emit (EmitContext ec)
2251                 {
2252                         VariableInfo vi = VariableInfo;
2253                         ILGenerator ig = ec.ig;
2254                         int idx = vi.Idx;
2255
2256                         switch (idx){
2257                         case 0:
2258                                 ig.Emit (OpCodes.Ldloc_0);
2259                                 break;
2260                                 
2261                         case 1:
2262                                 ig.Emit (OpCodes.Ldloc_1);
2263                                 break;
2264
2265                         case 2:
2266                                 ig.Emit (OpCodes.Ldloc_2);
2267                                 break;
2268
2269                         case 3:
2270                                 ig.Emit (OpCodes.Ldloc_3);
2271                                 break;
2272
2273                         default:
2274                                 if (idx < 255)
2275                                         ig.Emit (OpCodes.Ldloc_S, idx);
2276                                 else
2277                                         ig.Emit (OpCodes.Ldloc, idx);
2278                                 break;
2279                         }
2280                 }
2281
2282                 public void Store (ILGenerator ig)
2283                 {
2284                         VariableInfo vi = VariableInfo;
2285                         int idx = vi.Idx;
2286                                         
2287                         switch (idx){
2288                         case 0:
2289                                 ig.Emit (OpCodes.Stloc_0);
2290                                 break;
2291                                 
2292                         case 1:
2293                                 ig.Emit (OpCodes.Stloc_1);
2294                                 break;
2295                                 
2296                         case 2:
2297                                 ig.Emit (OpCodes.Stloc_2);
2298                                 break;
2299                                 
2300                         case 3:
2301                                 ig.Emit (OpCodes.Stloc_3);
2302                                 break;
2303                                 
2304                         default:
2305                                 if (idx < 255)
2306                                         ig.Emit (OpCodes.Stloc_S, idx);
2307                                 else
2308                                         ig.Emit (OpCodes.Stloc, idx);
2309                                 break;
2310                         }
2311                 }
2312         }
2313
2314         public class ParameterReference : Expression, LValue {
2315                 public readonly Parameters Pars;
2316                 public readonly String Name;
2317                 public readonly int Idx;
2318                 
2319                 public ParameterReference (Parameters pars, int idx, string name)
2320                 {
2321                         Pars = pars;
2322                         Idx  = idx;
2323                         Name = name;
2324                         eclass = ExprClass.Variable;
2325                 }
2326
2327                 public override Expression Resolve (TypeContainer tc)
2328                 {
2329                         Type [] types = Pars.GetParameterInfo (tc);
2330
2331                         type = types [Idx];
2332
2333                         return this;
2334                 }
2335
2336                 public override void Emit (EmitContext ec)
2337                 {
2338                         if (Idx < 255)
2339                                 ec.ig.Emit (OpCodes.Ldarg_S, Idx);
2340                         else
2341                                 ec.ig.Emit (OpCodes.Ldarg, Idx);
2342                 }
2343
2344                 public void Store (ILGenerator ig)
2345                 {
2346                         if (Idx < 255)
2347                                 ig.Emit (OpCodes.Starg_S, Idx);
2348                         else
2349                                 ig.Emit (OpCodes.Starg, Idx);
2350                         
2351                 }
2352         }
2353         
2354         // <summary>
2355         //   Used for arguments to New(), Invocation()
2356         // </summary>
2357         public class Argument {
2358                 public enum AType {
2359                         Expression,
2360                         Ref,
2361                         Out
2362                 };
2363
2364                 public readonly AType Type;
2365                 Expression expr;
2366
2367                 public Argument (Expression expr, AType type)
2368                 {
2369                         this.expr = expr;
2370                         this.Type = type;
2371                 }
2372
2373                 public Expression Expr {
2374                         get {
2375                                 return expr;
2376                         }
2377
2378                         set {
2379                                 expr = value;
2380                         }
2381                 }
2382
2383                 public bool Resolve (TypeContainer tc)
2384                 {
2385                         expr = expr.Resolve (tc);
2386
2387                         return expr != null;
2388                 }
2389
2390                 public void Emit (EmitContext ec)
2391                 {
2392                         expr.Emit (ec);
2393                 }
2394         }
2395
2396         // <summary>
2397         //   Invocation of methods or delegates.
2398         // </summary>
2399         public class Invocation : ExpressionStatement {
2400                 public readonly ArrayList Arguments;
2401                 public readonly Location Location;
2402                 
2403                 Expression expr;
2404                 MethodBase method = null;
2405                         
2406                 static Hashtable method_parameter_cache;
2407
2408                 static Invocation ()
2409                 {
2410                         method_parameter_cache = new Hashtable ();
2411                 }
2412                         
2413                 //
2414                 // arguments is an ArrayList, but we do not want to typecast,
2415                 // as it might be null.
2416                 //
2417                 // FIXME: only allow expr to be a method invocation or a
2418                 // delegate invocation (7.5.5)
2419                 //
2420                 public Invocation (Expression expr, ArrayList arguments, Location l)
2421                 {
2422                         this.expr = expr;
2423                         Arguments = arguments;
2424                         Location = l;
2425                 }
2426
2427                 public Expression Expr {
2428                         get {
2429                                 return expr;
2430                         }
2431                 }
2432
2433                 /// <summary>
2434                 ///   Computes whether Argument `a' and the Type t of the  ParameterInfo `pi' are
2435                 ///   compatible, and if so, how good is the match (in terms of
2436                 ///   "better conversions" (7.4.2.3).
2437                 ///
2438                 ///   0   is the best possible match.
2439                 ///   -1  represents a type mismatch.
2440                 ///   -2  represents a ref/out mismatch.
2441                 /// </summary>
2442                 static int Badness (Argument a, Type t)
2443                 {
2444                         Expression argument_expr = a.Expr;
2445                         Type argument_type = argument_expr.Type;
2446
2447                         if (argument_type == null){
2448                                 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
2449                         }
2450                         
2451                         if (t == argument_type) 
2452                                 return 0;
2453
2454                         //
2455                         // Now probe whether an implicit constant expression conversion
2456                         // can be used.
2457                         //
2458                         // An implicit constant expression conversion permits the following
2459                         // conversions:
2460                         //
2461                         //    * A constant-expression of type `int' can be converted to type
2462                         //      sbyte, byute, short, ushort, uint, ulong provided the value of
2463                         //      of the expression is withing the range of the destination type.
2464                         //
2465                         //    * A constant-expression of type long can be converted to type
2466                         //      ulong, provided the value of the constant expression is not negative
2467                         //
2468                         // FIXME: Note that this assumes that constant folding has
2469                         // taken place.  We dont do constant folding yet.
2470                         //
2471
2472                         if (argument_type == TypeManager.int32_type && argument_expr is IntLiteral){
2473                                 IntLiteral ei = (IntLiteral) argument_expr;
2474                                 int value = ei.Value;
2475                                 
2476                                 if (t == TypeManager.sbyte_type){
2477                                         if (value >= SByte.MinValue && value <= SByte.MaxValue)
2478                                                 return 1;
2479                                 } else if (t == TypeManager.byte_type){
2480                                         if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2481                                                 return 1;
2482                                 } else if (t == TypeManager.short_type){
2483                                         if (value >= Int16.MinValue && value <= Int16.MaxValue)
2484                                                 return 1;
2485                                 } else if (t == TypeManager.ushort_type){
2486                                         if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2487                                                 return 1;
2488                                 } else if (t == TypeManager.uint32_type){
2489                                         //
2490                                         // we can optimize this case: a positive int32
2491                                         // always fits on a uint32
2492                                         //
2493                                         if (value >= 0)
2494                                                 return 1;
2495                                 } else if (t == TypeManager.uint64_type){
2496                                         //
2497                                         // we can optimize this case: a positive int32
2498                                         // always fits on a uint64
2499                                         //
2500                                         if (value >= 0)
2501                                                 return 1;
2502                                 }
2503                         } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
2504                                 LongLiteral ll = (LongLiteral) argument_expr;
2505
2506                                 if (t == TypeManager.uint64_type)
2507                                         if (ll.Value > 0)
2508                                                 return 1;
2509                         }
2510                         
2511                         // FIXME: Implement user-defined implicit conversions here.
2512                         // FIXME: Implement better conversion here.
2513                         
2514                         return -1;
2515                 }
2516
2517                 // <summary>
2518                 //   Returns the Parameters (a ParameterData interface) for the
2519                 //   Method `mb'
2520                 // </summary>
2521                 static ParameterData GetParameterData (MethodBase mb)
2522                 {
2523                         object pd = method_parameter_cache [mb];
2524
2525                         if (pd != null)
2526                                 return (ParameterData) pd;
2527
2528                         if (mb is MethodBuilder || mb is ConstructorBuilder){
2529                                 MethodCore mc = TypeContainer.LookupMethodByBuilder (mb);
2530
2531                                 InternalParameters ip = mc.ParameterInfo;
2532                                 method_parameter_cache [mb] = ip;
2533
2534                                 return (ParameterData) ip;
2535                         } else {
2536                                 ParameterInfo [] pi = mb.GetParameters ();
2537                                 ReflectionParameters rp = new ReflectionParameters (pi);
2538                                 method_parameter_cache [mb] = rp;
2539
2540                                 return (ParameterData) rp;
2541                         }
2542                 }
2543
2544                 static bool ConversionExists (TypeContainer tc, Type from, Type to)
2545                 {
2546                         // Locate user-defined implicit operators
2547
2548                         Expression mg;
2549                         
2550                         mg = MemberLookup (tc.RootContext, to, "op_Implicit", false);
2551
2552                         if (mg != null) {
2553                                 MethodGroupExpr me = (MethodGroupExpr) mg;
2554                                 
2555                                 for (int i = me.Methods.Length; i > 0;) {
2556                                         i--;
2557                                         MethodBase mb = me.Methods [i];
2558                                         ParameterData pd = GetParameterData (mb);
2559                                         
2560                                         if (from == pd.ParameterType (0))
2561                                                 return true;
2562                                 }
2563                         }
2564
2565                         mg = MemberLookup (tc.RootContext, from, "op_Implicit", false);
2566
2567                         if (mg != null) {
2568                                 MethodGroupExpr me = (MethodGroupExpr) mg;
2569
2570                                 for (int i = me.Methods.Length; i > 0;) {
2571                                         i--;
2572                                         MethodBase mb = me.Methods [i];
2573                                         MethodInfo mi = (MethodInfo) mb;
2574                                         
2575                                         if (mi.ReturnType == to)
2576                                                 return true;
2577                                 }
2578                         }
2579                         
2580                         return false;
2581                 }
2582                 
2583                 // <summary>
2584                 //  Determines "better conversion" as specified in 7.4.2.3
2585                 //  Returns : 1 if a->p is better
2586                 //            0 if a->q or neither is better 
2587                 // </summary>
2588                 static int BetterConversion (TypeContainer tc, Argument a, Type p, Type q)
2589                 {
2590                         
2591                         Type argument_type = a.Expr.Type;
2592                         Expression argument_expr = a.Expr;
2593
2594                         if (argument_type == null)
2595                                 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
2596                         
2597                         
2598                         if (p == q)
2599                                 return 0;
2600
2601                         if (argument_type == p)
2602                                 return 1;
2603
2604                         if (argument_type == q)
2605                                 return 0;
2606
2607                         //
2608                         // Now probe whether an implicit constant expression conversion
2609                         // can be used.
2610                         //
2611                         // An implicit constant expression conversion permits the following
2612                         // conversions:
2613                         //
2614                         //    * A constant-expression of type `int' can be converted to type
2615                         //      sbyte, byute, short, ushort, uint, ulong provided the value of
2616                         //      of the expression is withing the range of the destination type.
2617                         //
2618                         //    * A constant-expression of type long can be converted to type
2619                         //      ulong, provided the value of the constant expression is not negative
2620                         //
2621                         // FIXME: Note that this assumes that constant folding has
2622                         // taken place.  We dont do constant folding yet.
2623                         //
2624
2625                         if (argument_type == TypeManager.int32_type && argument_expr is IntLiteral){
2626                                 IntLiteral ei = (IntLiteral) argument_expr;
2627                                 int value = ei.Value;
2628                                 
2629                                 if (p == TypeManager.sbyte_type){
2630                                         if (value >= SByte.MinValue && value <= SByte.MaxValue)
2631                                                 return 1;
2632                                 } else if (p == TypeManager.byte_type){
2633                                         if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2634                                                 return 1;
2635                                 } else if (p == TypeManager.short_type){
2636                                         if (value >= Int16.MinValue && value <= Int16.MaxValue)
2637                                                 return 1;
2638                                 } else if (p == TypeManager.ushort_type){
2639                                         if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2640                                                 return 1;
2641                                 } else if (p == TypeManager.uint32_type){
2642                                         //
2643                                         // we can optimize this case: a positive int32
2644                                         // always fits on a uint32
2645                                         //
2646                                         if (value >= 0)
2647                                                 return 1;
2648                                 } else if (p == TypeManager.uint64_type){
2649                                         //
2650                                         // we can optimize this case: a positive int32
2651                                         // always fits on a uint64
2652                                         //
2653                                         if (value >= 0)
2654                                                 return 1;
2655                                 }
2656                         } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
2657                                 LongLiteral ll = (LongLiteral) argument_expr;
2658                                 
2659                                 if (p == TypeManager.uint64_type){
2660                                         if (ll.Value > 0)
2661                                                 return 1;
2662                                 }
2663                         }
2664
2665                         // User-defined Implicit conversions come here
2666                         
2667                         if (q != null)
2668                                 if (ConversionExists (tc, p, q) == true &&
2669                                     ConversionExists (tc, q, p) == false)
2670                                         return 1;
2671                         
2672                         if (p == TypeManager.sbyte_type)
2673                                 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
2674                                     q == TypeManager.uint32_type || q == TypeManager.uint64_type) 
2675                                         return 1;
2676
2677                         if (p == TypeManager.short_type)
2678                                 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
2679                                     q == TypeManager.uint64_type)
2680                                         return 1;
2681
2682                         if (p == TypeManager.int32_type)
2683                                 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2684                                         return 1;
2685
2686                         if (p == TypeManager.int64_type)
2687                                 if (q == TypeManager.uint64_type)
2688                                         return 1;
2689
2690                         return 0;
2691                 }
2692                 
2693                 // <summary>
2694                 //  Determines "Better function" and returns an integer indicating :
2695                 //  0 if candidate ain't better
2696                 //  1 if candidate is better than the current best match
2697                 // </summary>
2698                 static int BetterFunction (TypeContainer tc, ArrayList args, MethodBase candidate, MethodBase best)
2699                 {
2700                         ParameterData candidate_pd = GetParameterData (candidate);
2701                         ParameterData best_pd;
2702                         int argument_count;
2703
2704                         if (args == null)
2705                                 argument_count = 0;
2706                         else
2707                                 argument_count = args.Count;
2708
2709                         if (candidate_pd.Count == 0 && argument_count == 0)
2710                                 return 1;
2711
2712                         if (best == null) {
2713                                 if (candidate_pd.Count == argument_count) {
2714                                         int x = 0;
2715                                         for (int j = argument_count; j > 0;) {
2716                                                 j--;
2717                                                 
2718                                                 Argument a = (Argument) args [j];
2719                                                 
2720                                                 x = BetterConversion (tc, a, candidate_pd.ParameterType (j), null);
2721                                                 
2722                                                 if (x > 0)
2723                                                         continue;
2724                                                 else 
2725                                                         break;
2726                                         }
2727                                         
2728                                         if (x > 0)
2729                                                 return 1;
2730                                         else
2731                                                 return 0;
2732                                         
2733                                 } else
2734                                         return 0;
2735                         }
2736
2737                         best_pd = GetParameterData (best);
2738
2739                         if (candidate_pd.Count == argument_count && best_pd.Count == argument_count) {
2740                                 int rating1 = 0, rating2 = 0;
2741                                 
2742                                 for (int j = argument_count; j > 0;) {
2743                                         j--;
2744                                         int x, y;
2745                                         
2746                                         Argument a = (Argument) args [j];
2747
2748                                         x = BetterConversion (tc, a, candidate_pd.ParameterType (j),
2749                                                               best_pd.ParameterType (j));
2750                                         y = BetterConversion (tc, a, best_pd.ParameterType (j),
2751                                                               candidate_pd.ParameterType (j));
2752                                         
2753                                         rating1 += x;
2754                                         rating2 += y;
2755                                 }
2756
2757                                 if (rating1 > rating2)
2758                                         return 1;
2759                                 else
2760                                         return 0;
2761                         } else
2762                                 return 0;
2763                         
2764                 }
2765
2766                 public static string FullMethodDesc (MethodBase mb)
2767                 {
2768                         StringBuilder sb = new StringBuilder (mb.Name);
2769                         ParameterData pd = GetParameterData (mb);
2770                         
2771                         sb.Append (" (");
2772                         for (int i = pd.Count; i > 0;) {
2773                                 i--;
2774                                 sb.Append (TypeManager.CSharpName (pd.ParameterType (i)));
2775                                 if (i != 0)
2776                                         sb.Append (",");
2777                         }
2778                         
2779                         sb.Append (")");
2780                         return sb.ToString ();
2781                 }
2782
2783                 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2)
2784                 {
2785
2786                         if (mg1 != null || mg2 != null) {
2787                                         
2788                                 MethodGroupExpr left_set = null, right_set = null;
2789                                 int length1 = 0, length2 = 0;
2790                                 
2791                                 if (mg1 != null) {
2792                                         left_set = (MethodGroupExpr) mg1;
2793                                         length1 = left_set.Methods.Length;
2794                                 }
2795                                 
2796                                 if (mg2 != null) {
2797                                         right_set = (MethodGroupExpr) mg2;
2798                                         length2 = right_set.Methods.Length;
2799                                 }
2800                                 
2801                                 MemberInfo [] miset = new MemberInfo [length1 + length2];
2802                                 if (left_set != null)
2803                                         left_set.Methods.CopyTo (miset, 0);
2804                                 if (right_set != null)
2805                                         right_set.Methods.CopyTo (miset, length1);
2806                                 
2807                                 MethodGroupExpr union = new MethodGroupExpr (miset);
2808
2809                                 return union;
2810                                 
2811                         }
2812
2813                         return null;
2814
2815                 }
2816                 
2817                 // <summary>
2818                 //   Find the Applicable Function Members (7.4.2.1)
2819                 //
2820                 //   me: Method Group expression with the members to select.
2821                 //       it might contain constructors or methods (or anything
2822                 //       that maps to a method).
2823                 //
2824                 //   Arguments: ArrayList containing resolved Argument objects.
2825                 //
2826                 //   Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
2827                 //            that is the best match of me on Arguments.
2828                 //
2829                 // </summary>
2830                 public static MethodBase OverloadResolve (TypeContainer tc, MethodGroupExpr me,
2831                                                           ArrayList Arguments, Location loc)
2832                 {
2833                         ArrayList afm = new ArrayList ();
2834                         int best_match_idx = -1;
2835                         MethodBase method = null;
2836                         int argument_count;
2837                         
2838                         for (int i = me.Methods.Length; i > 0; ){
2839                                 i--;
2840                                 MethodBase candidate  = me.Methods [i];
2841                                 int x;
2842                                 
2843                                 x = BetterFunction (tc, Arguments, candidate, method);
2844                                 
2845                                 if (x == 0)
2846                                         continue;
2847                                 else {
2848                                         best_match_idx = i;
2849                                         method = me.Methods [best_match_idx];
2850                                 }
2851                         }
2852                         
2853                         if (best_match_idx != -1)
2854                                 return method;
2855
2856                         // Now we see if we can at least find a method with the same number of arguments
2857                         // and then try doing implicit conversion on the arguments
2858
2859                         if (Arguments == null)
2860                                 argument_count = 0;
2861                         else
2862                                 argument_count = Arguments.Count;
2863
2864                         ParameterData pd = null;
2865                         
2866                         for (int i = me.Methods.Length; i > 0;) {
2867                                 i--;
2868                                 MethodBase mb = me.Methods [i];
2869                                 pd = GetParameterData (mb);
2870
2871                                 if (pd.Count == argument_count) {
2872                                         best_match_idx = i;
2873                                         method = me.Methods [best_match_idx];
2874                                         break;
2875                                 } else
2876                                         continue;
2877                         }
2878
2879                         if (best_match_idx == -1)
2880                                 return null;
2881
2882                         // And now convert implicitly, each argument to the required type
2883                         
2884                         pd = GetParameterData (method);
2885
2886                         for (int j = argument_count; j > 0;) {
2887                                 j--;
2888                                 Argument a = (Argument) Arguments [j];
2889                                 Expression a_expr = a.Expr;
2890                                 
2891                                 Expression conv = ConvertImplicit (tc, a_expr, pd.ParameterType (j));
2892
2893                                 if (conv == null) {
2894                                         tc.RootContext.Report.Error (1502, loc,
2895                                                "The best overloaded match for method '" + FullMethodDesc (method) +
2896                                                "' has some invalid arguments");
2897                                         tc.RootContext.Report.Error (1503, loc,
2898                                                "Argument " + (j+1) +
2899                                                " : Cannot convert from '" + TypeManager.CSharpName (a_expr.Type)
2900                                                + "' to '" + TypeManager.CSharpName (pd.ParameterType (j)) + "'");
2901                                         return null;
2902                                 }
2903
2904                                 //
2905                                 // Update the argument with the implicit conversion
2906                                 //
2907                                 if (a_expr != conv)
2908                                         a.Expr = conv;
2909                         }
2910                         
2911                         return method;
2912                 }
2913
2914                         
2915                 public override Expression Resolve (TypeContainer tc)
2916                 {
2917                         //
2918                         // First, resolve the expression that is used to
2919                         // trigger the invocation
2920                         //
2921                         this.expr = expr.Resolve (tc);
2922                         if (this.expr == null)
2923                                 return null;
2924
2925                         if (!(this.expr is MethodGroupExpr)){
2926                                 report118 (tc, this.expr, "method group");
2927                                 return null;
2928                         }
2929
2930                         //
2931                         // Next, evaluate all the expressions in the argument list
2932                         //
2933                         if (Arguments != null){
2934                                 for (int i = Arguments.Count; i > 0;){
2935                                         --i;
2936                                         Argument a = (Argument) Arguments [i];
2937
2938                                         if (!a.Resolve (tc))
2939                                                 return null;
2940                                 }
2941                         }
2942
2943                         method = OverloadResolve (tc, (MethodGroupExpr) this.expr, Arguments, Location);
2944
2945                         if (method == null){
2946                                 tc.RootContext.Report.Error (-6, Location,
2947                                        "Could not find any applicable function for this argument list");
2948                                 return null;
2949                         }
2950
2951                         if (method is MethodInfo)
2952                                 type = ((MethodInfo)method).ReturnType;
2953
2954                         return this;
2955                 }
2956
2957                 public static void EmitArguments (EmitContext ec, MethodBase method, ArrayList Arguments)
2958                 {
2959                         int top;
2960
2961                         if (Arguments != null)
2962                                 top = Arguments.Count;
2963                         else
2964                                 top = 0;
2965
2966                         for (int i = 0; i < top; i++){
2967                                 Argument a = (Argument) Arguments [i];
2968
2969                                 a.Emit (ec);
2970                         }
2971                 }
2972                 
2973                 public override void Emit (EmitContext ec)
2974                 {
2975                         bool is_static = method.IsStatic;
2976
2977                         if (!is_static){
2978                                 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
2979
2980                                 //
2981                                 // If this is ourselves, push "this"
2982                                 //
2983                                 if (mg.InstanceExpression == null){
2984                                         ec.ig.Emit (OpCodes.Ldarg_0);
2985                                 } else {
2986                                         //
2987                                         // Push the instance expression
2988                                         //
2989                                         mg.InstanceExpression.Emit (ec);
2990                                 }
2991                         }
2992
2993                         if (Arguments != null)
2994                                 EmitArguments (ec, method, Arguments);
2995
2996                         if (is_static){
2997                                 if (method is MethodInfo)
2998                                         ec.ig.Emit (OpCodes.Call, (MethodInfo) method);
2999                                 else
3000                                         ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3001                         } else {
3002                                 if (method is MethodInfo)
3003                                         ec.ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
3004                                 else
3005                                         ec.ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
3006                         }
3007                 }
3008
3009                 public override void EmitStatement (EmitContext ec)
3010                 {
3011                         Emit (ec);
3012
3013                         // 
3014                         // Pop the return value if there is one
3015                         //
3016                         if (method is MethodInfo){
3017                                 if (((MethodInfo)method).ReturnType != TypeManager.void_type)
3018                                         ec.ig.Emit (OpCodes.Pop);
3019                         }
3020                 }
3021         }
3022
3023         public class New : ExpressionStatement {
3024
3025                 public enum NType {
3026                         Object,
3027                         Array
3028                 };
3029
3030                 public readonly NType     NewType;
3031                 public readonly ArrayList Arguments;
3032                 public readonly string    RequestedType;
3033                 // These are for the case when we have an array
3034                 public readonly string    Rank;
3035                 public readonly ArrayList Indices;
3036                 public readonly ArrayList Initializers;
3037
3038                 Location Location;
3039                 MethodBase method = null;
3040
3041                 public New (string requested_type, ArrayList arguments, Location loc)
3042                 {
3043                         RequestedType = requested_type;
3044                         Arguments = arguments;
3045                         NewType = NType.Object;
3046                         Location = loc;
3047                 }
3048
3049                 public New (string requested_type, ArrayList exprs, string rank, ArrayList initializers, Location loc)
3050                 {
3051                         RequestedType = requested_type;
3052                         Indices       = exprs;
3053                         Rank          = rank;
3054                         Initializers  = initializers;
3055                         NewType       = NType.Array;
3056                         Location      = loc;
3057                 }
3058                 
3059                 public override Expression Resolve (TypeContainer tc)
3060                 {
3061                         type = tc.LookupType (RequestedType, false);
3062
3063                         if (type == null)
3064                                 return null;
3065
3066                         Expression ml;
3067
3068                         ml = MemberLookup (tc.RootContext, type, ".ctor", false,
3069                                            MemberTypes.Constructor, AllBindingsFlags);
3070
3071                         if (! (ml is MethodGroupExpr)){
3072                                 //
3073                                 // FIXME: Find proper error
3074                                 //
3075                                 report118 (tc, ml, "method group");
3076                                 return null;
3077                         }
3078                         
3079                         if (Arguments != null){
3080                                 for (int i = Arguments.Count; i > 0;){
3081                                         --i;
3082                                         Argument a = (Argument) Arguments [i];
3083
3084                                         if (!a.Resolve (tc))
3085                                                 return null;
3086                                 }
3087                         }
3088
3089                         method = Invocation.OverloadResolve (tc, (MethodGroupExpr) ml, Arguments, Location);
3090
3091                         if (method == null) {
3092                                 tc.RootContext.Report.Error (-6, Location,
3093                                 "New invocation: Can not find a constructor for this argument list");
3094                                 return null;
3095                         }
3096                         
3097                         return this;
3098                 }
3099
3100                 public override void Emit (EmitContext ec)
3101                 {
3102                         Invocation.EmitArguments (ec, method, Arguments);
3103                         ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
3104                 }
3105
3106                 public override void EmitStatement (EmitContext ec)
3107                 {
3108                         Emit (ec);
3109                         ec.ig.Emit (OpCodes.Pop);
3110                 }
3111         }
3112
3113         //
3114         // Represents the `this' construct
3115         //
3116         public class This : Expression, LValue {
3117                 public override Expression Resolve (TypeContainer tc)
3118                 {
3119                         eclass = ExprClass.Variable;
3120                         type = tc.TypeBuilder;
3121
3122                         //
3123                         // FIXME: Verify that this is only used in instance contexts.
3124                         //
3125                         return this;
3126                 }
3127
3128                 public override void Emit (EmitContext ec)
3129                 {
3130                         ec.ig.Emit (OpCodes.Ldarg_0);
3131                 }
3132
3133                 public void Store (ILGenerator ig)
3134                 {
3135                         //
3136                         // Assignment to the "this" variable
3137                         //
3138                         ig.Emit (OpCodes.Starg, 0);
3139                 }
3140         }
3141
3142         public class TypeOf : Expression {
3143                 public readonly string QueriedType;
3144                 
3145                 public TypeOf (string queried_type)
3146                 {
3147                         QueriedType = queried_type;
3148                 }
3149
3150                 public override Expression Resolve (TypeContainer tc)
3151                 {
3152                         type = tc.LookupType (QueriedType, false);
3153
3154                         if (type == null)
3155                                 return null;
3156                         
3157                         eclass = ExprClass.Type;
3158                         return this;
3159                 }
3160
3161                 public override void Emit (EmitContext ec)
3162                 {
3163                         throw new Exception ("Implement me");
3164                         // FIXME: Implement.
3165                 }
3166         }
3167
3168         public class SizeOf : Expression {
3169                 public readonly string QueriedType;
3170                 
3171                 public SizeOf (string queried_type)
3172                 {
3173                         this.QueriedType = queried_type;
3174                 }
3175
3176                 public override Expression Resolve (TypeContainer tc)
3177                 {
3178                         // FIXME: Implement;
3179                         throw new Exception ("Unimplemented");
3180                         // return this;
3181                 }
3182
3183                 public override void Emit (EmitContext ec)
3184                 {
3185                         throw new Exception ("Implement me");
3186                 }
3187         }
3188
3189         public class MemberAccess : Expression {
3190                 public readonly string Identifier;
3191                 Expression expr;
3192                 Expression member_lookup;
3193                 
3194                 public MemberAccess (Expression expr, string id)
3195                 {
3196                         this.expr = expr;
3197                         Identifier = id;
3198                 }
3199
3200                 public Expression Expr {
3201                         get {
3202                                 return expr;
3203                         }
3204                 }
3205                 
3206                 public override Expression Resolve (TypeContainer tc)
3207                 {
3208                         Expression new_expression = expr.Resolve (tc);
3209
3210                         if (new_expression == null)
3211                                 return null;
3212
3213                         member_lookup = MemberLookup (tc.RootContext, expr.Type, Identifier, false);
3214
3215                         if (member_lookup is MethodGroupExpr){
3216                                 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
3217
3218                                 //
3219                                 // Bind the instance expression to it
3220                                 //
3221                                 // FIXME: This is a horrible way of detecting if it is
3222                                 // an instance expression.  Figure out how to fix this.
3223                                 //
3224
3225                                 if (expr is LocalVariableReference ||
3226                                     expr is ParameterReference ||
3227                                     expr is FieldExpr)
3228                                         mg.InstanceExpression = expr;
3229                                         
3230                                 return member_lookup;
3231                         } else if (member_lookup is FieldExpr){
3232                                 FieldExpr fe = (FieldExpr) member_lookup;
3233
3234                                 fe.Instance = expr;
3235
3236                                 return member_lookup;
3237                         } else
3238                                 //
3239                                 // FIXME: This should generate the proper node
3240                                 // ie, for a Property Access, it should like call it
3241                                 // and stuff.
3242
3243                                 return member_lookup;
3244                 }
3245
3246                 public override void Emit (EmitContext ec)
3247                 {
3248                         throw new Exception ("Implement me");
3249                 }
3250
3251         }
3252
3253         // <summary>
3254         //   Nodes of type Namespace are created during the semantic
3255         //   analysis to resolve member_access/qualified_identifier/simple_name
3256         //   accesses.
3257         //
3258         //   They are born `resolved'. 
3259         // </summary>
3260         public class NamespaceExpr : Expression {
3261                 public readonly string Name;
3262                 
3263                 public NamespaceExpr (string name)
3264                 {
3265                         Name = name;
3266                         eclass = ExprClass.Namespace;
3267                 }
3268
3269                 public override Expression Resolve (TypeContainer tc)
3270                 {
3271                         return this;
3272                 }
3273
3274                 public override void Emit (EmitContext ec)
3275                 {
3276                         throw new Exception ("Namespace expressions should never be emitted");
3277                 }
3278         }
3279
3280         // <summary>
3281         //   Fully resolved expression that evaluates to a type
3282         // </summary>
3283         public class TypeExpr : Expression {
3284                 public TypeExpr (Type t)
3285                 {
3286                         Type = t;
3287                         eclass = ExprClass.Type;
3288                 }
3289
3290                 override public Expression Resolve (TypeContainer tc)
3291                 {
3292                         return this;
3293                 }
3294
3295                 override public void Emit (EmitContext ec)
3296                 {
3297                         throw new Exception ("Implement me");
3298                 }
3299         }
3300
3301         // <summary>
3302         //   MethodGroup Expression.
3303         //  
3304         //   This is a fully resolved expression that evaluates to a type
3305         // </summary>
3306         public class MethodGroupExpr : Expression {
3307                 public readonly MethodBase [] Methods;
3308                 Expression instance_expression = null;
3309                 
3310                 public MethodGroupExpr (MemberInfo [] mi)
3311                 {
3312                         Methods = new MethodBase [mi.Length];
3313                         mi.CopyTo (Methods, 0);
3314                         eclass = ExprClass.MethodGroup;
3315                 }
3316
3317                 //
3318                 // `A method group may have associated an instance expression' 
3319                 // 
3320                 public Expression InstanceExpression {
3321                         get {
3322                                 return instance_expression;
3323                         }
3324
3325                         set {
3326                                 instance_expression = value;
3327                         }
3328                 }
3329                 
3330                 override public Expression Resolve (TypeContainer tc)
3331                 {
3332                         return this;
3333                 }
3334
3335                 override public void Emit (EmitContext ec)
3336                 {
3337                         throw new Exception ("This should never be reached");
3338                 }
3339         }
3340         
3341         public class BuiltinTypeAccess : Expression {
3342                 public readonly string AccessBase;
3343                 public readonly string Method;
3344                 
3345                 public BuiltinTypeAccess (string type, string method)
3346                 {
3347                         System.Console.WriteLine ("DUDE! This type should be fully resolved!");
3348                         AccessBase = type;
3349                         Method = method;
3350                 }
3351
3352                 public override Expression Resolve (TypeContainer tc)
3353                 {
3354                         // FIXME: Implement;
3355                         throw new Exception ("Unimplemented");
3356                         // return this;
3357                 }
3358
3359                 public override void Emit (EmitContext ec)
3360                 {
3361                         throw new Exception ("Unimplemented");
3362                 }
3363         }
3364
3365
3366         //   Fully resolved expression that evaluates to a Field
3367         // </summary>
3368         public class FieldExpr : Expression, LValue {
3369                 public readonly FieldInfo FieldInfo;
3370                 public Expression Instance;
3371                         
3372                 public FieldExpr (FieldInfo fi)
3373                 {
3374                         FieldInfo = fi;
3375                         eclass = ExprClass.Variable;
3376                         type = fi.FieldType;
3377                 }
3378
3379                 override public Expression Resolve (TypeContainer tc)
3380                 {
3381                         if (!FieldInfo.IsStatic){
3382                                 if (Instance == null){
3383                                         throw new Exception ("non-static FieldExpr without instance var\n" +
3384                                                              "You have to assign the Instance variable\n" +
3385                                                              "Of the FieldExpr to set this\n");
3386                                 }
3387
3388                                 Instance = Instance.Resolve (tc);
3389                                 if (Instance == null)
3390                                         return null;
3391                                 
3392                         }
3393                         return this;
3394                 }
3395
3396                 override public void Emit (EmitContext ec)
3397                 {
3398                         ILGenerator ig = ec.ig;
3399
3400                         if (FieldInfo.IsStatic)
3401                                 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3402                         else {
3403                                 Instance.Emit (ec);
3404                                 
3405                                 ig.Emit (OpCodes.Ldfld, FieldInfo);
3406                         }
3407                 }
3408
3409                 public void Store (ILGenerator ig)
3410                 {
3411                         if (FieldInfo.IsStatic)
3412                                 ig.Emit (OpCodes.Stsfld, FieldInfo);
3413                         else
3414                                 ig.Emit (OpCodes.Stfld, FieldInfo);
3415                 }
3416         }
3417         
3418         // <summary>
3419         //   Fully resolved expression that evaluates to a Property
3420         // </summary>
3421         public class PropertyExpr : Expression {
3422                 public readonly PropertyInfo PropertyInfo;
3423                 public readonly bool IsStatic;
3424                 
3425                 public PropertyExpr (PropertyInfo pi)
3426                 {
3427                         PropertyInfo = pi;
3428                         eclass = ExprClass.PropertyAccess;
3429                         IsStatic = false;
3430                                 
3431                         MethodBase [] acc = pi.GetAccessors ();
3432
3433                         for (int i = 0; i < acc.Length; i++)
3434                                 if (acc [i].IsStatic)
3435                                         IsStatic = true;
3436
3437                         type = pi.PropertyType;
3438                 }
3439
3440                 override public Expression Resolve (TypeContainer tc)
3441                 {
3442                         // We are born in resolved state. 
3443                         return this;
3444                 }
3445
3446                 override public void Emit (EmitContext ec)
3447                 {
3448                         // FIXME: Implement;
3449                         throw new Exception ("Unimplemented");
3450                 }
3451         }
3452
3453         // <summary>
3454         //   Fully resolved expression that evaluates to a Property
3455         // </summary>
3456         public class EventExpr : Expression {
3457                 public readonly EventInfo EventInfo;
3458                 
3459                 public EventExpr (EventInfo ei)
3460                 {
3461                         EventInfo = ei;
3462                         eclass = ExprClass.EventAccess;
3463                 }
3464
3465                 override public Expression Resolve (TypeContainer tc)
3466                 {
3467                         // We are born in resolved state. 
3468                         return this;
3469                 }
3470
3471                 override public void Emit (EmitContext ec)
3472                 {
3473                         throw new Exception ("Implement me");
3474                         // FIXME: Implement.
3475                 }
3476         }
3477         
3478         public class CheckedExpr : Expression {
3479
3480                 public Expression Expr;
3481
3482                 public CheckedExpr (Expression e)
3483                 {
3484                         Expr = e;
3485                 }
3486
3487                 public override Expression Resolve (TypeContainer tc)
3488                 {
3489                         Expr = Expr.Resolve (tc);
3490
3491                         if (Expr == null)
3492                                 return null;
3493
3494                         eclass = Expr.ExprClass;
3495                         type = Expr.Type;
3496                         return this;
3497                 }
3498
3499                 public override void Emit (EmitContext ec)
3500                 {
3501                         bool last_check = ec.CheckState;
3502                         
3503                         ec.CheckState = true;
3504                         Expr.Emit (ec);
3505                         ec.CheckState = last_check;
3506                 }
3507                 
3508         }
3509
3510         public class UnCheckedExpr : Expression {
3511
3512                 public Expression Expr;
3513
3514                 public UnCheckedExpr (Expression e)
3515                 {
3516                         Expr = e;
3517                 }
3518
3519                 public override Expression Resolve (TypeContainer tc)
3520                 {
3521                         Expr = Expr.Resolve (tc);
3522
3523                         if (Expr == null)
3524                                 return null;
3525
3526                         eclass = Expr.ExprClass;
3527                         type = Expr.Type;
3528                         return this;
3529                 }
3530
3531                 public override void Emit (EmitContext ec)
3532                 {
3533                         bool last_check = ec.CheckState;
3534                         
3535                         ec.CheckState = false;
3536                         Expr.Emit (ec);
3537                         ec.CheckState = last_check;
3538                 }
3539                 
3540         }
3541         
3542         public class ElementAccess : Expression {
3543                 
3544                 public readonly ArrayList  Arguments;
3545                 public readonly Expression Expr;
3546                 
3547                 public ElementAccess (Expression e, ArrayList e_list)
3548                 {
3549                         Expr = e;
3550                         Arguments = e_list;
3551                 }
3552
3553                 public override Expression Resolve (TypeContainer tc)
3554                 {
3555                         // FIXME: Implement;
3556                         throw new Exception ("Unimplemented");
3557                         // return this;
3558                 }
3559                 
3560                 public override void Emit (EmitContext ec)
3561                 {
3562                         // FIXME : Implement !
3563                         throw new Exception ("Unimplemented");
3564                 }
3565                 
3566         }
3567         
3568         public class BaseAccess : Expression {
3569
3570                 public enum BaseAccessType {
3571                         Member,
3572                         Indexer
3573                 };
3574                 
3575                 public readonly BaseAccessType BAType;
3576                 public readonly string         Member;
3577                 public readonly ArrayList      Arguments;
3578
3579                 public BaseAccess (BaseAccessType t, string member, ArrayList args)
3580                 {
3581                         BAType = t;
3582                         Member = member;
3583                         Arguments = args;
3584                         
3585                 }
3586
3587                 public override Expression Resolve (TypeContainer tc)
3588                 {
3589                         // FIXME: Implement;
3590                         throw new Exception ("Unimplemented");
3591                         // return this;
3592                 }
3593
3594                 public override void Emit (EmitContext ec)
3595                 {
3596                         throw new Exception ("Unimplemented");
3597                 }
3598         }
3599
3600         public class UserImplicitCast : Expression {
3601
3602                 Expression source;
3603                 Type       target; 
3604                 MethodBase method;
3605                 ArrayList  arguments;
3606                 
3607                 public UserImplicitCast (Expression source, Type target)
3608                 {
3609                         this.source = source;
3610                         this.target = target;
3611                 }
3612
3613                 public override Expression Resolve (TypeContainer tc)
3614                 {
3615                         source = source.Resolve (tc);
3616                         
3617                         if (source == null)
3618                                 return null;
3619
3620                         Expression mg1, mg2;
3621                         MethodGroupExpr union;
3622                         MethodInfo mi;
3623                         
3624                         mg1 = MemberLookup (tc.RootContext, source.Type, "op_Implicit", false);
3625                         mg2 = MemberLookup (tc.RootContext, target, "op_Implicit", false);
3626                         
3627                         union = Invocation.MakeUnionSet (mg1, mg2);
3628
3629                         arguments = new ArrayList ();
3630                         arguments.Add (new Argument (source, Argument.AType.Expression));
3631                         
3632                         if (union != null) {
3633                                 method = Invocation.OverloadResolve (tc, union, arguments,
3634                                                                      new Location ("FIXME", 1, 1));
3635                                 
3636                                 if (method != null) {
3637                                         mi = (MethodInfo) method;
3638
3639                                         if (mi.ReturnType == target) {
3640                                                 type = mi.ReturnType;
3641                                                 return this;
3642                                         }
3643                                 }
3644                         }
3645                         
3646                         if (target == TypeManager.bool_type) {
3647
3648                                 mg1 = MemberLookup (tc.RootContext, source.Type, "op_True", false);
3649                                 mg2 = MemberLookup (tc.RootContext, target, "op_True", false);
3650                                 
3651                                 union = Invocation.MakeUnionSet (mg1, mg2);
3652
3653                                 if (union == null)
3654                                         return null;
3655                                 
3656                                 method = Invocation.OverloadResolve (tc, union, arguments,
3657                                                                      new Location ("FIXME", 1, 1));
3658                                 
3659                                 if (method != null) {
3660                                         mi = (MethodInfo) method;
3661
3662                                         if (mi.ReturnType == target) {
3663                                                 type = mi.ReturnType;
3664                                                 return this;
3665                                         }
3666                                 }
3667                         }
3668
3669                         return null;
3670                 }
3671
3672                 public static bool CanConvert (TypeContainer tc, Expression source, Type target)
3673                 {
3674                         source = source.Resolve (tc);
3675                         
3676                         if (source == null)
3677                                 return false;
3678
3679                         Expression mg1, mg2;
3680                         MethodBase method;
3681                         ArrayList arguments;
3682                         
3683                         mg1 = MemberLookup (tc.RootContext, source.Type, "op_Implicit", false);
3684                         mg2 = MemberLookup (tc.RootContext, target, "op_Implicit", false);
3685                         
3686                         MethodGroupExpr union = Invocation.MakeUnionSet (mg1, mg2);
3687
3688                         arguments = new ArrayList ();
3689                         arguments.Add (new Argument (source, Argument.AType.Expression));
3690                         
3691                         if (union != null) {
3692                                 
3693                                 method = Invocation.OverloadResolve (tc, union, arguments,
3694                                                                                 new Location ("FIXME", 1, 1));
3695                                 if (method != null) {
3696                                         MethodInfo mi = (MethodInfo) method;
3697                                         
3698                                         if (mi.ReturnType == target)
3699                                                 return true;
3700                                 }
3701                         }
3702                         
3703                         // If we have a boolean type, we need to check for the True
3704                         // and False operators too.
3705                         
3706                         if (target == TypeManager.bool_type) {
3707
3708                                 mg1 = MemberLookup (tc.RootContext, source.Type, "op_True", false);
3709                                 mg2 = MemberLookup (tc.RootContext, target, "op_True", false);
3710                                 
3711                                 union = Invocation.MakeUnionSet (mg1, mg2);
3712
3713                                 if (union == null)
3714                                         return false;
3715
3716                                 method = Invocation.OverloadResolve (tc, union, arguments,
3717                                                                      new Location ("FIXME", 1, 1));
3718                                 if (method != null) {
3719                                         MethodInfo mi = (MethodInfo) method;
3720
3721                                         if (mi.ReturnType == target) 
3722                                                 return true;
3723                                 }
3724                         }
3725                         
3726                         return false;
3727                         
3728                 }
3729                 
3730                 public override void Emit (EmitContext ec)
3731                 {
3732                         ILGenerator ig = ec.ig;
3733                         
3734                         if (method != null) {
3735
3736                                 // Note that operators are static anyway
3737                                 
3738                                 if (arguments != null) 
3739                                         Invocation.EmitArguments (ec, method, arguments);
3740                                 
3741                                 if (method is MethodInfo)
3742                                         ig.Emit (OpCodes.Call, (MethodInfo) method);
3743                                 else
3744                                         ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3745
3746                                 return;
3747                         }
3748
3749                         throw new Exception ("Implement me");
3750                 }
3751
3752         }
3753 }