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