2002-08-07 Martin Baulig <martin@gnome.org>
[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         /// <remarks>
40         ///   This is used to tell Resolve in which types of expressions we're
41         ///   interested.
42         /// </remarks>
43         [Flags]
44         public enum ResolveFlags {
45                 // Returns Value, Variable, PropertyAccess, EventAccess or IndexerAccess.
46                 VariableOrValue         = 1,
47
48                 // Returns a type expression.
49                 Type                    = 2,
50
51                 // Returns a method group.
52                 MethodGroup             = 4,
53
54                 // Allows SimpleNames to be returned.
55                 // This is used by MemberAccess to construct long names that can not be
56                 // partially resolved (namespace-qualified names for example).
57                 SimpleName              = 8
58         }
59
60         //
61         // This is just as a hint to AddressOf of what will be done with the
62         // address.
63         [Flags]
64         public enum AddressOp {
65                 Store = 1,
66                 Load  = 2,
67                 LoadStore = 3
68         };
69         
70         /// <summary>
71         ///   This interface is implemented by variables
72         /// </summary>
73         public interface IMemoryLocation {
74                 /// <summary>
75                 ///   The AddressOf method should generate code that loads
76                 ///   the address of the object and leaves it on the stack.
77                 ///
78                 ///   The `mode' argument is used to notify the expression
79                 ///   of whether this will be used to read from the address or
80                 ///   write to the address.
81                 ///
82                 ///   This is just a hint that can be used to provide good error
83                 ///   reporting, and should have no other side effects. 
84                 /// </summary>
85                 void AddressOf (EmitContext ec, AddressOp mode);
86         }
87
88         /// <summary>
89         ///   This interface denotes an expression which evaluates to a member
90         ///   of a struct or a class.
91         /// </summary>
92         public interface IMemberExpr
93         {
94                 /// <summary>
95                 ///   The name of this member.
96                 /// </summary>
97                 string Name {
98                         get;
99                 }
100
101                 /// <summary>
102                 ///   Whether this is a static member.
103                 /// </summary>
104                 bool IsStatic {
105                         get;
106                 }
107
108                 /// <summary>
109                 ///   The instance expression associated with this member, if it's a
110                 ///   non-static member.
111                 /// </summary>
112                 Expression InstanceExpression {
113                         get; set;
114                 }
115         }
116
117         /// <remarks>
118         ///   Base class for expressions
119         /// </remarks>
120         public abstract class Expression {
121                 public ExprClass eclass;
122                 protected Type type;
123                 protected Location loc;
124                 
125                 public Type Type {
126                         get {
127                                 return type;
128                         }
129
130                         set {
131                                 type = value;
132                         }
133                 }
134
135                 public Location Location {
136                         get {
137                                 return loc;
138                         }
139                 }
140
141                 /// <summary>
142                 ///   Utility wrapper routine for Error, just to beautify the code
143                 /// </summary>
144                 public void Error (int error, string s)
145                 {
146                         if (!Location.IsNull (loc))
147                                 Report.Error (error, loc, s);
148                         else
149                                 Report.Error (error, s);
150                 }
151
152                 /// <summary>
153                 ///   Utility wrapper routine for Warning, just to beautify the code
154                 /// </summary>
155                 public void Warning (int warning, string s)
156                 {
157                         if (!Location.IsNull (loc))
158                                 Report.Warning (warning, loc, s);
159                         else
160                                 Report.Warning (warning, s);
161                 }
162
163                 /// <summary>
164                 ///   Utility wrapper routine for Warning, only prints the warning if
165                 ///   warnings of level `level' are enabled.
166                 /// </summary>
167                 public void Warning (int warning, int level, string s)
168                 {
169                         if (level <= RootContext.WarningLevel)
170                                 Warning (warning, s);
171                 }
172
173                 static public void Error_CannotConvertType (Location loc, Type source, Type target)
174                 {
175                         Report.Error (30, loc, "Cannot convert type '" +
176                                       TypeManager.CSharpName (source) + "' to '" +
177                                       TypeManager.CSharpName (target) + "'");
178                 }
179
180                 /// <summary>
181                 ///   Performs semantic analysis on the Expression
182                 /// </summary>
183                 ///
184                 /// <remarks>
185                 ///   The Resolve method is invoked to perform the semantic analysis
186                 ///   on the node.
187                 ///
188                 ///   The return value is an expression (it can be the
189                 ///   same expression in some cases) or a new
190                 ///   expression that better represents this node.
191                 ///   
192                 ///   For example, optimizations of Unary (LiteralInt)
193                 ///   would return a new LiteralInt with a negated
194                 ///   value.
195                 ///   
196                 ///   If there is an error during semantic analysis,
197                 ///   then an error should be reported (using Report)
198                 ///   and a null value should be returned.
199                 ///   
200                 ///   There are two side effects expected from calling
201                 ///   Resolve(): the the field variable "eclass" should
202                 ///   be set to any value of the enumeration
203                 ///   `ExprClass' and the type variable should be set
204                 ///   to a valid type (this is the type of the
205                 ///   expression).
206                 /// </remarks>
207                 public abstract Expression DoResolve (EmitContext ec);
208
209                 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
210                 {
211                         return DoResolve (ec);
212                 }
213                 
214                 /// <summary>
215                 ///   Resolves an expression and performs semantic analysis on it.
216                 /// </summary>
217                 ///
218                 /// <remarks>
219                 ///   Currently Resolve wraps DoResolve to perform sanity
220                 ///   checking and assertion checking on what we expect from Resolve.
221                 /// </remarks>
222                 public Expression Resolve (EmitContext ec, ResolveFlags flags)
223                 {
224                         Expression e;
225
226                         if (this is SimpleName)
227                                 e = ((SimpleName) this).DoResolveAllowStatic (ec);
228                         else 
229                                 e = DoResolve (ec);
230
231                         if (e == null)
232                                 return null;
233
234                         if (e is SimpleName){
235                                 SimpleName s = (SimpleName) e;
236
237                                 if ((flags & ResolveFlags.SimpleName) == 0) {
238                                         Report.Error (
239                                                 103, loc,
240                                                 "The name `" + s.Name + "' could not be found in `" +
241                                                 ec.DeclSpace.Name + "'");
242                                         return null;
243                                 }
244
245                                 return s;
246                         }
247
248                         if ((e is TypeExpr) || (e is ComposedCast)) {
249                                 if ((flags & ResolveFlags.Type) == 0) {
250                                         e.Error118 (flags);
251                                         return null;
252                                 }
253
254                                 return e;
255                         }
256
257                         switch (e.eclass) {
258                         case ExprClass.Type:
259                                 if ((flags & ResolveFlags.VariableOrValue) == 0) {
260                                         e.Error118 (flags);
261                                         return null;
262                                 }
263                                 break;
264
265                         case ExprClass.MethodGroup:
266                                 if ((flags & ResolveFlags.MethodGroup) == 0) {
267                                         ((MethodGroupExpr) e).ReportUsageError ();
268                                         return null;
269                                 }
270                                 break;
271
272                         case ExprClass.Value:
273                         case ExprClass.Variable:
274                         case ExprClass.PropertyAccess:
275                         case ExprClass.EventAccess:
276                         case ExprClass.IndexerAccess:
277                                 if ((flags & ResolveFlags.VariableOrValue) == 0) {
278                                         e.Error118 (flags);
279                                         return null;
280                                 }
281                                 break;
282
283                         default:
284                                 throw new Exception ("Expression " + e.GetType () +
285                                                      " ExprClass is Invalid after resolve");
286                         }
287
288                         if (e.type == null)
289                                 throw new Exception (
290                                         "Expression " + e.GetType () +
291                                         " did not set its type after Resolve\n" +
292                                         "called from: " + this.GetType ());
293
294                         return e;
295                 }
296
297                 /// <summary>
298                 ///   Resolves an expression and performs semantic analysis on it.
299                 /// </summary>
300                 public Expression Resolve (EmitContext ec)
301                 {
302                         return Resolve (ec, ResolveFlags.VariableOrValue);
303                 }
304
305                 /// <summary>
306                 ///   Resolves an expression for LValue assignment
307                 /// </summary>
308                 ///
309                 /// <remarks>
310                 ///   Currently ResolveLValue wraps DoResolveLValue to perform sanity
311                 ///   checking and assertion checking on what we expect from Resolve
312                 /// </remarks>
313                 public Expression ResolveLValue (EmitContext ec, Expression right_side)
314                 {
315                         Expression e = DoResolveLValue (ec, right_side);
316
317                         if (e != null){
318                                 if (e is SimpleName){
319                                         SimpleName s = (SimpleName) e;
320
321                                         Report.Error (
322                                                 103, loc,
323                                                 "The name `" + s.Name + "' could not be found in `" +
324                                                 ec.DeclSpace.Name + "'");
325                                         return null;
326                                 }
327
328                                 if (e.eclass == ExprClass.Invalid)
329                                         throw new Exception ("Expression " + e +
330                                                              " ExprClass is Invalid after resolve");
331
332                                 if (e.eclass == ExprClass.MethodGroup) {
333                                         ((MethodGroupExpr) e).ReportUsageError ();
334                                         return null;
335                                 }
336
337                                 if (e.type == null)
338                                         throw new Exception ("Expression " + e +
339                                                              " did not set its type after Resolve");
340                         }
341
342                         return e;
343                 }
344                 
345                 /// <summary>
346                 ///   Emits the code for the expression
347                 /// </summary>
348                 ///
349                 /// <remarks>
350                 ///   The Emit method is invoked to generate the code
351                 ///   for the expression.  
352                 /// </remarks>
353                 public abstract void Emit (EmitContext ec);
354
355                 /// <summary>
356                 ///   Protected constructor.  Only derivate types should
357                 ///   be able to be created
358                 /// </summary>
359
360                 protected Expression ()
361                 {
362                         eclass = ExprClass.Invalid;
363                         type = null;
364                 }
365
366                 /// <summary>
367                 ///   Returns a literalized version of a literal FieldInfo
368                 /// </summary>
369                 ///
370                 /// <remarks>
371                 ///   The possible return values are:
372                 ///      IntConstant, UIntConstant
373                 ///      LongLiteral, ULongConstant
374                 ///      FloatConstant, DoubleConstant
375                 ///      StringConstant
376                 ///
377                 ///   The value returned is already resolved.
378                 /// </remarks>
379                 public static Constant Constantify (object v, Type t)
380                 {
381                         if (t == TypeManager.int32_type)
382                                 return new IntConstant ((int) v);
383                         else if (t == TypeManager.uint32_type)
384                                 return new UIntConstant ((uint) v);
385                         else if (t == TypeManager.int64_type)
386                                 return new LongConstant ((long) v);
387                         else if (t == TypeManager.uint64_type)
388                                 return new ULongConstant ((ulong) v);
389                         else if (t == TypeManager.float_type)
390                                 return new FloatConstant ((float) v);
391                         else if (t == TypeManager.double_type)
392                                 return new DoubleConstant ((double) v);
393                         else if (t == TypeManager.string_type)
394                                 return new StringConstant ((string) v);
395                         else if (t == TypeManager.short_type)
396                                 return new ShortConstant ((short)v);
397                         else if (t == TypeManager.ushort_type)
398                                 return new UShortConstant ((ushort)v);
399                         else if (t == TypeManager.sbyte_type)
400                                 return new SByteConstant (((sbyte)v));
401                         else if (t == TypeManager.byte_type)
402                                 return new ByteConstant ((byte)v);
403                         else if (t == TypeManager.char_type)
404                                 return new CharConstant ((char)v);
405                         else if (t == TypeManager.bool_type)
406                                 return new BoolConstant ((bool) v);
407                         else if (TypeManager.IsEnumType (t)){
408                                 Constant e = Constantify (v, TypeManager.TypeToCoreType (v.GetType ()));
409
410                                 return new EnumConstant (e, t);
411                         } else
412                                 throw new Exception ("Unknown type for constant (" + t +
413                                                      "), details: " + v);
414                 }
415
416                 /// <summary>
417                 ///   Returns a fully formed expression after a MemberLookup
418                 /// </summary>
419                 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
420                 {
421                         if (mi is EventInfo)
422                                 return new EventExpr ((EventInfo) mi, loc);
423                         else if (mi is FieldInfo)
424                                 return new FieldExpr ((FieldInfo) mi, loc);
425                         else if (mi is PropertyInfo)
426                                 return new PropertyExpr ((PropertyInfo) mi, loc);
427                         else if (mi is Type){
428                                 return new TypeExpr ((System.Type) mi, loc);
429                         }
430
431                         return null;
432                 }
433
434                 //
435                 // FIXME: Probably implement a cache for (t,name,current_access_set)?
436                 //
437                 // This code could use some optimizations, but we need to do some
438                 // measurements.  For example, we could use a delegate to `flag' when
439                 // something can not any longer be a method-group (because it is something
440                 // else).
441                 //
442                 // Return values:
443                 //     If the return value is an Array, then it is an array of
444                 //     MethodBases
445                 //   
446                 //     If the return value is an MemberInfo, it is anything, but a Method
447                 //
448                 //     null on error.
449                 //
450                 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
451                 // the arguments here and have MemberLookup return only the methods that
452                 // match the argument count/type, unlike we are doing now (we delay this
453                 // decision).
454                 //
455                 // This is so we can catch correctly attempts to invoke instance methods
456                 // from a static body (scan for error 120 in ResolveSimpleName).
457                 //
458                 //
459                 // FIXME: Potential optimization, have a static ArrayList
460                 //
461
462                 public static Expression MemberLookup (EmitContext ec, Type t, string name,
463                                                        MemberTypes mt, BindingFlags bf, Location loc)
464                 {
465                         MemberInfo [] mi = TypeManager.MemberLookup (ec.ContainerType, t, mt, bf, name);
466
467                         if (mi == null)
468                                 return null;
469
470                         int count = mi.Length;
471
472                         if (count > 1)
473                                 return new MethodGroupExpr (mi, loc);
474
475                         if (mi [0] is MethodBase)
476                                 return new MethodGroupExpr (mi, loc);
477
478                         return ExprClassFromMemberInfo (ec, mi [0], loc);
479                 }
480
481                 public const MemberTypes AllMemberTypes =
482                         MemberTypes.Constructor |
483                         MemberTypes.Event       |
484                         MemberTypes.Field       |
485                         MemberTypes.Method      |
486                         MemberTypes.NestedType  |
487                         MemberTypes.Property;
488                 
489                 public const BindingFlags AllBindingFlags =
490                         BindingFlags.Public |
491                         BindingFlags.Static |
492                         BindingFlags.Instance;
493
494                 public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
495                 {
496                         return MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
497                 }
498
499                 public static Expression MethodLookup (EmitContext ec, Type t, string name, Location loc)
500                 {
501                         return MemberLookup (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
502                 }
503
504                 /// <summary>
505                 ///   This is a wrapper for MemberLookup that is not used to "probe", but
506                 ///   to find a final definition.  If the final definition is not found, we
507                 ///   look for private members and display a useful debugging message if we
508                 ///   find it.
509                 /// </summary>
510                 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name, 
511                                                             Location loc)
512                 {
513                         return MemberLookupFinal (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
514                 }
515
516                 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
517                                                             MemberTypes mt, BindingFlags bf, Location loc)
518                 {
519                         Expression e;
520
521                         int errors = Report.Errors;
522
523                         e = MemberLookup (ec, t, name, mt, bf, loc);
524
525                         if (e != null)
526                                 return e;
527
528                         // Error has already been reported.
529                         if (errors < Report.Errors)
530                                 return null;
531                         
532                         e = MemberLookup (ec, t, name, AllMemberTypes,
533                                           AllBindingFlags | BindingFlags.NonPublic, loc);
534                         if (e == null){
535                                 Report.Error (
536                                         117, loc, "`" + t + "' does not contain a definition " +
537                                         "for `" + name + "'");
538                         } else {
539                                 Report.Error (
540                                         122, loc, "`" + t + "." + name +
541                                         "' is inaccessible due to its protection level");
542                         }
543                         
544                         return null;
545                 }
546
547                 static public MemberInfo GetFieldFromEvent (EventExpr event_expr)
548                 {
549                         EventInfo ei = event_expr.EventInfo;
550
551                         return TypeManager.GetPrivateFieldOfEvent (ei);
552                 }
553                 
554                 static EmptyExpression MyEmptyExpr;
555                 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
556                 {
557                         Type expr_type = expr.Type;
558
559                         if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
560                                 // if we are a method group, emit a warning
561
562                                 expr.Emit (null);
563                         }
564                         
565                         if (target_type == TypeManager.object_type) {
566                                 //
567                                 // A pointer type cannot be converted to object
568                                 // 
569                                 if (expr_type.IsPointer)
570                                         return null;
571
572                                 if (expr_type.IsValueType)
573                                         return new BoxedCast (expr);
574                                 if (expr_type.IsClass || expr_type.IsInterface)
575                                         return new EmptyCast (expr, target_type);
576                         } else if (expr_type.IsSubclassOf (target_type)) {
577                                 return new EmptyCast (expr, target_type);
578                         } else {
579
580                                 // This code is kind of mirrored inside StandardConversionExists
581                                 // with the small distinction that we only probe there
582                                 //
583                                 // Always ensure that the code here and there is in sync
584                                 
585                                 // from the null type to any reference-type.
586                                 if (expr is NullLiteral && !target_type.IsValueType)
587                                         return new EmptyCast (expr, target_type);
588
589                                 // from any class-type S to any interface-type T.
590                                 if (expr_type.IsClass && target_type.IsInterface) {
591                                         if (TypeManager.ImplementsInterface (expr_type, target_type))
592                                                 return new EmptyCast (expr, target_type);
593                                         else
594                                                 return null;
595                                 }
596
597                                 // from any interface type S to interface-type T.
598                                 if (expr_type.IsInterface && target_type.IsInterface) {
599
600                                         if (TypeManager.ImplementsInterface (expr_type, target_type))
601                                                 return new EmptyCast (expr, target_type);
602                                         else
603                                                 return null;
604                                 }
605                                 
606                                 // from an array-type S to an array-type of type T
607                                 if (expr_type.IsArray && target_type.IsArray) {
608                                         if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
609
610                                                 Type expr_element_type = expr_type.GetElementType ();
611
612                                                 if (MyEmptyExpr == null)
613                                                         MyEmptyExpr = new EmptyExpression ();
614                                                 
615                                                 MyEmptyExpr.SetType (expr_element_type);
616                                                 Type target_element_type = target_type.GetElementType ();
617
618                                                 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
619                                                         if (StandardConversionExists (MyEmptyExpr,
620                                                                                       target_element_type))
621                                                                 return new EmptyCast (expr, target_type);
622                                         }
623                                 }
624                                 
625                                 
626                                 // from an array-type to System.Array
627                                 if (expr_type.IsArray && target_type == TypeManager.array_type)
628                                         return new EmptyCast (expr, target_type);
629                                 
630                                 // from any delegate type to System.Delegate
631                                 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
632                                     target_type == TypeManager.delegate_type)
633                                         return new EmptyCast (expr, target_type);
634                                         
635                                 // from any array-type or delegate type into System.ICloneable.
636                                 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
637                                         if (target_type == TypeManager.icloneable_type)
638                                                 return new EmptyCast (expr, target_type);
639                                 
640                                 return null;
641
642                         }
643                         
644                         return null;
645                 }
646
647                 /// <summary>
648                 ///   Handles expressions like this: decimal d; d = 1;
649                 ///   and changes them into: decimal d; d = new System.Decimal (1);
650                 /// </summary>
651                 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
652                 {
653                         ArrayList args = new ArrayList ();
654
655                         args.Add (new Argument (expr, Argument.AType.Expression));
656
657                         Expression ne = new New (new TypeExpr (target, Location.Null), args, Location.Null);
658
659                         return ne.Resolve (ec);
660                 }
661
662                 /// <summary>
663                 ///   Implicit Numeric Conversions.
664                 ///
665                 ///   expr is the expression to convert, returns a new expression of type
666                 ///   target_type or null if an implicit conversion is not possible.
667                 /// </summary>
668                 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
669                                                                     Type target_type, Location loc)
670                 {
671                         Type expr_type = expr.Type;
672                         
673                         //
674                         // Attempt to do the implicit constant expression conversions
675
676                         if (expr is IntConstant){
677                                 Expression e;
678                                 
679                                 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
680
681                                 if (e != null)
682                                         return e;
683                         } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
684                                 //
685                                 // Try the implicit constant expression conversion
686                                 // from long to ulong, instead of a nice routine,
687                                 // we just inline it
688                                 //
689                                 long v = ((LongConstant) expr).Value;
690                                 if (v > 0)
691                                         return new ULongConstant ((ulong) v);
692                         }
693
694                         //
695                         // If we have an enumeration, extract the underlying type,
696                         // use this during the comparission, but wrap around the original
697                         // target_type
698                         //
699                         Type real_target_type = target_type;
700
701                         if (TypeManager.IsEnumType (real_target_type))
702                                 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
703
704                         if (expr_type == real_target_type)
705                                 return new EmptyCast (expr, target_type);
706                         
707                         if (expr_type == TypeManager.sbyte_type){
708                                 //
709                                 // From sbyte to short, int, long, float, double.
710                                 //
711                                 if (real_target_type == TypeManager.int32_type)
712                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
713                                 if (real_target_type == TypeManager.int64_type)
714                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
715                                 if (real_target_type == TypeManager.double_type)
716                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
717                                 if (real_target_type == TypeManager.float_type)
718                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
719                                 if (real_target_type == TypeManager.short_type)
720                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
721                                 if (real_target_type == TypeManager.decimal_type)
722                                         return InternalTypeConstructor (ec, expr, target_type);
723                         } else if (expr_type == TypeManager.byte_type){
724                                 //
725                                 // From byte to short, ushort, int, uint, long, ulong, float, double
726                                 // 
727                                 if ((real_target_type == TypeManager.short_type) ||
728                                     (real_target_type == TypeManager.ushort_type) ||
729                                     (real_target_type == TypeManager.int32_type) ||
730                                     (real_target_type == TypeManager.uint32_type))
731                                         return new EmptyCast (expr, target_type);
732
733                                 if (real_target_type == TypeManager.uint64_type)
734                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
735                                 if (real_target_type == TypeManager.int64_type)
736                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
737                                 if (real_target_type == TypeManager.float_type)
738                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
739                                 if (real_target_type == TypeManager.double_type)
740                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
741                                 if (real_target_type == TypeManager.decimal_type)
742                                         return InternalTypeConstructor (ec, expr, target_type);
743                         } else if (expr_type == TypeManager.short_type){
744                                 //
745                                 // From short to int, long, float, double
746                                 // 
747                                 if (real_target_type == TypeManager.int32_type)
748                                         return new EmptyCast (expr, target_type);
749                                 if (real_target_type == TypeManager.int64_type)
750                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
751                                 if (real_target_type == TypeManager.double_type)
752                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
753                                 if (real_target_type == TypeManager.float_type)
754                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
755                                 if (real_target_type == TypeManager.decimal_type)
756                                         return InternalTypeConstructor (ec, expr, target_type);
757                         } else if (expr_type == TypeManager.ushort_type){
758                                 //
759                                 // From ushort to int, uint, long, ulong, float, double
760                                 //
761                                 if (real_target_type == TypeManager.uint32_type)
762                                         return new EmptyCast (expr, target_type);
763
764                                 if (real_target_type == TypeManager.uint64_type)
765                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
766                                 if (real_target_type == TypeManager.int32_type)
767                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
768                                 if (real_target_type == TypeManager.int64_type)
769                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
770                                 if (real_target_type == TypeManager.double_type)
771                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
772                                 if (real_target_type == TypeManager.float_type)
773                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
774                                 if (real_target_type == TypeManager.decimal_type)
775                                         return InternalTypeConstructor (ec, expr, target_type);
776                         } else if (expr_type == TypeManager.int32_type){
777                                 //
778                                 // From int to long, float, double
779                                 //
780                                 if (real_target_type == TypeManager.int64_type)
781                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
782                                 if (real_target_type == TypeManager.double_type)
783                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
784                                 if (real_target_type == TypeManager.float_type)
785                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
786                                 if (real_target_type == TypeManager.decimal_type)
787                                         return InternalTypeConstructor (ec, expr, target_type);
788                         } else if (expr_type == TypeManager.uint32_type){
789                                 //
790                                 // From uint to long, ulong, float, double
791                                 //
792                                 if (real_target_type == TypeManager.int64_type)
793                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
794                                 if (real_target_type == TypeManager.uint64_type)
795                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
796                                 if (real_target_type == TypeManager.double_type)
797                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
798                                                                OpCodes.Conv_R8);
799                                 if (real_target_type == TypeManager.float_type)
800                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
801                                                                OpCodes.Conv_R4);
802                                 if (real_target_type == TypeManager.decimal_type)
803                                         return InternalTypeConstructor (ec, expr, target_type);
804                         } else if (expr_type == TypeManager.int64_type){
805                                 //
806                                 // From long/ulong to float, double
807                                 //
808                                 if (real_target_type == TypeManager.double_type)
809                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
810                                 if (real_target_type == TypeManager.float_type)
811                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);     
812                                 if (real_target_type == TypeManager.decimal_type)
813                                         return InternalTypeConstructor (ec, expr, target_type);
814                         } else if (expr_type == TypeManager.uint64_type){
815                                 //
816                                 // From ulong to float, double
817                                 //
818                                 if (real_target_type == TypeManager.double_type)
819                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
820                                                                OpCodes.Conv_R8);
821                                 if (real_target_type == TypeManager.float_type)
822                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
823                                                                OpCodes.Conv_R4);        
824                                 if (real_target_type == TypeManager.decimal_type)
825                                         return InternalTypeConstructor (ec, expr, target_type);
826                         } else if (expr_type == TypeManager.char_type){
827                                 //
828                                 // From char to ushort, int, uint, long, ulong, float, double
829                                 // 
830                                 if ((real_target_type == TypeManager.ushort_type) ||
831                                     (real_target_type == TypeManager.int32_type) ||
832                                     (real_target_type == TypeManager.uint32_type))
833                                         return new EmptyCast (expr, target_type);
834                                 if (real_target_type == TypeManager.uint64_type)
835                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
836                                 if (real_target_type == TypeManager.int64_type)
837                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
838                                 if (real_target_type == TypeManager.float_type)
839                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
840                                 if (real_target_type == TypeManager.double_type)
841                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
842                                 if (real_target_type == TypeManager.decimal_type)
843                                         return InternalTypeConstructor (ec, expr, target_type);
844                         } else if (expr_type == TypeManager.float_type){
845                                 //
846                                 // float to double
847                                 //
848                                 if (real_target_type == TypeManager.double_type)
849                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
850                         }
851
852                         return null;
853                 }
854
855                 //
856                 // Tests whether an implicit reference conversion exists between expr_type
857                 // and target_type
858                 //
859                 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
860                 {
861                         Type expr_type = expr.Type;
862                         
863                         //
864                         // This is the boxed case.
865                         //
866                         if (target_type == TypeManager.object_type) {
867                                 if ((expr_type.IsClass) ||
868                                     (expr_type.IsValueType) ||
869                                     (expr_type.IsInterface))
870                                         return true;
871                                 
872                         } else if (expr_type.IsSubclassOf (target_type)) {
873                                 return true;
874                                 
875                         } else {
876                                 // Please remember that all code below actually comes
877                                 // from ImplicitReferenceConversion so make sure code remains in sync
878                                 
879                                 // from any class-type S to any interface-type T.
880                                 if (expr_type.IsClass && target_type.IsInterface) {
881                                         if (TypeManager.ImplementsInterface (expr_type, target_type))
882                                                 return true;
883                                 }
884                                 
885                                 // from any interface type S to interface-type T.
886                                 if (expr_type.IsInterface && target_type.IsInterface)
887                                         if (TypeManager.ImplementsInterface (expr_type, target_type))
888                                                 return true;
889                                 
890                                 // from an array-type S to an array-type of type T
891                                 if (expr_type.IsArray && target_type.IsArray) {
892                                         if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
893                                                 
894                                                 Type expr_element_type = expr_type.GetElementType ();
895
896                                                 if (MyEmptyExpr == null)
897                                                         MyEmptyExpr = new EmptyExpression ();
898                                                 
899                                                 MyEmptyExpr.SetType (expr_element_type);
900                                                 Type target_element_type = target_type.GetElementType ();
901                                                 
902                                                 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
903                                                         if (StandardConversionExists (MyEmptyExpr,
904                                                                                       target_element_type))
905                                                                 return true;
906                                         }
907                                 }
908                                 
909                                 // from an array-type to System.Array
910                                 if (expr_type.IsArray && (target_type == TypeManager.array_type))
911                                         return true;
912                                 
913                                 // from any delegate type to System.Delegate
914                                 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
915                                     target_type == TypeManager.delegate_type)
916                                         if (target_type.IsAssignableFrom (expr_type))
917                                                 return true;
918                                         
919                                 // from any array-type or delegate type into System.ICloneable.
920                                 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
921                                         if (target_type == TypeManager.icloneable_type)
922                                                 return true;
923                                 
924                                 // from the null type to any reference-type.
925                                 if (expr is NullLiteral && !target_type.IsValueType &&
926                                     !TypeManager.IsEnumType (target_type))
927                                         return true;
928                                 
929                         }
930
931                         return false;
932                 }
933
934                 /// <summary>
935                 ///  Same as StandardConversionExists except that it also looks at
936                 ///  implicit user defined conversions - needed for overload resolution
937                 /// </summary>
938                 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
939                 {
940                         if (StandardConversionExists (expr, target_type) == true)
941                                 return true;
942
943                         Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
944
945                         if (dummy != null)
946                                 return true;
947
948                         return false;
949                 }
950                 
951                 /// <summary>
952                 ///  Determines if a standard implicit conversion exists from
953                 ///  expr_type to target_type
954                 /// </summary>
955                 public static bool StandardConversionExists (Expression expr, Type target_type)
956                 {
957                         Type expr_type = expr.Type;
958                         
959                         if (expr_type == target_type)
960                                 return true;
961
962                         // First numeric conversions 
963
964                         if (expr_type == TypeManager.sbyte_type){
965                                 //
966                                 // From sbyte to short, int, long, float, double.
967                                 //
968                                 if ((target_type == TypeManager.int32_type) || 
969                                     (target_type == TypeManager.int64_type) ||
970                                     (target_type == TypeManager.double_type) ||
971                                     (target_type == TypeManager.float_type)  ||
972                                     (target_type == TypeManager.short_type) ||
973                                     (target_type == TypeManager.decimal_type))
974                                         return true;
975                                 
976                         } else if (expr_type == TypeManager.byte_type){
977                                 //
978                                 // From byte to short, ushort, int, uint, long, ulong, float, double
979                                 // 
980                                 if ((target_type == TypeManager.short_type) ||
981                                     (target_type == TypeManager.ushort_type) ||
982                                     (target_type == TypeManager.int32_type) ||
983                                     (target_type == TypeManager.uint32_type) ||
984                                     (target_type == TypeManager.uint64_type) ||
985                                     (target_type == TypeManager.int64_type) ||
986                                     (target_type == TypeManager.float_type) ||
987                                     (target_type == TypeManager.double_type) ||
988                                     (target_type == TypeManager.decimal_type))
989                                         return true;
990         
991                         } else if (expr_type == TypeManager.short_type){
992                                 //
993                                 // From short to int, long, float, double
994                                 // 
995                                 if ((target_type == TypeManager.int32_type) ||
996                                     (target_type == TypeManager.int64_type) ||
997                                     (target_type == TypeManager.double_type) ||
998                                     (target_type == TypeManager.float_type) ||
999                                     (target_type == TypeManager.decimal_type))
1000                                         return true;
1001                                         
1002                         } else if (expr_type == TypeManager.ushort_type){
1003                                 //
1004                                 // From ushort to int, uint, long, ulong, float, double
1005                                 //
1006                                 if ((target_type == TypeManager.uint32_type) ||
1007                                     (target_type == TypeManager.uint64_type) ||
1008                                     (target_type == TypeManager.int32_type) ||
1009                                     (target_type == TypeManager.int64_type) ||
1010                                     (target_type == TypeManager.double_type) ||
1011                                     (target_type == TypeManager.float_type) ||
1012                                     (target_type == TypeManager.decimal_type))
1013                                         return true;
1014                                     
1015                         } else if (expr_type == TypeManager.int32_type){
1016                                 //
1017                                 // From int to long, float, double
1018                                 //
1019                                 if ((target_type == TypeManager.int64_type) ||
1020                                     (target_type == TypeManager.double_type) ||
1021                                     (target_type == TypeManager.float_type) ||
1022                                     (target_type == TypeManager.decimal_type))
1023                                         return true;
1024                                         
1025                         } else if (expr_type == TypeManager.uint32_type){
1026                                 //
1027                                 // From uint to long, ulong, float, double
1028                                 //
1029                                 if ((target_type == TypeManager.int64_type) ||
1030                                     (target_type == TypeManager.uint64_type) ||
1031                                     (target_type == TypeManager.double_type) ||
1032                                     (target_type == TypeManager.float_type) ||
1033                                     (target_type == TypeManager.decimal_type))
1034                                         return true;
1035                                         
1036                         } else if ((expr_type == TypeManager.uint64_type) ||
1037                                    (expr_type == TypeManager.int64_type)) {
1038                                 //
1039                                 // From long/ulong to float, double
1040                                 //
1041                                 if ((target_type == TypeManager.double_type) ||
1042                                     (target_type == TypeManager.float_type) ||
1043                                     (target_type == TypeManager.decimal_type))
1044                                         return true;
1045                                     
1046                         } else if (expr_type == TypeManager.char_type){
1047                                 //
1048                                 // From char to ushort, int, uint, long, ulong, float, double
1049                                 // 
1050                                 if ((target_type == TypeManager.ushort_type) ||
1051                                     (target_type == TypeManager.int32_type) ||
1052                                     (target_type == TypeManager.uint32_type) ||
1053                                     (target_type == TypeManager.uint64_type) ||
1054                                     (target_type == TypeManager.int64_type) ||
1055                                     (target_type == TypeManager.float_type) ||
1056                                     (target_type == TypeManager.double_type) ||
1057                                     (target_type == TypeManager.decimal_type))
1058                                         return true;
1059
1060                         } else if (expr_type == TypeManager.float_type){
1061                                 //
1062                                 // float to double
1063                                 //
1064                                 if (target_type == TypeManager.double_type)
1065                                         return true;
1066                         }       
1067                         
1068                         if (ImplicitReferenceConversionExists (expr, target_type))
1069                                 return true;
1070                         
1071                         if (expr is IntConstant){
1072                                 int value = ((IntConstant) expr).Value;
1073
1074                                 if (target_type == TypeManager.sbyte_type){
1075                                         if (value >= SByte.MinValue && value <= SByte.MaxValue)
1076                                                 return true;
1077                                 } else if (target_type == TypeManager.byte_type){
1078                                         if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1079                                                 return true;
1080                                 } else if (target_type == TypeManager.short_type){
1081                                         if (value >= Int16.MinValue && value <= Int16.MaxValue)
1082                                                 return true;
1083                                 } else if (target_type == TypeManager.ushort_type){
1084                                         if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1085                                                 return true;
1086                                 } else if (target_type == TypeManager.uint32_type){
1087                                         if (value >= 0)
1088                                                 return true;
1089                                 } else if (target_type == TypeManager.uint64_type){
1090                                          //
1091                                          // we can optimize this case: a positive int32
1092                                          // always fits on a uint64.  But we need an opcode
1093                                          // to do it.
1094                                          //
1095                                         if (value >= 0)
1096                                                 return true;
1097                                 }
1098                                 
1099                                 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
1100                                         return true;
1101                         }
1102
1103                         if (expr is LongConstant && target_type == TypeManager.uint64_type){
1104                                 //
1105                                 // Try the implicit constant expression conversion
1106                                 // from long to ulong, instead of a nice routine,
1107                                 // we just inline it
1108                                 //
1109                                 long v = ((LongConstant) expr).Value;
1110                                 if (v > 0)
1111                                         return true;
1112                         }
1113                         
1114                         if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1115                                 IntLiteral i = (IntLiteral) expr;
1116
1117                                 if (i.Value == 0)
1118                                         return true;
1119                         }
1120                         return false;
1121                 }
1122
1123                 //
1124                 // Used internally by FindMostEncompassedType, this is used
1125                 // to avoid creating lots of objects in the tight loop inside
1126                 // FindMostEncompassedType
1127                 //
1128                 static EmptyExpression priv_fmet_param;
1129                 
1130                 /// <summary>
1131                 ///  Finds "most encompassed type" according to the spec (13.4.2)
1132                 ///  amongst the methods in the MethodGroupExpr
1133                 /// </summary>
1134                 static Type FindMostEncompassedType (ArrayList types)
1135                 {
1136                         Type best = null;
1137
1138                         if (priv_fmet_param == null)
1139                                 priv_fmet_param = new EmptyExpression ();
1140
1141                         foreach (Type t in types){
1142                                 priv_fmet_param.SetType (t);
1143                                 
1144                                 if (best == null) {
1145                                         best = t;
1146                                         continue;
1147                                 }
1148                                 
1149                                 if (StandardConversionExists (priv_fmet_param, best))
1150                                         best = t;
1151                         }
1152
1153                         return best;
1154                 }
1155
1156                 //
1157                 // Used internally by FindMostEncompassingType, this is used
1158                 // to avoid creating lots of objects in the tight loop inside
1159                 // FindMostEncompassingType
1160                 //
1161                 static EmptyExpression priv_fmee_ret;
1162                 
1163                 /// <summary>
1164                 ///  Finds "most encompassing type" according to the spec (13.4.2)
1165                 ///  amongst the types in the given set
1166                 /// </summary>
1167                 static Type FindMostEncompassingType (ArrayList types)
1168                 {
1169                         Type best = null;
1170
1171                         if (priv_fmee_ret == null)
1172                                 priv_fmee_ret = new EmptyExpression ();
1173
1174                         foreach (Type t in types){
1175                                 priv_fmee_ret.SetType (best);
1176
1177                                 if (best == null) {
1178                                         best = t;
1179                                         continue;
1180                                 }
1181
1182                                 if (StandardConversionExists (priv_fmee_ret, t))
1183                                         best = t;
1184                         }
1185                         
1186                         return best;
1187                 }
1188
1189                 //
1190                 // Used to avoid creating too many objects
1191                 //
1192                 static EmptyExpression priv_fms_expr;
1193                 
1194                 /// <summary>
1195                 ///   Finds the most specific source Sx according to the rules of the spec (13.4.4)
1196                 ///   by making use of FindMostEncomp* methods. Applies the correct rules separately
1197                 ///   for explicit and implicit conversion operators.
1198                 /// </summary>
1199                 static public Type FindMostSpecificSource (MethodGroupExpr me, Type source_type,
1200                                                            bool apply_explicit_conv_rules,
1201                                                            Location loc)
1202                 {
1203                         ArrayList src_types_set = new ArrayList ();
1204                         
1205                         if (priv_fms_expr == null)
1206                                 priv_fms_expr = new EmptyExpression ();
1207                         
1208                         //
1209                         // If any operator converts from S then Sx = S
1210                         //
1211                         foreach (MethodBase mb in me.Methods){
1212                                 ParameterData pd = Invocation.GetParameterData (mb);
1213                                 Type param_type = pd.ParameterType (0);
1214
1215                                 if (param_type == source_type)
1216                                         return param_type;
1217
1218                                 if (apply_explicit_conv_rules) {
1219                                         //
1220                                         // From the spec :
1221                                         // Find the set of applicable user-defined conversion operators, U.  This set
1222                                         // consists of the
1223                                         // user-defined implicit or explicit conversion operators declared by
1224                                         // the classes or structs in D that convert from a type encompassing
1225                                         // or encompassed by S to a type encompassing or encompassed by T
1226                                         //
1227                                         priv_fms_expr.SetType (param_type);
1228                                         if (StandardConversionExists (priv_fms_expr, source_type))
1229                                                 src_types_set.Add (param_type);
1230                                         else {
1231                                                 priv_fms_expr.SetType (source_type);
1232                                                 if (StandardConversionExists (priv_fms_expr, param_type))
1233                                                         src_types_set.Add (param_type);
1234                                         }
1235                                 } else {
1236                                         //
1237                                         // Only if S is encompassed by param_type
1238                                         //
1239                                         priv_fms_expr.SetType (source_type);
1240                                         if (StandardConversionExists (priv_fms_expr, param_type))
1241                                                 src_types_set.Add (param_type);
1242                                 }
1243                         }
1244                         
1245                         //
1246                         // Explicit Conv rules
1247                         //
1248                         if (apply_explicit_conv_rules) {
1249                                 ArrayList candidate_set = new ArrayList ();
1250
1251                                 foreach (Type param_type in src_types_set){
1252                                         priv_fms_expr.SetType (source_type);
1253                                         
1254                                         if (StandardConversionExists (priv_fms_expr, param_type))
1255                                                 candidate_set.Add (param_type);
1256                                 }
1257
1258                                 if (candidate_set.Count != 0)
1259                                         return FindMostEncompassedType (candidate_set);
1260                         }
1261
1262                         //
1263                         // Final case
1264                         //
1265                         if (apply_explicit_conv_rules)
1266                                 return FindMostEncompassingType (src_types_set);
1267                         else
1268                                 return FindMostEncompassedType (src_types_set);
1269                 }
1270
1271                 //
1272                 // Useful in avoiding proliferation of objects
1273                 //
1274                 static EmptyExpression priv_fmt_expr;
1275                 
1276                 /// <summary>
1277                 ///  Finds the most specific target Tx according to section 13.4.4
1278                 /// </summary>
1279                 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
1280                                                            bool apply_explicit_conv_rules,
1281                                                            Location loc)
1282                 {
1283                         ArrayList tgt_types_set = new ArrayList ();
1284                         
1285                         if (priv_fmt_expr == null)
1286                                 priv_fmt_expr = new EmptyExpression ();
1287                         
1288                         //
1289                         // If any operator converts to T then Tx = T
1290                         //
1291                         foreach (MethodInfo mi in me.Methods){
1292                                 Type ret_type = mi.ReturnType;
1293
1294                                 if (ret_type == target)
1295                                         return ret_type;
1296
1297                                 if (apply_explicit_conv_rules) {
1298                                         //
1299                                         // From the spec :
1300                                         // Find the set of applicable user-defined conversion operators, U.
1301                                         //
1302                                         // This set consists of the
1303                                         // user-defined implicit or explicit conversion operators declared by
1304                                         // the classes or structs in D that convert from a type encompassing
1305                                         // or encompassed by S to a type encompassing or encompassed by T
1306                                         //
1307                                         priv_fms_expr.SetType (ret_type);
1308                                         if (StandardConversionExists (priv_fms_expr, target))
1309                                                 tgt_types_set.Add (ret_type);
1310                                         else {
1311                                                 priv_fms_expr.SetType (target);
1312                                                 if (StandardConversionExists (priv_fms_expr, ret_type))
1313                                                         tgt_types_set.Add (ret_type);
1314                                         }
1315                                 } else {
1316                                         //
1317                                         // Only if T is encompassed by param_type
1318                                         //
1319                                         priv_fms_expr.SetType (ret_type);
1320                                         if (StandardConversionExists (priv_fms_expr, target))
1321                                                 tgt_types_set.Add (ret_type);
1322                                 }
1323                         }
1324
1325                         //
1326                         // Explicit conv rules
1327                         //
1328                         if (apply_explicit_conv_rules) {
1329                                 ArrayList candidate_set = new ArrayList ();
1330
1331                                 foreach (Type ret_type in tgt_types_set){
1332                                         priv_fmt_expr.SetType (ret_type);
1333                                         
1334                                         if (StandardConversionExists (priv_fmt_expr, target))
1335                                                 candidate_set.Add (ret_type);
1336                                 }
1337
1338                                 if (candidate_set.Count != 0)
1339                                         return FindMostEncompassingType (candidate_set);
1340                         }
1341                         
1342                         //
1343                         // Okay, final case !
1344                         //
1345                         if (apply_explicit_conv_rules)
1346                                 return FindMostEncompassedType (tgt_types_set);
1347                         else
1348                                 return FindMostEncompassingType (tgt_types_set);
1349                 }
1350                 
1351                 /// <summary>
1352                 ///  User-defined Implicit conversions
1353                 /// </summary>
1354                 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1355                                                                  Type target, Location loc)
1356                 {
1357                         return UserDefinedConversion (ec, source, target, loc, false);
1358                 }
1359
1360                 /// <summary>
1361                 ///  User-defined Explicit conversions
1362                 /// </summary>
1363                 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1364                                                                  Type target, Location loc)
1365                 {
1366                         return UserDefinedConversion (ec, source, target, loc, true);
1367                 }
1368
1369                 /// <summary>
1370                 ///   Computes the MethodGroup for the user-defined conversion
1371                 ///   operators from source_type to target_type.  `look_for_explicit'
1372                 ///   controls whether we should also include the list of explicit
1373                 ///   operators
1374                 /// </summary>
1375                 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1376                                                                Type source_type, Type target_type,
1377                                                                Location loc, bool look_for_explicit)
1378                 {
1379                         Expression mg1 = null, mg2 = null;
1380                         Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1381                         string op_name;
1382
1383                         //
1384                         // FIXME : How does the False operator come into the picture ?
1385                         // This doesn't look complete and very correct !
1386                         //
1387                         if (target_type == TypeManager.bool_type && !look_for_explicit)
1388                                 op_name = "op_True";
1389                         else
1390                                 op_name = "op_Implicit";
1391
1392                         MethodGroupExpr union3;
1393                         
1394                         mg1 = MethodLookup (ec, source_type, op_name, loc);
1395                         if (source_type.BaseType != null)
1396                                 mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1397
1398                         if (mg1 == null)
1399                                 union3 = (MethodGroupExpr) mg2;
1400                         else if (mg2 == null)
1401                                 union3 = (MethodGroupExpr) mg1;
1402                         else
1403                                 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1404
1405                         mg1 = MethodLookup (ec, target_type, op_name, loc);
1406                         if (mg1 != null){
1407                                 if (union3 != null)
1408                                         union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1409                                 else
1410                                         union3 = (MethodGroupExpr) mg1;
1411                         }
1412
1413                         if (target_type.BaseType != null)
1414                                 mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1415                         
1416                         if (mg1 != null){
1417                                 if (union3 != null)
1418                                         union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1419                                 else
1420                                         union3 = (MethodGroupExpr) mg1;
1421                         }
1422
1423                         MethodGroupExpr union4 = null;
1424
1425                         if (look_for_explicit) {
1426                                 op_name = "op_Explicit";
1427
1428                                 mg5 = MemberLookup (ec, source_type, op_name, loc);
1429                                 if (source_type.BaseType != null)
1430                                         mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1431                                 
1432                                 mg7 = MemberLookup (ec, target_type, op_name, loc);
1433                                 if (target_type.BaseType != null)
1434                                         mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1435                                 
1436                                 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1437                                 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1438
1439                                 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1440                         }
1441                         
1442                         return Invocation.MakeUnionSet (union3, union4, loc);
1443                 }
1444                 
1445                 /// <summary>
1446                 ///   User-defined conversions
1447                 /// </summary>
1448                 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1449                                                                 Type target, Location loc,
1450                                                                 bool look_for_explicit)
1451                 {
1452                         MethodGroupExpr union;
1453                         Type source_type = source.Type;
1454                         MethodBase method = null;
1455                         
1456                         union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1457                         if (union == null)
1458                                 return null;
1459                         
1460                         Type most_specific_source, most_specific_target;
1461
1462 #if BLAH
1463                         foreach (MethodBase m in union.Methods){
1464                                 Console.WriteLine ("Name: " + m.Name);
1465                                 Console.WriteLine ("    : " + ((MethodInfo)m).ReturnType);
1466                         }
1467 #endif
1468                         
1469                         most_specific_source = FindMostSpecificSource (union, source_type, look_for_explicit, loc);
1470                         if (most_specific_source == null)
1471                                 return null;
1472
1473                         most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1474                         if (most_specific_target == null) 
1475                                 return null;
1476                         
1477                         int count = 0;
1478
1479                         foreach (MethodBase mb in union.Methods){
1480                                 ParameterData pd = Invocation.GetParameterData (mb);
1481                                 MethodInfo mi = (MethodInfo) mb;
1482                                 
1483                                 if (pd.ParameterType (0) == most_specific_source &&
1484                                     mi.ReturnType == most_specific_target) {
1485                                         method = mb;
1486                                         count++;
1487                                 }
1488                         }
1489                         
1490                         if (method == null || count > 1) {
1491                                 Report.Error (-11, loc, "Ambiguous user defined conversion");
1492                                 return null;
1493                         }
1494                         
1495                         //
1496                         // This will do the conversion to the best match that we
1497                         // found.  Now we need to perform an implict standard conversion
1498                         // if the best match was not the type that we were requested
1499                         // by target.
1500                         //
1501                         if (look_for_explicit)
1502                                 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1503                         else
1504                                 source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
1505
1506                         if (source == null)
1507                                 return null;
1508
1509                         Expression e;
1510                         e =  new UserCast ((MethodInfo) method, source, loc);
1511                         if (e.Type != target){
1512                                 if (!look_for_explicit)
1513                                         e = ConvertImplicitStandard (ec, e, target, loc);
1514                                 else
1515                                         e = ConvertExplicitStandard (ec, e, target, loc);
1516                         } 
1517                         return e;
1518                 }
1519                 
1520                 /// <summary>
1521                 ///   Converts implicitly the resolved expression `expr' into the
1522                 ///   `target_type'.  It returns a new expression that can be used
1523                 ///   in a context that expects a `target_type'. 
1524                 /// </summary>
1525                 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1526                                                           Type target_type, Location loc)
1527                 {
1528                         Type expr_type = expr.Type;
1529                         Expression e;
1530
1531                         if (expr_type == target_type)
1532                                 return expr;
1533
1534                         if (target_type == null)
1535                                 throw new Exception ("Target type is null");
1536
1537                         e = ConvertImplicitStandard (ec, expr, target_type, loc);
1538                         if (e != null)
1539                                 return e;
1540
1541                         e = ImplicitUserConversion (ec, expr, target_type, loc);
1542                         if (e != null)
1543                                 return e;
1544
1545                         return null;
1546                 }
1547
1548                 
1549                 /// <summary>
1550                 ///   Attempts to apply the `Standard Implicit
1551                 ///   Conversion' rules to the expression `expr' into
1552                 ///   the `target_type'.  It returns a new expression
1553                 ///   that can be used in a context that expects a
1554                 ///   `target_type'.
1555                 ///
1556                 ///   This is different from `ConvertImplicit' in that the
1557                 ///   user defined implicit conversions are excluded. 
1558                 /// </summary>
1559                 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1560                                                                   Type target_type, Location loc)
1561                 {
1562                         Type expr_type = expr.Type;
1563                         Expression e;
1564
1565                         if (expr_type == target_type)
1566                                 return expr;
1567
1568                         e = ImplicitNumericConversion (ec, expr, target_type, loc);
1569                         if (e != null)
1570                                 return e;
1571
1572                         e = ImplicitReferenceConversion (expr, target_type);
1573                         if (e != null)
1574                                 return e;
1575
1576                         if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1577                                 IntLiteral i = (IntLiteral) expr;
1578
1579                                 if (i.Value == 0)
1580                                         return new EmptyCast (expr, target_type);
1581                         }
1582
1583                         if (ec.InUnsafe) {
1584                                 if (expr_type.IsPointer){
1585                                         if (target_type == TypeManager.void_ptr_type)
1586                                                 return new EmptyCast (expr, target_type);
1587
1588                                         //
1589                                         // yep, comparing pointer types cant be done with
1590                                         // t1 == t2, we have to compare their element types.
1591                                         //
1592                                         if (target_type.IsPointer){
1593                                                 if (target_type.GetElementType()==expr_type.GetElementType())
1594                                                         return expr;
1595                                         }
1596                                 }
1597                                 
1598                                 if (target_type.IsPointer){
1599                                         if (expr is NullLiteral)
1600                                                 return new EmptyCast (expr, target_type);
1601                                 }
1602                         }
1603
1604                         return null;
1605                 }
1606
1607                 /// <summary>
1608                 ///   Attemps to perform an implict constant conversion of the IntConstant
1609                 ///   into a different data type using casts (See Implicit Constant
1610                 ///   Expression Conversions)
1611                 /// </summary>
1612                 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1613                 {
1614                         int value = ic.Value;
1615
1616                         //
1617                         // FIXME: This could return constants instead of EmptyCasts
1618                         //
1619                         if (target_type == TypeManager.sbyte_type){
1620                                 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1621                                         return new SByteConstant ((sbyte) value);
1622                         } else if (target_type == TypeManager.byte_type){
1623                                 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1624                                         return new ByteConstant ((byte) value);
1625                         } else if (target_type == TypeManager.short_type){
1626                                 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1627                                         return new ShortConstant ((short) value);
1628                         } else if (target_type == TypeManager.ushort_type){
1629                                 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1630                                         return new UShortConstant ((ushort) value);
1631                         } else if (target_type == TypeManager.uint32_type){
1632                                 if (value >= 0)
1633                                         return new UIntConstant ((uint) value);
1634                         } else if (target_type == TypeManager.uint64_type){
1635                                 //
1636                                 // we can optimize this case: a positive int32
1637                                 // always fits on a uint64.  But we need an opcode
1638                                 // to do it.
1639                                 //
1640                                 if (value >= 0)
1641                                         return new ULongConstant ((ulong) value);
1642                         }
1643                         
1644                         if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type))
1645                                 return new EnumConstant (ic, target_type);
1646
1647                         return null;
1648                 }
1649
1650                 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1651                 {
1652                         string msg = "Cannot convert implicitly from `"+
1653                                 TypeManager.CSharpName (source) + "' to `" +
1654                                 TypeManager.CSharpName (target) + "'";
1655
1656                         Report.Error (29, loc, msg);
1657                 }
1658
1659                 /// <summary>
1660                 ///   Attemptes to implicityly convert `target' into `type', using
1661                 ///   ConvertImplicit.  If there is no implicit conversion, then
1662                 ///   an error is signaled
1663                 /// </summary>
1664                 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1665                                                                   Type target_type, Location loc)
1666                 {
1667                         Expression e;
1668                         
1669                         e = ConvertImplicit (ec, source, target_type, loc);
1670                         if (e != null)
1671                                 return e;
1672
1673                         if (source is DoubleLiteral && target_type == TypeManager.float_type){
1674                                 Report.Error (664, loc,
1675                                               "Double literal cannot be implicitly converted to " +
1676                                               "float type, use F suffix to create a float literal");
1677                         }
1678                         
1679                         Error_CannotConvertImplicit (loc, source.Type, target_type);
1680
1681                         return null;
1682                 }
1683
1684                 /// <summary>
1685                 ///   Performs the explicit numeric conversions
1686                 /// </summary>
1687                 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type)
1688                 {
1689                         Type expr_type = expr.Type;
1690
1691                         //
1692                         // If we have an enumeration, extract the underlying type,
1693                         // use this during the comparission, but wrap around the original
1694                         // target_type
1695                         //
1696                         Type real_target_type = target_type;
1697
1698                         if (TypeManager.IsEnumType (real_target_type))
1699                                 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1700
1701                         if (expr_type == TypeManager.sbyte_type){
1702                                 //
1703                                 // From sbyte to byte, ushort, uint, ulong, char
1704                                 //
1705                                 if (real_target_type == TypeManager.byte_type)
1706                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1707                                 if (real_target_type == TypeManager.ushort_type)
1708                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1709                                 if (real_target_type == TypeManager.uint32_type)
1710                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1711                                 if (real_target_type == TypeManager.uint64_type)
1712                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1713                                 if (real_target_type == TypeManager.char_type)
1714                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1715                         } else if (expr_type == TypeManager.byte_type){
1716                                 //
1717                                 // From byte to sbyte and char
1718                                 //
1719                                 if (real_target_type == TypeManager.sbyte_type)
1720                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1721                                 if (real_target_type == TypeManager.char_type)
1722                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1723                         } else if (expr_type == TypeManager.short_type){
1724                                 //
1725                                 // From short to sbyte, byte, ushort, uint, ulong, char
1726                                 //
1727                                 if (real_target_type == TypeManager.sbyte_type)
1728                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1729                                 if (real_target_type == TypeManager.byte_type)
1730                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1731                                 if (real_target_type == TypeManager.ushort_type)
1732                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1733                                 if (real_target_type == TypeManager.uint32_type)
1734                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1735                                 if (real_target_type == TypeManager.uint64_type)
1736                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1737                                 if (real_target_type == TypeManager.char_type)
1738                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1739                         } else if (expr_type == TypeManager.ushort_type){
1740                                 //
1741                                 // From ushort to sbyte, byte, short, char
1742                                 //
1743                                 if (real_target_type == TypeManager.sbyte_type)
1744                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1745                                 if (real_target_type == TypeManager.byte_type)
1746                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1747                                 if (real_target_type == TypeManager.short_type)
1748                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1749                                 if (real_target_type == TypeManager.char_type)
1750                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1751                         } else if (expr_type == TypeManager.int32_type){
1752                                 //
1753                                 // From int to sbyte, byte, short, ushort, uint, ulong, char
1754                                 //
1755                                 if (real_target_type == TypeManager.sbyte_type)
1756                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1757                                 if (real_target_type == TypeManager.byte_type)
1758                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1759                                 if (real_target_type == TypeManager.short_type)
1760                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1761                                 if (real_target_type == TypeManager.ushort_type)
1762                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1763                                 if (real_target_type == TypeManager.uint32_type)
1764                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1765                                 if (real_target_type == TypeManager.uint64_type)
1766                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1767                                 if (real_target_type == TypeManager.char_type)
1768                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1769                         } else if (expr_type == TypeManager.uint32_type){
1770                                 //
1771                                 // From uint to sbyte, byte, short, ushort, int, char
1772                                 //
1773                                 if (real_target_type == TypeManager.sbyte_type)
1774                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1775                                 if (real_target_type == TypeManager.byte_type)
1776                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1777                                 if (real_target_type == TypeManager.short_type)
1778                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1779                                 if (real_target_type == TypeManager.ushort_type)
1780                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1781                                 if (real_target_type == TypeManager.int32_type)
1782                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1783                                 if (real_target_type == TypeManager.char_type)
1784                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1785                         } else if (expr_type == TypeManager.int64_type){
1786                                 //
1787                                 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1788                                 //
1789                                 if (real_target_type == TypeManager.sbyte_type)
1790                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1791                                 if (real_target_type == TypeManager.byte_type)
1792                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1793                                 if (real_target_type == TypeManager.short_type)
1794                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1795                                 if (real_target_type == TypeManager.ushort_type)
1796                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1797                                 if (real_target_type == TypeManager.int32_type)
1798                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1799                                 if (real_target_type == TypeManager.uint32_type)
1800                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1801                                 if (real_target_type == TypeManager.uint64_type)
1802                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1803                                 if (real_target_type == TypeManager.char_type)
1804                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1805                         } else if (expr_type == TypeManager.uint64_type){
1806                                 //
1807                                 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1808                                 //
1809                                 if (real_target_type == TypeManager.sbyte_type)
1810                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1811                                 if (real_target_type == TypeManager.byte_type)
1812                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1813                                 if (real_target_type == TypeManager.short_type)
1814                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1815                                 if (real_target_type == TypeManager.ushort_type)
1816                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1817                                 if (real_target_type == TypeManager.int32_type)
1818                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1819                                 if (real_target_type == TypeManager.uint32_type)
1820                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1821                                 if (real_target_type == TypeManager.int64_type)
1822                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1823                                 if (real_target_type == TypeManager.char_type)
1824                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1825                         } else if (expr_type == TypeManager.char_type){
1826                                 //
1827                                 // From char to sbyte, byte, short
1828                                 //
1829                                 if (real_target_type == TypeManager.sbyte_type)
1830                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1831                                 if (real_target_type == TypeManager.byte_type)
1832                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1833                                 if (real_target_type == TypeManager.short_type)
1834                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1835                         } else if (expr_type == TypeManager.float_type){
1836                                 //
1837                                 // From float to sbyte, byte, short,
1838                                 // ushort, int, uint, long, ulong, char
1839                                 // or decimal
1840                                 //
1841                                 if (real_target_type == TypeManager.sbyte_type)
1842                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1843                                 if (real_target_type == TypeManager.byte_type)
1844                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1845                                 if (real_target_type == TypeManager.short_type)
1846                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1847                                 if (real_target_type == TypeManager.ushort_type)
1848                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1849                                 if (real_target_type == TypeManager.int32_type)
1850                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1851                                 if (real_target_type == TypeManager.uint32_type)
1852                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1853                                 if (real_target_type == TypeManager.int64_type)
1854                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1855                                 if (real_target_type == TypeManager.uint64_type)
1856                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1857                                 if (real_target_type == TypeManager.char_type)
1858                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1859                                 if (real_target_type == TypeManager.decimal_type)
1860                                         return InternalTypeConstructor (ec, expr, target_type);
1861                         } else if (expr_type == TypeManager.double_type){
1862                                 //
1863                                 // From double to byte, byte, short,
1864                                 // ushort, int, uint, long, ulong,
1865                                 // char, float or decimal
1866                                 //
1867                                 if (real_target_type == TypeManager.sbyte_type)
1868                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1869                                 if (real_target_type == TypeManager.byte_type)
1870                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1871                                 if (real_target_type == TypeManager.short_type)
1872                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1873                                 if (real_target_type == TypeManager.ushort_type)
1874                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1875                                 if (real_target_type == TypeManager.int32_type)
1876                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1877                                 if (real_target_type == TypeManager.uint32_type)
1878                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1879                                 if (real_target_type == TypeManager.int64_type)
1880                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1881                                 if (real_target_type == TypeManager.uint64_type)
1882                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1883                                 if (real_target_type == TypeManager.char_type)
1884                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1885                                 if (real_target_type == TypeManager.float_type)
1886                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1887                                 if (real_target_type == TypeManager.decimal_type)
1888                                         return InternalTypeConstructor (ec, expr, target_type);
1889                         } 
1890
1891                         // decimal is taken care of by the op_Explicit methods.
1892
1893                         return null;
1894                 }
1895
1896                 /// <summary>
1897                 ///  Returns whether an explicit reference conversion can be performed
1898                 ///  from source_type to target_type
1899                 /// </summary>
1900                 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1901                 {
1902                         bool target_is_value_type = target_type.IsValueType;
1903                         
1904                         if (source_type == target_type)
1905                                 return true;
1906                         
1907                         //
1908                         // From object to any reference type
1909                         //
1910                         if (source_type == TypeManager.object_type && !target_is_value_type)
1911                                 return true;
1912                                         
1913                         //
1914                         // From any class S to any class-type T, provided S is a base class of T
1915                         //
1916                         if (target_type.IsSubclassOf (source_type))
1917                                 return true;
1918
1919                         //
1920                         // From any interface type S to any interface T provided S is not derived from T
1921                         //
1922                         if (source_type.IsInterface && target_type.IsInterface){
1923                                 if (!target_type.IsSubclassOf (source_type))
1924                                         return true;
1925                         }
1926                             
1927                         //
1928                         // From any class type S to any interface T, provided S is not sealed
1929                         // and provided S does not implement T.
1930                         //
1931                         if (target_type.IsInterface && !source_type.IsSealed &&
1932                             !TypeManager.ImplementsInterface (source_type, target_type))
1933                                 return true;
1934
1935                         //
1936                         // From any interface-type S to to any class type T, provided T is not
1937                         // sealed, or provided T implements S.
1938                         //
1939                         if (source_type.IsInterface &&
1940                             (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1941                                 return true;
1942                         
1943                         
1944                         // From an array type S with an element type Se to an array type T with an 
1945                         // element type Te provided all the following are true:
1946                         //     * S and T differe only in element type, in other words, S and T
1947                         //       have the same number of dimensions.
1948                         //     * Both Se and Te are reference types
1949                         //     * An explicit referenc conversions exist from Se to Te
1950                         //
1951                         if (source_type.IsArray && target_type.IsArray) {
1952                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1953                                         
1954                                         Type source_element_type = source_type.GetElementType ();
1955                                         Type target_element_type = target_type.GetElementType ();
1956                                         
1957                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1958                                                 if (ExplicitReferenceConversionExists (source_element_type,
1959                                                                                        target_element_type))
1960                                                         return true;
1961                                 }
1962                         }
1963                         
1964
1965                         // From System.Array to any array-type
1966                         if (source_type == TypeManager.array_type &&
1967                             target_type.IsArray){
1968                                 return true;
1969                         }
1970
1971                         //
1972                         // From System delegate to any delegate-type
1973                         //
1974                         if (source_type == TypeManager.delegate_type &&
1975                             target_type.IsSubclassOf (TypeManager.delegate_type))
1976                                 return true;
1977
1978                         //
1979                         // From ICloneable to Array or Delegate types
1980                         //
1981                         if (source_type == TypeManager.icloneable_type &&
1982                             (target_type == TypeManager.array_type ||
1983                              target_type == TypeManager.delegate_type))
1984                                 return true;
1985                         
1986                         return false;
1987                 }
1988
1989                 /// <summary>
1990                 ///   Implements Explicit Reference conversions
1991                 /// </summary>
1992                 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1993                 {
1994                         Type source_type = source.Type;
1995                         bool target_is_value_type = target_type.IsValueType;
1996
1997                         //
1998                         // From object to any reference type
1999                         //
2000                         if (source_type == TypeManager.object_type && !target_is_value_type)
2001                                 return new ClassCast (source, target_type);
2002
2003
2004                         //
2005                         // From any class S to any class-type T, provided S is a base class of T
2006                         //
2007                         if (target_type.IsSubclassOf (source_type))
2008                                 return new ClassCast (source, target_type);
2009
2010                         //
2011                         // From any interface type S to any interface T provided S is not derived from T
2012                         //
2013                         if (source_type.IsInterface && target_type.IsInterface){
2014                                 if (TypeManager.ImplementsInterface (source_type, target_type))
2015                                         return null;
2016                                 else
2017                                         return new ClassCast (source, target_type);
2018                         }
2019                             
2020                         //
2021                         // From any class type S to any interface T, provides S is not sealed
2022                         // and provided S does not implement T.
2023                         //
2024                         if (target_type.IsInterface && !source_type.IsSealed) {
2025                                 if (TypeManager.ImplementsInterface (source_type, target_type))
2026                                         return null;
2027                                 else
2028                                         return new ClassCast (source, target_type);
2029                                 
2030                         }
2031
2032                         //
2033                         // From any interface-type S to to any class type T, provided T is not
2034                         // sealed, or provided T implements S.
2035                         //
2036                         if (source_type.IsInterface) {
2037                                 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
2038                                         return new ClassCast (source, target_type);
2039                                 else
2040                                         return null;
2041                         }
2042                         
2043                         // From an array type S with an element type Se to an array type T with an 
2044                         // element type Te provided all the following are true:
2045                         //     * S and T differe only in element type, in other words, S and T
2046                         //       have the same number of dimensions.
2047                         //     * Both Se and Te are reference types
2048                         //     * An explicit referenc conversions exist from Se to Te
2049                         //
2050                         if (source_type.IsArray && target_type.IsArray) {
2051                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2052                                         
2053                                         Type source_element_type = source_type.GetElementType ();
2054                                         Type target_element_type = target_type.GetElementType ();
2055                                         
2056                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2057                                                 if (ExplicitReferenceConversionExists (source_element_type,
2058                                                                                        target_element_type))
2059                                                         return new ClassCast (source, target_type);
2060                                 }
2061                         }
2062                         
2063
2064                         // From System.Array to any array-type
2065                         if (source_type == TypeManager.array_type &&
2066                             target_type.IsArray) {
2067                                 return new ClassCast (source, target_type);
2068                         }
2069
2070                         //
2071                         // From System delegate to any delegate-type
2072                         //
2073                         if (source_type == TypeManager.delegate_type &&
2074                             target_type.IsSubclassOf (TypeManager.delegate_type))
2075                                 return new ClassCast (source, target_type);
2076
2077                         //
2078                         // From ICloneable to Array or Delegate types
2079                         //
2080                         if (source_type == TypeManager.icloneable_type &&
2081                             (target_type == TypeManager.array_type ||
2082                              target_type == TypeManager.delegate_type))
2083                                 return new ClassCast (source, target_type);
2084                         
2085                         return null;
2086                 }
2087                 
2088                 /// <summary>
2089                 ///   Performs an explicit conversion of the expression `expr' whose
2090                 ///   type is expr.Type to `target_type'.
2091                 /// </summary>
2092                 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
2093                                                           Type target_type, Location loc)
2094                 {
2095                         Type expr_type = expr.Type;
2096                         Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
2097
2098                         if (ne != null)
2099                                 return ne;
2100
2101                         ne = ConvertNumericExplicit (ec, expr, target_type);
2102                         if (ne != null)
2103                                 return ne;
2104
2105                         //
2106                         // Unboxing conversion.
2107                         //
2108                         if (expr_type == TypeManager.object_type && target_type.IsValueType)
2109                                 return new UnboxCast (expr, target_type);
2110
2111                         //
2112                         // Enum types
2113                         //
2114                         if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
2115                                 Expression e;
2116
2117                                 //
2118                                 // FIXME: Is there any reason we should have EnumConstant
2119                                 // dealt with here instead of just using always the
2120                                 // UnderlyingSystemType to wrap the type?
2121                                 //
2122                                 if (expr is EnumConstant)
2123                                         e = ((EnumConstant) expr).Child;
2124                                 else {
2125                                         e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
2126                                 }
2127                                 
2128                                 Expression t = ConvertImplicit (ec, e, target_type, loc);
2129                                 if (t != null)
2130                                         return t;
2131                                 
2132                                 return ConvertNumericExplicit (ec, e, target_type);
2133                         }
2134                         
2135                         ne = ConvertReferenceExplicit (expr, target_type);
2136                         if (ne != null)
2137                                 return ne;
2138
2139                         if (ec.InUnsafe){
2140                                 if (target_type.IsPointer){
2141                                         if (expr_type.IsPointer)
2142                                                 return new EmptyCast (expr, target_type);
2143                                         
2144                                         if (expr_type == TypeManager.sbyte_type ||
2145                                             expr_type == TypeManager.byte_type ||
2146                                             expr_type == TypeManager.short_type ||
2147                                             expr_type == TypeManager.ushort_type ||
2148                                             expr_type == TypeManager.int32_type ||
2149                                             expr_type == TypeManager.uint32_type ||
2150                                             expr_type == TypeManager.uint64_type ||
2151                                             expr_type == TypeManager.int64_type)
2152                                                 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2153                                 }
2154                                 if (expr_type.IsPointer){
2155                                         if (target_type == TypeManager.sbyte_type ||
2156                                             target_type == TypeManager.byte_type ||
2157                                             target_type == TypeManager.short_type ||
2158                                             target_type == TypeManager.ushort_type ||
2159                                             target_type == TypeManager.int32_type ||
2160                                             target_type == TypeManager.uint32_type ||
2161                                             target_type == TypeManager.uint64_type ||
2162                                             target_type == TypeManager.int64_type){
2163                                                 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
2164                                                 Expression ci, ce;
2165
2166                                                 ci = ConvertImplicitStandard (ec, e, target_type, loc);
2167
2168                                                 if (ci != null)
2169                                                         return ci;
2170
2171                                                 ce = ConvertNumericExplicit (ec, e, target_type);
2172                                                 if (ce != null)
2173                                                         return ce;
2174                                                 //
2175                                                 // We should always be able to go from an uint32
2176                                                 // implicitly or explicitly to the other integral
2177                                                 // types
2178                                                 //
2179                                                 throw new Exception ("Internal compiler error");
2180                                         }
2181                                 }
2182                         }
2183                         
2184                         ne = ExplicitUserConversion (ec, expr, target_type, loc);
2185                         if (ne != null)
2186                                 return ne;
2187
2188                         Error_CannotConvertType (loc, expr_type, target_type);
2189                         return null;
2190                 }
2191
2192                 /// <summary>
2193                 ///   Same as ConvertExplicit, only it doesn't include user defined conversions
2194                 /// </summary>
2195                 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2196                                                                   Type target_type, Location l)
2197                 {
2198                         Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2199
2200                         if (ne != null)
2201                                 return ne;
2202
2203                         ne = ConvertNumericExplicit (ec, expr, target_type);
2204                         if (ne != null)
2205                                 return ne;
2206
2207                         ne = ConvertReferenceExplicit (expr, target_type);
2208                         if (ne != null)
2209                                 return ne;
2210
2211                         Error_CannotConvertType (l, expr.Type, target_type);
2212                         return null;
2213                 }
2214
2215                 static string ExprClassName (ExprClass c)
2216                 {
2217                         switch (c){
2218                         case ExprClass.Invalid:
2219                                 return "Invalid";
2220                         case ExprClass.Value:
2221                                 return "value";
2222                         case ExprClass.Variable:
2223                                 return "variable";
2224                         case ExprClass.Namespace:
2225                                 return "namespace";
2226                         case ExprClass.Type:
2227                                 return "type";
2228                         case ExprClass.MethodGroup:
2229                                 return "method group";
2230                         case ExprClass.PropertyAccess:
2231                                 return "property access";
2232                         case ExprClass.EventAccess:
2233                                 return "event access";
2234                         case ExprClass.IndexerAccess:
2235                                 return "indexer access";
2236                         case ExprClass.Nothing:
2237                                 return "null";
2238                         }
2239                         throw new Exception ("Should not happen");
2240                 }
2241                 
2242                 /// <summary>
2243                 ///   Reports that we were expecting `expr' to be of class `expected'
2244                 /// </summary>
2245                 public void Error118 (string expected)
2246                 {
2247                         string kind = "Unknown";
2248                         
2249                         kind = ExprClassName (eclass);
2250
2251                         Error (118, "Expression denotes a `" + kind +
2252                                "' where a `" + expected + "' was expected");
2253                 }
2254
2255                 public void Error118 (ResolveFlags flags)
2256                 {
2257                         ArrayList valid = new ArrayList (10);
2258
2259                         if ((flags & ResolveFlags.VariableOrValue) != 0) {
2260                                 valid.Add ("variable");
2261                                 valid.Add ("value");
2262                         }
2263
2264                         if ((flags & ResolveFlags.Type) != 0)
2265                                 valid.Add ("type");
2266
2267                         if ((flags & ResolveFlags.MethodGroup) != 0)
2268                                 valid.Add ("method group");
2269
2270                         if ((flags & ResolveFlags.SimpleName) != 0)
2271                                 valid.Add ("simple name");
2272
2273                         if (valid.Count == 0)
2274                                 valid.Add ("unknown");
2275
2276                         StringBuilder sb = new StringBuilder ();
2277                         for (int i = 0; i < valid.Count; i++) {
2278                                 if (i > 0)
2279                                         sb.Append (", ");
2280                                 else if (i == valid.Count)
2281                                         sb.Append (" or ");
2282                                 sb.Append (valid [i]);
2283                         }
2284
2285                         string kind = ExprClassName (eclass);
2286
2287                         Error (119, "Expression denotes a `" + kind + "' where " +
2288                                "a `" + sb.ToString () + "' was expected");
2289                 }
2290                 
2291                 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2292                 {
2293                         Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
2294                                       TypeManager.CSharpName (t));
2295                 }
2296
2297                 public static void UnsafeError (Location loc)
2298                 {
2299                         Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2300                 }
2301                 
2302                 /// <summary>
2303                 ///   Converts the IntConstant, UIntConstant, LongConstant or
2304                 ///   ULongConstant into the integral target_type.   Notice
2305                 ///   that we do not return an `Expression' we do return
2306                 ///   a boxed integral type.
2307                 ///
2308                 ///   FIXME: Since I added the new constants, we need to
2309                 ///   also support conversions from CharConstant, ByteConstant,
2310                 ///   SByteConstant, UShortConstant, ShortConstant
2311                 ///
2312                 ///   This is used by the switch statement, so the domain
2313                 ///   of work is restricted to the literals above, and the
2314                 ///   targets are int32, uint32, char, byte, sbyte, ushort,
2315                 ///   short, uint64 and int64
2316                 /// </summary>
2317                 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2318                 {
2319                         string s = "";
2320
2321                         if (c.Type == target_type)
2322                                 return ((Constant) c).GetValue ();
2323
2324                         //
2325                         // Make into one of the literals we handle, we dont really care
2326                         // about this value as we will just return a few limited types
2327                         // 
2328                         if (c is EnumConstant)
2329                                 c = ((EnumConstant)c).WidenToCompilerConstant ();
2330
2331                         if (c is IntConstant){
2332                                 int v = ((IntConstant) c).Value;
2333                                 
2334                                 if (target_type == TypeManager.uint32_type){
2335                                         if (v >= 0)
2336                                                 return (uint) v;
2337                                 } else if (target_type == TypeManager.char_type){
2338                                         if (v >= Char.MinValue && v <= Char.MaxValue)
2339                                                 return (char) v;
2340                                 } else if (target_type == TypeManager.byte_type){
2341                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2342                                                 return (byte) v;
2343                                 } else if (target_type == TypeManager.sbyte_type){
2344                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
2345                                                 return (sbyte) v;
2346                                 } else if (target_type == TypeManager.short_type){
2347                                         if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2348                                                 return (short) v;
2349                                 } else if (target_type == TypeManager.ushort_type){
2350                                         if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2351                                                 return (ushort) v;
2352                                 } else if (target_type == TypeManager.int64_type)
2353                                         return (long) v;
2354                                 else if (target_type == TypeManager.uint64_type){
2355                                         if (v > 0)
2356                                                 return (ulong) v;
2357                                 }
2358
2359                                 s = v.ToString ();
2360                         } else if (c is UIntConstant){
2361                                 uint v = ((UIntConstant) c).Value;
2362
2363                                 if (target_type == TypeManager.int32_type){
2364                                         if (v <= Int32.MaxValue)
2365                                                 return (int) v;
2366                                 } else if (target_type == TypeManager.char_type){
2367                                         if (v >= Char.MinValue && v <= Char.MaxValue)
2368                                                 return (char) v;
2369                                 } else if (target_type == TypeManager.byte_type){
2370                                         if (v <= Byte.MaxValue)
2371                                                 return (byte) v;
2372                                 } else if (target_type == TypeManager.sbyte_type){
2373                                         if (v <= SByte.MaxValue)
2374                                                 return (sbyte) v;
2375                                 } else if (target_type == TypeManager.short_type){
2376                                         if (v <= UInt16.MaxValue)
2377                                                 return (short) v;
2378                                 } else if (target_type == TypeManager.ushort_type){
2379                                         if (v <= UInt16.MaxValue)
2380                                                 return (ushort) v;
2381                                 } else if (target_type == TypeManager.int64_type)
2382                                         return (long) v;
2383                                 else if (target_type == TypeManager.uint64_type)
2384                                         return (ulong) v;
2385                                 s = v.ToString ();
2386                         } else if (c is LongConstant){ 
2387                                 long v = ((LongConstant) c).Value;
2388
2389                                 if (target_type == TypeManager.int32_type){
2390                                         if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2391                                                 return (int) v;
2392                                 } else if (target_type == TypeManager.uint32_type){
2393                                         if (v >= 0 && v <= UInt32.MaxValue)
2394                                                 return (uint) v;
2395                                 } else if (target_type == TypeManager.char_type){
2396                                         if (v >= Char.MinValue && v <= Char.MaxValue)
2397                                                 return (char) v;
2398                                 } else if (target_type == TypeManager.byte_type){
2399                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2400                                                 return (byte) v;
2401                                 } else if (target_type == TypeManager.sbyte_type){
2402                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
2403                                                 return (sbyte) v;
2404                                 } else if (target_type == TypeManager.short_type){
2405                                         if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2406                                                 return (short) v;
2407                                 } else if (target_type == TypeManager.ushort_type){
2408                                         if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2409                                                 return (ushort) v;
2410                                 } else if (target_type == TypeManager.uint64_type){
2411                                         if (v > 0)
2412                                                 return (ulong) v;
2413                                 }
2414                                 s = v.ToString ();
2415                         } else if (c is ULongConstant){
2416                                 ulong v = ((ULongConstant) c).Value;
2417
2418                                 if (target_type == TypeManager.int32_type){
2419                                         if (v <= Int32.MaxValue)
2420                                                 return (int) v;
2421                                 } else if (target_type == TypeManager.uint32_type){
2422                                         if (v <= UInt32.MaxValue)
2423                                                 return (uint) v;
2424                                 } else if (target_type == TypeManager.char_type){
2425                                         if (v >= Char.MinValue && v <= Char.MaxValue)
2426                                                 return (char) v;
2427                                 } else if (target_type == TypeManager.byte_type){
2428                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2429                                                 return (byte) v;
2430                                 } else if (target_type == TypeManager.sbyte_type){
2431                                         if (v <= (int) SByte.MaxValue)
2432                                                 return (sbyte) v;
2433                                 } else if (target_type == TypeManager.short_type){
2434                                         if (v <= UInt16.MaxValue)
2435                                                 return (short) v;
2436                                 } else if (target_type == TypeManager.ushort_type){
2437                                         if (v <= UInt16.MaxValue)
2438                                                 return (ushort) v;
2439                                 } else if (target_type == TypeManager.int64_type){
2440                                         if (v <= Int64.MaxValue)
2441                                                 return (long) v;
2442                                 }
2443                                 s = v.ToString ();
2444                         } else if (c is ByteConstant){
2445                                 byte v = ((ByteConstant) c).Value;
2446                                 
2447                                 if (target_type == TypeManager.int32_type)
2448                                         return (int) v;
2449                                 else if (target_type == TypeManager.uint32_type)
2450                                         return (uint) v;
2451                                 else if (target_type == TypeManager.char_type)
2452                                         return (char) v;
2453                                 else if (target_type == TypeManager.sbyte_type){
2454                                         if (v <= SByte.MaxValue)
2455                                                 return (sbyte) v;
2456                                 } else if (target_type == TypeManager.short_type)
2457                                         return (short) v;
2458                                 else if (target_type == TypeManager.ushort_type)
2459                                         return (ushort) v;
2460                                 else if (target_type == TypeManager.int64_type)
2461                                         return (long) v;
2462                                 else if (target_type == TypeManager.uint64_type)
2463                                         return (ulong) v;
2464                                 s = v.ToString ();
2465                         } else if (c is SByteConstant){
2466                                 sbyte v = ((SByteConstant) c).Value;
2467                                 
2468                                 if (target_type == TypeManager.int32_type)
2469                                         return (int) v;
2470                                 else if (target_type == TypeManager.uint32_type){
2471                                         if (v >= 0)
2472                                                 return (uint) v;
2473                                 } else if (target_type == TypeManager.char_type){
2474                                         if (v >= 0)
2475                                                 return (char) v;
2476                                 } else if (target_type == TypeManager.byte_type){
2477                                         if (v >= 0)
2478                                                 return (byte) v;
2479                                 } else if (target_type == TypeManager.short_type)
2480                                         return (short) v;
2481                                 else if (target_type == TypeManager.ushort_type){
2482                                         if (v >= 0)
2483                                                 return (ushort) v;
2484                                 } else if (target_type == TypeManager.int64_type)
2485                                         return (long) v;
2486                                 else if (target_type == TypeManager.uint64_type){
2487                                         if (v >= 0)
2488                                                 return (ulong) v;
2489                                 }
2490                                 s = v.ToString ();
2491                         } else if (c is ShortConstant){
2492                                 short v = ((ShortConstant) c).Value;
2493                                 
2494                                 if (target_type == TypeManager.int32_type){
2495                                         return (int) v;
2496                                 } else if (target_type == TypeManager.uint32_type){
2497                                         if (v >= 0)
2498                                                 return (uint) v;
2499                                 } else if (target_type == TypeManager.char_type){
2500                                         if (v >= 0)
2501                                                 return (char) v;
2502                                 } else if (target_type == TypeManager.byte_type){
2503                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2504                                                 return (byte) v;
2505                                 } else if (target_type == TypeManager.sbyte_type){
2506                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
2507                                                 return (sbyte) v;
2508                                 } else if (target_type == TypeManager.ushort_type){
2509                                         if (v >= 0)
2510                                                 return (ushort) v;
2511                                 } else if (target_type == TypeManager.int64_type)
2512                                         return (long) v;
2513                                 else if (target_type == TypeManager.uint64_type)
2514                                         return (ulong) v;
2515
2516                                 s = v.ToString ();
2517                         } else if (c is UShortConstant){
2518                                 ushort v = ((UShortConstant) c).Value;
2519                                 
2520                                 if (target_type == TypeManager.int32_type)
2521                                         return (int) v;
2522                                 else if (target_type == TypeManager.uint32_type)
2523                                         return (uint) v;
2524                                 else if (target_type == TypeManager.char_type){
2525                                         if (v >= Char.MinValue && v <= Char.MaxValue)
2526                                                 return (char) v;
2527                                 } else if (target_type == TypeManager.byte_type){
2528                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2529                                                 return (byte) v;
2530                                 } else if (target_type == TypeManager.sbyte_type){
2531                                         if (v <= SByte.MaxValue)
2532                                                 return (byte) v;
2533                                 } else if (target_type == TypeManager.short_type){
2534                                         if (v <= Int16.MaxValue)
2535                                                 return (short) v;
2536                                 } else if (target_type == TypeManager.int64_type)
2537                                         return (long) v;
2538                                 else if (target_type == TypeManager.uint64_type)
2539                                         return (ulong) v;
2540
2541                                 s = v.ToString ();
2542                         } else if (c is CharConstant){
2543                                 char v = ((CharConstant) c).Value;
2544                                 
2545                                 if (target_type == TypeManager.int32_type)
2546                                         return (int) v;
2547                                 else if (target_type == TypeManager.uint32_type)
2548                                         return (uint) v;
2549                                 else if (target_type == TypeManager.byte_type){
2550                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2551                                                 return (byte) v;
2552                                 } else if (target_type == TypeManager.sbyte_type){
2553                                         if (v <= SByte.MaxValue)
2554                                                 return (sbyte) v;
2555                                 } else if (target_type == TypeManager.short_type){
2556                                         if (v <= Int16.MaxValue)
2557                                                 return (short) v;
2558                                 } else if (target_type == TypeManager.ushort_type)
2559                                         return (short) v;
2560                                 else if (target_type == TypeManager.int64_type)
2561                                         return (long) v;
2562                                 else if (target_type == TypeManager.uint64_type)
2563                                         return (ulong) v;
2564
2565                                 s = v.ToString ();
2566                         }
2567                         Error_ConstantValueCannotBeConverted (loc, s, target_type);
2568                         return null;
2569                 }
2570
2571                 //
2572                 // Load the object from the pointer.  
2573                 //
2574                 public static void LoadFromPtr (ILGenerator ig, Type t)
2575                 {
2576                         if (t == TypeManager.int32_type)
2577                                 ig.Emit (OpCodes.Ldind_I4);
2578                         else if (t == TypeManager.uint32_type)
2579                                 ig.Emit (OpCodes.Ldind_U4);
2580                         else if (t == TypeManager.short_type)
2581                                 ig.Emit (OpCodes.Ldind_I2);
2582                         else if (t == TypeManager.ushort_type)
2583                                 ig.Emit (OpCodes.Ldind_U2);
2584                         else if (t == TypeManager.char_type)
2585                                 ig.Emit (OpCodes.Ldind_U2);
2586                         else if (t == TypeManager.byte_type)
2587                                 ig.Emit (OpCodes.Ldind_U1);
2588                         else if (t == TypeManager.sbyte_type)
2589                                 ig.Emit (OpCodes.Ldind_I1);
2590                         else if (t == TypeManager.uint64_type)
2591                                 ig.Emit (OpCodes.Ldind_I8);
2592                         else if (t == TypeManager.int64_type)
2593                                 ig.Emit (OpCodes.Ldind_I8);
2594                         else if (t == TypeManager.float_type)
2595                                 ig.Emit (OpCodes.Ldind_R4);
2596                         else if (t == TypeManager.double_type)
2597                                 ig.Emit (OpCodes.Ldind_R8);
2598                         else if (t == TypeManager.bool_type)
2599                                 ig.Emit (OpCodes.Ldind_I1);
2600                         else if (t == TypeManager.intptr_type)
2601                                 ig.Emit (OpCodes.Ldind_I);
2602                         else if (TypeManager.IsEnumType (t)) {
2603                                 if (t == TypeManager.enum_type)
2604                                         ig.Emit (OpCodes.Ldind_Ref);
2605                                 else
2606                                         LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
2607                         } else if (t.IsValueType)
2608                                 ig.Emit (OpCodes.Ldobj, t);
2609                         else
2610                                 ig.Emit (OpCodes.Ldind_Ref);
2611                 }
2612
2613                 //
2614                 // The stack contains the pointer and the value of type `type'
2615                 //
2616                 public static void StoreFromPtr (ILGenerator ig, Type type)
2617                 {
2618                         if (type.IsEnum)
2619                                 type = TypeManager.EnumToUnderlying (type);
2620                         if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2621                                 ig.Emit (OpCodes.Stind_I4);
2622                         else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2623                                 ig.Emit (OpCodes.Stind_I8);
2624                         else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2625                                  type == TypeManager.ushort_type)
2626                                 ig.Emit (OpCodes.Stind_I2);
2627                         else if (type == TypeManager.float_type)
2628                                 ig.Emit (OpCodes.Stind_R4);
2629                         else if (type == TypeManager.double_type)
2630                                 ig.Emit (OpCodes.Stind_R8);
2631                         else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2632                                  type == TypeManager.bool_type)
2633                                 ig.Emit (OpCodes.Stind_I1);
2634                         else if (type == TypeManager.intptr_type)
2635                                 ig.Emit (OpCodes.Stind_I);
2636                         else if (type.IsValueType)
2637                                 ig.Emit (OpCodes.Stobj, type);
2638                         else
2639                                 ig.Emit (OpCodes.Stind_Ref);
2640                 }
2641                 
2642                 //
2643                 // Returns the size of type `t' if known, otherwise, 0
2644                 //
2645                 public static int GetTypeSize (Type t)
2646                 {
2647                         t = TypeManager.TypeToCoreType (t);
2648                         if (t == TypeManager.int32_type ||
2649                             t == TypeManager.uint32_type ||
2650                             t == TypeManager.float_type)
2651                                 return 4;
2652                         else if (t == TypeManager.int64_type ||
2653                                  t == TypeManager.uint64_type ||
2654                                  t == TypeManager.double_type)
2655                                 return 8;
2656                         else if (t == TypeManager.byte_type ||
2657                                  t == TypeManager.sbyte_type ||
2658                                  t == TypeManager.bool_type)    
2659                                 return 1;
2660                         else if (t == TypeManager.short_type ||
2661                                  t == TypeManager.char_type ||
2662                                  t == TypeManager.ushort_type)
2663                                 return 2;
2664                         else
2665                                 return 0;
2666                 }
2667
2668                 //
2669                 // Default implementation of IAssignMethod.CacheTemporaries
2670                 //
2671                 public void CacheTemporaries (EmitContext ec)
2672                 {
2673                 }
2674
2675                 static void Error_NegativeArrayIndex (Location loc)
2676                 {
2677                         Report.Error (284, loc, "Can not create array with a negative size");
2678                 }
2679                 
2680                 //
2681                 // Converts `source' to an int, uint, long or ulong.
2682                 //
2683                 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
2684                 {
2685                         Expression target;
2686                         
2687                         bool old_checked = ec.CheckState;
2688                         ec.CheckState = true;
2689                         
2690                         target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
2691                         if (target == null){
2692                                 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
2693                                 if (target == null){
2694                                         target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
2695                                         if (target == null){
2696                                                 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
2697                                                 if (target == null)
2698                                                         Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
2699                                         }
2700                                 }
2701                         } 
2702                         ec.CheckState = old_checked;
2703
2704                         //
2705                         // Only positive constants are allowed at compile time
2706                         //
2707                         if (target is Constant){
2708                                 if (target is IntConstant){
2709                                         if (((IntConstant) target).Value < 0){
2710                                                 Error_NegativeArrayIndex (loc);
2711                                                 return null;
2712                                         }
2713                                 }
2714
2715                                 if (target is LongConstant){
2716                                         if (((LongConstant) target).Value < 0){
2717                                                 Error_NegativeArrayIndex (loc);
2718                                                 return null;
2719                                         }
2720                                 }
2721                                 
2722                         }
2723
2724                         return target;
2725                 }
2726                 
2727         }
2728
2729         /// <summary>
2730         ///   This is just a base class for expressions that can
2731         ///   appear on statements (invocations, object creation,
2732         ///   assignments, post/pre increment and decrement).  The idea
2733         ///   being that they would support an extra Emition interface that
2734         ///   does not leave a result on the stack.
2735         /// </summary>
2736         public abstract class ExpressionStatement : Expression {
2737
2738                 /// <summary>
2739                 ///   Requests the expression to be emitted in a `statement'
2740                 ///   context.  This means that no new value is left on the
2741                 ///   stack after invoking this method (constrasted with
2742                 ///   Emit that will always leave a value on the stack).
2743                 /// </summary>
2744                 public abstract void EmitStatement (EmitContext ec);
2745         }
2746
2747         /// <summary>
2748         ///   This kind of cast is used to encapsulate the child
2749         ///   whose type is child.Type into an expression that is
2750         ///   reported to return "return_type".  This is used to encapsulate
2751         ///   expressions which have compatible types, but need to be dealt
2752         ///   at higher levels with.
2753         ///
2754         ///   For example, a "byte" expression could be encapsulated in one
2755         ///   of these as an "unsigned int".  The type for the expression
2756         ///   would be "unsigned int".
2757         ///
2758         /// </summary>
2759         public class EmptyCast : Expression {
2760                 protected Expression child;
2761
2762                 public EmptyCast (Expression child, Type return_type)
2763                 {
2764                         eclass = child.eclass;
2765                         type = return_type;
2766                         this.child = child;
2767                 }
2768
2769                 public override Expression DoResolve (EmitContext ec)
2770                 {
2771                         // This should never be invoked, we are born in fully
2772                         // initialized state.
2773
2774                         return this;
2775                 }
2776
2777                 public override void Emit (EmitContext ec)
2778                 {
2779                         child.Emit (ec);
2780                 }
2781         }
2782
2783         /// <summary>
2784         ///  This class is used to wrap literals which belong inside Enums
2785         /// </summary>
2786         public class EnumConstant : Constant {
2787                 public Constant Child;
2788
2789                 public EnumConstant (Constant child, Type enum_type)
2790                 {
2791                         eclass = child.eclass;
2792                         this.Child = child;
2793                         type = enum_type;
2794                 }
2795                 
2796                 public override Expression DoResolve (EmitContext ec)
2797                 {
2798                         // This should never be invoked, we are born in fully
2799                         // initialized state.
2800
2801                         return this;
2802                 }
2803
2804                 public override void Emit (EmitContext ec)
2805                 {
2806                         Child.Emit (ec);
2807                 }
2808
2809                 public override object GetValue ()
2810                 {
2811                         return Child.GetValue ();
2812                 }
2813
2814                 //
2815                 // Converts from one of the valid underlying types for an enumeration
2816                 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2817                 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2818                 //
2819                 public Constant WidenToCompilerConstant ()
2820                 {
2821                         Type t = TypeManager.EnumToUnderlying (Child.Type);
2822                         object v = ((Constant) Child).GetValue ();;
2823                         
2824                         if (t == TypeManager.int32_type)
2825                                 return new IntConstant ((int) v);
2826                         if (t == TypeManager.uint32_type)
2827                                 return new UIntConstant ((uint) v);
2828                         if (t == TypeManager.int64_type)
2829                                 return new LongConstant ((long) v);
2830                         if (t == TypeManager.uint64_type)
2831                                 return new ULongConstant ((ulong) v);
2832                         if (t == TypeManager.short_type)
2833                                 return new ShortConstant ((short) v);
2834                         if (t == TypeManager.ushort_type)
2835                                 return new UShortConstant ((ushort) v);
2836                         if (t == TypeManager.byte_type)
2837                                 return new ByteConstant ((byte) v);
2838                         if (t == TypeManager.sbyte_type)
2839                                 return new SByteConstant ((sbyte) v);
2840
2841                         throw new Exception ("Invalid enumeration underlying type: " + t);
2842                 }
2843
2844                 //
2845                 // Extracts the value in the enumeration on its native representation
2846                 //
2847                 public object GetPlainValue ()
2848                 {
2849                         Type t = TypeManager.EnumToUnderlying (Child.Type);
2850                         object v = ((Constant) Child).GetValue ();;
2851                         
2852                         if (t == TypeManager.int32_type)
2853                                 return (int) v;
2854                         if (t == TypeManager.uint32_type)
2855                                 return (uint) v;
2856                         if (t == TypeManager.int64_type)
2857                                 return (long) v;
2858                         if (t == TypeManager.uint64_type)
2859                                 return (ulong) v;
2860                         if (t == TypeManager.short_type)
2861                                 return (short) v;
2862                         if (t == TypeManager.ushort_type)
2863                                 return (ushort) v;
2864                         if (t == TypeManager.byte_type)
2865                                 return (byte) v;
2866                         if (t == TypeManager.sbyte_type)
2867                                 return (sbyte) v;
2868
2869                         return null;
2870                 }
2871                 
2872                 public override string AsString ()
2873                 {
2874                         return Child.AsString ();
2875                 }
2876
2877                 public override DoubleConstant ConvertToDouble ()
2878                 {
2879                         return Child.ConvertToDouble ();
2880                 }
2881
2882                 public override FloatConstant ConvertToFloat ()
2883                 {
2884                         return Child.ConvertToFloat ();
2885                 }
2886
2887                 public override ULongConstant ConvertToULong ()
2888                 {
2889                         return Child.ConvertToULong ();
2890                 }
2891
2892                 public override LongConstant ConvertToLong ()
2893                 {
2894                         return Child.ConvertToLong ();
2895                 }
2896
2897                 public override UIntConstant ConvertToUInt ()
2898                 {
2899                         return Child.ConvertToUInt ();
2900                 }
2901
2902                 public override IntConstant ConvertToInt ()
2903                 {
2904                         return Child.ConvertToInt ();
2905                 }
2906         }
2907
2908         /// <summary>
2909         ///   This kind of cast is used to encapsulate Value Types in objects.
2910         ///
2911         ///   The effect of it is to box the value type emitted by the previous
2912         ///   operation.
2913         /// </summary>
2914         public class BoxedCast : EmptyCast {
2915
2916                 public BoxedCast (Expression expr)
2917                         : base (expr, TypeManager.object_type)
2918                 {
2919                 }
2920
2921                 public override Expression DoResolve (EmitContext ec)
2922                 {
2923                         // This should never be invoked, we are born in fully
2924                         // initialized state.
2925
2926                         return this;
2927                 }
2928
2929                 public override void Emit (EmitContext ec)
2930                 {
2931                         base.Emit (ec);
2932                         
2933                         ec.ig.Emit (OpCodes.Box, child.Type);
2934                 }
2935         }
2936
2937         public class UnboxCast : EmptyCast {
2938                 public UnboxCast (Expression expr, Type return_type)
2939                         : base (expr, return_type)
2940                 {
2941                 }
2942
2943                 public override Expression DoResolve (EmitContext ec)
2944                 {
2945                         // This should never be invoked, we are born in fully
2946                         // initialized state.
2947
2948                         return this;
2949                 }
2950
2951                 public override void Emit (EmitContext ec)
2952                 {
2953                         Type t = type;
2954                         ILGenerator ig = ec.ig;
2955                         
2956                         base.Emit (ec);
2957                         ig.Emit (OpCodes.Unbox, t);
2958
2959                         LoadFromPtr (ig, t);
2960                 }
2961         }
2962         
2963         /// <summary>
2964         ///   This is used to perform explicit numeric conversions.
2965         ///
2966         ///   Explicit numeric conversions might trigger exceptions in a checked
2967         ///   context, so they should generate the conv.ovf opcodes instead of
2968         ///   conv opcodes.
2969         /// </summary>
2970         public class ConvCast : EmptyCast {
2971                 public enum Mode : byte {
2972                         I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
2973                         U1_I1, U1_CH,
2974                         I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
2975                         U2_I1, U2_U1, U2_I2, U2_CH,
2976                         I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
2977                         U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
2978                         I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
2979                         U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
2980                         CH_I1, CH_U1, CH_I2,
2981                         R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
2982                         R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
2983                 }
2984
2985                 Mode mode;
2986                 bool checked_state;
2987                 
2988                 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
2989                         : base (child, return_type)
2990                 {
2991                         checked_state = ec.CheckState;
2992                         mode = m;
2993                 }
2994
2995                 public override Expression DoResolve (EmitContext ec)
2996                 {
2997                         // This should never be invoked, we are born in fully
2998                         // initialized state.
2999
3000                         return this;
3001                 }
3002
3003                 public override void Emit (EmitContext ec)
3004                 {
3005                         ILGenerator ig = ec.ig;
3006                         
3007                         base.Emit (ec);
3008
3009                         if (checked_state){
3010                                 switch (mode){
3011                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3012                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3013                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3014                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3015                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3016
3017                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3018                                 case Mode.U1_CH: /* nothing */ break;
3019
3020                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3021                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3022                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3023                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3024                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3025                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3026
3027                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3028                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3029                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3030                                 case Mode.U2_CH: /* nothing */ break;
3031
3032                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3033                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3034                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3035                                 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3036                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3037                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3038                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3039
3040                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3041                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3042                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3043                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3044                                 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3045                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3046
3047                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3048                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3049                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3050                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3051                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3052                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3053                                 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3054                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3055
3056                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3057                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3058                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3059                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3060                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3061                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
3062                                 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
3063                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3064
3065                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3066                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3067                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3068
3069                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3070                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3071                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3072                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3073                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3074                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3075                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3076                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3077                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3078
3079                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3080                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3081                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3082                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3083                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3084                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3085                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3086                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3087                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3088                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3089                                 }
3090                         } else {
3091                                 switch (mode){
3092                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
3093                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
3094                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
3095                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
3096                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
3097
3098                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
3099                                 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
3100
3101                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
3102                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
3103                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
3104                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
3105                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
3106                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
3107
3108                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
3109                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
3110                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
3111                                 case Mode.U2_CH: /* nothing */ break;
3112
3113                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
3114                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
3115                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
3116                                 case Mode.I4_U4: /* nothing */ break;
3117                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
3118                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
3119                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
3120
3121                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
3122                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
3123                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
3124                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
3125                                 case Mode.U4_I4: /* nothing */ break;
3126                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
3127
3128                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
3129                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
3130                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
3131                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
3132                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
3133                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
3134                                 case Mode.I8_U8: /* nothing */ break;
3135                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
3136
3137                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
3138                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
3139                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
3140                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
3141                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
3142                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
3143                                 case Mode.U8_I8: /* nothing */ break;
3144                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
3145
3146                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
3147                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
3148                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
3149
3150                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
3151                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
3152                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
3153                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
3154                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
3155                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
3156                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
3157                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
3158                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
3159
3160                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
3161                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
3162                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
3163                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
3164                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
3165                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
3166                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
3167                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
3168                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
3169                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3170                                 }
3171                         }
3172                 }
3173         }
3174         
3175         public class OpcodeCast : EmptyCast {
3176                 OpCode op, op2;
3177                 bool second_valid;
3178                 
3179                 public OpcodeCast (Expression child, Type return_type, OpCode op)
3180                         : base (child, return_type)
3181                         
3182                 {
3183                         this.op = op;
3184                         second_valid = false;
3185                 }
3186
3187                 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
3188                         : base (child, return_type)
3189                         
3190                 {
3191                         this.op = op;
3192                         this.op2 = op2;
3193                         second_valid = true;
3194                 }
3195
3196                 public override Expression DoResolve (EmitContext ec)
3197                 {
3198                         // This should never be invoked, we are born in fully
3199                         // initialized state.
3200
3201                         return this;
3202                 }
3203
3204                 public override void Emit (EmitContext ec)
3205                 {
3206                         base.Emit (ec);
3207                         ec.ig.Emit (op);
3208
3209                         if (second_valid)
3210                                 ec.ig.Emit (op2);
3211                 }                       
3212         }
3213
3214         /// <summary>
3215         ///   This kind of cast is used to encapsulate a child and cast it
3216         ///   to the class requested
3217         /// </summary>
3218         public class ClassCast : EmptyCast {
3219                 public ClassCast (Expression child, Type return_type)
3220                         : base (child, return_type)
3221                         
3222                 {
3223                 }
3224
3225                 public override Expression DoResolve (EmitContext ec)
3226                 {
3227                         // This should never be invoked, we are born in fully
3228                         // initialized state.
3229
3230                         return this;
3231                 }
3232
3233                 public override void Emit (EmitContext ec)
3234                 {
3235                         base.Emit (ec);
3236
3237                         ec.ig.Emit (OpCodes.Castclass, type);
3238                 }                       
3239                 
3240         }
3241         
3242         /// <summary>
3243         ///   SimpleName expressions are initially formed of a single
3244         ///   word and it only happens at the beginning of the expression.
3245         /// </summary>
3246         ///
3247         /// <remarks>
3248         ///   The expression will try to be bound to a Field, a Method
3249         ///   group or a Property.  If those fail we pass the name to our
3250         ///   caller and the SimpleName is compounded to perform a type
3251         ///   lookup.  The idea behind this process is that we want to avoid
3252         ///   creating a namespace map from the assemblies, as that requires
3253         ///   the GetExportedTypes function to be called and a hashtable to
3254         ///   be constructed which reduces startup time.  If later we find
3255         ///   that this is slower, we should create a `NamespaceExpr' expression
3256         ///   that fully participates in the resolution process. 
3257         ///   
3258         ///   For example `System.Console.WriteLine' is decomposed into
3259         ///   MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3260         ///   
3261         ///   The first SimpleName wont produce a match on its own, so it will
3262         ///   be turned into:
3263         ///   MemberAccess (SimpleName ("System.Console"), "WriteLine").
3264         ///   
3265         ///   System.Console will produce a TypeExpr match.
3266         ///   
3267         ///   The downside of this is that we might be hitting `LookupType' too many
3268         ///   times with this scheme.
3269         /// </remarks>
3270         public class SimpleName : Expression {
3271                 public readonly string Name;
3272                 
3273                 public SimpleName (string name, Location l)
3274                 {
3275                         Name = name;
3276                         loc = l;
3277                 }
3278
3279                 public static void Error_ObjectRefRequired (Location l, string name)
3280                 {
3281                         Report.Error (
3282                                 120, l,
3283                                 "An object reference is required " +
3284                                 "for the non-static field `"+name+"'");
3285                 }
3286                 
3287                 //
3288                 // Checks whether we are trying to access an instance
3289                 // property, method or field from a static body.
3290                 //
3291                 Expression MemberStaticCheck (Expression e)
3292                 {
3293                         if (e is IMemberExpr){
3294                                 IMemberExpr member = (IMemberExpr) e;
3295                                 
3296                                 if (!member.IsStatic){
3297                                         Error_ObjectRefRequired (loc, Name);
3298                                         return null;
3299                                 }
3300                         } else if (e is MethodGroupExpr){
3301                                 MethodGroupExpr mg = (MethodGroupExpr) e;
3302
3303                                 if (!mg.RemoveInstanceMethods ()){
3304                                         Error_ObjectRefRequired (loc, mg.Methods [0].Name);
3305                                         return null;
3306                                 }
3307                                 return e;
3308                         }
3309
3310                         return e;
3311                 }
3312                 
3313                 public override Expression DoResolve (EmitContext ec)
3314                 {
3315                         return SimpleNameResolve (ec, null, false);
3316                 }
3317
3318                 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3319                 {
3320                         return SimpleNameResolve (ec, right_side, false);
3321                 }
3322                 
3323
3324                 public Expression DoResolveAllowStatic (EmitContext ec)
3325                 {
3326                         return SimpleNameResolve (ec, null, true);
3327                 }
3328
3329                 /// <remarks>
3330                 ///   7.5.2: Simple Names. 
3331                 ///
3332                 ///   Local Variables and Parameters are handled at
3333                 ///   parse time, so they never occur as SimpleNames.
3334                 ///
3335                 ///   The `allow_static' flag is used by MemberAccess only
3336                 ///   and it is used to inform us that it is ok for us to 
3337                 ///   avoid the static check, because MemberAccess might end
3338                 ///   up resolving the Name as a Type name and the access as
3339                 ///   a static type access.
3340                 ///
3341                 ///   ie: Type Type; .... { Type.GetType (""); }
3342                 ///
3343                 ///   Type is both an instance variable and a Type;  Type.GetType
3344                 ///   is the static method not an instance method of type.
3345                 /// </remarks>
3346                 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
3347                 {
3348                         Expression e = null;
3349
3350                         //
3351                         // Stage 1: Performed by the parser (binding to locals or parameters).
3352                         //
3353                         if (!ec.OnlyLookupTypes){
3354                                 Block current_block = ec.CurrentBlock;
3355                                 if (current_block != null && current_block.IsVariableDefined (Name)){
3356                                         LocalVariableReference var;
3357                                         
3358                                         var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
3359
3360                                         if (right_side != null)
3361                                                 return var.ResolveLValue (ec, right_side);
3362                                         else
3363                                                 return var.Resolve (ec);
3364                                 }
3365
3366                                 if (current_block != null){
3367                                         int idx = -1;
3368                                         Parameter par = null;
3369                                         Parameters pars = current_block.Parameters;
3370                                         if (pars != null)
3371                                                 par = pars.GetParameterByName (Name, out idx);
3372
3373                                         if (par != null) {
3374                                                 ParameterReference param;
3375                                         
3376                                                 param = new ParameterReference (pars, idx, Name, loc);
3377
3378                                                 if (right_side != null)
3379                                                         return param.ResolveLValue (ec, right_side);
3380                                                 else
3381                                                         return param.Resolve (ec);
3382                                         }
3383                                 }
3384
3385                                 //
3386                                 // Stage 2: Lookup members 
3387                                 //
3388
3389                                 //
3390                                 // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
3391                                 // Hence we have two different cases
3392                                 //
3393
3394                                 DeclSpace lookup_ds = ec.DeclSpace;
3395                                 do {
3396                                         if (lookup_ds.TypeBuilder == null)
3397                                                 break;
3398
3399                                         e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
3400                                         if (e != null)
3401                                                 break;
3402
3403                                         //
3404                                         // Classes/structs keep looking, enums break
3405                                         //
3406                                         if (lookup_ds is TypeContainer)
3407                                                 lookup_ds = ((TypeContainer) lookup_ds).Parent;
3408                                         else
3409                                                 break;
3410                                 } while (lookup_ds != null);
3411                                 
3412                                 if (e == null && ec.ContainerType != null)
3413                                         e = MemberLookup (ec, ec.ContainerType, Name, loc);
3414                         }
3415
3416                         // Continuation of stage 2
3417                         if (e == null){
3418                                 //
3419                                 // Stage 3: Lookup symbol in the various namespaces. 
3420                                 //
3421                                 DeclSpace ds = ec.DeclSpace;
3422                                 Type t;
3423                                 string alias_value;
3424
3425                                 if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
3426                                         return new TypeExpr (t, loc);
3427                                 
3428                                 //
3429                                 // Stage 2 part b: Lookup up if we are an alias to a type
3430                                 // or a namespace.
3431                                 //
3432                                 // Since we are cheating: we only do the Alias lookup for
3433                                 // namespaces if the name does not include any dots in it
3434                                 //
3435                                 
3436                                 alias_value = ec.DeclSpace.LookupAlias (Name);
3437                                 
3438                                 if (Name.IndexOf ('.') == -1 && alias_value != null) {
3439                                         if ((t = RootContext.LookupType (ds, alias_value, true, loc))
3440                                             != null)
3441                                                 return new TypeExpr (t, loc);
3442                                         
3443                                 // we have alias value, but it isn't Type, so try if it's namespace
3444                                         return new SimpleName (alias_value, loc);
3445                                 }
3446                                 
3447                                 if (ec.ResolvingTypeTree){
3448                                         Type dt = ec.DeclSpace.FindType (Name);
3449                                         if (dt != null)
3450                                                 return new TypeExpr (dt, loc);
3451                                 }
3452                                 
3453                                 // No match, maybe our parent can compose us
3454                                 // into something meaningful.
3455                                 return this;
3456                         }
3457
3458                         //
3459                         // Stage 2 continues here. 
3460                         // 
3461                         if (e is TypeExpr)
3462                                 return e;
3463
3464                         if (ec.OnlyLookupTypes)
3465                                 return null;
3466
3467                         if (e is IMemberExpr)
3468                                 return MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
3469
3470                         if (ec.IsStatic){
3471                                 if (allow_static)
3472                                         return e;
3473
3474                                 return MemberStaticCheck (e);
3475                         } else
3476                                 return e;
3477                 }
3478                 
3479                 public override void Emit (EmitContext ec)
3480                 {
3481                         //
3482                         // If this is ever reached, then we failed to
3483                         // find the name as a namespace
3484                         //
3485
3486                         Error (103, "The name `" + Name +
3487                                "' does not exist in the class `" +
3488                                ec.DeclSpace.Name + "'");
3489                 }
3490
3491                 public override string ToString ()
3492                 {
3493                         return Name;
3494                 }
3495         }
3496         
3497         /// <summary>
3498         ///   Fully resolved expression that evaluates to a type
3499         /// </summary>
3500         public class TypeExpr : Expression {
3501                 public TypeExpr (Type t, Location l)
3502                 {
3503                         Type = t;
3504                         eclass = ExprClass.Type;
3505                         loc = l;
3506                 }
3507
3508                 override public Expression DoResolve (EmitContext ec)
3509                 {
3510                         return this;
3511                 }
3512
3513                 override public void Emit (EmitContext ec)
3514                 {
3515                         throw new Exception ("Should never be called");
3516                 }
3517         }
3518
3519         /// <summary>
3520         ///   Used to create types from a fully qualified name.  These are just used
3521         ///   by the parser to setup the core types.  A TypeExpression is always
3522         ///   classified as a type.
3523         /// </summary>
3524         public class TypeExpression : TypeExpr {
3525                 string name;
3526                 
3527                 public TypeExpression (string name) : base (null, Location.Null)
3528                 {
3529                         this.name = name;
3530                 }
3531
3532                 public override Expression DoResolve (EmitContext ec)
3533                 {
3534                         if (type == null)
3535                                 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
3536                         return this;
3537                 }
3538
3539                 public override void Emit (EmitContext ec)
3540                 {
3541                         throw new Exception ("Should never be called");                 
3542                 }
3543
3544                 public override string ToString ()
3545                 {
3546                         return name;
3547                 }
3548         }
3549
3550         /// <summary>
3551         ///   MethodGroup Expression.
3552         ///  
3553         ///   This is a fully resolved expression that evaluates to a type
3554         /// </summary>
3555         public class MethodGroupExpr : Expression {
3556                 public MethodBase [] Methods;
3557                 Expression instance_expression = null;
3558                 
3559                 public MethodGroupExpr (MemberInfo [] mi, Location l)
3560                 {
3561                         Methods = new MethodBase [mi.Length];
3562                         mi.CopyTo (Methods, 0);
3563                         eclass = ExprClass.MethodGroup;
3564                         type = TypeManager.object_type;
3565                         loc = l;
3566                 }
3567
3568                 public MethodGroupExpr (ArrayList list, Location l)
3569                 {
3570                         Methods = new MethodBase [list.Count];
3571
3572                         try {
3573                                 list.CopyTo (Methods, 0);
3574                         } catch {
3575                                 foreach (MemberInfo m in list){
3576                                         if (!(m is MethodBase)){
3577                                                 Console.WriteLine ("Name " + m.Name);
3578                                                 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3579                                         }
3580                                 }
3581                                 throw;
3582                         }
3583                         loc = l;
3584                         eclass = ExprClass.MethodGroup;
3585                         type = TypeManager.object_type;
3586                 }
3587                 
3588                 //
3589                 // `A method group may have associated an instance expression' 
3590                 // 
3591                 public Expression InstanceExpression {
3592                         get {
3593                                 return instance_expression;
3594                         }
3595
3596                         set {
3597                                 instance_expression = value;
3598                         }
3599                 }
3600                 
3601                 override public Expression DoResolve (EmitContext ec)
3602                 {
3603                         return this;
3604                 }
3605
3606                 public void ReportUsageError ()
3607                 {
3608                         Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
3609                                       Methods [0].Name + "()' is referenced without parentheses");
3610                 }
3611
3612                 override public void Emit (EmitContext ec)
3613                 {
3614                         ReportUsageError ();
3615                 }
3616
3617                 bool RemoveMethods (bool keep_static)
3618                 {
3619                         ArrayList smethods = new ArrayList ();
3620
3621                         foreach (MethodBase mb in Methods){
3622                                 if (mb.IsStatic == keep_static)
3623                                         smethods.Add (mb);
3624                         }
3625
3626                         if (smethods.Count == 0)
3627                                 return false;
3628
3629                         Methods = new MethodBase [smethods.Count];
3630                         smethods.CopyTo (Methods, 0);
3631
3632                         return true;
3633                 }
3634                 
3635                 /// <summary>
3636                 ///   Removes any instance methods from the MethodGroup, returns
3637                 ///   false if the resulting set is empty.
3638                 /// </summary>
3639                 public bool RemoveInstanceMethods ()
3640                 {
3641                         return RemoveMethods (true);
3642                 }
3643
3644                 /// <summary>
3645                 ///   Removes any static methods from the MethodGroup, returns
3646                 ///   false if the resulting set is empty.
3647                 /// </summary>
3648                 public bool RemoveStaticMethods ()
3649                 {
3650                         return RemoveMethods (false);
3651                 }
3652         }
3653
3654         /// <summary>
3655         ///   Fully resolved expression that evaluates to a Field
3656         /// </summary>
3657         public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr {
3658                 public readonly FieldInfo FieldInfo;
3659                 Expression instance_expr;
3660                 
3661                 public FieldExpr (FieldInfo fi, Location l)
3662                 {
3663                         FieldInfo = fi;
3664                         eclass = ExprClass.Variable;
3665                         type = fi.FieldType;
3666                         loc = l;
3667                 }
3668
3669                 public string Name {
3670                         get {
3671                                 return FieldInfo.Name;
3672                         }
3673                 }
3674
3675                 public bool IsStatic {
3676                         get {
3677                                 return FieldInfo.IsStatic;
3678                         }
3679                 }
3680
3681                 public Expression InstanceExpression {
3682                         get {
3683                                 return instance_expr;
3684                         }
3685
3686                         set {
3687                                 instance_expr = value;
3688                         }
3689                 }
3690
3691                 override public Expression DoResolve (EmitContext ec)
3692                 {
3693                         if (!FieldInfo.IsStatic){
3694                                 if (instance_expr == null){
3695                                         throw new Exception ("non-static FieldExpr without instance var\n" +
3696                                                              "You have to assign the Instance variable\n" +
3697                                                              "Of the FieldExpr to set this\n");
3698                                 }
3699
3700                                 instance_expr = instance_expr.Resolve (ec);
3701                                 if (instance_expr == null)
3702                                         return null;
3703                         }
3704
3705                         return this;
3706                 }
3707
3708                 void Report_AssignToReadonly (bool is_instance)
3709                 {
3710                         string msg;
3711                         
3712                         if (is_instance)
3713                                 msg = "Readonly field can not be assigned outside " +
3714                                 "of constructor or variable initializer";
3715                         else
3716                                 msg = "A static readonly field can only be assigned in " +
3717                                 "a static constructor";
3718
3719                         Report.Error (is_instance ? 191 : 198, loc, msg);
3720                 }
3721                 
3722                 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3723                 {
3724                         Expression e = DoResolve (ec);
3725
3726                         if (e == null)
3727                                 return null;
3728                         
3729                         if (!FieldInfo.IsInitOnly)
3730                                 return this;
3731
3732                         //
3733                         // InitOnly fields can only be assigned in constructors
3734                         //
3735
3736                         if (ec.IsConstructor)
3737                                 return this;
3738
3739                         Report_AssignToReadonly (true);
3740                         
3741                         return null;
3742                 }
3743
3744                 override public void Emit (EmitContext ec)
3745                 {
3746                         ILGenerator ig = ec.ig;
3747                         bool is_volatile = false;
3748                                 
3749                         if (FieldInfo is FieldBuilder){
3750                                 FieldBase f = TypeManager.GetField (FieldInfo);
3751
3752                                 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3753                                         is_volatile = true;
3754                                 
3755                                 f.status |= Field.Status.USED;
3756                         }
3757                         
3758                         if (FieldInfo.IsStatic){
3759                                 if (is_volatile)
3760                                         ig.Emit (OpCodes.Volatile);
3761                                 
3762                                 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3763                         } else {
3764                                 if (instance_expr.Type.IsValueType){
3765                                         IMemoryLocation ml;
3766                                         LocalTemporary tempo = null;
3767                                         
3768                                         if (!(instance_expr is IMemoryLocation)){
3769                                                 tempo = new LocalTemporary (
3770                                                         ec, instance_expr.Type);
3771
3772                                                 InstanceExpression.Emit (ec);
3773                                                 tempo.Store (ec);
3774                                                 ml = tempo;
3775                                         } else
3776                                                 ml = (IMemoryLocation) instance_expr;
3777
3778                                         ml.AddressOf (ec, AddressOp.Load);
3779                                 } else 
3780                                         instance_expr.Emit (ec);
3781
3782                                 if (is_volatile)
3783                                         ig.Emit (OpCodes.Volatile);
3784                                 
3785                                 ig.Emit (OpCodes.Ldfld, FieldInfo);
3786                         }
3787                 }
3788
3789                 public void EmitAssign (EmitContext ec, Expression source)
3790                 {
3791                         FieldAttributes fa = FieldInfo.Attributes;
3792                         bool is_static = (fa & FieldAttributes.Static) != 0;
3793                         bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
3794                         ILGenerator ig = ec.ig;
3795
3796                         if (is_readonly && !ec.IsConstructor){
3797                                 Report_AssignToReadonly (!is_static);
3798                                 return;
3799                         }
3800                         
3801                         if (!is_static){
3802                                 Expression instance = instance_expr;
3803
3804                                 if (instance.Type.IsValueType){
3805                                         if (instance is IMemoryLocation){
3806                                                 IMemoryLocation ml = (IMemoryLocation) instance;
3807
3808                                                 ml.AddressOf (ec, AddressOp.Store);
3809                                         } else
3810                                                 throw new Exception ("The " + instance + " of type " +
3811                                                                      instance.Type +
3812                                                                      " represents a ValueType and does " +
3813                                                                      "not implement IMemoryLocation");
3814                                 } else
3815                                         instance.Emit (ec);
3816                         }
3817                         source.Emit (ec);
3818
3819                         if (FieldInfo is FieldBuilder){
3820                                 FieldBase f = TypeManager.GetField (FieldInfo);
3821                                 
3822                                 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3823                                         ig.Emit (OpCodes.Volatile);
3824                         }
3825                         
3826                         if (is_static)
3827                                 ig.Emit (OpCodes.Stsfld, FieldInfo);
3828                         else 
3829                                 ig.Emit (OpCodes.Stfld, FieldInfo);
3830
3831                         if (FieldInfo is FieldBuilder){
3832                                 FieldBase f = TypeManager.GetField (FieldInfo);
3833
3834                                 f.status |= Field.Status.ASSIGNED;
3835                         }
3836                 }
3837                 
3838                 public void AddressOf (EmitContext ec, AddressOp mode)
3839                 {
3840                         ILGenerator ig = ec.ig;
3841                         
3842                         if (FieldInfo is FieldBuilder){
3843                                 FieldBase f = TypeManager.GetField (FieldInfo);
3844                                 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3845                                         ig.Emit (OpCodes.Volatile);
3846                         }
3847
3848                         if (FieldInfo is FieldBuilder){
3849                                 FieldBase f = TypeManager.GetField (FieldInfo);
3850
3851                                 if ((mode & AddressOp.Store) != 0)
3852                                         f.status |= Field.Status.ASSIGNED;
3853                                 if ((mode & AddressOp.Load) != 0)
3854                                         f.status |= Field.Status.USED;
3855                         }
3856
3857                         //
3858                         // Handle initonly fields specially: make a copy and then
3859                         // get the address of the copy.
3860                         //
3861                         if (FieldInfo.IsInitOnly){
3862                                 if (ec.IsConstructor) {
3863                                         ig.Emit (OpCodes.Ldsflda, FieldInfo);
3864                                 } else {
3865                                         LocalBuilder local;
3866                                 
3867                                         Emit (ec);
3868                                         local = ig.DeclareLocal (type);
3869                                         ig.Emit (OpCodes.Stloc, local);
3870                                         ig.Emit (OpCodes.Ldloca, local);
3871                                 }
3872                                 return;
3873                         }
3874
3875                         if (FieldInfo.IsStatic)
3876                                 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3877                         else {
3878                                 if (instance_expr is IMemoryLocation)
3879                                         ((IMemoryLocation)instance_expr).AddressOf (ec, AddressOp.LoadStore);
3880                                 else
3881                                         instance_expr.Emit (ec);
3882                                 ig.Emit (OpCodes.Ldflda, FieldInfo);
3883                         }
3884                 }
3885         }
3886         
3887         /// <summary>
3888         ///   Expression that evaluates to a Property.  The Assign class
3889         ///   might set the `Value' expression if we are in an assignment.
3890         ///
3891         ///   This is not an LValue because we need to re-write the expression, we
3892         ///   can not take data from the stack and store it.  
3893         /// </summary>
3894         public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
3895                 public readonly PropertyInfo PropertyInfo;
3896                 public bool IsBase;
3897                 MethodInfo [] Accessors;
3898                 bool is_static;
3899                 
3900                 Expression instance_expr;
3901
3902                 public PropertyExpr (PropertyInfo pi, Location l)
3903                 {
3904                         PropertyInfo = pi;
3905                         eclass = ExprClass.PropertyAccess;
3906                         is_static = false;
3907                         loc = l;
3908                         Accessors = TypeManager.GetAccessors (pi);
3909
3910                         if (Accessors != null)
3911                                 foreach (MethodInfo mi in Accessors){
3912                                         if (mi != null)
3913                                                 if (mi.IsStatic)
3914                                                         is_static = true;
3915                                 }
3916                         else
3917                                 Accessors = new MethodInfo [2];
3918                         
3919                         type = TypeManager.TypeToCoreType (pi.PropertyType);
3920                 }
3921
3922                 public string Name {
3923                         get {
3924                                 return PropertyInfo.Name;
3925                         }
3926                 }
3927
3928                 public bool IsStatic {
3929                         get {
3930                                 return is_static;
3931                         }
3932                 }
3933                 
3934                 //
3935                 // The instance expression associated with this expression
3936                 //
3937                 public Expression InstanceExpression {
3938                         set {
3939                                 instance_expr = value;
3940                         }
3941
3942                         get {
3943                                 return instance_expr;
3944                         }
3945                 }
3946
3947                 public bool VerifyAssignable ()
3948                 {
3949                         if (!PropertyInfo.CanWrite){
3950                                 Report.Error (200, loc, 
3951                                               "The property `" + PropertyInfo.Name +
3952                                               "' can not be assigned to, as it has not set accessor");
3953                                 return false;
3954                         }
3955
3956                         return true;
3957                 }
3958
3959                 override public Expression DoResolve (EmitContext ec)
3960                 {
3961                         if (!PropertyInfo.CanRead){
3962                                 Report.Error (154, loc, 
3963                                               "The property `" + PropertyInfo.Name +
3964                                               "' can not be used in " +
3965                                               "this context because it lacks a get accessor");
3966                                 return null;
3967                         }
3968
3969                         type = PropertyInfo.PropertyType;
3970
3971                         return this;
3972                 }
3973
3974                 override public void Emit (EmitContext ec)
3975                 {
3976                         MethodInfo method = Accessors [0];
3977
3978                         //
3979                         // Special case: length of single dimension array is turned into ldlen
3980                         //
3981                         if (method == TypeManager.int_array_get_length){
3982                                 Type iet = instance_expr.Type;
3983
3984                                 //
3985                                 // System.Array.Length can be called, but the Type does not
3986                                 // support invoking GetArrayRank, so test for that case first
3987                                 //
3988                                 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
3989                                         instance_expr.Emit (ec);
3990                                         ec.ig.Emit (OpCodes.Ldlen);
3991                                         return;
3992                                 }
3993                         }
3994
3995                         Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, method, null, loc);
3996                         
3997                 }
3998
3999                 //
4000                 // Implements the IAssignMethod interface for assignments
4001                 //
4002                 public void EmitAssign (EmitContext ec, Expression source)
4003                 {
4004                         Argument arg = new Argument (source, Argument.AType.Expression);
4005                         ArrayList args = new ArrayList ();
4006
4007                         args.Add (arg);
4008                         Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args, loc);
4009                 }
4010
4011                 override public void EmitStatement (EmitContext ec)
4012                 {
4013                         Emit (ec);
4014                         ec.ig.Emit (OpCodes.Pop);
4015                 }
4016         }
4017
4018         /// <summary>
4019         ///   Fully resolved expression that evaluates to an Event
4020         /// </summary>
4021         public class EventExpr : Expression, IMemberExpr {
4022                 public readonly EventInfo EventInfo;
4023                 public Expression instance_expr;
4024
4025                 bool is_static;
4026                 MethodInfo add_accessor, remove_accessor;
4027                 
4028                 public EventExpr (EventInfo ei, Location loc)
4029                 {
4030                         EventInfo = ei;
4031                         this.loc = loc;
4032                         eclass = ExprClass.EventAccess;
4033
4034                         add_accessor = TypeManager.GetAddMethod (ei);
4035                         remove_accessor = TypeManager.GetRemoveMethod (ei);
4036                         
4037                         if (add_accessor.IsStatic || remove_accessor.IsStatic)
4038                                 is_static = true;
4039
4040                         if (EventInfo is MyEventBuilder)
4041                                 type = ((MyEventBuilder) EventInfo).EventType;
4042                         else
4043                                 type = EventInfo.EventHandlerType;
4044                 }
4045
4046                 public string Name {
4047                         get {
4048                                 return EventInfo.Name;
4049                         }
4050                 }
4051
4052                 public bool IsStatic {
4053                         get {
4054                                 return is_static;
4055                         }
4056                 }
4057
4058                 public Expression InstanceExpression {
4059                         get {
4060                                 return instance_expr;
4061                         }
4062
4063                         set {
4064                                 instance_expr = value;
4065                         }
4066                 }
4067
4068                 public override Expression DoResolve (EmitContext ec)
4069                 {
4070                         // We are born fully resolved
4071                         return this;
4072                 }
4073
4074                 public override void Emit (EmitContext ec)
4075                 {
4076                         throw new Exception ("Should not happen I think");
4077                 }
4078
4079                 public void EmitAddOrRemove (EmitContext ec, Expression source)
4080                 {
4081                         Expression handler = ((Binary) source).Right;
4082                         
4083                         Argument arg = new Argument (handler, Argument.AType.Expression);
4084                         ArrayList args = new ArrayList ();
4085                                 
4086                         args.Add (arg);
4087                         
4088                         if (((Binary) source).Oper == Binary.Operator.Addition)
4089                                 Invocation.EmitCall (
4090                                         ec, false, IsStatic, instance_expr, add_accessor, args, loc);
4091                         else
4092                                 Invocation.EmitCall (
4093                                         ec, false, IsStatic, instance_expr, remove_accessor, args, loc);
4094                 }
4095         }
4096 }