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