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