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