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