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