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