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