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