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