2001-12-20 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / mcs / ecore.cs
1 //
2 // ecore.cs: Core of the Expression representation for the intermediate tree.
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.
8 //
9 //
10
11 namespace 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                 // We copy methods from `new_members' into `target_list' if the signature
332                 // for the method from in the new list does not exist in the target_list
333                 //
334                 // The name is assumed to be the same.
335                 //
336                 static ArrayList CopyNewMethods (ArrayList target_list, MemberInfo [] new_members)
337                 {
338                         if (target_list == null){
339                                 target_list = new ArrayList ();
340
341                                 target_list.AddRange (new_members);
342                                 return target_list;
343                         }
344                         
345                         MemberInfo [] target_array = new MemberInfo [target_list.Count];
346                         target_list.CopyTo (target_array, 0);
347                         
348                         foreach (MemberInfo mi in new_members){
349                                 MethodBase new_method = (MethodBase) mi;
350                                 Type [] new_args = TypeManager.GetArgumentTypes (new_method);
351                                         
352                                 foreach (MethodBase method in target_array){
353                                         Type [] old_args = TypeManager.GetArgumentTypes (method);
354                                         int new_count = new_args.Length;
355                                         int old_count = old_args.Length;
356                                         
357                                         if (new_count != old_count){
358                                                 target_list.Add (method);
359                                                 continue;
360                                         }
361
362                                         for (int i = 0; i < old_count; i++){
363                                                 if (old_args [i] == new_args [i])
364                                                         continue;
365                                                 target_list.Add (method);
366                                                 break;
367                                         }
368                                 }
369                         }
370                         return target_list;
371                 }
372                 
373                 //
374                 // FIXME: Probably implement a cache for (t,name,current_access_set)?
375                 //
376                 // FIXME: We need to cope with access permissions here, or this wont
377                 // work!
378                 //
379                 // This code could use some optimizations, but we need to do some
380                 // measurements.  For example, we could use a delegate to `flag' when
381                 // something can not any longer be a method-group (because it is something
382                 // else).
383                 //
384                 // Return values:
385                 //     If the return value is an Array, then it is an array of
386                 //     MethodBases
387                 //   
388                 //     If the return value is an MemberInfo, it is anything, but a Method
389                 //
390                 //     null on error.
391                 //
392                 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
393                 // the arguments here and have MemberLookup return only the methods that
394                 // match the argument count/type, unlike we are doing now (we delay this
395                 // decision).
396                 //
397                 // This is so we can catch correctly attempts to invoke instance methods
398                 // from a static body (scan for error 120 in ResolveSimpleName).
399                 //
400                 //
401                 // FIXME: Potential optimization, have a static ArrayList
402                 //
403                 public static Expression MemberLookup (EmitContext ec, Type t, string name,
404                                                        bool same_type, MemberTypes mt,
405                                                        BindingFlags bf, Location loc)
406                 {
407                         if (same_type)
408                                 bf |= BindingFlags.NonPublic;
409
410                         //
411                         // Lookup for members starting in the type requested and going
412                         // up the hierarchy until a match is found.
413                         //
414                         // As soon as a non-method match is found, we return.
415                         //
416                         // If methods are found though, then the search proceeds scanning
417                         // for more public methods in the hierarchy with signatures that
418                         // do not match any of the signatures found so far.
419                         //
420                         ArrayList method_list = null;
421                         Type current_type = t;
422                         bool searching = true;
423                         do {
424                                 MemberInfo [] mi;
425
426                                 mi = RootContext.TypeManager.FindMembers (
427                                         current_type, mt, bf | BindingFlags.DeclaredOnly,
428                                         Type.FilterName, name);
429                                 
430                                 if (current_type == TypeManager.object_type)
431                                         searching = false;
432                                 else {
433                                         current_type = current_type.BaseType;
434
435                                         //
436                                         // This happens with interfaces, they have a null
437                                         // basetype
438                                         //
439                                         if (current_type == null)
440                                                 searching = false;
441                                 }
442
443                                 if (mi == null)
444                                         continue;
445                                 
446                                 int count = mi.Length;
447
448                                 if (count == 0)
449                                         continue;
450                                 
451                                 if (count == 1 && !(mi [0] is MethodBase))
452                                         return Expression.ExprClassFromMemberInfo (ec, mi [0], loc);
453
454                                 //
455                                 // We found methods, turn the search into "method scan"
456                                 // mode.
457                                 //
458                                 method_list = CopyNewMethods (method_list, mi);
459                                 mt &= (MemberTypes.Method | MemberTypes.Constructor);
460                         } while (searching);
461
462                         if (method_list != null && method_list.Count > 0)
463                                 return new MethodGroupExpr (method_list);
464
465                         return null;
466                 }
467
468                 public const MemberTypes AllMemberTypes =
469                         MemberTypes.Constructor |
470                         MemberTypes.Event       |
471                         MemberTypes.Field       |
472                         MemberTypes.Method      |
473                         MemberTypes.NestedType  |
474                         MemberTypes.Property;
475                 
476                 public const BindingFlags AllBindingsFlags =
477                         BindingFlags.Public |
478                         BindingFlags.Static |
479                         BindingFlags.Instance;
480
481                 public static Expression MemberLookup (EmitContext ec, Type t, string name,
482                                                        bool same_type, Location loc)
483                 {
484                         return MemberLookup (ec, t, name, same_type, AllMemberTypes, AllBindingsFlags, loc);
485                 }
486
487                 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
488                 {
489                         Type expr_type = expr.Type;
490
491                         if (target_type == TypeManager.object_type) {
492                                 if (expr_type.IsClass)
493                                         return new EmptyCast (expr, target_type);
494                                 if (expr_type.IsValueType)
495                                         return new BoxedCast (expr);
496                         } else if (expr_type.IsSubclassOf (target_type)) {
497                                 return new EmptyCast (expr, target_type);
498                         } else {
499                                 // from any class-type S to any interface-type T.
500                                 if (expr_type.IsClass && target_type.IsInterface) {
501                                         if (TypeManager.ImplementsInterface (expr_type, target_type))
502                                                 return new EmptyCast (expr, target_type);
503                                         else
504                                                 return null;
505                                 }
506
507                                 // from any interface type S to interface-type T.
508                                 if (expr_type.IsInterface && target_type.IsInterface) {
509
510                                         if (TypeManager.ImplementsInterface (expr_type, target_type))
511                                                 return new EmptyCast (expr, target_type);
512                                         else
513                                                 return null;
514                                 }
515                                 
516                                 // from an array-type S to an array-type of type T
517                                 if (expr_type.IsArray && target_type.IsArray) {
518                                         if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
519
520                                                 Type expr_element_type = expr_type.GetElementType ();
521                                                 Type target_element_type = target_type.GetElementType ();
522
523                                                 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
524                                                         if (StandardConversionExists (expr_element_type,
525                                                                                       target_element_type))
526                                                                 return new EmptyCast (expr, target_type);
527                                         }
528                                 }
529                                 
530                                 
531                                 // from an array-type to System.Array
532                                 if (expr_type.IsArray && target_type == TypeManager.array_type)
533                                         return new EmptyCast (expr, target_type);
534                                 
535                                 // from any delegate type to System.Delegate
536                                 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
537                                     target_type == TypeManager.delegate_type)
538                                         return new EmptyCast (expr, target_type);
539                                         
540                                 // from any array-type or delegate type into System.ICloneable.
541                                 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
542                                         if (target_type == TypeManager.icloneable_type)
543                                                 return new EmptyCast (expr, target_type);
544                                 
545                                 // from the null type to any reference-type.
546                                 if (expr is NullLiteral)
547                                         return new EmptyCast (expr, target_type);
548
549                                 return null;
550
551                         }
552                         
553                         return null;
554                 }
555
556                 /// <summary>
557                 ///   Handles expressions like this: decimal d; d = 1;
558                 ///   and changes them into: decimal d; d = new System.Decimal (1);
559                 /// </summary>
560                 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
561                 {
562                         ArrayList args = new ArrayList ();
563
564                         args.Add (new Argument (expr, Argument.AType.Expression));
565
566                         Expression ne = new New (target.FullName, args,
567                                                  new Location (-1));
568
569                         return ne.Resolve (ec);
570                 }
571
572                 /// <summary>
573                 ///   Implicit Numeric Conversions.
574                 ///
575                 ///   expr is the expression to convert, returns a new expression of type
576                 ///   target_type or null if an implicit conversion is not possible.
577                 /// </summary>
578                 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
579                                                                     Type target_type, Location loc)
580                 {
581                         Type expr_type = expr.Type;
582                         
583                         //
584                         // Attempt to do the implicit constant expression conversions
585
586                         if (expr is IntLiteral){
587                                 Expression e;
588                                 
589                                 e = TryImplicitIntConversion (target_type, (IntLiteral) expr);
590
591                                 if (e != null)
592                                         return e;
593                         } else if (expr is LongLiteral && target_type == TypeManager.uint64_type){
594                                 //
595                                 // Try the implicit constant expression conversion
596                                 // from long to ulong, instead of a nice routine,
597                                 // we just inline it
598                                 //
599                                 if (((LongLiteral) expr).Value > 0)
600                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
601                         }
602                         
603                         if (expr_type == TypeManager.sbyte_type){
604                                 //
605                                 // From sbyte to short, int, long, float, double.
606                                 //
607                                 if (target_type == TypeManager.int32_type)
608                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
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.short_type)
616                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
617                                 if (target_type == TypeManager.decimal_type)
618                                         return InternalTypeConstructor (ec, expr, target_type);
619                         } else if (expr_type == TypeManager.byte_type){
620                                 //
621                                 // From byte to short, ushort, int, uint, long, ulong, float, double
622                                 // 
623                                 if ((target_type == TypeManager.short_type) ||
624                                     (target_type == TypeManager.ushort_type) ||
625                                     (target_type == TypeManager.int32_type) ||
626                                     (target_type == TypeManager.uint32_type))
627                                         return new EmptyCast (expr, target_type);
628
629                                 if (target_type == TypeManager.uint64_type)
630                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
631                                 if (target_type == TypeManager.int64_type)
632                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
633                                 if (target_type == TypeManager.float_type)
634                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
635                                 if (target_type == TypeManager.double_type)
636                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
637                                 if (target_type == TypeManager.decimal_type)
638                                         return InternalTypeConstructor (ec, expr, target_type);
639                         } else if (expr_type == TypeManager.short_type){
640                                 //
641                                 // From short to int, long, float, double
642                                 // 
643                                 if (target_type == TypeManager.int32_type)
644                                         return new EmptyCast (expr, target_type);
645                                 if (target_type == TypeManager.int64_type)
646                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
647                                 if (target_type == TypeManager.double_type)
648                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
649                                 if (target_type == TypeManager.float_type)
650                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
651                                 if (target_type == TypeManager.decimal_type)
652                                         return InternalTypeConstructor (ec, expr, target_type);
653                         } else if (expr_type == TypeManager.ushort_type){
654                                 //
655                                 // From ushort to int, uint, long, ulong, float, double
656                                 //
657                                 if (target_type == TypeManager.uint32_type)
658                                         return new EmptyCast (expr, target_type);
659
660                                 if (target_type == TypeManager.uint64_type)
661                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
662                                 if (target_type == TypeManager.int32_type)
663                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
664                                 if (target_type == TypeManager.int64_type)
665                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
666                                 if (target_type == TypeManager.double_type)
667                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
668                                 if (target_type == TypeManager.float_type)
669                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
670                                 if (target_type == TypeManager.decimal_type)
671                                         return InternalTypeConstructor (ec, expr, target_type);
672                         } else if (expr_type == TypeManager.int32_type){
673                                 //
674                                 // From int to long, float, double
675                                 //
676                                 if (target_type == TypeManager.int64_type)
677                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
678                                 if (target_type == TypeManager.double_type)
679                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
680                                 if (target_type == TypeManager.float_type)
681                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
682                                 if (target_type == TypeManager.decimal_type)
683                                         return InternalTypeConstructor (ec, expr, target_type);
684                         } else if (expr_type == TypeManager.uint32_type){
685                                 //
686                                 // From uint to long, ulong, float, double
687                                 //
688                                 if (target_type == TypeManager.int64_type)
689                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
690                                 if (target_type == TypeManager.uint64_type)
691                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
692                                 if (target_type == TypeManager.double_type)
693                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
694                                                                OpCodes.Conv_R8);
695                                 if (target_type == TypeManager.float_type)
696                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
697                                                                OpCodes.Conv_R4);
698                                 if (target_type == TypeManager.decimal_type)
699                                         return InternalTypeConstructor (ec, expr, target_type);
700                         } else if ((expr_type == TypeManager.uint64_type) ||
701                                    (expr_type == TypeManager.int64_type)){
702                                 //
703                                 // From long/ulong to float, double
704                                 //
705                                 if (target_type == TypeManager.double_type)
706                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
707                                                                OpCodes.Conv_R8);
708                                 if (target_type == TypeManager.float_type)
709                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
710                                                                OpCodes.Conv_R4);        
711                                 if (target_type == TypeManager.decimal_type)
712                                         return InternalTypeConstructor (ec, expr, target_type);
713                         } else if (expr_type == TypeManager.char_type){
714                                 //
715                                 // From char to ushort, int, uint, long, ulong, float, double
716                                 // 
717                                 if ((target_type == TypeManager.ushort_type) ||
718                                     (target_type == TypeManager.int32_type) ||
719                                     (target_type == TypeManager.uint32_type))
720                                         return new EmptyCast (expr, target_type);
721                                 if (target_type == TypeManager.uint64_type)
722                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
723                                 if (target_type == TypeManager.int64_type)
724                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
725                                 if (target_type == TypeManager.float_type)
726                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
727                                 if (target_type == TypeManager.double_type)
728                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
729                                 if (target_type == TypeManager.decimal_type)
730                                         return InternalTypeConstructor (ec, expr, target_type);
731                         } else if (expr_type == TypeManager.float_type){
732                                 //
733                                 // float to double
734                                 //
735                                 if (target_type == TypeManager.double_type)
736                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
737                         }
738
739                         return null;
740                 }
741
742                 /// <summary>
743                 ///  Determines if a standard implicit conversion exists from
744                 ///  expr_type to target_type
745                 /// </summary>
746                 public static bool StandardConversionExists (Type expr_type, Type target_type)
747                 {
748                         if (expr_type == target_type)
749                                 return true;
750
751                         // First numeric conversions 
752                         
753                         if (expr_type == TypeManager.sbyte_type){
754                                 //
755                                 // From sbyte to short, int, long, float, double.
756                                 //
757                                 if ((target_type == TypeManager.int32_type) || 
758                                     (target_type == TypeManager.int64_type) ||
759                                     (target_type == TypeManager.double_type) ||
760                                     (target_type == TypeManager.float_type)  ||
761                                     (target_type == TypeManager.short_type) ||
762                                     (target_type == TypeManager.decimal_type))
763                                         return true;
764                                 
765                         } else if (expr_type == TypeManager.byte_type){
766                                 //
767                                 // From byte to short, ushort, int, uint, long, ulong, float, double
768                                 // 
769                                 if ((target_type == TypeManager.short_type) ||
770                                     (target_type == TypeManager.ushort_type) ||
771                                     (target_type == TypeManager.int32_type) ||
772                                     (target_type == TypeManager.uint32_type) ||
773                                     (target_type == TypeManager.uint64_type) ||
774                                     (target_type == TypeManager.int64_type) ||
775                                     (target_type == TypeManager.float_type) ||
776                                     (target_type == TypeManager.double_type) ||
777                                     (target_type == TypeManager.decimal_type))
778                                         return true;
779         
780                         } else if (expr_type == TypeManager.short_type){
781                                 //
782                                 // From short to int, long, float, double
783                                 // 
784                                 if ((target_type == TypeManager.int32_type) ||
785                                     (target_type == TypeManager.int64_type) ||
786                                     (target_type == TypeManager.double_type) ||
787                                     (target_type == TypeManager.float_type) ||
788                                     (target_type == TypeManager.decimal_type))
789                                         return true;
790                                         
791                         } else if (expr_type == TypeManager.ushort_type){
792                                 //
793                                 // From ushort to int, uint, long, ulong, float, double
794                                 //
795                                 if ((target_type == TypeManager.uint32_type) ||
796                                     (target_type == TypeManager.uint64_type) ||
797                                     (target_type == TypeManager.int32_type) ||
798                                     (target_type == TypeManager.int64_type) ||
799                                     (target_type == TypeManager.double_type) ||
800                                     (target_type == TypeManager.float_type) ||
801                                     (target_type == TypeManager.decimal_type))
802                                         return true;
803                                     
804                         } else if (expr_type == TypeManager.int32_type){
805                                 //
806                                 // From int to long, float, double
807                                 //
808                                 if ((target_type == TypeManager.int64_type) ||
809                                     (target_type == TypeManager.double_type) ||
810                                     (target_type == TypeManager.float_type) ||
811                                     (target_type == TypeManager.decimal_type))
812                                         return true;
813                                         
814                         } else if (expr_type == TypeManager.uint32_type){
815                                 //
816                                 // From uint to long, ulong, float, double
817                                 //
818                                 if ((target_type == TypeManager.int64_type) ||
819                                     (target_type == TypeManager.uint64_type) ||
820                                     (target_type == TypeManager.double_type) ||
821                                     (target_type == TypeManager.float_type) ||
822                                     (target_type == TypeManager.decimal_type))
823                                         return true;
824                                         
825                         } else if ((expr_type == TypeManager.uint64_type) ||
826                                    (expr_type == TypeManager.int64_type)) {
827                                 //
828                                 // From long/ulong to float, double
829                                 //
830                                 if ((target_type == TypeManager.double_type) ||
831                                     (target_type == TypeManager.float_type) ||
832                                     (target_type == TypeManager.decimal_type))
833                                         return true;
834                                     
835                         } else if (expr_type == TypeManager.char_type){
836                                 //
837                                 // From char to ushort, int, uint, long, ulong, float, double
838                                 // 
839                                 if ((target_type == TypeManager.ushort_type) ||
840                                     (target_type == TypeManager.int32_type) ||
841                                     (target_type == TypeManager.uint32_type) ||
842                                     (target_type == TypeManager.uint64_type) ||
843                                     (target_type == TypeManager.int64_type) ||
844                                     (target_type == TypeManager.float_type) ||
845                                     (target_type == TypeManager.double_type) ||
846                                     (target_type == TypeManager.decimal_type))
847                                         return true;
848
849                         } else if (expr_type == TypeManager.float_type){
850                                 //
851                                 // float to double
852                                 //
853                                 if (target_type == TypeManager.double_type)
854                                         return true;
855                         }       
856                         
857                         // Next reference conversions
858
859                         if (target_type == TypeManager.object_type) {
860                                 if ((expr_type.IsClass) ||
861                                     (expr_type.IsValueType))
862                                         return true;
863                                 
864                         } else if (expr_type.IsSubclassOf (target_type)) {
865                                 return true;
866                                 
867                         } else {
868                                 // from any class-type S to any interface-type T.
869                                 if (expr_type.IsClass && target_type.IsInterface)
870                                         return true;
871                                 
872                                 // from any interface type S to interface-type T.
873                                 // FIXME : Is it right to use IsAssignableFrom ?
874                                 if (expr_type.IsInterface && target_type.IsInterface)
875                                         if (target_type.IsAssignableFrom (expr_type))
876                                                 return true;
877                                 
878                                 // from an array-type S to an array-type of type T
879                                 if (expr_type.IsArray && target_type.IsArray) {
880                                         if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
881                                                 
882                                                 Type expr_element_type = expr_type.GetElementType ();
883                                                 Type target_element_type = target_type.GetElementType ();
884                                                 
885                                                 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
886                                                         if (StandardConversionExists (expr_element_type,
887                                                                                       target_element_type))
888                                                                 return true;
889                                         }
890                                 }
891                                 
892                                 // from an array-type to System.Array
893                                 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
894                                         return true;
895                                 
896                                 // from any delegate type to System.Delegate
897                                 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
898                                     target_type == TypeManager.delegate_type)
899                                         if (target_type.IsAssignableFrom (expr_type))
900                                                 return true;
901                                         
902                                 // from any array-type or delegate type into System.ICloneable.
903                                 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
904                                         if (target_type == TypeManager.icloneable_type)
905                                                 return true;
906                                 
907                                 // from the null type to any reference-type.
908                                 // FIXME : How do we do this ?
909
910                         }
911
912                         return false;
913                 }
914
915                 static EmptyExpression MyEmptyExpr;
916                 /// <summary>
917                 ///   Tells whether an implicit conversion exists from expr_type to
918                 ///   target_type
919                 /// </summary>
920                 public bool ImplicitConversionExists (EmitContext ec, Type expr_type, Type target_type,
921                                                       Location l)
922                 {
923                         if (MyEmptyExpr == null)
924                                 MyEmptyExpr = new EmptyExpression (expr_type);
925                         else
926                                 MyEmptyExpr.SetType (expr_type);
927
928                         return ConvertImplicit (ec, MyEmptyExpr, target_type, l) != null;
929                 }
930                 
931                 /// <summary>
932                 ///  Finds "most encompassed type" according to the spec (13.4.2)
933                 ///  amongst the methods in the MethodGroupExpr which convert from a
934                 ///  type encompassing source_type
935                 /// </summary>
936                 static Type FindMostEncompassedType (MethodGroupExpr me, Type source_type)
937                 {
938                         Type best = null;
939                         
940                         for (int i = me.Methods.Length; i > 0; ) {
941                                 i--;
942
943                                 MethodBase mb = me.Methods [i];
944                                 ParameterData pd = Invocation.GetParameterData (mb);
945                                 Type param_type = pd.ParameterType (0);
946
947                                 if (StandardConversionExists (source_type, param_type)) {
948                                         if (best == null)
949                                                 best = param_type;
950                                         
951                                         if (StandardConversionExists (param_type, best))
952                                                 best = param_type;
953                                 }
954                         }
955
956                         return best;
957                 }
958                 
959                 /// <summary>
960                 ///  Finds "most encompassing type" according to the spec (13.4.2)
961                 ///  amongst the methods in the MethodGroupExpr which convert to a
962                 ///  type encompassed by target_type
963                 /// </summary>
964                 static Type FindMostEncompassingType (MethodGroupExpr me, Type target)
965                 {
966                         Type best = null;
967                         
968                         for (int i = me.Methods.Length; i > 0; ) {
969                                 i--;
970                                 
971                                 MethodInfo mi = (MethodInfo) me.Methods [i];
972                                 Type ret_type = mi.ReturnType;
973                                 
974                                 if (StandardConversionExists (ret_type, target)) {
975                                         if (best == null)
976                                                 best = ret_type;
977
978                                         if (!StandardConversionExists (ret_type, best))
979                                                 best = ret_type;
980                                 }
981                                 
982                         }
983                         
984                         return best;
985
986                 }
987                 
988
989                 /// <summary>
990                 ///  User-defined Implicit conversions
991                 /// </summary>
992                 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
993                                                                  Type target, Location loc)
994                 {
995                         return UserDefinedConversion (ec, source, target, loc, false);
996                 }
997
998                 /// <summary>
999                 ///  User-defined Explicit conversions
1000                 /// </summary>
1001                 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1002                                                                  Type target, Location loc)
1003                 {
1004                         return UserDefinedConversion (ec, source, target, loc, true);
1005                 }
1006                 
1007                 /// <summary>
1008                 ///   User-defined conversions
1009                 /// </summary>
1010                 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1011                                                                 Type target, Location loc,
1012                                                                 bool look_for_explicit)
1013                 {
1014                         Expression mg1 = null, mg2 = null, mg3 = null, mg4 = null;
1015                         Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1016                         Expression e;
1017                         MethodBase method = null;
1018                         Type source_type = source.Type;
1019
1020                         string op_name;
1021                         
1022                         // If we have a boolean type, we need to check for the True operator
1023
1024                         // FIXME : How does the False operator come into the picture ?
1025                         // FIXME : This doesn't look complete and very correct !
1026                         if (target == TypeManager.bool_type)
1027                                 op_name = "op_True";
1028                         else
1029                                 op_name = "op_Implicit";
1030                         
1031                         mg1 = MemberLookup (ec, source_type, op_name, false, loc);
1032
1033                         if (source_type.BaseType != null)
1034                                 mg2 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
1035                         
1036                         mg3 = MemberLookup (ec, target, op_name, false, loc);
1037
1038                         if (target.BaseType != null)
1039                                 mg4 = MemberLookup (ec, target.BaseType, op_name, false, loc);
1040
1041                         MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
1042                         MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
1043
1044                         MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2);
1045
1046                         MethodGroupExpr union4 = null;
1047
1048                         if (look_for_explicit) {
1049
1050                                 op_name = "op_Explicit";
1051                                 
1052                                 mg5 = MemberLookup (ec, source_type, op_name, false, loc);
1053
1054                                 if (source_type.BaseType != null)
1055                                         mg6 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
1056                                 
1057                                 mg7 = MemberLookup (ec, target, op_name, false, loc);
1058                                 
1059                                 if (target.BaseType != null)
1060                                         mg8 = MemberLookup (ec, target.BaseType, op_name, false, loc);
1061                                 
1062                                 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
1063                                 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
1064
1065                                 union4 = Invocation.MakeUnionSet (union5, union6);
1066                         }
1067                         
1068                         MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4);
1069
1070                         if (union != null) {
1071
1072                                 Type most_specific_source, most_specific_target;
1073
1074                                 most_specific_source = FindMostEncompassedType (union, source_type);
1075                                 if (most_specific_source == null)
1076                                         return null;
1077
1078                                 most_specific_target = FindMostEncompassingType (union, target);
1079                                 if (most_specific_target == null) 
1080                                         return null;
1081                                 
1082                                 int count = 0;
1083                                 
1084                                 for (int i = union.Methods.Length; i > 0;) {
1085                                         i--;
1086
1087                                         MethodBase mb = union.Methods [i];
1088                                         ParameterData pd = Invocation.GetParameterData (mb);
1089                                         MethodInfo mi = (MethodInfo) union.Methods [i];
1090
1091                                         if (pd.ParameterType (0) == most_specific_source &&
1092                                             mi.ReturnType == most_specific_target) {
1093                                                 method = mb;
1094                                                 count++;
1095                                         }
1096                                 }
1097
1098                                 if (method == null || count > 1) {
1099                                         Report.Error (-11, loc, "Ambiguous user defined conversion");
1100                                         return null;
1101                                 }
1102                                 
1103                                 //
1104                                 // This will do the conversion to the best match that we
1105                                 // found.  Now we need to perform an implict standard conversion
1106                                 // if the best match was not the type that we were requested
1107                                 // by target.
1108                                 //
1109                                 if (look_for_explicit)
1110                                         source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1111                                 else
1112                                         source = ConvertImplicitStandard (ec, source,
1113                                                                           most_specific_source, loc);
1114
1115                                 if (source == null)
1116                                         return null;
1117
1118                                 e =  new UserCast ((MethodInfo) method, source);
1119                                 
1120                                 if (e.Type != target){
1121                                         if (!look_for_explicit)
1122                                                 e = ConvertImplicitStandard (ec, e, target, loc);
1123                                         else
1124                                                 e = ConvertExplicitStandard (ec, e, target, loc);
1125
1126                                         return e;
1127                                 } else
1128                                         return e;
1129                         }
1130                         
1131                         return null;
1132                 }
1133                 
1134                 /// <summary>
1135                 ///   Converts implicitly the resolved expression `expr' into the
1136                 ///   `target_type'.  It returns a new expression that can be used
1137                 ///   in a context that expects a `target_type'. 
1138                 /// </summary>
1139                 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1140                                                           Type target_type, Location loc)
1141                 {
1142                         Type expr_type = expr.Type;
1143                         Expression e;
1144
1145                         if (expr_type == target_type)
1146                                 return expr;
1147
1148                         if (target_type == null)
1149                                 throw new Exception ("Target type is null");
1150
1151                         e = ImplicitNumericConversion (ec, expr, target_type, loc);
1152                         if (e != null)
1153                                 return e;
1154
1155                         e = ImplicitReferenceConversion (expr, target_type);
1156                         if (e != null)
1157                                 return e;
1158
1159                         e = ImplicitUserConversion (ec, expr, target_type, loc);
1160                         if (e != null)
1161                                 return e;
1162
1163                         if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1164                                 IntLiteral i = (IntLiteral) expr;
1165
1166                                 if (i.Value == 0)
1167                                         return new EmptyCast (expr, target_type);
1168                         }
1169
1170                         return null;
1171                 }
1172
1173                 
1174                 /// <summary>
1175                 ///   Attempts to apply the `Standard Implicit
1176                 ///   Conversion' rules to the expression `expr' into
1177                 ///   the `target_type'.  It returns a new expression
1178                 ///   that can be used in a context that expects a
1179                 ///   `target_type'.
1180                 ///
1181                 ///   This is different from `ConvertImplicit' in that the
1182                 ///   user defined implicit conversions are excluded. 
1183                 /// </summary>
1184                 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1185                                                                   Type target_type, Location loc)
1186                 {
1187                         Type expr_type = expr.Type;
1188                         Expression e;
1189
1190                         if (expr_type == target_type)
1191                                 return expr;
1192
1193                         e = ImplicitNumericConversion (ec, expr, target_type, loc);
1194                         if (e != null)
1195                                 return e;
1196
1197                         e = ImplicitReferenceConversion (expr, target_type);
1198                         if (e != null)
1199                                 return e;
1200
1201                         if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1202                                 IntLiteral i = (IntLiteral) expr;
1203
1204                                 if (i.Value == 0)
1205                                         return new EmptyCast (expr, target_type);
1206                         }
1207                         return null;
1208                 }
1209
1210                 /// <summary>
1211                 ///   Attemps to perform an implict constant conversion of the IntLiteral
1212                 ///   into a different data type using casts (See Implicit Constant
1213                 ///   Expression Conversions)
1214                 /// </summary>
1215                 static protected Expression TryImplicitIntConversion (Type target_type, IntLiteral il)
1216                 {
1217                         int value = il.Value;
1218                         
1219                         if (target_type == TypeManager.sbyte_type){
1220                                 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1221                                         return new EmptyCast (il, target_type);
1222                         } else if (target_type == TypeManager.byte_type){
1223                                 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1224                                         return new EmptyCast (il, target_type);
1225                         } else if (target_type == TypeManager.short_type){
1226                                 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1227                                         return new EmptyCast (il, target_type);
1228                         } else if (target_type == TypeManager.ushort_type){
1229                                 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1230                                         return new EmptyCast (il, target_type);
1231                         } else if (target_type == TypeManager.uint32_type){
1232                                 //
1233                                 // we can optimize this case: a positive int32
1234                                 // always fits on a uint32
1235                                 //
1236                                 if (value >= 0)
1237                                         return new EmptyCast (il, target_type);
1238                         } else if (target_type == TypeManager.uint64_type){
1239                                 //
1240                                 // we can optimize this case: a positive int32
1241                                 // always fits on a uint64.  But we need an opcode
1242                                 // to do it.
1243                                 //
1244                                 if (value >= 0)
1245                                         return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
1246                         }
1247
1248                         return null;
1249                 }
1250
1251                 /// <summary>
1252                 ///   Attemptes to implicityly convert `target' into `type', using
1253                 ///   ConvertImplicit.  If there is no implicit conversion, then
1254                 ///   an error is signaled
1255                 /// </summary>
1256                 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1257                                                                   Type target_type, Location loc)
1258                 {
1259                         Expression e;
1260                         
1261                         e = ConvertImplicit (ec, source, target_type, loc);
1262                         if (e != null)
1263                                 return e;
1264
1265                         if (source is DoubleLiteral && target_type == TypeManager.float_type){
1266                                 Error (664, loc,
1267                                        "Double literal cannot be implicitly converted to " +
1268                                        "float type, use F suffix to create a float literal");
1269                         }
1270                         
1271                         string msg = "Cannot convert implicitly from `"+
1272                                 TypeManager.CSharpName (source.Type) + "' to `" +
1273                                 TypeManager.CSharpName (target_type) + "'";
1274
1275                         Error (29, loc, msg);
1276
1277                         return null;
1278                 }
1279
1280                 /// <summary>
1281                 ///   Performs the explicit numeric conversions
1282                 /// </summary>
1283                 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
1284                                                           Type target_type)
1285                 {
1286                         Type expr_type = expr.Type;
1287                         
1288                         if (expr_type == TypeManager.sbyte_type){
1289                                 //
1290                                 // From sbyte to byte, ushort, uint, ulong, char
1291                                 //
1292                                 if (target_type == TypeManager.byte_type)
1293                                         return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1294                                 if (target_type == TypeManager.ushort_type)
1295                                         return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1296                                 if (target_type == TypeManager.uint32_type)
1297                                         return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1298                                 if (target_type == TypeManager.uint64_type)
1299                                         return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1300                                 if (target_type == TypeManager.char_type)
1301                                         return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1302                         } else if (expr_type == TypeManager.byte_type){
1303                                 //
1304                                 // From byte to sbyte and char
1305                                 //
1306                                 if (target_type == TypeManager.sbyte_type)
1307                                         return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1308                                 if (target_type == TypeManager.char_type)
1309                                         return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1310                         } else if (expr_type == TypeManager.short_type){
1311                                 //
1312                                 // From short to sbyte, byte, ushort, uint, ulong, char
1313                                 //
1314                                 if (target_type == TypeManager.sbyte_type)
1315                                         return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1316                                 if (target_type == TypeManager.byte_type)
1317                                         return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1318                                 if (target_type == TypeManager.ushort_type)
1319                                         return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1320                                 if (target_type == TypeManager.uint32_type)
1321                                         return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1322                                 if (target_type == TypeManager.uint64_type)
1323                                         return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1324                                 if (target_type == TypeManager.char_type)
1325                                         return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1326                         } else if (expr_type == TypeManager.ushort_type){
1327                                 //
1328                                 // From ushort to sbyte, byte, short, char
1329                                 //
1330                                 if (target_type == TypeManager.sbyte_type)
1331                                         return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1332                                 if (target_type == TypeManager.byte_type)
1333                                         return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1334                                 if (target_type == TypeManager.short_type)
1335                                         return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1336                                 if (target_type == TypeManager.char_type)
1337                                         return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1338                         } else if (expr_type == TypeManager.int32_type){
1339                                 //
1340                                 // From int to sbyte, byte, short, ushort, uint, ulong, char
1341                                 //
1342                                 if (target_type == TypeManager.sbyte_type)
1343                                         return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1344                                 if (target_type == TypeManager.byte_type)
1345                                         return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1346                                 if (target_type == TypeManager.short_type)
1347                                         return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1348                                 if (target_type == TypeManager.ushort_type)
1349                                         return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1350                                 if (target_type == TypeManager.uint32_type)
1351                                         return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1352                                 if (target_type == TypeManager.uint64_type)
1353                                         return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1354                                 if (target_type == TypeManager.char_type)
1355                                         return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1356                         } else if (expr_type == TypeManager.uint32_type){
1357                                 //
1358                                 // From uint to sbyte, byte, short, ushort, int, char
1359                                 //
1360                                 if (target_type == TypeManager.sbyte_type)
1361                                         return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1362                                 if (target_type == TypeManager.byte_type)
1363                                         return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1364                                 if (target_type == TypeManager.short_type)
1365                                         return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1366                                 if (target_type == TypeManager.ushort_type)
1367                                         return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1368                                 if (target_type == TypeManager.int32_type)
1369                                         return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1370                                 if (target_type == TypeManager.char_type)
1371                                         return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1372                         } else if (expr_type == TypeManager.int64_type){
1373                                 //
1374                                 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1375                                 //
1376                                 if (target_type == TypeManager.sbyte_type)
1377                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1378                                 if (target_type == TypeManager.byte_type)
1379                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1380                                 if (target_type == TypeManager.short_type)
1381                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1382                                 if (target_type == TypeManager.ushort_type)
1383                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1384                                 if (target_type == TypeManager.int32_type)
1385                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1386                                 if (target_type == TypeManager.uint32_type)
1387                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1388                                 if (target_type == TypeManager.uint64_type)
1389                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1390                                 if (target_type == TypeManager.char_type)
1391                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1392                         } else if (expr_type == TypeManager.uint64_type){
1393                                 //
1394                                 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1395                                 //
1396                                 if (target_type == TypeManager.sbyte_type)
1397                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1398                                 if (target_type == TypeManager.byte_type)
1399                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1400                                 if (target_type == TypeManager.short_type)
1401                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1402                                 if (target_type == TypeManager.ushort_type)
1403                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1404                                 if (target_type == TypeManager.int32_type)
1405                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1406                                 if (target_type == TypeManager.uint32_type)
1407                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1408                                 if (target_type == TypeManager.int64_type)
1409                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1410                                 if (target_type == TypeManager.char_type)
1411                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1412                         } else if (expr_type == TypeManager.char_type){
1413                                 //
1414                                 // From char to sbyte, byte, short
1415                                 //
1416                                 if (target_type == TypeManager.sbyte_type)
1417                                         return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1418                                 if (target_type == TypeManager.byte_type)
1419                                         return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1420                                 if (target_type == TypeManager.short_type)
1421                                         return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1422                         } else if (expr_type == TypeManager.float_type){
1423                                 //
1424                                 // From float to sbyte, byte, short,
1425                                 // ushort, int, uint, long, ulong, char
1426                                 // or decimal
1427                                 //
1428                                 if (target_type == TypeManager.sbyte_type)
1429                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1430                                 if (target_type == TypeManager.byte_type)
1431                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1432                                 if (target_type == TypeManager.short_type)
1433                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1434                                 if (target_type == TypeManager.ushort_type)
1435                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1436                                 if (target_type == TypeManager.int32_type)
1437                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1438                                 if (target_type == TypeManager.uint32_type)
1439                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1440                                 if (target_type == TypeManager.int64_type)
1441                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1442                                 if (target_type == TypeManager.uint64_type)
1443                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1444                                 if (target_type == TypeManager.char_type)
1445                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1446                                 if (target_type == TypeManager.decimal_type)
1447                                         return InternalTypeConstructor (ec, expr, target_type);
1448                         } else if (expr_type == TypeManager.double_type){
1449                                 //
1450                                 // From double to byte, byte, short,
1451                                 // ushort, int, uint, long, ulong,
1452                                 // char, float or decimal
1453                                 //
1454                                 if (target_type == TypeManager.sbyte_type)
1455                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1456                                 if (target_type == TypeManager.byte_type)
1457                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1458                                 if (target_type == TypeManager.short_type)
1459                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1460                                 if (target_type == TypeManager.ushort_type)
1461                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1462                                 if (target_type == TypeManager.int32_type)
1463                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1464                                 if (target_type == TypeManager.uint32_type)
1465                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1466                                 if (target_type == TypeManager.int64_type)
1467                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1468                                 if (target_type == TypeManager.uint64_type)
1469                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1470                                 if (target_type == TypeManager.char_type)
1471                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1472                                 if (target_type == TypeManager.float_type)
1473                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1474                                 if (target_type == TypeManager.decimal_type)
1475                                         return InternalTypeConstructor (ec, expr, target_type);
1476                         } 
1477
1478                         // decimal is taken care of by the op_Explicit methods.
1479
1480                         return null;
1481                 }
1482
1483                 /// <summary>
1484                 ///  Returns whether an explicit reference conversion can be performed
1485                 ///  from source_type to target_type
1486                 /// </summary>
1487                 static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1488                 {
1489                         bool target_is_value_type = target_type.IsValueType;
1490                         
1491                         if (source_type == target_type)
1492                                 return true;
1493                         
1494                         //
1495                         // From object to any reference type
1496                         //
1497                         if (source_type == TypeManager.object_type && !target_is_value_type)
1498                                 return true;
1499                                         
1500                         //
1501                         // From any class S to any class-type T, provided S is a base class of T
1502                         //
1503                         if (target_type.IsSubclassOf (source_type))
1504                                 return true;
1505
1506                         //
1507                         // From any interface type S to any interface T provided S is not derived from T
1508                         //
1509                         if (source_type.IsInterface && target_type.IsInterface){
1510                                 if (!target_type.IsSubclassOf (source_type))
1511                                         return true;
1512                         }
1513                             
1514                         //
1515                         // From any class type S to any interface T, provides S is not sealed
1516                         // and provided S does not implement T.
1517                         //
1518                         if (target_type.IsInterface && !source_type.IsSealed &&
1519                             !target_type.IsAssignableFrom (source_type))
1520                                 return true;
1521
1522                         //
1523                         // From any interface-type S to to any class type T, provided T is not
1524                         // sealed, or provided T implements S.
1525                         //
1526                         if (source_type.IsInterface &&
1527                             (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
1528                                 return true;
1529
1530                         // From an array type S with an element type Se to an array type T with an 
1531                         // element type Te provided all the following are true:
1532                         //     * S and T differe only in element type, in other words, S and T
1533                         //       have the same number of dimensions.
1534                         //     * Both Se and Te are reference types
1535                         //     * An explicit referenc conversions exist from Se to Te
1536                         //
1537                         if (source_type.IsArray && target_type.IsArray) {
1538                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1539                                         
1540                                         Type source_element_type = source_type.GetElementType ();
1541                                         Type target_element_type = target_type.GetElementType ();
1542                                         
1543                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1544                                                 if (ExplicitReferenceConversionExists (source_element_type,
1545                                                                                        target_element_type))
1546                                                         return true;
1547                                 }
1548                         }
1549                         
1550
1551                         // From System.Array to any array-type
1552                         if (source_type == TypeManager.array_type &&
1553                             target_type.IsSubclassOf (TypeManager.array_type)){
1554                                 return true;
1555                         }
1556
1557                         //
1558                         // From System delegate to any delegate-type
1559                         //
1560                         if (source_type == TypeManager.delegate_type &&
1561                             target_type.IsSubclassOf (TypeManager.delegate_type))
1562                                 return true;
1563
1564                         //
1565                         // From ICloneable to Array or Delegate types
1566                         //
1567                         if (source_type == TypeManager.icloneable_type &&
1568                             (target_type == TypeManager.array_type ||
1569                              target_type == TypeManager.delegate_type))
1570                                 return true;
1571                         
1572                         return false;
1573                 }
1574
1575                 /// <summary>
1576                 ///   Implements Explicit Reference conversions
1577                 /// </summary>
1578                 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1579                 {
1580                         Type source_type = source.Type;
1581                         bool target_is_value_type = target_type.IsValueType;
1582                         
1583                         //
1584                         // From object to any reference type
1585                         //
1586                         if (source_type == TypeManager.object_type && !target_is_value_type)
1587                                 return new ClassCast (source, target_type);
1588
1589
1590                         //
1591                         // From any class S to any class-type T, provided S is a base class of T
1592                         //
1593                         if (target_type.IsSubclassOf (source_type))
1594                                 return new ClassCast (source, target_type);
1595
1596                         //
1597                         // From any interface type S to any interface T provided S is not derived from T
1598                         //
1599                         if (source_type.IsInterface && target_type.IsInterface){
1600
1601                                 Type [] ifaces = source_type.GetInterfaces ();
1602
1603                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1604                                         return null;
1605                                 else
1606                                         return new ClassCast (source, target_type);
1607                         }
1608                             
1609                         //
1610                         // From any class type S to any interface T, provides S is not sealed
1611                         // and provided S does not implement T.
1612                         //
1613                         if (target_type.IsInterface && !source_type.IsSealed) {
1614                                 
1615                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1616                                         return null;
1617                                 else
1618                                         return new ClassCast (source, target_type);
1619                                 
1620                         }
1621
1622                         //
1623                         // From any interface-type S to to any class type T, provided T is not
1624                         // sealed, or provided T implements S.
1625                         //
1626                         if (source_type.IsInterface) {
1627
1628                                 if (target_type.IsSealed)
1629                                         return null;
1630                                 
1631                                 if (TypeManager.ImplementsInterface (target_type, source_type))
1632                                         return new ClassCast (source, target_type);
1633                                 else
1634                                         return null;
1635                         }
1636                         
1637                         // From an array type S with an element type Se to an array type T with an 
1638                         // element type Te provided all the following are true:
1639                         //     * S and T differe only in element type, in other words, S and T
1640                         //       have the same number of dimensions.
1641                         //     * Both Se and Te are reference types
1642                         //     * An explicit referenc conversions exist from Se to Te
1643                         //
1644                         if (source_type.IsArray && target_type.IsArray) {
1645                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1646                                         
1647                                         Type source_element_type = source_type.GetElementType ();
1648                                         Type target_element_type = target_type.GetElementType ();
1649                                         
1650                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1651                                                 if (ExplicitReferenceConversionExists (source_element_type,
1652                                                                                        target_element_type))
1653                                                         return new ClassCast (source, target_type);
1654                                 }
1655                         }
1656                         
1657
1658                         // From System.Array to any array-type
1659                         if (source_type == TypeManager.array_type &&
1660                             target_type.IsSubclassOf (TypeManager.array_type)){
1661                                 return new ClassCast (source, target_type);
1662                         }
1663
1664                         //
1665                         // From System delegate to any delegate-type
1666                         //
1667                         if (source_type == TypeManager.delegate_type &&
1668                             target_type.IsSubclassOf (TypeManager.delegate_type))
1669                                 return new ClassCast (source, target_type);
1670
1671                         //
1672                         // From ICloneable to Array or Delegate types
1673                         //
1674                         if (source_type == TypeManager.icloneable_type &&
1675                             (target_type == TypeManager.array_type ||
1676                              target_type == TypeManager.delegate_type))
1677                                 return new ClassCast (source, target_type);
1678                         
1679                         return null;
1680                 }
1681                 
1682                 /// <summary>
1683                 ///   Performs an explicit conversion of the expression `expr' whose
1684                 ///   type is expr.Type to `target_type'.
1685                 /// </summary>
1686                 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1687                                                           Type target_type, Location loc)
1688                 {
1689                         Type expr_type = expr.Type;
1690                         Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1691
1692                         if (ne != null)
1693                                 return ne;
1694
1695                         ne = ConvertNumericExplicit (ec, expr, target_type);
1696                         if (ne != null)
1697                                 return ne;
1698
1699                         //
1700                         // Unboxing conversion.
1701                         //
1702                         if (expr_type == TypeManager.object_type && target_type.IsValueType)
1703                                 return new UnboxCast (expr, target_type);
1704
1705                         //
1706                         // Enum types
1707                         //
1708                         if (expr is EnumLiteral) {
1709                                 Expression e = ((EnumLiteral) expr).Child;
1710                                 
1711                                 return ConvertImplicit (ec, e, target_type, loc);
1712                         }
1713                         
1714                         ne = ConvertReferenceExplicit (expr, target_type);
1715                         if (ne != null)
1716                                 return ne;
1717
1718                         ne = ExplicitUserConversion (ec, expr, target_type, loc);
1719                         if (ne != null)
1720                                 return ne;
1721
1722                         error30 (loc, expr_type, target_type);
1723                         return null;
1724                 }
1725
1726                 /// <summary>
1727                 ///   Same as ConverExplicit, only it doesn't include user defined conversions
1728                 /// </summary>
1729                 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
1730                                                                   Type target_type, Location l)
1731                 {
1732                         Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
1733
1734                         if (ne != null)
1735                                 return ne;
1736
1737                         ne = ConvertNumericExplicit (ec, expr, target_type);
1738                         if (ne != null)
1739                                 return ne;
1740
1741                         ne = ConvertReferenceExplicit (expr, target_type);
1742                         if (ne != null)
1743                                 return ne;
1744
1745                         error30 (l, expr.Type, target_type);
1746                         return null;
1747                 }
1748
1749                 static string ExprClassName (ExprClass c)
1750                 {
1751                         switch (c){
1752                         case ExprClass.Invalid:
1753                                 return "Invalid";
1754                         case ExprClass.Value:
1755                                 return "value";
1756                         case ExprClass.Variable:
1757                                 return "variable";
1758                         case ExprClass.Namespace:
1759                                 return "namespace";
1760                         case ExprClass.Type:
1761                                 return "type";
1762                         case ExprClass.MethodGroup:
1763                                 return "method group";
1764                         case ExprClass.PropertyAccess:
1765                                 return "property access";
1766                         case ExprClass.EventAccess:
1767                                 return "event access";
1768                         case ExprClass.IndexerAccess:
1769                                 return "indexer access";
1770                         case ExprClass.Nothing:
1771                                 return "null";
1772                         }
1773                         throw new Exception ("Should not happen");
1774                 }
1775                 
1776                 /// <summary>
1777                 ///   Reports that we were expecting `expr' to be of class `expected'
1778                 /// </summary>
1779                 protected void report118 (Location loc, Expression expr, string expected)
1780                 {
1781                         string kind = "Unknown";
1782                         
1783                         if (expr != null)
1784                                 kind = ExprClassName (expr.ExprClass);
1785
1786                         Error (118, loc, "Expression denotes a `" + kind +
1787                                "' where a `" + expected + "' was expected");
1788                 }
1789
1790                 /// <summary>
1791                 ///   This function tries to reduce the expression performing
1792                 ///   constant folding and common subexpression elimination
1793                 /// </summary>
1794                 static public Expression Reduce (EmitContext ec, Expression e)
1795                 {
1796                         //Console.WriteLine ("Calling reduce");
1797                         return e.Reduce (ec);
1798                 }
1799
1800                 static void error31 (Location l, string val, Type t)
1801                 {
1802                         Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
1803                                       TypeManager.CSharpName (t));
1804                 }
1805                 
1806                 /// <summary>
1807                 ///   Converts the IntLiteral, UIntLiteral, LongLiteral or
1808                 ///   ULongLiteral into the integral target_type.
1809                 ///
1810                 ///   This is used by the switch statement, so the domain
1811                 ///   of work is restricted to the literals above, and the
1812                 ///   targets are int32, uint32, char, byte, sbyte, ushort,
1813                 ///   short, uint64 and int64
1814                 /// </summary>
1815                 public static Literal ConvertIntLiteral (Literal l, Type target_type, Location loc)
1816                 {
1817                         string s = "";
1818
1819                         if (l.Type == target_type)
1820                                 return l;
1821
1822                         //
1823                         // Make into one of the literals we handle, we dont really care
1824                         // about this value as we will just return a few limited types
1825                         // 
1826                         if (l is EnumLiteral)
1827                                 l = ((EnumLiteral)l).WidenToCompilerLiteral ();
1828
1829                         if (l is IntLiteral){
1830                                 int v = ((IntLiteral) l).Value;
1831                                 
1832                                 if (target_type == TypeManager.uint32_type){
1833                                         if (v >= 0)
1834                                                 return new UIntLiteral ((uint) v);
1835                                 } else if (target_type == TypeManager.char_type){
1836                                         if (v >= Char.MinValue && v <= Char.MaxValue)
1837                                                 return l;
1838                                 } else if (target_type == TypeManager.byte_type){
1839                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
1840                                                 return l;
1841                                 } else if (target_type == TypeManager.sbyte_type){
1842                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
1843                                                 return l;
1844                                 } else if (target_type == TypeManager.short_type){
1845                                         if (v >= Int16.MinValue && v <= UInt16.MaxValue)
1846                                                 return l;
1847                                 } else if (target_type == TypeManager.ushort_type){
1848                                         if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
1849                                                 return l;
1850                                 } else if (target_type == TypeManager.int64_type)
1851                                         return new LongLiteral (v);
1852                                 else if (target_type == TypeManager.uint64_type){
1853                                         if (v > 0)
1854                                                 return new ULongLiteral ((ulong) v);
1855                                 }
1856
1857                                 s = v.ToString ();
1858                         } else if (l is UIntLiteral){
1859                                 uint v = ((UIntLiteral) l).Value;
1860
1861                                 if (target_type == TypeManager.int32_type){
1862                                         if (v <= Int32.MaxValue)
1863                                                 return new IntLiteral ((int) v);
1864                                 } else if (target_type == TypeManager.char_type){
1865                                         if (v >= Char.MinValue && v <= Char.MaxValue)
1866                                                 return l;
1867                                 } else if (target_type == TypeManager.byte_type){
1868                                         if (v <= Byte.MaxValue)
1869                                                 return l;
1870                                 } else if (target_type == TypeManager.sbyte_type){
1871                                         if (v <= SByte.MaxValue)
1872                                                 return l;
1873                                 } else if (target_type == TypeManager.short_type){
1874                                         if (v <= UInt16.MaxValue)
1875                                                 return l;
1876                                 } else if (target_type == TypeManager.ushort_type){
1877                                         if (v <= UInt16.MaxValue)
1878                                                 return l;
1879                                 } else if (target_type == TypeManager.int64_type)
1880                                         return new LongLiteral (v);
1881                                 else if (target_type == TypeManager.uint64_type)
1882                                         return new ULongLiteral (v);
1883                                 s = v.ToString ();
1884                         } else if (l is LongLiteral){ 
1885                                 long v = ((LongLiteral) l).Value;
1886
1887                                 if (target_type == TypeManager.int32_type){
1888                                         if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
1889                                                 return new IntLiteral ((int) v);
1890                                 } else if (target_type == TypeManager.uint32_type){
1891                                         if (v >= 0 && v <= UInt32.MaxValue)
1892                                                 return new UIntLiteral ((uint) v);
1893                                 } else if (target_type == TypeManager.char_type){
1894                                         if (v >= Char.MinValue && v <= Char.MaxValue)
1895                                                 return new IntLiteral ((int) v);
1896                                 } else if (target_type == TypeManager.byte_type){
1897                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
1898                                                 return new IntLiteral ((int) v);
1899                                 } else if (target_type == TypeManager.sbyte_type){
1900                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
1901                                                 return new IntLiteral ((int) v);
1902                                 } else if (target_type == TypeManager.short_type){
1903                                         if (v >= Int16.MinValue && v <= UInt16.MaxValue)
1904                                                 return new IntLiteral ((int) v);
1905                                 } else if (target_type == TypeManager.ushort_type){
1906                                         if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
1907                                                 return new IntLiteral ((int) v);
1908                                 } else if (target_type == TypeManager.uint64_type){
1909                                         if (v > 0)
1910                                                 return new ULongLiteral ((ulong) v);
1911                                 }
1912                                 s = v.ToString ();
1913                         } else if (l is ULongLiteral){
1914                                 ulong v = ((ULongLiteral) l).Value;
1915
1916                                 if (target_type == TypeManager.int32_type){
1917                                         if (v <= Int32.MaxValue)
1918                                                 return new IntLiteral ((int) v);
1919                                 } else if (target_type == TypeManager.uint32_type){
1920                                         if (v <= UInt32.MaxValue)
1921                                                 return new UIntLiteral ((uint) v);
1922                                 } else if (target_type == TypeManager.char_type){
1923                                         if (v >= Char.MinValue && v <= Char.MaxValue)
1924                                                 return new IntLiteral ((int) v);
1925                                 } else if (target_type == TypeManager.byte_type){
1926                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
1927                                                 return new IntLiteral ((int) v);
1928                                 } else if (target_type == TypeManager.sbyte_type){
1929                                         if (v <= (int) SByte.MaxValue)
1930                                                 return new IntLiteral ((int) v);
1931                                 } else if (target_type == TypeManager.short_type){
1932                                         if (v <= UInt16.MaxValue)
1933                                                 return new IntLiteral ((int) v);
1934                                 } else if (target_type == TypeManager.ushort_type){
1935                                         if (v <= UInt16.MaxValue)
1936                                                 return new IntLiteral ((int) v);
1937                                 } else if (target_type == TypeManager.int64_type){
1938                                         if (v <= Int64.MaxValue)
1939                                                 return new LongLiteral ((long) v);
1940                                 }
1941                                 s = v.ToString ();
1942                         } 
1943                         
1944                         error31 (loc, s, target_type);
1945                         return null;
1946                 }
1947                         
1948         }
1949
1950         /// <summary>
1951         ///   This is just a base class for expressions that can
1952         ///   appear on statements (invocations, object creation,
1953         ///   assignments, post/pre increment and decrement).  The idea
1954         ///   being that they would support an extra Emition interface that
1955         ///   does not leave a result on the stack.
1956         /// </summary>
1957         public abstract class ExpressionStatement : Expression {
1958
1959                 /// <summary>
1960                 ///   Requests the expression to be emitted in a `statement'
1961                 ///   context.  This means that no new value is left on the
1962                 ///   stack after invoking this method (constrasted with
1963                 ///   Emit that will always leave a value on the stack).
1964                 /// </summary>
1965                 public abstract void EmitStatement (EmitContext ec);
1966         }
1967
1968         /// <summary>
1969         ///   This kind of cast is used to encapsulate the child
1970         ///   whose type is child.Type into an expression that is
1971         ///   reported to return "return_type".  This is used to encapsulate
1972         ///   expressions which have compatible types, but need to be dealt
1973         ///   at higher levels with.
1974         ///
1975         ///   For example, a "byte" expression could be encapsulated in one
1976         ///   of these as an "unsigned int".  The type for the expression
1977         ///   would be "unsigned int".
1978         ///
1979         /// </summary>
1980         public class EmptyCast : Expression {
1981                 protected Expression child;
1982
1983                 public EmptyCast (Expression child, Type return_type)
1984                 {
1985                         ExprClass = child.ExprClass;
1986                         type = return_type;
1987                         this.child = child;
1988                 }
1989
1990                 public override Expression DoResolve (EmitContext ec)
1991                 {
1992                         // This should never be invoked, we are born in fully
1993                         // initialized state.
1994
1995                         return this;
1996                 }
1997
1998                 public override void Emit (EmitContext ec)
1999                 {
2000                         child.Emit (ec);
2001                 }
2002
2003         }
2004
2005         /// <summary>
2006         ///  This class is used to wrap literals which belong inside Enums
2007         /// </summary>
2008         public class EnumLiteral : Literal {
2009                 public Expression Child;
2010
2011                 public EnumLiteral (Expression child, Type enum_type)
2012                 {
2013                         ExprClass = child.ExprClass;
2014                         this.Child = child;
2015                         type = enum_type;
2016                 }
2017                 
2018                 public override Expression DoResolve (EmitContext ec)
2019                 {
2020                         // This should never be invoked, we are born in fully
2021                         // initialized state.
2022
2023                         return this;
2024                 }
2025
2026                 public override void Emit (EmitContext ec)
2027                 {
2028                         Child.Emit (ec);
2029                 }
2030
2031                 public override object GetValue ()
2032                 {
2033                         return ((Literal) Child).GetValue ();
2034                 }
2035
2036                 //
2037                 // Converts from one of the valid underlying types for an enumeration
2038                 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2039                 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2040                 //
2041                 public Literal WidenToCompilerLiteral ()
2042                 {
2043                         Type t = Child.Type.UnderlyingSystemType;
2044                         object v = ((Literal) Child).GetValue ();;
2045                         
2046                         if (t == TypeManager.int32_type)
2047                                 return new IntLiteral ((int) v);
2048                         if (t == TypeManager.uint32_type)
2049                                 return new UIntLiteral ((uint) v);
2050                         if (t == TypeManager.int64_type)
2051                                 return new LongLiteral ((long) v);
2052                         if (t == TypeManager.uint64_type)
2053                                 return new ULongLiteral ((ulong) v);
2054                         if (t == TypeManager.short_type)
2055                                 return new IntLiteral ((short) v);
2056                         if (t == TypeManager.ushort_type)
2057                                 return new UIntLiteral ((ushort) v);
2058                         if (t == TypeManager.byte_type)
2059                                 return new UIntLiteral ((byte) v);
2060                         if (t == TypeManager.sbyte_type)
2061                                 return new IntLiteral ((sbyte) v);
2062
2063                         throw new Exception ("Invalid enumeration underlying type: " + t);
2064                 }
2065
2066                 public override string AsString ()
2067                 {
2068                         return ((Literal) Child).AsString ();
2069                 }
2070         }
2071
2072         /// <summary>
2073         ///   This kind of cast is used to encapsulate Value Types in objects.
2074         ///
2075         ///   The effect of it is to box the value type emitted by the previous
2076         ///   operation.
2077         /// </summary>
2078         public class BoxedCast : EmptyCast {
2079
2080                 public BoxedCast (Expression expr)
2081                         : base (expr, TypeManager.object_type)
2082                 {
2083                 }
2084
2085                 public override Expression DoResolve (EmitContext ec)
2086                 {
2087                         // This should never be invoked, we are born in fully
2088                         // initialized state.
2089
2090                         return this;
2091                 }
2092
2093                 public override void Emit (EmitContext ec)
2094                 {
2095                         base.Emit (ec);
2096                         ec.ig.Emit (OpCodes.Box, child.Type);
2097                 }
2098         }
2099
2100         public class UnboxCast : EmptyCast {
2101                 public UnboxCast (Expression expr, Type return_type)
2102                         : base (expr, return_type)
2103                 {
2104                 }
2105
2106                 public override Expression DoResolve (EmitContext ec)
2107                 {
2108                         // This should never be invoked, we are born in fully
2109                         // initialized state.
2110
2111                         return this;
2112                 }
2113
2114                 public override void Emit (EmitContext ec)
2115                 {
2116                         Type t = type;
2117                         ILGenerator ig = ec.ig;
2118                         
2119                         base.Emit (ec);
2120                         ig.Emit (OpCodes.Unbox, t);
2121
2122                         //
2123                         // Load the object from the pointer
2124                         //
2125                         if (t == TypeManager.int32_type)
2126                                 ig.Emit (OpCodes.Ldind_I4);
2127                         else if (t == TypeManager.uint32_type)
2128                                 ig.Emit (OpCodes.Ldind_U4);
2129                         else if (t == TypeManager.short_type)
2130                                 ig.Emit (OpCodes.Ldind_I2);
2131                         else if (t == TypeManager.ushort_type)
2132                                 ig.Emit (OpCodes.Ldind_U2);
2133                         else if (t == TypeManager.char_type)
2134                                 ig.Emit (OpCodes.Ldind_U2);
2135                         else if (t == TypeManager.byte_type)
2136                                 ig.Emit (OpCodes.Ldind_U1);
2137                         else if (t == TypeManager.sbyte_type)
2138                                 ig.Emit (OpCodes.Ldind_I1);
2139                         else if (t == TypeManager.uint64_type)
2140                                 ig.Emit (OpCodes.Ldind_I8);
2141                         else if (t == TypeManager.int64_type)
2142                                 ig.Emit (OpCodes.Ldind_I8);
2143                         else if (t == TypeManager.float_type)
2144                                 ig.Emit (OpCodes.Ldind_R4);
2145                         else if (t == TypeManager.double_type)
2146                                 ig.Emit (OpCodes.Ldind_R8);
2147                         else if (t == TypeManager.bool_type)
2148                                 ig.Emit (OpCodes.Ldind_I1);
2149                         else if (t == TypeManager.intptr_type)
2150                                 ig.Emit (OpCodes.Ldind_I);
2151                         else 
2152                                 ig.Emit (OpCodes.Ldobj, t);
2153                 }
2154         }
2155         
2156         /// <summary>
2157         ///   This is used to perform explicit numeric conversions.
2158         ///
2159         ///   Explicit numeric conversions might trigger exceptions in a checked
2160         ///   context, so they should generate the conv.ovf opcodes instead of
2161         ///   conv opcodes.
2162         /// </summary>
2163         public class ConvCast : EmptyCast {
2164                 public enum Mode : byte {
2165                         I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
2166                         U1_I1, U1_CH,
2167                         I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
2168                         U2_I1, U2_U1, U2_I2, U2_CH,
2169                         I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
2170                         U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
2171                         I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
2172                         U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
2173                         CH_I1, CH_U1, CH_I2,
2174                         R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
2175                         R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
2176                 }
2177
2178                 Mode mode;
2179                 
2180                 public ConvCast (Expression child, Type return_type, Mode m)
2181                         : base (child, return_type)
2182                 {
2183                         mode = m;
2184                 }
2185
2186                 public override Expression DoResolve (EmitContext ec)
2187                 {
2188                         // This should never be invoked, we are born in fully
2189                         // initialized state.
2190
2191                         return this;
2192                 }
2193
2194                 public override void Emit (EmitContext ec)
2195                 {
2196                         ILGenerator ig = ec.ig;
2197                         
2198                         base.Emit (ec);
2199
2200                         if (ec.CheckState){
2201                                 switch (mode){
2202                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2203                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2204                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2205                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2206                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2207
2208                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2209                                 case Mode.U1_CH: /* nothing */ break;
2210
2211                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2212                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2213                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2214                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2215                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2216                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2217
2218                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2219                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2220                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2221                                 case Mode.U2_CH: /* nothing */ break;
2222
2223                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2224                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2225                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2226                                 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2227                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2228                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2229                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2230
2231                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2232                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2233                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2234                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2235                                 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2236                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2237
2238                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2239                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2240                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2241                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2242                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2243                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2244                                 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2245                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2246
2247                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2248                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2249                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2250                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2251                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2252                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
2253                                 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2254                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2255
2256                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2257                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2258                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2259
2260                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2261                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2262                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2263                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2264                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2265                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2266                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2267                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2268                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2269
2270                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2271                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2272                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2273                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2274                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2275                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2276                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2277                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2278                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2279                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2280                                 }
2281                         } else {
2282                                 switch (mode){
2283                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
2284                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
2285                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
2286                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
2287                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
2288
2289                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
2290                                 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
2291
2292                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
2293                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
2294                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
2295                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
2296                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
2297                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
2298
2299                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
2300                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
2301                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
2302                                 case Mode.U2_CH: /* nothing */ break;
2303
2304                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
2305                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
2306                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
2307                                 case Mode.I4_U4: /* nothing */ break;
2308                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
2309                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
2310                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
2311
2312                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
2313                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
2314                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
2315                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
2316                                 case Mode.U4_I4: /* nothing */ break;
2317                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
2318
2319                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
2320                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
2321                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
2322                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
2323                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
2324                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
2325                                 case Mode.I8_U8: /* nothing */ break;
2326                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
2327
2328                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
2329                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
2330                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
2331                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
2332                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
2333                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
2334                                 case Mode.U8_I8: /* nothing */ break;
2335                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
2336
2337                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
2338                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
2339                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
2340
2341                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
2342                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
2343                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
2344                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
2345                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
2346                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
2347                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
2348                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
2349                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
2350
2351                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
2352                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
2353                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
2354                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
2355                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
2356                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
2357                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
2358                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
2359                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
2360                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2361                                 }
2362                         }
2363                 }
2364         }
2365         
2366         public class OpcodeCast : EmptyCast {
2367                 OpCode op, op2;
2368                 bool second_valid;
2369                 
2370                 public OpcodeCast (Expression child, Type return_type, OpCode op)
2371                         : base (child, return_type)
2372                         
2373                 {
2374                         this.op = op;
2375                         second_valid = false;
2376                 }
2377
2378                 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
2379                         : base (child, return_type)
2380                         
2381                 {
2382                         this.op = op;
2383                         this.op2 = op2;
2384                         second_valid = true;
2385                 }
2386
2387                 public override Expression DoResolve (EmitContext ec)
2388                 {
2389                         // This should never be invoked, we are born in fully
2390                         // initialized state.
2391
2392                         return this;
2393                 }
2394
2395                 public override void Emit (EmitContext ec)
2396                 {
2397                         base.Emit (ec);
2398                         ec.ig.Emit (op);
2399
2400                         if (second_valid)
2401                                 ec.ig.Emit (op2);
2402                 }                       
2403         }
2404
2405         /// <summary>
2406         ///   This kind of cast is used to encapsulate a child and cast it
2407         ///   to the class requested
2408         /// </summary>
2409         public class ClassCast : EmptyCast {
2410                 public ClassCast (Expression child, Type return_type)
2411                         : base (child, return_type)
2412                         
2413                 {
2414                 }
2415
2416                 public override Expression DoResolve (EmitContext ec)
2417                 {
2418                         // This should never be invoked, we are born in fully
2419                         // initialized state.
2420
2421                         return this;
2422                 }
2423
2424                 public override void Emit (EmitContext ec)
2425                 {
2426                         base.Emit (ec);
2427
2428                         ec.ig.Emit (OpCodes.Castclass, type);
2429                 }                       
2430                 
2431         }
2432         
2433         /// <summary>
2434         ///   SimpleName expressions are initially formed of a single
2435         ///   word and it only happens at the beginning of the expression.
2436         /// </summary>
2437         ///
2438         /// <remarks>
2439         ///   The expression will try to be bound to a Field, a Method
2440         ///   group or a Property.  If those fail we pass the name to our
2441         ///   caller and the SimpleName is compounded to perform a type
2442         ///   lookup.  The idea behind this process is that we want to avoid
2443         ///   creating a namespace map from the assemblies, as that requires
2444         ///   the GetExportedTypes function to be called and a hashtable to
2445         ///   be constructed which reduces startup time.  If later we find
2446         ///   that this is slower, we should create a `NamespaceExpr' expression
2447         ///   that fully participates in the resolution process. 
2448         ///   
2449         ///   For example `System.Console.WriteLine' is decomposed into
2450         ///   MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
2451         ///   
2452         ///   The first SimpleName wont produce a match on its own, so it will
2453         ///   be turned into:
2454         ///   MemberAccess (SimpleName ("System.Console"), "WriteLine").
2455         ///   
2456         ///   System.Console will produce a TypeExpr match.
2457         ///   
2458         ///   The downside of this is that we might be hitting `LookupType' too many
2459         ///   times with this scheme.
2460         /// </remarks>
2461         public class SimpleName : Expression {
2462                 public readonly string Name;
2463                 public readonly Location Location;
2464                 
2465                 public SimpleName (string name, Location l)
2466                 {
2467                         Name = name;
2468                         Location = l;
2469                 }
2470
2471                 public static void Error120 (Location l, string name)
2472                 {
2473                         Report.Error (
2474                                 120, l,
2475                                 "An object reference is required " +
2476                                 "for the non-static field `"+name+"'");
2477                 }
2478                 
2479                 //
2480                 // Checks whether we are trying to access an instance
2481                 // property, method or field from a static body.
2482                 //
2483                 Expression MemberStaticCheck (Expression e)
2484                 {
2485                         if (e is FieldExpr){
2486                                 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2487                                 
2488                                 if (!fi.IsStatic){
2489                                         Error120 (Location, Name);
2490                                         return null;
2491                                 }
2492                         } else if (e is MethodGroupExpr){
2493                                 MethodGroupExpr mg = (MethodGroupExpr) e;
2494
2495                                 if (!mg.RemoveInstanceMethods ()){
2496                                         Error120 (Location, mg.Methods [0].Name);
2497                                         return null;
2498                                 }
2499                                 return e;
2500                         } else if (e is PropertyExpr){
2501                                 if (!((PropertyExpr) e).IsStatic){
2502                                         Error120 (Location, Name);
2503                                         return null;
2504                                 }
2505                         }
2506
2507                         return e;
2508                 }
2509                 
2510                 // <remarks>
2511                 //   7.5.2: Simple Names. 
2512                 //
2513                 //   Local Variables and Parameters are handled at
2514                 //   parse time, so they never occur as SimpleNames.
2515                 // </remarks>
2516                 public override Expression DoResolve (EmitContext ec)
2517                 {
2518                         Expression e;
2519
2520                         //
2521                         // Stage 1: Performed by the parser (binding to locals or parameters).
2522                         //
2523
2524                         //
2525                         // Stage 2: Lookup members
2526                         //
2527                         e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, true, Location);
2528                         if (e == null) {
2529                                 //
2530                                 // Stage 3: Lookup symbol in the various namespaces. 
2531                                 // 
2532                                 DeclSpace ds = ec.TypeContainer;
2533                                 Type t;
2534                                 string alias_value;
2535
2536                                 if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
2537                                         return new TypeExpr (t);
2538
2539                                 //
2540                                 // Stage 3 part b: Lookup up if we are an alias to a type
2541                                 // or a namespace.
2542                                 //
2543                                 // Since we are cheating: we only do the Alias lookup for
2544                                 // namespaces if the name does not include any dots in it
2545                                 //
2546
2547                                 if (Name.IndexOf ('.') == -1 && (alias_value = ec.TypeContainer.LookupAlias (Name)) != null) {
2548                                         // System.Console.WriteLine (Name + " --> " + alias_value);
2549                                         if ((t = RootContext.LookupType (ds, alias_value, true, Location))
2550                                             != null)
2551                                                 return new TypeExpr (t);
2552
2553                                         // we have alias value, but it isn't Type, so try if it's namespace
2554                                         return new SimpleName (alias_value, Location);
2555                                 }
2556
2557                                 // No match, maybe our parent can compose us
2558                                 // into something meaningful.
2559                                 //
2560                                 return this;
2561                         }
2562
2563                         // Step 2, continues here.
2564                         if (e is TypeExpr)
2565                                 return e;
2566
2567                         if (e is FieldExpr){
2568                                 FieldExpr fe = (FieldExpr) e;
2569                                 
2570                                 if (!fe.FieldInfo.IsStatic){
2571                                         This t = new This (Location.Null);
2572
2573                                         fe.InstanceExpression = t.DoResolve (ec);
2574                                 }
2575
2576                                 FieldInfo fi = fe.FieldInfo;
2577                                 
2578                                 if (fi is FieldBuilder) {
2579                                         Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
2580                                         
2581                                         if (c != null) {
2582                                                 object o = c.LookupConstantValue (ec);
2583                                                 Expression l = Literalize (o, fi.FieldType);
2584                                                 l = l.Resolve (ec);
2585                                                 return ((Literal) l);
2586                                         }
2587                                 }
2588                         }                               
2589
2590                         if (ec.IsStatic)
2591                                 return MemberStaticCheck (e);
2592                         else
2593                                 return e;
2594                 }
2595
2596                 public override void Emit (EmitContext ec)
2597                 {
2598                         //
2599                         // If this is ever reached, then we failed to
2600                         // find the name as a namespace
2601                         //
2602
2603                         Error (103, Location, "The name `" + Name +
2604                                "' does not exist in the class `" +
2605                                ec.TypeContainer.Name + "'");
2606                 }
2607         }
2608         
2609         /// <summary>
2610         ///   Fully resolved expression that evaluates to a type
2611         /// </summary>
2612         public class TypeExpr : Expression {
2613                 public TypeExpr (Type t)
2614                 {
2615                         Type = t;
2616                         eclass = ExprClass.Type;
2617                 }
2618
2619                 override public Expression DoResolve (EmitContext ec)
2620                 {
2621                         return this;
2622                 }
2623
2624                 override public void Emit (EmitContext ec)
2625                 {
2626                         throw new Exception ("Implement me");
2627                 }
2628         }
2629
2630         /// <summary>
2631         ///   MethodGroup Expression.
2632         ///  
2633         ///   This is a fully resolved expression that evaluates to a type
2634         /// </summary>
2635         public class MethodGroupExpr : Expression {
2636                 public MethodBase [] Methods;
2637                 Expression instance_expression = null;
2638                 
2639                 public MethodGroupExpr (MemberInfo [] mi)
2640                 {
2641                         Methods = new MethodBase [mi.Length];
2642                         mi.CopyTo (Methods, 0);
2643                         eclass = ExprClass.MethodGroup;
2644                 }
2645
2646                 public MethodGroupExpr (ArrayList l)
2647                 {
2648                         Methods = new MethodBase [l.Count];
2649
2650                         l.CopyTo (Methods, 0);
2651                         eclass = ExprClass.MethodGroup;
2652                 }
2653                 
2654                 //
2655                 // `A method group may have associated an instance expression' 
2656                 // 
2657                 public Expression InstanceExpression {
2658                         get {
2659                                 return instance_expression;
2660                         }
2661
2662                         set {
2663                                 instance_expression = value;
2664                         }
2665                 }
2666                 
2667                 override public Expression DoResolve (EmitContext ec)
2668                 {
2669                         return this;
2670                 }
2671
2672                 override public void Emit (EmitContext ec)
2673                 {
2674                         throw new Exception ("This should never be reached");
2675                 }
2676
2677                 bool RemoveMethods (bool keep_static)
2678                 {
2679                         ArrayList smethods = new ArrayList ();
2680                         int top = Methods.Length;
2681                         int i;
2682                         
2683                         for (i = 0; i < top; i++){
2684                                 MethodBase mb = Methods [i];
2685
2686                                 if (mb.IsStatic == keep_static)
2687                                         smethods.Add (mb);
2688                         }
2689
2690                         if (smethods.Count == 0)
2691                                 return false;
2692
2693                         Methods = new MethodBase [smethods.Count];
2694                         smethods.CopyTo (Methods, 0);
2695
2696                         return true;
2697                 }
2698                 
2699                 /// <summary>
2700                 ///   Removes any instance methods from the MethodGroup, returns
2701                 ///   false if the resulting set is empty.
2702                 /// </summary>
2703                 public bool RemoveInstanceMethods ()
2704                 {
2705                         return RemoveMethods (true);
2706                 }
2707
2708                 /// <summary>
2709                 ///   Removes any static methods from the MethodGroup, returns
2710                 ///   false if the resulting set is empty.
2711                 /// </summary>
2712                 public bool RemoveStaticMethods ()
2713                 {
2714                         return RemoveMethods (false);
2715                 }
2716         }
2717
2718         /// <summary>
2719         ///   Fully resolved expression that evaluates to a Field
2720         /// </summary>
2721         public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
2722                 public readonly FieldInfo FieldInfo;
2723                 public Expression InstanceExpression;
2724                 Location loc;
2725                 
2726                 public FieldExpr (FieldInfo fi, Location l)
2727                 {
2728                         FieldInfo = fi;
2729                         eclass = ExprClass.Variable;
2730                         type = fi.FieldType;
2731                         loc = l;
2732                 }
2733
2734                 override public Expression DoResolve (EmitContext ec)
2735                 {
2736                         if (!FieldInfo.IsStatic){
2737                                 if (InstanceExpression == null){
2738                                         throw new Exception ("non-static FieldExpr without instance var\n" +
2739                                                              "You have to assign the Instance variable\n" +
2740                                                              "Of the FieldExpr to set this\n");
2741                                 }
2742
2743                                 InstanceExpression = InstanceExpression.Resolve (ec);
2744                                 if (InstanceExpression == null)
2745                                         return null;
2746                                 
2747                         }
2748
2749                         return this;
2750                 }
2751
2752                 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
2753                 {
2754                         Expression e = DoResolve (ec);
2755
2756                         if (e == null)
2757                                 return null;
2758                         
2759                         if (!FieldInfo.IsInitOnly)
2760                                 return this;
2761
2762                         //
2763                         // InitOnly fields can only be assigned in constructors
2764                         //
2765
2766                         if (ec.IsConstructor)
2767                                 return this;
2768
2769                         Report.Error (191, loc,
2770                                       "Readonly field can not be assigned outside " +
2771                                       "of constructor or variable initializer");
2772                         
2773                         return null;
2774                 }
2775
2776                 override public void Emit (EmitContext ec)
2777                 {
2778                         ILGenerator ig = ec.ig;
2779
2780                         if (FieldInfo.IsStatic)
2781                                 ig.Emit (OpCodes.Ldsfld, FieldInfo);
2782                         else {
2783                                 InstanceExpression.Emit (ec);
2784                                 
2785                                 ig.Emit (OpCodes.Ldfld, FieldInfo);
2786                         }
2787                 }
2788
2789                 public void EmitAssign (EmitContext ec, Expression source)
2790                 {
2791                         bool is_static = FieldInfo.IsStatic;
2792
2793                         if (!is_static){
2794                                 Expression instance = InstanceExpression;
2795
2796                                 if (instance.Type.IsValueType){
2797                                         if (instance is IMemoryLocation){
2798                                                 IMemoryLocation ml = (IMemoryLocation) instance;
2799
2800                                                 ml.AddressOf (ec);
2801                                         } else
2802                                                 throw new Exception ("The " + instance + " of type " + Type+
2803                                                                      "represents a ValueType and does not " +
2804                                                                      "implement IMemoryLocation");
2805                                 } else
2806                                         instance.Emit (ec);
2807                         }
2808                         source.Emit (ec);
2809                         
2810                         if (is_static)
2811                                 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
2812                         else {
2813                                 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
2814                         }
2815                 }
2816                 
2817                 public void AddressOf (EmitContext ec)
2818                 {
2819                         if (FieldInfo.IsStatic)
2820                                 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
2821                         else {
2822                                 InstanceExpression.Emit (ec);
2823                                 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
2824                         }
2825                 }
2826         }
2827         
2828         /// <summary>
2829         ///   Expression that evaluates to a Property.  The Assign class
2830         ///   might set the `Value' expression if we are in an assignment.
2831         ///
2832         ///   This is not an LValue because we need to re-write the expression, we
2833         ///   can not take data from the stack and store it.  
2834         /// </summary>
2835         public class PropertyExpr : ExpressionStatement, IAssignMethod {
2836                 public readonly PropertyInfo PropertyInfo;
2837                 public readonly bool IsStatic;
2838                 MethodInfo [] Accessors;
2839                 Location loc;
2840                 
2841                 Expression instance_expr;
2842                 
2843                 public PropertyExpr (PropertyInfo pi, Location l)
2844                 {
2845                         PropertyInfo = pi;
2846                         eclass = ExprClass.PropertyAccess;
2847                         IsStatic = false;
2848                         loc = l;
2849                         Accessors = TypeManager.GetAccessors (pi);
2850
2851                         if (Accessors != null)
2852                                 for (int i = 0; i < Accessors.Length; i++){
2853                                         if (Accessors [i] != null)
2854                                                 if (Accessors [i].IsStatic)
2855                                                         IsStatic = true;
2856                                 }
2857                         else
2858                                 Accessors = new MethodInfo [2];
2859                         
2860                         type = pi.PropertyType;
2861                 }
2862
2863                 //
2864                 // The instance expression associated with this expression
2865                 //
2866                 public Expression InstanceExpression {
2867                         set {
2868                                 instance_expr = value;
2869                         }
2870
2871                         get {
2872                                 return instance_expr;
2873                         }
2874                 }
2875
2876                 public bool VerifyAssignable ()
2877                 {
2878                         if (!PropertyInfo.CanWrite){
2879                                 Report.Error (200, loc, 
2880                                               "The property `" + PropertyInfo.Name +
2881                                               "' can not be assigned to, as it has not set accessor");
2882                                 return false;
2883                         }
2884
2885                         return true;
2886                 }
2887
2888                 override public Expression DoResolve (EmitContext ec)
2889                 {
2890                         if (!PropertyInfo.CanRead){
2891                                 Report.Error (154, loc, 
2892                                               "The property `" + PropertyInfo.Name +
2893                                               "' can not be used in " +
2894                                               "this context because it lacks a get accessor");
2895                                 return null;
2896                         }
2897
2898                         type = PropertyInfo.PropertyType;
2899
2900                         return this;
2901                 }
2902
2903                 override public void Emit (EmitContext ec)
2904                 {
2905                         Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [0], null);
2906                         
2907                 }
2908
2909                 //
2910                 // Implements the IAssignMethod interface for assignments
2911                 //
2912                 public void EmitAssign (EmitContext ec, Expression source)
2913                 {
2914                         Argument arg = new Argument (source, Argument.AType.Expression);
2915                         ArrayList args = new ArrayList ();
2916
2917                         args.Add (arg);
2918                         Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [1], args);
2919                 }
2920
2921                 override public void EmitStatement (EmitContext ec)
2922                 {
2923                         Emit (ec);
2924                         ec.ig.Emit (OpCodes.Pop);
2925                 }
2926         }
2927
2928         /// <summary>
2929         ///   Fully resolved expression that evaluates to a Expression
2930         /// </summary>
2931         public class EventExpr : Expression {
2932                 public readonly EventInfo EventInfo;
2933                 Location loc;
2934                 
2935                 public EventExpr (EventInfo ei, Location loc)
2936                 {
2937                         EventInfo = ei;
2938                         this.loc = loc;
2939                         eclass = ExprClass.EventAccess;
2940                 }
2941
2942                 override public Expression DoResolve (EmitContext ec)
2943                 {
2944                         // We are born in resolved state.
2945                         return this;
2946                 }
2947
2948                 override public void Emit (EmitContext ec)
2949                 {
2950                         throw new Exception ("Implement me");
2951                         // FIXME: Implement.
2952                 }
2953         }
2954 }