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