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