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