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