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