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