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