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