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