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