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