2001-12-11 Ravi Pratap <ravi@ximian.com>
[mono.git] / mcs / mcs / ecore.cs
1 //
2 // ecore.cs: Core of the Expression representation for the intermediate tree.
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.
8 //
9 //
10
11 namespace CIR {
12         using System;
13         using System.Collections;
14         using System.Diagnostics;
15         using System.Reflection;
16         using System.Reflection.Emit;
17         using System.Text;
18
19         // <remarks>
20         //   The ExprClass class contains the is used to pass the 
21         //   classification of an expression (value, variable, namespace,
22         //   type, method group, property access, event access, indexer access,
23         //   nothing).
24         // </remarks>
25         public enum ExprClass {
26                 Invalid,
27                 
28                 Value,
29                 Variable,
30                 Namespace,
31                 Type,
32                 MethodGroup,
33                 PropertyAccess,
34                 EventAccess,
35                 IndexerAccess,
36                 Nothing, 
37         }
38
39         // <summary>
40         //   An interface provided by expressions that can be used as
41         //   LValues and can store the value on the top of the stack on
42         //   their storage
43         // </summary>
44         public interface IStackStore {
45
46                 // <summary>
47                 //   The Store method should store the contents of the top
48                 //   of the stack into the storage that is implemented by
49                 //   the particular implementation of LValue
50                 // </summary>
51                 void Store     (EmitContext ec);
52         }
53
54         // <summary>
55         //   This interface is implemented by variables
56         // </summary>
57         public interface IMemoryLocation {
58                 // <summary>
59                 //   The AddressOf method should generate code that loads
60                 //   the address of the object and leaves it on the stack
61                 // </summary>
62                 void AddressOf (EmitContext ec);
63         }
64
65         // <remarks>
66         //   Base class for expressions
67         // </remarks>
68         public abstract class Expression {
69                 protected ExprClass eclass;
70                 protected Type      type;
71                 
72                 public Type Type {
73                         get {
74                                 return type;
75                         }
76
77                         set {
78                                 type = value;
79                         }
80                 }
81
82                 public ExprClass ExprClass {
83                         get {
84                                 return eclass;
85                         }
86
87                         set {
88                                 eclass = value;
89                         }
90                 }
91
92                 // <summary>
93                 //   Utility wrapper routine for Error, just to beautify the code
94                 // </summary>
95                 static protected void Error (int error, string s)
96                 {
97                         Report.Error (error, s);
98                 }
99
100                 static protected void Error (int error, Location loc, string s)
101                 {
102                         Report.Error (error, loc, s);
103                 }
104                 
105                 // <summary>
106                 //   Utility wrapper routine for Warning, just to beautify the code
107                 // </summary>
108                 static protected void Warning (int warning, string s)
109                 {
110                         Report.Warning (warning, s);
111                 }
112
113                 // <summary>
114                 //   Performs semantic analysis on the Expression
115                 // </summary>
116                 //
117                 // <remarks>
118                 //   The Resolve method is invoked to perform the semantic analysis
119                 //   on the node.
120                 //
121                 //   The return value is an expression (it can be the
122                 //   same expression in some cases) or a new
123                 //   expression that better represents this node.
124                 //   
125                 //   For example, optimizations of Unary (LiteralInt)
126                 //   would return a new LiteralInt with a negated
127                 //   value.
128                 //   
129                 //   If there is an error during semantic analysis,
130                 //   then an error should be reported (using Report)
131                 //   and a null value should be returned.
132                 //   
133                 //   There are two side effects expected from calling
134                 //   Resolve(): the the field variable "eclass" should
135                 //   be set to any value of the enumeration
136                 //   `ExprClass' and the type variable should be set
137                 //   to a valid type (this is the type of the
138                 //   expression).
139                 // </remarks>
140                 
141                 public abstract Expression DoResolve (EmitContext ec);
142
143                 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
144                 {
145                         return DoResolve (ec);
146                 }
147                 
148                 //
149                 // Currently Resolve wraps DoResolve to perform sanity
150                 // checking and assertion checking on what we expect from Resolve
151                 //
152                 public Expression Resolve (EmitContext ec)
153                 {
154                         Expression e = DoResolve (ec);
155
156                         if (e != null){
157                                 if (e is SimpleName){
158                                         SimpleName s = (SimpleName) e;
159                                         
160                                         Report.Error (
161                                                 103, s.Location,
162                                                 "The name `" + s.Name + "' could not be found in `" +
163                                                 ec.TypeContainer.Name + "'");
164                                         return null;
165                                 }
166                                 
167                                 if (e.ExprClass == ExprClass.Invalid)
168                                         throw new Exception ("Expression " + e +
169                                                              " ExprClass is Invalid after resolve");
170
171                                 if (e.ExprClass != ExprClass.MethodGroup)
172                                         if (e.type == null)
173                                                 throw new Exception ("Expression " + e +
174                                                                      " did not set its type after Resolve");
175                         }
176
177                         return e;
178                 }
179
180                 //
181                 // Just like `Resolve' above, but this allows SimpleNames to be returned.
182                 // This is used by MemberAccess to construct long names that can not be
183                 // partially resolved (namespace-qualified names for example).
184                 //
185                 public Expression ResolveWithSimpleName (EmitContext ec)
186                 {
187                         Expression e = DoResolve (ec);
188
189                         if (e != null){
190                                 if (e is SimpleName)
191                                         return e;
192
193                                 if (e.ExprClass == ExprClass.Invalid)
194                                         throw new Exception ("Expression " + e +
195                                                              " ExprClass is Invalid after resolve");
196
197                                 if (e.ExprClass != ExprClass.MethodGroup)
198                                         if (e.type == null)
199                                                 throw new Exception ("Expression " + e +
200                                                                      " did not set its type after Resolve");
201                         }
202
203                         return e;
204                 }
205                 
206                 //
207                 // Currently ResolveLValue wraps DoResolveLValue to perform sanity
208                 // checking and assertion checking on what we expect from Resolve
209                 //
210                 public Expression ResolveLValue (EmitContext ec, Expression right_side)
211                 {
212                         Expression e = DoResolveLValue (ec, right_side);
213
214                         if (e != null){
215                                 if (e is SimpleName){
216                                         SimpleName s = (SimpleName) e;
217                                         
218                                         Report.Error (
219                                                 103, s.Location,
220                                                 "The name `" + s.Name + "' could not be found in `" +
221                                                 ec.TypeContainer.Name + "'");
222                                         return null;
223                                 }
224
225                                 if (e.ExprClass == ExprClass.Invalid)
226                                         throw new Exception ("Expression " + e +
227                                                              " ExprClass is Invalid after resolve");
228
229                                 if (e.ExprClass != ExprClass.MethodGroup)
230                                         if (e.type == null)
231                                                 throw new Exception ("Expression " + e +
232                                                                      " did not set its type after Resolve");
233                         }
234
235                         return e;
236                 }
237                 
238                 // <summary>
239                 //   Emits the code for the expression
240                 // </summary>
241                 //
242                 // <remarks>
243                 // 
244                 //   The Emit method is invoked to generate the code
245                 //   for the expression.  
246                 //
247                 // </remarks>
248                 public abstract void Emit (EmitContext ec);
249
250                 // <summary>
251                 //   This method should perform a reduction of the expression.  This should
252                 //   never return null.
253                 // </summary>
254                 public virtual Expression Reduce (EmitContext ec)
255                 {
256                         return this;
257                 }
258
259                 // <summary>
260                 //   Protected constructor.  Only derivate types should
261                 //   be able to be created
262                 // </summary>
263
264                 protected Expression ()
265                 {
266                         eclass = ExprClass.Invalid;
267                         type = null;
268                 }
269
270                 // <summary>
271                 //   Returns a literalized version of a literal FieldInfo
272                 // </summary>
273                 public static Expression Literalize (object v, Type t)
274                 {
275                         if (t == TypeManager.int32_type)
276                                 return new IntLiteral ((int) v);
277                         else if (t == TypeManager.uint32_type)
278                                 return new UIntLiteral ((uint) v);
279                         else if (t == TypeManager.int64_type)
280                                 return new LongLiteral ((long) v);
281                         else if (t == TypeManager.uint64_type)
282                                 return new ULongLiteral ((ulong) v);
283                         else if (t == TypeManager.float_type)
284                                 return new FloatLiteral ((float) v);
285                         else if (t == TypeManager.double_type)
286                                 return new DoubleLiteral ((double) v);
287                         else if (t == TypeManager.string_type)
288                                 return new StringLiteral ((string) v);
289                         else if (t == TypeManager.short_type)
290                                 return new IntLiteral ((int) ((short)v));
291                         else if (t == TypeManager.ushort_type)
292                                 return new IntLiteral ((int) ((ushort)v));
293                         else if (t == TypeManager.sbyte_type)
294                                 return new IntLiteral ((int) ((sbyte)v));
295                         else if (t == TypeManager.byte_type)
296                                 return new IntLiteral ((int) ((byte)v));
297                         else if (t == TypeManager.char_type)
298                                 return new IntLiteral ((int) ((char)v));
299                         else
300                                 throw new Exception ("Unknown type for literal (" + t +
301                                                      "), details: " + v);
302                 }
303
304                 // 
305                 // Returns a fully formed expression after a MemberLookup
306                 //
307                 static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
308                 {
309                         if (mi is EventInfo)
310                                 return new EventExpr ((EventInfo) mi, loc);
311                         else if (mi is FieldInfo)
312                                 return new FieldExpr ((FieldInfo) mi, loc);
313                         else if (mi is PropertyInfo)
314                                 return new PropertyExpr ((PropertyInfo) mi, loc);
315                         else if (mi is Type)
316                                 return new TypeExpr ((Type) mi);
317
318                         return null;
319                 }
320
321                 //
322                 // FIXME: Probably implement a cache for (t,name,current_access_set)?
323                 //
324                 // FIXME: We need to cope with access permissions here, or this wont
325                 // work!
326                 //
327                 // This code could use some optimizations, but we need to do some
328                 // measurements.  For example, we could use a delegate to `flag' when
329                 // something can not any longer be a method-group (because it is something
330                 // else).
331                 //
332                 // Return values:
333                 //     If the return value is an Array, then it is an array of
334                 //     MethodBases
335                 //   
336                 //     If the return value is an MemberInfo, it is anything, but a Method
337                 //
338                 //     null on error.
339                 //
340                 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
341                 // the arguments here and have MemberLookup return only the methods that
342                 // match the argument count/type, unlike we are doing now (we delay this
343                 // decision).
344                 //
345                 // This is so we can catch correctly attempts to invoke instance methods
346                 // from a static body (scan for error 120 in ResolveSimpleName).
347                 //
348                 public static Expression MemberLookup (EmitContext ec, Type t, string name,
349                                                        bool same_type, MemberTypes mt,
350                                                        BindingFlags bf, Location loc)
351                 {
352                         if (same_type)
353                                 bf |= BindingFlags.NonPublic;
354
355                         MemberInfo [] mi = ec.TypeContainer.RootContext.TypeManager.FindMembers (
356                                 t, mt, bf, Type.FilterName, name);
357
358                         if (mi == null)
359                                 return null;
360
361                         // Empty array ...
362                         if (mi.Length == 0) 
363                                 return null;
364
365                         
366                         if (mi.Length == 1 && !(mi [0] is MethodBase))
367                                 return Expression.ExprClassFromMemberInfo (ec, mi [0], loc);
368                         
369                         for (int i = 0; i < mi.Length; i++)
370                                 if (!(mi [i] is MethodBase)){
371                                         Error (-5, "Do not know how to reproduce this case: " + 
372                                                "Methods and non-Method with the same name, " +
373                                                "report this please");
374
375                                         for (i = 0; i < mi.Length; i++){
376                                                 Type tt = mi [i].GetType ();
377
378                                                 Console.WriteLine (i + ": " + mi [i]);
379                                                 while (tt != TypeManager.object_type){
380                                                         Console.WriteLine (tt);
381                                                         tt = tt.BaseType;
382                                                 }
383                                         }
384                                 }
385
386                         return new MethodGroupExpr (mi);
387                 }
388
389                 public const MemberTypes AllMemberTypes =
390                         MemberTypes.Constructor |
391                         MemberTypes.Event       |
392                         MemberTypes.Field       |
393                         MemberTypes.Method      |
394                         MemberTypes.NestedType  |
395                         MemberTypes.Property;
396                 
397                 public const BindingFlags AllBindingsFlags =
398                         BindingFlags.Public |
399                         BindingFlags.Static |
400                         BindingFlags.Instance;
401
402                 public static Expression MemberLookup (EmitContext ec, Type t, string name,
403                                                        bool same_type, Location loc)
404                 {
405                         return MemberLookup (ec, t, name, same_type, AllMemberTypes, AllBindingsFlags, loc);
406                 }
407
408                 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
409                 {
410                         Type expr_type = expr.Type;
411
412                         if (target_type == TypeManager.object_type) {
413                                 if (expr_type.IsClass)
414                                         return new EmptyCast (expr, target_type);
415                                 if (expr_type.IsValueType)
416                                         return new BoxedCast (expr);
417                         } else if (expr_type.IsSubclassOf (target_type)) {
418                                 return new EmptyCast (expr, target_type);
419                         } else {
420                                 // from any class-type S to any interface-type T.
421                                 if (expr_type.IsClass && target_type.IsInterface) {
422
423                                         if (TypeManager.ImplementsInterface (expr_type, target_type))
424                                                 return new EmptyCast (expr, target_type);
425                                         else
426                                                 return null;
427                                 }
428
429                                 // from any interface type S to interface-type T.
430                                 if (expr_type.IsInterface && target_type.IsInterface) {
431
432                                         if (TypeManager.ImplementsInterface (expr_type, target_type))
433                                                 return new EmptyCast (expr, target_type);
434                                         else
435                                                 return null;
436                                 }
437                                 
438                                 // from an array-type S to an array-type of type T
439                                 if (expr_type.IsArray && target_type.IsArray) {
440                                         if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
441
442                                                 Type expr_element_type = expr_type.GetElementType ();
443                                                 Type target_element_type = target_type.GetElementType ();
444
445                                                 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
446                                                         if (StandardConversionExists (expr_element_type,
447                                                                                       target_element_type))
448                                                                 return new EmptyCast (expr, target_type);
449                                         }
450                                 }
451                                 
452                                 
453                                 // from an array-type to System.Array
454                                 if (expr_type.IsArray && target_type == TypeManager.array_type)
455                                         return new EmptyCast (expr, target_type);
456                                 
457                                 // from any delegate type to System.Delegate
458                                 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
459                                     target_type == TypeManager.delegate_type)
460                                         return new EmptyCast (expr, target_type);
461                                         
462                                 // from any array-type or delegate type into System.ICloneable.
463                                 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
464                                         if (target_type == TypeManager.icloneable_type)
465                                                 return new EmptyCast (expr, target_type);
466                                 
467                                 // from the null type to any reference-type.
468                                 if (expr is NullLiteral)
469                                         return new EmptyCast (expr, target_type);
470
471                                 return null;
472
473                         }
474                         
475                         return null;
476                 }
477
478                 // <summary>
479                 //   Handles expressions like this: decimal d; d = 1;
480                 //   and changes them into: decimal d; d = new System.Decimal (1);
481                 // </summary>
482                 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
483                 {
484                         ArrayList args = new ArrayList ();
485
486                         args.Add (new Argument (expr, Argument.AType.Expression));
487
488                         Expression ne = new New (target.FullName, args,
489                                                  new Location (-1));
490
491                         return ne.Resolve (ec);
492                 }
493
494                 // <summary>
495                 //   Implicit Numeric Conversions.
496                 //
497                 //   expr is the expression to convert, returns a new expression of type
498                 //   target_type or null if an implicit conversion is not possible.
499                 //
500                 // </summary>
501                 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
502                                                                     Type target_type, Location loc)
503                 {
504                         Type expr_type = expr.Type;
505                         
506                         //
507                         // Attempt to do the implicit constant expression conversions
508
509                         if (expr is IntLiteral){
510                                 Expression e;
511                                 
512                                 e = TryImplicitIntConversion (target_type, (IntLiteral) expr);
513                                 if (e != null)
514                                         return e;
515                         } else if (expr is LongLiteral){
516                                 //
517                                 // Try the implicit constant expression conversion
518                                 // from long to ulong, instead of a nice routine,
519                                 // we just inline it
520                                 //
521                                 if (((LongLiteral) expr).Value > 0)
522                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
523                         }
524                         
525                         if (expr_type == TypeManager.sbyte_type){
526                                 //
527                                 // From sbyte to short, int, long, float, double.
528                                 //
529                                 if (target_type == TypeManager.int32_type)
530                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
531                                 if (target_type == TypeManager.int64_type)
532                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
533                                 if (target_type == TypeManager.double_type)
534                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
535                                 if (target_type == TypeManager.float_type)
536                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
537                                 if (target_type == TypeManager.short_type)
538                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
539                                 if (target_type == TypeManager.decimal_type)
540                                         return InternalTypeConstructor (ec, expr, target_type);
541                         } else if (expr_type == TypeManager.byte_type){
542                                 //
543                                 // From byte to short, ushort, int, uint, long, ulong, float, double
544                                 // 
545                                 if ((target_type == TypeManager.short_type) ||
546                                     (target_type == TypeManager.ushort_type) ||
547                                     (target_type == TypeManager.int32_type) ||
548                                     (target_type == TypeManager.uint32_type))
549                                         return new EmptyCast (expr, target_type);
550
551                                 if (target_type == TypeManager.uint64_type)
552                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
553                                 if (target_type == TypeManager.int64_type)
554                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
555                                 
556                                 if (target_type == TypeManager.float_type)
557                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
558                                 if (target_type == TypeManager.double_type)
559                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
560                                 if (target_type == TypeManager.decimal_type)
561                                         return InternalTypeConstructor (ec, expr, target_type);
562                         } else if (expr_type == TypeManager.short_type){
563                                 //
564                                 // From short to int, long, float, double
565                                 // 
566                                 if (target_type == TypeManager.int32_type)
567                                         return new EmptyCast (expr, target_type);
568                                 if (target_type == TypeManager.int64_type)
569                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
570                                 if (target_type == TypeManager.double_type)
571                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
572                                 if (target_type == TypeManager.float_type)
573                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
574                                 if (target_type == TypeManager.decimal_type)
575                                         return InternalTypeConstructor (ec, expr, target_type);
576                         } else if (expr_type == TypeManager.ushort_type){
577                                 //
578                                 // From ushort to int, uint, long, ulong, float, double
579                                 //
580                                 if (target_type == TypeManager.uint32_type)
581                                         return new EmptyCast (expr, target_type);
582
583                                 if (target_type == TypeManager.uint64_type)
584                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
585                                 if (target_type == TypeManager.int32_type)
586                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
587                                 if (target_type == TypeManager.int64_type)
588                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
589                                 if (target_type == TypeManager.double_type)
590                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
591                                 if (target_type == TypeManager.float_type)
592                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
593                                 if (target_type == TypeManager.decimal_type)
594                                         return InternalTypeConstructor (ec, expr, target_type);
595                         } else if (expr_type == TypeManager.int32_type){
596                                 //
597                                 // From int to long, float, double
598                                 //
599                                 if (target_type == TypeManager.int64_type)
600                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
601                                 if (target_type == TypeManager.double_type)
602                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
603                                 if (target_type == TypeManager.float_type)
604                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
605                                 if (target_type == TypeManager.decimal_type)
606                                         return InternalTypeConstructor (ec, expr, target_type);
607                         } else if (expr_type == TypeManager.uint32_type){
608                                 //
609                                 // From uint to long, ulong, float, double
610                                 //
611                                 if (target_type == TypeManager.int64_type)
612                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
613                                 if (target_type == TypeManager.uint64_type)
614                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
615                                 if (target_type == TypeManager.double_type)
616                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
617                                                                OpCodes.Conv_R8);
618                                 if (target_type == TypeManager.float_type)
619                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
620                                                                OpCodes.Conv_R4);
621                                 if (target_type == TypeManager.decimal_type)
622                                         return InternalTypeConstructor (ec, expr, target_type);
623                         } else if ((expr_type == TypeManager.uint64_type) ||
624                                    (expr_type == TypeManager.int64_type)){
625                                 //
626                                 // From long/ulong to float, double
627                                 //
628                                 if (target_type == TypeManager.double_type)
629                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
630                                                                OpCodes.Conv_R8);
631                                 if (target_type == TypeManager.float_type)
632                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
633                                                                OpCodes.Conv_R4);        
634                                 if (target_type == TypeManager.decimal_type)
635                                         return InternalTypeConstructor (ec, expr, target_type);
636                         } else if (expr_type == TypeManager.char_type){
637                                 //
638                                 // From char to ushort, int, uint, long, ulong, float, double
639                                 // 
640                                 if ((target_type == TypeManager.ushort_type) ||
641                                     (target_type == TypeManager.int32_type) ||
642                                     (target_type == TypeManager.uint32_type))
643                                         return new EmptyCast (expr, target_type);
644                                 if (target_type == TypeManager.uint64_type)
645                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
646                                 if (target_type == TypeManager.int64_type)
647                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
648                                 if (target_type == TypeManager.float_type)
649                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
650                                 if (target_type == TypeManager.double_type)
651                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
652                                 if (target_type == TypeManager.decimal_type)
653                                         return InternalTypeConstructor (ec, expr, target_type);
654                         } else if (expr_type == TypeManager.float_type){
655                                 //
656                                 // float to double
657                                 //
658                                 if (target_type == TypeManager.double_type)
659                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
660                         }
661
662                         return null;
663                 }
664
665                 // <summary>
666                 //  Determines if a standard implicit conversion exists from
667                 //  expr_type to target_type
668                 // </summary>
669                 public static bool StandardConversionExists (Type expr_type, Type target_type)
670                 {
671                         if (expr_type == target_type)
672                                 return true;
673
674                         // First numeric conversions 
675                         
676                         if (expr_type == TypeManager.sbyte_type){
677                                 //
678                                 // From sbyte to short, int, long, float, double.
679                                 //
680                                 if ((target_type == TypeManager.int32_type) || 
681                                     (target_type == TypeManager.int64_type) ||
682                                     (target_type == TypeManager.double_type) ||
683                                     (target_type == TypeManager.float_type)  ||
684                                     (target_type == TypeManager.short_type) ||
685                                     (target_type == TypeManager.decimal_type))
686                                         return true;
687                                 
688                         } else if (expr_type == TypeManager.byte_type){
689                                 //
690                                 // From byte to short, ushort, int, uint, long, ulong, float, double
691                                 // 
692                                 if ((target_type == TypeManager.short_type) ||
693                                     (target_type == TypeManager.ushort_type) ||
694                                     (target_type == TypeManager.int32_type) ||
695                                     (target_type == TypeManager.uint32_type) ||
696                                     (target_type == TypeManager.uint64_type) ||
697                                     (target_type == TypeManager.int64_type) ||
698                                     (target_type == TypeManager.float_type) ||
699                                     (target_type == TypeManager.double_type) ||
700                                     (target_type == TypeManager.decimal_type))
701                                         return true;
702         
703                         } else if (expr_type == TypeManager.short_type){
704                                 //
705                                 // From short to int, long, float, double
706                                 // 
707                                 if ((target_type == TypeManager.int32_type) ||
708                                     (target_type == TypeManager.int64_type) ||
709                                     (target_type == TypeManager.double_type) ||
710                                     (target_type == TypeManager.float_type) ||
711                                     (target_type == TypeManager.decimal_type))
712                                         return true;
713                                         
714                         } else if (expr_type == TypeManager.ushort_type){
715                                 //
716                                 // From ushort to int, uint, long, ulong, float, double
717                                 //
718                                 if ((target_type == TypeManager.uint32_type) ||
719                                     (target_type == TypeManager.uint64_type) ||
720                                     (target_type == TypeManager.int32_type) ||
721                                     (target_type == TypeManager.int64_type) ||
722                                     (target_type == TypeManager.double_type) ||
723                                     (target_type == TypeManager.float_type) ||
724                                     (target_type == TypeManager.decimal_type))
725                                         return true;
726                                     
727                         } else if (expr_type == TypeManager.int32_type){
728                                 //
729                                 // From int to long, float, double
730                                 //
731                                 if ((target_type == TypeManager.int64_type) ||
732                                     (target_type == TypeManager.double_type) ||
733                                     (target_type == TypeManager.float_type) ||
734                                     (target_type == TypeManager.decimal_type))
735                                         return true;
736                                         
737                         } else if (expr_type == TypeManager.uint32_type){
738                                 //
739                                 // From uint to long, ulong, float, double
740                                 //
741                                 if ((target_type == TypeManager.int64_type) ||
742                                     (target_type == TypeManager.uint64_type) ||
743                                     (target_type == TypeManager.double_type) ||
744                                     (target_type == TypeManager.float_type) ||
745                                     (target_type == TypeManager.decimal_type))
746                                         return true;
747                                         
748                         } else if ((expr_type == TypeManager.uint64_type) ||
749                                    (expr_type == TypeManager.int64_type)) {
750                                 //
751                                 // From long/ulong to float, double
752                                 //
753                                 if ((target_type == TypeManager.double_type) ||
754                                     (target_type == TypeManager.float_type) ||
755                                     (target_type == TypeManager.decimal_type))
756                                         return true;
757                                     
758                         } else if (expr_type == TypeManager.char_type){
759                                 //
760                                 // From char to ushort, int, uint, long, ulong, float, double
761                                 // 
762                                 if ((target_type == TypeManager.ushort_type) ||
763                                     (target_type == TypeManager.int32_type) ||
764                                     (target_type == TypeManager.uint32_type) ||
765                                     (target_type == TypeManager.uint64_type) ||
766                                     (target_type == TypeManager.int64_type) ||
767                                     (target_type == TypeManager.float_type) ||
768                                     (target_type == TypeManager.double_type) ||
769                                     (target_type == TypeManager.decimal_type))
770                                         return true;
771
772                         } else if (expr_type == TypeManager.float_type){
773                                 //
774                                 // float to double
775                                 //
776                                 if (target_type == TypeManager.double_type)
777                                         return true;
778                         }       
779                         
780                         // Next reference conversions
781
782                         if (target_type == TypeManager.object_type) {
783                                 if ((expr_type.IsClass) ||
784                                     (expr_type.IsValueType))
785                                         return true;
786                                 
787                         } else if (expr_type.IsSubclassOf (target_type)) {
788                                 return true;
789                                 
790                         } else {
791                                 // from any class-type S to any interface-type T.
792                                 if (expr_type.IsClass && target_type.IsInterface)
793                                         return true;
794                                 
795                                 // from any interface type S to interface-type T.
796                                 // FIXME : Is it right to use IsAssignableFrom ?
797                                 if (expr_type.IsInterface && target_type.IsInterface)
798                                         if (target_type.IsAssignableFrom (expr_type))
799                                                 return true;
800                                 
801                                 // from an array-type S to an array-type of type T
802                                 if (expr_type.IsArray && target_type.IsArray) {
803                                         if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
804                                                 
805                                                 Type expr_element_type = expr_type.GetElementType ();
806                                                 Type target_element_type = target_type.GetElementType ();
807                                                 
808                                                 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
809                                                         if (StandardConversionExists (expr_element_type,
810                                                                                       target_element_type))
811                                                                 return true;
812                                         }
813                                 }
814                                 
815                                 // from an array-type to System.Array
816                                 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
817                                         return true;
818                                 
819                                 // from any delegate type to System.Delegate
820                                 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
821                                     target_type == TypeManager.delegate_type)
822                                         if (target_type.IsAssignableFrom (expr_type))
823                                                 return true;
824                                         
825                                 // from any array-type or delegate type into System.ICloneable.
826                                 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
827                                         if (target_type == TypeManager.icloneable_type)
828                                                 return true;
829                                 
830                                 // from the null type to any reference-type.
831                                 // FIXME : How do we do this ?
832
833                         }
834
835                         return false;
836                 }
837                 
838                 // <summary>
839                 //  Finds "most encompassed type" according to the spec (13.4.2)
840                 //  amongst the methods in the MethodGroupExpr which convert from a
841                 //  type encompassing source_type
842                 // </summary>
843                 static Type FindMostEncompassedType (MethodGroupExpr me, Type source_type)
844                 {
845                         Type best = null;
846                         
847                         for (int i = me.Methods.Length; i > 0; ) {
848                                 i--;
849
850                                 MethodBase mb = me.Methods [i];
851                                 ParameterData pd = Invocation.GetParameterData (mb);
852                                 Type param_type = pd.ParameterType (0);
853
854                                 if (StandardConversionExists (source_type, param_type)) {
855                                         if (best == null)
856                                                 best = param_type;
857                                         
858                                         if (StandardConversionExists (param_type, best))
859                                                 best = param_type;
860                                 }
861                         }
862
863                         return best;
864                 }
865                 
866                 // <summary>
867                 //  Finds "most encompassing type" according to the spec (13.4.2)
868                 //  amongst the methods in the MethodGroupExpr which convert to a
869                 //  type encompassed by target_type
870                 // </summary>
871                 static Type FindMostEncompassingType (MethodGroupExpr me, Type target)
872                 {
873                         Type best = null;
874                         
875                         for (int i = me.Methods.Length; i > 0; ) {
876                                 i--;
877                                 
878                                 MethodInfo mi = (MethodInfo) me.Methods [i];
879                                 Type ret_type = mi.ReturnType;
880                                 
881                                 if (StandardConversionExists (ret_type, target)) {
882                                         if (best == null)
883                                                 best = ret_type;
884
885                                         if (!StandardConversionExists (ret_type, best))
886                                                 best = ret_type;
887                                 }
888                                 
889                         }
890                         
891                         return best;
892
893                 }
894                 
895
896                 // <summary>
897                 //  User-defined Implicit conversions
898                 // </summary>
899                 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
900                                                                  Type target, Location loc)
901                 {
902                         return UserDefinedConversion (ec, source, target, loc, false);
903                 }
904
905                 // <summary>
906                 //  User-defined Explicit conversions
907                 // </summary>
908                 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
909                                                                  Type target, Location loc)
910                 {
911                         return UserDefinedConversion (ec, source, target, loc, true);
912                 }
913                 
914                 // <summary>
915                 //   User-defined conversions
916                 // </summary>
917                 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
918                                                                 Type target, Location loc,
919                                                                 bool look_for_explicit)
920                 {
921                         Expression mg1 = null, mg2 = null, mg3 = null, mg4 = null;
922                         Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
923                         Expression e;
924                         MethodBase method = null;
925                         Type source_type = source.Type;
926
927                         string op_name;
928                         
929                         // If we have a boolean type, we need to check for the True operator
930
931                         // FIXME : How does the False operator come into the picture ?
932                         // FIXME : This doesn't look complete and very correct !
933                         if (target == TypeManager.bool_type)
934                                 op_name = "op_True";
935                         else
936                                 op_name = "op_Implicit";
937                         
938                         mg1 = MemberLookup (ec, source_type, op_name, false, loc);
939
940                         if (source_type.BaseType != null)
941                                 mg2 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
942                         
943                         mg3 = MemberLookup (ec, target, op_name, false, loc);
944
945                         if (target.BaseType != null)
946                                 mg4 = MemberLookup (ec, target.BaseType, op_name, false, loc);
947
948                         MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
949                         MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
950
951                         MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2);
952
953                         MethodGroupExpr union4 = null;
954
955                         if (look_for_explicit) {
956
957                                 op_name = "op_Explicit";
958                                 
959                                 mg5 = MemberLookup (ec, source_type, op_name, false, loc);
960
961                                 if (source_type.BaseType != null)
962                                         mg6 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
963                                 
964                                 mg7 = MemberLookup (ec, target, op_name, false, loc);
965                                 
966                                 if (target.BaseType != null)
967                                         mg8 = MemberLookup (ec, target.BaseType, op_name, false, loc);
968                                 
969                                 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
970                                 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
971
972                                 union4 = Invocation.MakeUnionSet (union5, union6);
973                         }
974                         
975                         MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4);
976
977                         if (union != null) {
978
979                                 Type most_specific_source, most_specific_target;
980
981                                 most_specific_source = FindMostEncompassedType (union, source_type);
982                                 if (most_specific_source == null)
983                                         return null;
984
985                                 most_specific_target = FindMostEncompassingType (union, target);
986                                 if (most_specific_target == null) 
987                                         return null;
988                                 
989                                 int count = 0;
990                                 
991                                 for (int i = union.Methods.Length; i > 0;) {
992                                         i--;
993
994                                         MethodBase mb = union.Methods [i];
995                                         ParameterData pd = Invocation.GetParameterData (mb);
996                                         MethodInfo mi = (MethodInfo) union.Methods [i];
997
998                                         if (pd.ParameterType (0) == most_specific_source &&
999                                             mi.ReturnType == most_specific_target) {
1000                                                 method = mb;
1001                                                 count++;
1002                                         }
1003                                 }
1004
1005                                 if (method == null || count > 1) {
1006                                         Report.Error (-11, loc, "Ambiguous user defined conversion");
1007                                         return null;
1008                                 }
1009                                 
1010                                 //
1011                                 // This will do the conversion to the best match that we
1012                                 // found.  Now we need to perform an implict standard conversion
1013                                 // if the best match was not the type that we were requested
1014                                 // by target.
1015                                 //
1016                                 if (look_for_explicit)
1017                                         source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1018                                 else
1019                                         source = ConvertImplicitStandard (ec, source,
1020                                                                           most_specific_source, loc);
1021
1022                                 if (source == null)
1023                                         return null;
1024                                 
1025                                 e =  new UserCast ((MethodInfo) method, source);
1026                                 
1027                                 if (e.Type != target){
1028                                         if (!look_for_explicit)
1029                                                 e = ConvertImplicitStandard (ec, e, target, loc);
1030                                         else
1031                                                 e = ConvertExplicitStandard (ec, e, target, loc);
1032
1033                                         return e;
1034                                 } else
1035                                         return e;
1036                         }
1037                         
1038                         return null;
1039                 }
1040                 
1041                 // <summary>
1042                 //   Converts implicitly the resolved expression `expr' into the
1043                 //   `target_type'.  It returns a new expression that can be used
1044                 //   in a context that expects a `target_type'. 
1045                 // </summary>
1046                 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1047                                                           Type target_type, Location loc)
1048                 {
1049                         Type expr_type = expr.Type;
1050                         Expression e;
1051
1052                         if (expr_type == target_type)
1053                                 return expr;
1054
1055                         e = ImplicitNumericConversion (ec, expr, target_type, loc);
1056                         if (e != null)
1057                                 return e;
1058
1059                         e = ImplicitReferenceConversion (expr, target_type);
1060                         if (e != null)
1061                                 return e;
1062
1063                         e = ImplicitUserConversion (ec, expr, target_type, loc);
1064                         if (e != null)
1065                                 return e;
1066
1067                         if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1068                                 IntLiteral i = (IntLiteral) expr;
1069
1070                                 if (i.Value == 0)
1071                                         return new EmptyCast (expr, target_type);
1072                         }
1073
1074                         return null;
1075                 }
1076
1077                 
1078                 // <summary>
1079                 //   Attempts to apply the `Standard Implicit
1080                 //   Conversion' rules to the expression `expr' into
1081                 //   the `target_type'.  It returns a new expression
1082                 //   that can be used in a context that expects a
1083                 //   `target_type'.
1084                 //
1085                 //   This is different from `ConvertImplicit' in that the
1086                 //   user defined implicit conversions are excluded. 
1087                 // </summary>
1088                 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1089                                                                   Type target_type, Location loc)
1090                 {
1091                         Type expr_type = expr.Type;
1092                         Expression e;
1093
1094                         if (expr_type == target_type)
1095                                 return expr;
1096
1097                         e = ImplicitNumericConversion (ec, expr, target_type, loc);
1098                         if (e != null)
1099                                 return e;
1100
1101                         e = ImplicitReferenceConversion (expr, target_type);
1102                         if (e != null)
1103                                 return e;
1104
1105                         if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1106                                 IntLiteral i = (IntLiteral) expr;
1107
1108                                 if (i.Value == 0)
1109                                         return new EmptyCast (expr, target_type);
1110                         }
1111                         return null;
1112                 }
1113                 // <summary>
1114                 //   Attemps to perform an implict constant conversion of the IntLiteral
1115                 //   into a different data type using casts (See Implicit Constant
1116                 //   Expression Conversions)
1117                 // </summary>
1118                 static protected Expression TryImplicitIntConversion (Type target_type, IntLiteral il)
1119                 {
1120                         int value = il.Value;
1121                         
1122                         if (target_type == TypeManager.sbyte_type){
1123                                 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1124                                         return il;
1125                         } else if (target_type == TypeManager.byte_type){
1126                                 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1127                                         return il;
1128                         } else if (target_type == TypeManager.short_type){
1129                                 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1130                                         return il;
1131                         } else if (target_type == TypeManager.ushort_type){
1132                                 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1133                                         return il;
1134                         } else if (target_type == TypeManager.uint32_type){
1135                                 //
1136                                 // we can optimize this case: a positive int32
1137                                 // always fits on a uint32
1138                                 //
1139                                 if (value >= 0)
1140                                         return il;
1141                         } else if (target_type == TypeManager.uint64_type){
1142                                 //
1143                                 // we can optimize this case: a positive int32
1144                                 // always fits on a uint64.  But we need an opcode
1145                                 // to do it.
1146                                 //
1147                                 if (value >= 0)
1148                                         return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
1149                         }
1150
1151                         return null;
1152                 }
1153
1154                 // <summary>
1155                 //   Attemptes to implicityly convert `target' into `type', using
1156                 //   ConvertImplicit.  If there is no implicit conversion, then
1157                 //   an error is signaled
1158                 // </summary>
1159                 static public Expression ConvertImplicitRequired (EmitContext ec, Expression target,
1160                                                                   Type type, Location loc)
1161                 {
1162                         Expression e;
1163                         
1164                         e = ConvertImplicit (ec, target, type, loc);
1165                         if (e != null)
1166                                 return e;
1167                         
1168                         string msg = "Can not convert implicitly from `"+
1169                                 TypeManager.CSharpName (target.Type) + "' to `" +
1170                                 TypeManager.CSharpName (type) + "'";
1171
1172                         Error (29, loc, msg);
1173
1174                         return null;
1175                 }
1176
1177                 // <summary>
1178                 //   Performs the explicit numeric conversions
1179                 // </summary>
1180                 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
1181                                                           Type target_type)
1182                 {
1183                         Type expr_type = expr.Type;
1184                         
1185                         if (expr_type == TypeManager.sbyte_type){
1186                                 //
1187                                 // From sbyte to byte, ushort, uint, ulong, char
1188                                 //
1189                                 if (target_type == TypeManager.byte_type)
1190                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1191                                 if (target_type == TypeManager.ushort_type)
1192                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1193                                 if (target_type == TypeManager.uint32_type)
1194                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1195                                 if (target_type == TypeManager.uint64_type)
1196                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1197                                 if (target_type == TypeManager.char_type)
1198                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1199                         } else if (expr_type == TypeManager.byte_type){
1200                                 //
1201                                 // From byte to sbyte and char
1202                                 //
1203                                 if (target_type == TypeManager.sbyte_type)
1204                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1205                                 if (target_type == TypeManager.char_type)
1206                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1207                         } else if (expr_type == TypeManager.short_type){
1208                                 //
1209                                 // From short to sbyte, byte, ushort, uint, ulong, char
1210                                 //
1211                                 if (target_type == TypeManager.sbyte_type)
1212                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1213                                 if (target_type == TypeManager.byte_type)
1214                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1215                                 if (target_type == TypeManager.ushort_type)
1216                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1217                                 if (target_type == TypeManager.uint32_type)
1218                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1219                                 if (target_type == TypeManager.uint64_type)
1220                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1221                                 if (target_type == TypeManager.char_type)
1222                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1223                         } else if (expr_type == TypeManager.ushort_type){
1224                                 //
1225                                 // From ushort to sbyte, byte, short, char
1226                                 //
1227                                 if (target_type == TypeManager.sbyte_type)
1228                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1229                                 if (target_type == TypeManager.byte_type)
1230                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1231                                 if (target_type == TypeManager.short_type)
1232                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1233                                 if (target_type == TypeManager.char_type)
1234                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1235                         } else if (expr_type == TypeManager.int32_type){
1236                                 //
1237                                 // From int to sbyte, byte, short, ushort, uint, ulong, char
1238                                 //
1239                                 if (target_type == TypeManager.sbyte_type)
1240                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1241                                 if (target_type == TypeManager.byte_type)
1242                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1243                                 if (target_type == TypeManager.short_type)
1244                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1245                                 if (target_type == TypeManager.ushort_type)
1246                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1247                                 if (target_type == TypeManager.uint32_type)
1248                                         return new EmptyCast (expr, target_type);
1249                                 if (target_type == TypeManager.uint64_type)
1250                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1251                                 if (target_type == TypeManager.char_type)
1252                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1253                         } else if (expr_type == TypeManager.uint32_type){
1254                                 //
1255                                 // From uint to sbyte, byte, short, ushort, int, char
1256                                 //
1257                                 if (target_type == TypeManager.sbyte_type)
1258                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1259                                 if (target_type == TypeManager.byte_type)
1260                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1261                                 if (target_type == TypeManager.short_type)
1262                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1263                                 if (target_type == TypeManager.ushort_type)
1264                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1265                                 if (target_type == TypeManager.int32_type)
1266                                         return new EmptyCast (expr, target_type);
1267                                 if (target_type == TypeManager.char_type)
1268                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1269                         } else if (expr_type == TypeManager.int64_type){
1270                                 //
1271                                 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1272                                 //
1273                                 if (target_type == TypeManager.sbyte_type)
1274                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1275                                 if (target_type == TypeManager.byte_type)
1276                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1277                                 if (target_type == TypeManager.short_type)
1278                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1279                                 if (target_type == TypeManager.ushort_type)
1280                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1281                                 if (target_type == TypeManager.int32_type)
1282                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1283                                 if (target_type == TypeManager.uint32_type)
1284                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1285                                 if (target_type == TypeManager.uint64_type)
1286                                         return new EmptyCast (expr, target_type);
1287                                 if (target_type == TypeManager.char_type)
1288                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1289                         } else if (expr_type == TypeManager.uint64_type){
1290                                 //
1291                                 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1292                                 //
1293                                 if (target_type == TypeManager.sbyte_type)
1294                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1295                                 if (target_type == TypeManager.byte_type)
1296                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1297                                 if (target_type == TypeManager.short_type)
1298                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1299                                 if (target_type == TypeManager.ushort_type)
1300                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1301                                 if (target_type == TypeManager.int32_type)
1302                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1303                                 if (target_type == TypeManager.uint32_type)
1304                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1305                                 if (target_type == TypeManager.int64_type)
1306                                         return new EmptyCast (expr, target_type);
1307                                 if (target_type == TypeManager.char_type)
1308                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1309                         } else if (expr_type == TypeManager.char_type){
1310                                 //
1311                                 // From char to sbyte, byte, short
1312                                 //
1313                                 if (target_type == TypeManager.sbyte_type)
1314                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1315                                 if (target_type == TypeManager.byte_type)
1316                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1317                                 if (target_type == TypeManager.short_type)
1318                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1319                         } else if (expr_type == TypeManager.float_type){
1320                                 //
1321                                 // From float to sbyte, byte, short,
1322                                 // ushort, int, uint, long, ulong, char
1323                                 // or decimal
1324                                 //
1325                                 if (target_type == TypeManager.sbyte_type)
1326                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1327                                 if (target_type == TypeManager.byte_type)
1328                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1329                                 if (target_type == TypeManager.short_type)
1330                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1331                                 if (target_type == TypeManager.ushort_type)
1332                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1333                                 if (target_type == TypeManager.int32_type)
1334                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1335                                 if (target_type == TypeManager.uint32_type)
1336                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1337                                 if (target_type == TypeManager.int64_type)
1338                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1339                                 if (target_type == TypeManager.uint64_type)
1340                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1341                                 if (target_type == TypeManager.char_type)
1342                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1343                                 if (target_type == TypeManager.decimal_type)
1344                                         return InternalTypeConstructor (ec, expr, target_type);
1345                         } else if (expr_type == TypeManager.double_type){
1346                                 //
1347                                 // From double to byte, byte, short,
1348                                 // ushort, int, uint, long, ulong,
1349                                 // char, float or decimal
1350                                 //
1351                                 if (target_type == TypeManager.sbyte_type)
1352                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1353                                 if (target_type == TypeManager.byte_type)
1354                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1355                                 if (target_type == TypeManager.short_type)
1356                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1357                                 if (target_type == TypeManager.ushort_type)
1358                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1359                                 if (target_type == TypeManager.int32_type)
1360                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1361                                 if (target_type == TypeManager.uint32_type)
1362                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1363                                 if (target_type == TypeManager.int64_type)
1364                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1365                                 if (target_type == TypeManager.uint64_type)
1366                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1367                                 if (target_type == TypeManager.char_type)
1368                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1369                                 if (target_type == TypeManager.float_type)
1370                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
1371                                 if (target_type == TypeManager.decimal_type)
1372                                         return InternalTypeConstructor (ec, expr, target_type);
1373                         } 
1374
1375                         // decimal is taken care of by the op_Explicit methods.
1376
1377                         return null;
1378                 }
1379
1380                 // <summary>
1381                 //  Returns whether an explicit reference conversion can be performed
1382                 //  from source_type to target_type
1383                 // </summary>
1384                 static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1385                 {
1386                         bool target_is_value_type = target_type.IsValueType;
1387                         
1388                         if (source_type == target_type)
1389                                 return true;
1390                         
1391                         //
1392                         // From object to any reference type
1393                         //
1394                         if (source_type == TypeManager.object_type && !target_is_value_type)
1395                                 return true;
1396                                         
1397                         //
1398                         // From any class S to any class-type T, provided S is a base class of T
1399                         //
1400                         if (target_type.IsSubclassOf (source_type))
1401                                 return true;
1402
1403                         //
1404                         // From any interface type S to any interface T provided S is not derived from T
1405                         //
1406                         if (source_type.IsInterface && target_type.IsInterface){
1407                                 if (!target_type.IsSubclassOf (source_type))
1408                                         return true;
1409                         }
1410                             
1411                         //
1412                         // From any class type S to any interface T, provides S is not sealed
1413                         // and provided S does not implement T.
1414                         //
1415                         if (target_type.IsInterface && !source_type.IsSealed &&
1416                             !target_type.IsAssignableFrom (source_type))
1417                                 return true;
1418
1419                         //
1420                         // From any interface-type S to to any class type T, provided T is not
1421                         // sealed, or provided T implements S.
1422                         //
1423                         if (source_type.IsInterface &&
1424                             (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
1425                                 return true;
1426
1427                         // From an array type S with an element type Se to an array type T with an 
1428                         // element type Te provided all the following are true:
1429                         //     * S and T differe only in element type, in other words, S and T
1430                         //       have the same number of dimensions.
1431                         //     * Both Se and Te are reference types
1432                         //     * An explicit referenc conversions exist from Se to Te
1433                         //
1434                         if (source_type.IsArray && target_type.IsArray) {
1435                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1436                                         
1437                                         Type source_element_type = source_type.GetElementType ();
1438                                         Type target_element_type = target_type.GetElementType ();
1439                                         
1440                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1441                                                 if (ExplicitReferenceConversionExists (source_element_type,
1442                                                                                        target_element_type))
1443                                                         return true;
1444                                 }
1445                         }
1446                         
1447
1448                         // From System.Array to any array-type
1449                         if (source_type == TypeManager.array_type &&
1450                             target_type.IsSubclassOf (TypeManager.array_type)){
1451                                 return true;
1452                         }
1453
1454                         //
1455                         // From System delegate to any delegate-type
1456                         //
1457                         if (source_type == TypeManager.delegate_type &&
1458                             target_type.IsSubclassOf (TypeManager.delegate_type))
1459                                 return true;
1460
1461                         //
1462                         // From ICloneable to Array or Delegate types
1463                         //
1464                         if (source_type == TypeManager.icloneable_type &&
1465                             (target_type == TypeManager.array_type ||
1466                              target_type == TypeManager.delegate_type))
1467                                 return true;
1468                         
1469                         return false;
1470                 }
1471
1472                 // <summary>
1473                 //   Implements Explicit Reference conversions
1474                 // </summary>
1475                 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1476                 {
1477                         Type source_type = source.Type;
1478                         bool target_is_value_type = target_type.IsValueType;
1479                         
1480                         //
1481                         // From object to any reference type
1482                         //
1483                         if (source_type == TypeManager.object_type && !target_is_value_type)
1484                                 return new ClassCast (source, target_type);
1485
1486
1487                         //
1488                         // From any class S to any class-type T, provided S is a base class of T
1489                         //
1490                         if (target_type.IsSubclassOf (source_type))
1491                                 return new ClassCast (source, target_type);
1492
1493                         //
1494                         // From any interface type S to any interface T provided S is not derived from T
1495                         //
1496                         if (source_type.IsInterface && target_type.IsInterface){
1497
1498                                 Type [] ifaces = source_type.GetInterfaces ();
1499
1500                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1501                                         return null;
1502                                 else
1503                                         return new ClassCast (source, target_type);
1504                         }
1505                             
1506                         //
1507                         // From any class type S to any interface T, provides S is not sealed
1508                         // and provided S does not implement T.
1509                         //
1510                         if (target_type.IsInterface && !source_type.IsSealed) {
1511                                 
1512                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1513                                         return null;
1514                                 else
1515                                         return new ClassCast (source, target_type);
1516                                 
1517                         }
1518
1519                         //
1520                         // From any interface-type S to to any class type T, provided T is not
1521                         // sealed, or provided T implements S.
1522                         //
1523                         if (source_type.IsInterface) {
1524
1525                                 if (target_type.IsSealed)
1526                                         return null;
1527                                 
1528                                 if (TypeManager.ImplementsInterface (target_type, source_type))
1529                                         return new ClassCast (source, target_type);
1530                                 else
1531                                         return null;
1532                         }
1533                         
1534                         // From an array type S with an element type Se to an array type T with an 
1535                         // element type Te provided all the following are true:
1536                         //     * S and T differe only in element type, in other words, S and T
1537                         //       have the same number of dimensions.
1538                         //     * Both Se and Te are reference types
1539                         //     * An explicit referenc conversions exist from Se to Te
1540                         //
1541                         if (source_type.IsArray && target_type.IsArray) {
1542                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1543                                         
1544                                         Type source_element_type = source_type.GetElementType ();
1545                                         Type target_element_type = target_type.GetElementType ();
1546                                         
1547                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1548                                                 if (ExplicitReferenceConversionExists (source_element_type,
1549                                                                                        target_element_type))
1550                                                         return new ClassCast (source, target_type);
1551                                 }
1552                         }
1553                         
1554
1555                         // From System.Array to any array-type
1556                         if (source_type == TypeManager.array_type &&
1557                             target_type.IsSubclassOf (TypeManager.array_type)){
1558                                 return new ClassCast (source, target_type);
1559                         }
1560
1561                         //
1562                         // From System delegate to any delegate-type
1563                         //
1564                         if (source_type == TypeManager.delegate_type &&
1565                             target_type.IsSubclassOf (TypeManager.delegate_type))
1566                                 return new ClassCast (source, target_type);
1567
1568                         //
1569                         // From ICloneable to Array or Delegate types
1570                         //
1571                         if (source_type == TypeManager.icloneable_type &&
1572                             (target_type == TypeManager.array_type ||
1573                              target_type == TypeManager.delegate_type))
1574                                 return new ClassCast (source, target_type);
1575                         
1576                         return null;
1577                 }
1578                 
1579                 // <summary>
1580                 //   Performs an explicit conversion of the expression `expr' whose
1581                 //   type is expr.Type to `target_type'.
1582                 // </summary>
1583                 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1584                                                           Type target_type, Location loc)
1585                 {
1586                         Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1587
1588                         if (ne != null)
1589                                 return ne;
1590
1591                         ne = ConvertNumericExplicit (ec, expr, target_type);
1592                         if (ne != null)
1593                                 return ne;
1594
1595                         ne = ConvertReferenceExplicit (expr, target_type);
1596                         if (ne != null)
1597                                 return ne;
1598
1599                         ne = ExplicitUserConversion (ec, expr, target_type, loc);
1600                         if (ne != null)
1601                                 return ne;
1602
1603                         Report.Error (30, loc, "Cannot convert type '" + TypeManager.CSharpName (expr.Type) + "' to '"
1604                                       + TypeManager.CSharpName (target_type) + "'");
1605                         return null;
1606                 }
1607
1608                 // <summary>
1609                 //   Same as ConverExplicit, only it doesn't include user defined conversions
1610                 // </summary>
1611                 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
1612                                                                   Type target_type, Location l)
1613                 {
1614                         Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
1615
1616                         if (ne != null)
1617                                 return ne;
1618
1619                         ne = ConvertNumericExplicit (ec, expr, target_type);
1620                         if (ne != null)
1621                                 return ne;
1622
1623                         ne = ConvertReferenceExplicit (expr, target_type);
1624                         if (ne != null)
1625                                 return ne;
1626
1627                         Report.Error (30, l, "Cannot convert type '" +
1628                                       TypeManager.CSharpName (expr.Type) + "' to '" + 
1629                                       TypeManager.CSharpName (target_type) + "'");
1630                         return null;
1631                 }
1632
1633                 static string ExprClassName (ExprClass c)
1634                 {
1635                         switch (c){
1636                         case ExprClass.Invalid:
1637                                 return "Invalid";
1638                         case ExprClass.Value:
1639                                 return "value";
1640                         case ExprClass.Variable:
1641                                 return "variable";
1642                         case ExprClass.Namespace:
1643                                 return "namespace";
1644                         case ExprClass.Type:
1645                                 return "type";
1646                         case ExprClass.MethodGroup:
1647                                 return "method group";
1648                         case ExprClass.PropertyAccess:
1649                                 return "property access";
1650                         case ExprClass.EventAccess:
1651                                 return "event access";
1652                         case ExprClass.IndexerAccess:
1653                                 return "indexer access";
1654                         case ExprClass.Nothing:
1655                                 return "null";
1656                         }
1657                         throw new Exception ("Should not happen");
1658                 }
1659                 
1660                 // <summary>
1661                 //   Reports that we were expecting `expr' to be of class `expected'
1662                 // </summary>
1663                 protected void report118 (Location loc, Expression expr, string expected)
1664                 {
1665                         string kind = "Unknown";
1666                         
1667                         if (expr != null)
1668                                 kind = ExprClassName (expr.ExprClass);
1669
1670                         Error (118, loc, "Expression denotes a '" + kind +
1671                                "' where an " + expected + " was expected");
1672                 }
1673
1674                 // <summary>
1675                 //   This function tries to reduce the expression performing
1676                 //   constant folding and common subexpression elimination
1677                 // </summary>
1678                 static public Expression Reduce (EmitContext ec, Expression e)
1679                 {
1680                         //Console.WriteLine ("Calling reduce");
1681                         return e.Reduce (ec);
1682                 }
1683         }
1684
1685         // <summary>
1686         //   This is just a base class for expressions that can
1687         //   appear on statements (invocations, object creation,
1688         //   assignments, post/pre increment and decrement).  The idea
1689         //   being that they would support an extra Emition interface that
1690         //   does not leave a result on the stack.
1691         // </summary>
1692
1693         public abstract class ExpressionStatement : Expression {
1694
1695                 // <summary>
1696                 //   Requests the expression to be emitted in a `statement'
1697                 //   context.  This means that no new value is left on the
1698                 //   stack after invoking this method (constrasted with
1699                 //   Emit that will always leave a value on the stack).
1700                 // </summary>
1701                 public abstract void EmitStatement (EmitContext ec);
1702         }
1703
1704         // <summary>
1705         //   This kind of cast is used to encapsulate the child
1706         //   whose type is child.Type into an expression that is
1707         //   reported to return "return_type".  This is used to encapsulate
1708         //   expressions which have compatible types, but need to be dealt
1709         //   at higher levels with.
1710         //
1711         //   For example, a "byte" expression could be encapsulated in one
1712         //   of these as an "unsigned int".  The type for the expression
1713         //   would be "unsigned int".
1714         //
1715         // </summary>
1716         
1717         public class EmptyCast : Expression {
1718                 protected Expression child;
1719
1720                 public EmptyCast (Expression child, Type return_type)
1721                 {
1722                         ExprClass = child.ExprClass;
1723                         type = return_type;
1724                         this.child = child;
1725                 }
1726
1727                 public override Expression DoResolve (EmitContext ec)
1728                 {
1729                         // This should never be invoked, we are born in fully
1730                         // initialized state.
1731
1732                         return this;
1733                 }
1734
1735                 public override void Emit (EmitContext ec)
1736                 {
1737                         child.Emit (ec);
1738                 }
1739
1740         }
1741
1742         // <summary>
1743         //  This class is used to wrap literals which belong inside Enums
1744         // </summary>
1745
1746         public class EnumLiteral : Literal {
1747                 Expression child;
1748
1749                 public EnumLiteral (Expression child, Type enum_type)
1750                 {
1751                         ExprClass = child.ExprClass;
1752                         this.child = child;
1753                         type = enum_type;
1754                 }
1755                 
1756                 public override Expression DoResolve (EmitContext ec)
1757                 {
1758                         // This should never be invoked, we are born in fully
1759                         // initialized state.
1760
1761                         return this;
1762                 }
1763
1764                 public override void Emit (EmitContext ec)
1765                 {
1766                         child.Emit (ec);
1767                 }
1768
1769                 public override object GetValue ()
1770                 {
1771                         return ((Literal) child).GetValue ();
1772                 }
1773
1774                 public override string AsString ()
1775                 {
1776                         return ((Literal) child).AsString ();
1777                 }
1778         }
1779
1780         // <summary>
1781         //   This kind of cast is used to encapsulate Value Types in objects.
1782         //
1783         //   The effect of it is to box the value type emitted by the previous
1784         //   operation.
1785         // </summary>
1786         public class BoxedCast : EmptyCast {
1787
1788                 public BoxedCast (Expression expr)
1789                         : base (expr, TypeManager.object_type)
1790                 {
1791                 }
1792
1793                 public override Expression DoResolve (EmitContext ec)
1794                 {
1795                         // This should never be invoked, we are born in fully
1796                         // initialized state.
1797
1798                         return this;
1799                 }
1800
1801                 public override void Emit (EmitContext ec)
1802                 {
1803                         base.Emit (ec);
1804                         ec.ig.Emit (OpCodes.Box, child.Type);
1805                 }
1806         }
1807
1808         // <summary>
1809         //   This kind of cast is used to encapsulate a child expression
1810         //   that can be trivially converted to a target type using one or 
1811         //   two opcodes.  The opcodes are passed as arguments.
1812         // </summary>
1813         public class OpcodeCast : EmptyCast {
1814                 OpCode op, op2;
1815                 bool second_valid;
1816                 
1817                 public OpcodeCast (Expression child, Type return_type, OpCode op)
1818                         : base (child, return_type)
1819                         
1820                 {
1821                         this.op = op;
1822                         second_valid = false;
1823                 }
1824
1825                 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
1826                         : base (child, return_type)
1827                         
1828                 {
1829                         this.op = op;
1830                         this.op2 = op2;
1831                         second_valid = true;
1832                 }
1833
1834                 public override Expression DoResolve (EmitContext ec)
1835                 {
1836                         // This should never be invoked, we are born in fully
1837                         // initialized state.
1838
1839                         return this;
1840                 }
1841
1842                 public override void Emit (EmitContext ec)
1843                 {
1844                         base.Emit (ec);
1845                         ec.ig.Emit (op);
1846
1847                         if (second_valid)
1848                                 ec.ig.Emit (op2);
1849                 }                       
1850                 
1851         }
1852
1853         // <summary>
1854         //   This kind of cast is used to encapsulate a child and cast it
1855         //   to the class requested
1856         // </summary>
1857         public class ClassCast : EmptyCast {
1858                 public ClassCast (Expression child, Type return_type)
1859                         : base (child, return_type)
1860                         
1861                 {
1862                 }
1863
1864                 public override Expression DoResolve (EmitContext ec)
1865                 {
1866                         // This should never be invoked, we are born in fully
1867                         // initialized state.
1868
1869                         return this;
1870                 }
1871
1872                 public override void Emit (EmitContext ec)
1873                 {
1874                         base.Emit (ec);
1875
1876                         ec.ig.Emit (OpCodes.Castclass, type);
1877                 }                       
1878                 
1879         }
1880         
1881         //
1882         // SimpleName expressions are initially formed of a single
1883         // word and it only happens at the beginning of the expression.
1884         //
1885         // The expression will try to be bound to a Field, a Method
1886         // group or a Property.  If those fail we pass the name to our
1887         // caller and the SimpleName is compounded to perform a type
1888         // lookup.  The idea behind this process is that we want to avoid
1889         // creating a namespace map from the assemblies, as that requires
1890         // the GetExportedTypes function to be called and a hashtable to
1891         // be constructed which reduces startup time.  If later we find
1892         // that this is slower, we should create a `NamespaceExpr' expression
1893         // that fully participates in the resolution process. 
1894         //
1895         // For example `System.Console.WriteLine' is decomposed into
1896         // MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
1897         //
1898         // The first SimpleName wont produce a match on its own, so it will
1899         // be turned into:
1900         // MemberAccess (SimpleName ("System.Console"), "WriteLine").
1901         //
1902         // System.Console will produce a TypeExpr match.
1903         //
1904         // The downside of this is that we might be hitting `LookupType' too many
1905         // times with this scheme.
1906         //
1907         public class SimpleName : Expression {
1908                 public readonly string Name;
1909                 public readonly Location Location;
1910                 
1911                 public SimpleName (string name, Location l)
1912                 {
1913                         Name = name;
1914                         Location = l;
1915                 }
1916
1917                 public static void Error120 (Location l, string name)
1918                 {
1919                         Report.Error (
1920                                 120, l,
1921                                 "An object reference is required " +
1922                                 "for the non-static field `"+name+"'");
1923                 }
1924                 
1925                 //
1926                 // Checks whether we are trying to access an instance
1927                 // property, method or field from a static body.
1928                 //
1929                 Expression MemberStaticCheck (Expression e)
1930                 {
1931                         if (e is FieldExpr){
1932                                 FieldInfo fi = ((FieldExpr) e).FieldInfo;
1933                                 
1934                                 if (!fi.IsStatic){
1935                                         Error120 (Location, Name);
1936                                         return null;
1937                                 }
1938                         } else if (e is MethodGroupExpr){
1939                                 MethodGroupExpr mg = (MethodGroupExpr) e;
1940
1941                                 if (!mg.RemoveInstanceMethods ()){
1942                                         Error120 (Location, mg.Methods [0].Name);
1943                                         return null;
1944                                 }
1945                                 return e;
1946                         } else if (e is PropertyExpr){
1947                                 if (!((PropertyExpr) e).IsStatic){
1948                                         Error120 (Location, Name);
1949                                         return null;
1950                                 }
1951                         }
1952
1953                         return e;
1954                 }
1955                 
1956                 //
1957                 // 7.5.2: Simple Names. 
1958                 //
1959                 // Local Variables and Parameters are handled at
1960                 // parse time, so they never occur as SimpleNames.
1961                 //
1962                 public override Expression DoResolve (EmitContext ec)
1963                 {
1964                         Expression e;
1965
1966                         //
1967                         // Stage 1: Performed by the parser (binding to local or parameters).
1968                         //
1969
1970                         //
1971                         // Stage 2: Lookup members
1972                         //
1973                         e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, true, Location);
1974                         if (e == null){
1975                                 //
1976                                 // Stage 3: Lookup symbol in the various namespaces. 
1977                                 // 
1978                                 Type t;
1979                                 
1980                                 if ((t = ec.TypeContainer.LookupType (Name, true)) != null)
1981                                         return new TypeExpr (t);
1982
1983                                 //
1984                                 // Stage 3 part b: Lookup up if we are an alias to a type
1985                                 // or a namespace.
1986                                 //
1987                                 // Since we are cheating: we only do the Alias lookup for
1988                                 // namespaces if the name does not include any dots in it
1989                                 //
1990                                 
1991                                 // IMPLEMENT ME.  Read mcs/mcs/TODO for ideas, or rewrite
1992                                 // using NamespaceExprs (dunno how that fixes the alias
1993                                 // per-file though).
1994                                 
1995                                 // No match, maybe our parent can compose us
1996                                 // into something meaningful.
1997                                 //
1998                                 return this;
1999                         }
2000
2001                         // Step 2, continues here.
2002                         if (e is TypeExpr)
2003                                 return e;
2004
2005                         if (e is FieldExpr){
2006                                 FieldExpr fe = (FieldExpr) e;
2007                                 
2008                                 if (!fe.FieldInfo.IsStatic)
2009                                         fe.InstanceExpression = new This (Location.Null);
2010                         }                               
2011
2012                         if (ec.IsStatic)
2013                                 return MemberStaticCheck (e);
2014                         else
2015                                 return e;
2016                 }
2017
2018                 public override void Emit (EmitContext ec)
2019                 {
2020                         //
2021                         // If this is ever reached, then we failed to
2022                         // find the name as a namespace
2023                         //
2024
2025                         Error (103, Location, "The name `" + Name +
2026                                "' does not exist in the class `" +
2027                                ec.TypeContainer.Name + "'");
2028                 }
2029         }
2030         
2031         // <summary>
2032         //   Fully resolved expression that evaluates to a type
2033         // </summary>
2034         public class TypeExpr : Expression {
2035                 public TypeExpr (Type t)
2036                 {
2037                         Type = t;
2038                         eclass = ExprClass.Type;
2039                 }
2040
2041                 override public Expression DoResolve (EmitContext ec)
2042                 {
2043                         return this;
2044                 }
2045
2046                 override public void Emit (EmitContext ec)
2047                 {
2048                         throw new Exception ("Implement me");
2049                 }
2050         }
2051
2052         // <summary>
2053         //   MethodGroup Expression.
2054         //  
2055         //   This is a fully resolved expression that evaluates to a type
2056         // </summary>
2057         public class MethodGroupExpr : Expression {
2058                 public MethodBase [] Methods;
2059                 Expression instance_expression = null;
2060                 
2061                 public MethodGroupExpr (MemberInfo [] mi)
2062                 {
2063                         Methods = new MethodBase [mi.Length];
2064                         mi.CopyTo (Methods, 0);
2065                         eclass = ExprClass.MethodGroup;
2066                 }
2067
2068                 public MethodGroupExpr (ArrayList l)
2069                 {
2070                         Methods = new MethodBase [l.Count];
2071
2072                         l.CopyTo (Methods, 0);
2073                         eclass = ExprClass.MethodGroup;
2074                 }
2075                 
2076                 //
2077                 // `A method group may have associated an instance expression' 
2078                 // 
2079                 public Expression InstanceExpression {
2080                         get {
2081                                 return instance_expression;
2082                         }
2083
2084                         set {
2085                                 instance_expression = value;
2086                         }
2087                 }
2088                 
2089                 override public Expression DoResolve (EmitContext ec)
2090                 {
2091                         return this;
2092                 }
2093
2094                 override public void Emit (EmitContext ec)
2095                 {
2096                         throw new Exception ("This should never be reached");
2097                 }
2098
2099                 bool RemoveMethods (bool keep_static)
2100                 {
2101                         ArrayList smethods = new ArrayList ();
2102                         int top = Methods.Length;
2103                         int i;
2104                         
2105                         for (i = 0; i < top; i++){
2106                                 MethodBase mb = Methods [i];
2107
2108                                 if (mb.IsStatic == keep_static)
2109                                         smethods.Add (mb);
2110                         }
2111
2112                         if (smethods.Count == 0)
2113                                 return false;
2114
2115                         Methods = new MethodBase [smethods.Count];
2116                         smethods.CopyTo (Methods, 0);
2117
2118                         return true;
2119                 }
2120                 
2121                 // <summary>
2122                 //   Removes any instance methods from the MethodGroup, returns
2123                 //   false if the resulting set is empty.
2124                 // </summary>
2125                 public bool RemoveInstanceMethods ()
2126                 {
2127                         return RemoveMethods (true);
2128                 }
2129
2130                 // <summary>
2131                 //   Removes any static methods from the MethodGroup, returns
2132                 //   false if the resulting set is empty.
2133                 // </summary>
2134                 public bool RemoveStaticMethods ()
2135                 {
2136                         return RemoveMethods (false);
2137                 }
2138         }
2139
2140         // <summary>
2141         //   Fully resolved expression that evaluates to a Field
2142         // </summary>
2143         public class FieldExpr : Expression, IStackStore, IMemoryLocation {
2144                 public readonly FieldInfo FieldInfo;
2145                 public Expression InstanceExpression;
2146                 Location loc;
2147                 
2148                 public FieldExpr (FieldInfo fi, Location l)
2149                 {
2150                         FieldInfo = fi;
2151                         eclass = ExprClass.Variable;
2152                         type = fi.FieldType;
2153                         loc = l;
2154                 }
2155
2156                 override public Expression DoResolve (EmitContext ec)
2157                 {
2158                         if (!FieldInfo.IsStatic){
2159                                 if (InstanceExpression == null){
2160                                         throw new Exception ("non-static FieldExpr without instance var\n" +
2161                                                              "You have to assign the Instance variable\n" +
2162                                                              "Of the FieldExpr to set this\n");
2163                                 }
2164
2165                                 InstanceExpression = InstanceExpression.Resolve (ec);
2166                                 if (InstanceExpression == null)
2167                                         return null;
2168                                 
2169                         }
2170                         return this;
2171                 }
2172
2173                 public Expression DoResolveLValue (EmitContext ec)
2174                 {
2175                         if (!FieldInfo.IsInitOnly)
2176                                 return this;
2177
2178                         //
2179                         // InitOnly fields can only be assigned in constructors
2180                         //
2181
2182                         if (ec.IsConstructor)
2183                                 return this;
2184
2185                         Report.Error (191, loc,
2186                                       "Readonly field can not be assigned outside " +
2187                                       "of constructor or variable initializer");
2188                         
2189                         return null;
2190                 }
2191
2192                 override public void Emit (EmitContext ec)
2193                 {
2194                         ILGenerator ig = ec.ig;
2195
2196                         if (FieldInfo.IsStatic)
2197                                 ig.Emit (OpCodes.Ldsfld, FieldInfo);
2198                         else {
2199                                 InstanceExpression.Emit (ec);
2200                                 
2201                                 ig.Emit (OpCodes.Ldfld, FieldInfo);
2202                         }
2203                 }
2204
2205                 public void Store (EmitContext ec)
2206                 {
2207                         if (FieldInfo.IsStatic)
2208                                 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
2209                         else
2210                                 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
2211                 }
2212
2213                 public void AddressOf (EmitContext ec)
2214                 {
2215                         if (FieldInfo.IsStatic)
2216                                 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
2217                         else {
2218                                 InstanceExpression.Emit (ec);
2219                                 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
2220                         }
2221                 }
2222         }
2223         
2224         // <summary>
2225         //   Expression that evaluates to a Property.  The Assign class
2226         //   might set the `Value' expression if we are in an assignment.
2227         //
2228         //   This is not an LValue because we need to re-write the expression, we
2229         //   can not take data from the stack and store it.  
2230         // </summary>
2231         public class PropertyExpr : ExpressionStatement, IAssignMethod {
2232                 public readonly PropertyInfo PropertyInfo;
2233                 public readonly bool IsStatic;
2234                 MethodInfo [] Accessors;
2235                 Location loc;
2236                 
2237                 Expression instance_expr;
2238                 
2239                 public PropertyExpr (PropertyInfo pi, Location l)
2240                 {
2241                         PropertyInfo = pi;
2242                         eclass = ExprClass.PropertyAccess;
2243                         IsStatic = false;
2244                         loc = l;
2245                         Accessors = TypeManager.GetAccessors (pi);
2246
2247                         if (Accessors != null)
2248                                 for (int i = 0; i < Accessors.Length; i++){
2249                                         if (Accessors [i] != null)
2250                                                 if (Accessors [i].IsStatic)
2251                                                         IsStatic = true;
2252                                 }
2253                         else
2254                                 Accessors = new MethodInfo [2];
2255                         
2256                         type = pi.PropertyType;
2257                 }
2258
2259                 //
2260                 // The instance expression associated with this expression
2261                 //
2262                 public Expression InstanceExpression {
2263                         set {
2264                                 instance_expr = value;
2265                         }
2266
2267                         get {
2268                                 return instance_expr;
2269                         }
2270                 }
2271
2272                 public bool VerifyAssignable ()
2273                 {
2274                         if (!PropertyInfo.CanWrite){
2275                                 Report.Error (200, loc, 
2276                                               "The property `" + PropertyInfo.Name +
2277                                               "' can not be assigned to, as it has not set accessor");
2278                                 return false;
2279                         }
2280
2281                         return true;
2282                 }
2283
2284                 override public Expression DoResolve (EmitContext ec)
2285                 {
2286                         if (!PropertyInfo.CanRead){
2287                                 Report.Error (154, loc, 
2288                                               "The property `" + PropertyInfo.Name +
2289                                               "' can not be used in " +
2290                                               "this context because it lacks a get accessor");
2291                                 return null;
2292                         }
2293
2294                         return this;
2295                 }
2296
2297                 override public void Emit (EmitContext ec)
2298                 {
2299                         Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [0], null);
2300                         
2301                 }
2302
2303                 //
2304                 // Implements the IAssignMethod interface for assignments
2305                 //
2306                 public void EmitAssign (EmitContext ec, Expression source)
2307                 {
2308                         Argument arg = new Argument (source, Argument.AType.Expression);
2309                         ArrayList args = new ArrayList ();
2310
2311                         args.Add (arg);
2312                         Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [1], args);
2313                 }
2314
2315                 override public void EmitStatement (EmitContext ec)
2316                 {
2317                         Emit (ec);
2318                         ec.ig.Emit (OpCodes.Pop);
2319                 }
2320         }
2321
2322         // <summary>
2323         //   Fully resolved expression that evaluates to a Expression
2324         // </summary>
2325         public class EventExpr : Expression {
2326                 public readonly EventInfo EventInfo;
2327                 Location loc;
2328                 
2329                 public EventExpr (EventInfo ei, Location loc)
2330                 {
2331                         EventInfo = ei;
2332                         this.loc = loc;
2333                         eclass = ExprClass.EventAccess;
2334                 }
2335
2336                 override public Expression DoResolve (EmitContext ec)
2337                 {
2338                         // We are born in resolved state. 
2339                         return this;
2340                 }
2341
2342                 override public void Emit (EmitContext ec)
2343                 {
2344                         throw new Exception ("Implement me");
2345                         // FIXME: Implement.
2346                 }
2347         }
2348         
2349 }