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