Modify few emits to deal with await expression
[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 // Copyright 2001, 2002, 2003 Ximian, Inc.
9 // Copyright 2003-2008 Novell, Inc.
10 //
11 //
12
13 using System;
14 using System.Collections.Generic;
15 using System.Text;
16 using SLE = System.Linq.Expressions;
17 using System.Linq;
18
19 #if STATIC
20 using IKVM.Reflection;
21 using IKVM.Reflection.Emit;
22 #else
23 using System.Reflection;
24 using System.Reflection.Emit;
25 #endif
26
27 namespace Mono.CSharp {
28
29         /// <remarks>
30         ///   The ExprClass class contains the is used to pass the 
31         ///   classification of an expression (value, variable, namespace,
32         ///   type, method group, property access, event access, indexer access,
33         ///   nothing).
34         /// </remarks>
35         public enum ExprClass : byte {
36                 Unresolved      = 0,
37                 
38                 Value,
39                 Variable,
40                 Namespace,
41                 Type,
42                 TypeParameter,
43                 MethodGroup,
44                 PropertyAccess,
45                 EventAccess,
46                 IndexerAccess,
47                 Nothing, 
48         }
49
50         /// <remarks>
51         ///   This is used to tell Resolve in which types of expressions we're
52         ///   interested.
53         /// </remarks>
54         [Flags]
55         public enum ResolveFlags {
56                 // Returns Value, Variable, PropertyAccess, EventAccess or IndexerAccess.
57                 VariableOrValue         = 1,
58
59                 // Returns a type expression.
60                 Type                    = 1 << 1,
61
62                 // Returns a method group.
63                 MethodGroup             = 1 << 2,
64
65                 TypeParameter   = 1 << 3,
66
67                 // Mask of all the expression class flags.
68                 MaskExprClass = VariableOrValue | Type | MethodGroup | TypeParameter,
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         //
100         // An expressions resolved as a direct variable reference
101         //
102         public interface IVariableReference : IFixedExpression
103         {
104                 bool IsHoisted { get; }
105                 string Name { get; }
106                 VariableInfo VariableInfo { get; }
107
108                 void SetHasAddressTaken ();
109         }
110
111         //
112         // Implemented by an expression which could be or is always
113         // fixed
114         //
115         public interface IFixedExpression
116         {
117                 bool IsFixed { get; }
118         }
119
120         /// <remarks>
121         ///   Base class for expressions
122         /// </remarks>
123         public abstract class Expression {
124                 public ExprClass eclass;
125                 protected TypeSpec type;
126                 protected Location loc;
127                 
128                 public TypeSpec Type {
129                         get { return type; }
130                         set { type = value; }
131                 }
132
133                 public virtual bool IsSideEffectFree {
134                         get {
135                                 return false;
136                         }
137                 }
138
139                 public Location Location {
140                         get { return loc; }
141                 }
142
143                 public virtual bool IsNull {
144                         get {
145                                 return false;
146                         }
147                 }
148
149                 //
150                 // Returns true when the expression during Emit phase breaks stack
151                 // by using await expression
152                 //
153                 public virtual bool ContainsEmitWithAwait ()
154                 {
155                         return false;
156                 }
157
158                 /// <summary>
159                 ///   Performs semantic analysis on the Expression
160                 /// </summary>
161                 ///
162                 /// <remarks>
163                 ///   The Resolve method is invoked to perform the semantic analysis
164                 ///   on the node.
165                 ///
166                 ///   The return value is an expression (it can be the
167                 ///   same expression in some cases) or a new
168                 ///   expression that better represents this node.
169                 ///   
170                 ///   For example, optimizations of Unary (LiteralInt)
171                 ///   would return a new LiteralInt with a negated
172                 ///   value.
173                 ///   
174                 ///   If there is an error during semantic analysis,
175                 ///   then an error should be reported (using Report)
176                 ///   and a null value should be returned.
177                 ///   
178                 ///   There are two side effects expected from calling
179                 ///   Resolve(): the the field variable "eclass" should
180                 ///   be set to any value of the enumeration
181                 ///   `ExprClass' and the type variable should be set
182                 ///   to a valid type (this is the type of the
183                 ///   expression).
184                 /// </remarks>
185                 protected abstract Expression DoResolve (ResolveContext rc);
186
187                 public virtual Expression DoResolveLValue (ResolveContext rc, Expression right_side)
188                 {
189                         return null;
190                 }
191
192                 //
193                 // This is used if the expression should be resolved as a type or namespace name.
194                 // the default implementation fails.   
195                 //
196                 public virtual TypeSpec ResolveAsType (IMemberContext mc)
197                 {
198                         ResolveContext ec = new ResolveContext (mc);
199                         Expression e = Resolve (ec);
200                         if (e != null)
201                                 e.Error_UnexpectedKind (ec, ResolveFlags.Type, loc);
202
203                         return null;
204                 }
205
206                 public static void ErrorIsInaccesible (IMemberContext rc, string member, Location loc)
207                 {
208                         rc.Module.Compiler.Report.Error (122, loc, "`{0}' is inaccessible due to its protection level", member);
209                 }
210
211                 public void Error_ExpressionMustBeConstant (ResolveContext rc, Location loc, string e_name)
212                 {
213                         rc.Report.Error (133, loc, "The expression being assigned to `{0}' must be constant", e_name);
214                 }
215
216                 public void Error_ConstantCanBeInitializedWithNullOnly (ResolveContext rc, TypeSpec type, Location loc, string name)
217                 {
218                         rc.Report.Error (134, loc, "A constant `{0}' of reference type `{1}' can only be initialized with null",
219                                 name, TypeManager.CSharpName (type));
220                 }
221
222                 public static void Error_InvalidExpressionStatement (Report Report, Location loc)
223                 {
224                         Report.Error (201, loc, "Only assignment, call, increment, decrement, and new object " +
225                                        "expressions can be used as a statement");
226                 }
227                 
228                 public void Error_InvalidExpressionStatement (BlockContext ec)
229                 {
230                         Error_InvalidExpressionStatement (ec.Report, loc);
231                 }
232
233                 public static void Error_VoidInvalidInTheContext (Location loc, Report Report)
234                 {
235                         Report.Error (1547, loc, "Keyword `void' cannot be used in this context");
236                 }
237
238                 public virtual void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec target, bool expl)
239                 {
240                         Error_ValueCannotBeConvertedCore (ec, loc, target, expl);
241                 }
242
243                 protected void Error_ValueCannotBeConvertedCore (ResolveContext ec, Location loc, TypeSpec target, bool expl)
244                 {
245                         // The error was already reported as CS1660
246                         if (type == InternalType.AnonymousMethod)
247                                 return;
248
249                         string from_type = type.GetSignatureForError ();
250                         string to_type = target.GetSignatureForError ();
251                         if (from_type == to_type) {
252                                 from_type = type.GetSignatureForErrorIncludingAssemblyName ();
253                                 to_type = target.GetSignatureForErrorIncludingAssemblyName ();
254                         }
255
256                         if (expl) {
257                                 ec.Report.Error (30, loc, "Cannot convert type `{0}' to `{1}'",
258                                         from_type, to_type);
259                                 return;
260                         }
261
262                         ec.Report.DisableReporting ();
263                         bool expl_exists = Convert.ExplicitConversion (ec, this, target, Location.Null) != null;
264                         ec.Report.EnableReporting ();
265
266                         if (expl_exists) {
267                                 ec.Report.Error (266, loc,
268                                         "Cannot implicitly convert type `{0}' to `{1}'. An explicit conversion exists (are you missing a cast?)",
269                                         from_type, to_type);
270                         } else {
271                                 ec.Report.Error (29, loc, "Cannot implicitly convert type `{0}' to `{1}'",
272                                         from_type, to_type);
273                         }
274                 }
275
276                 public void Error_TypeArgumentsCannotBeUsed (IMemberContext context, MemberSpec member, int arity, Location loc)
277                 {
278                         // Better message for possible generic expressions
279                         if (member != null && (member.Kind & MemberKind.GenericMask) != 0) {
280                                 var report = context.Module.Compiler.Report;
281                                 report.SymbolRelatedToPreviousError (member);
282                                 if (member is TypeSpec)
283                                         member = ((TypeSpec) member).GetDefinition ();
284                                 else
285                                         member = ((MethodSpec) member).GetGenericMethodDefinition ();
286
287                                 string name = member.Kind == MemberKind.Method ? "method" : "type";
288                                 if (member.IsGeneric) {
289                                         report.Error (305, loc, "Using the generic {0} `{1}' requires `{2}' type argument(s)",
290                                                 name, member.GetSignatureForError (), member.Arity.ToString ());
291                                 } else {
292                                         report.Error (308, loc, "The non-generic {0} `{1}' cannot be used with the type arguments",
293                                                 name, member.GetSignatureForError ());
294                                 }
295                         } else {
296                                 Error_TypeArgumentsCannotBeUsed (context, ExprClassName, GetSignatureForError (), loc);
297                         }
298                 }
299
300                 public void Error_TypeArgumentsCannotBeUsed (IMemberContext context, string exprType, string name, Location loc)
301                 {
302                         context.Module.Compiler.Report.Error (307, loc, "The {0} `{1}' cannot be used with type arguments",
303                                 exprType, name);
304                 }
305
306                 protected virtual void Error_TypeDoesNotContainDefinition (ResolveContext ec, TypeSpec type, string name)
307                 {
308                         Error_TypeDoesNotContainDefinition (ec, loc, type, name);
309                 }
310
311                 public static void Error_TypeDoesNotContainDefinition (ResolveContext ec, Location loc, TypeSpec type, string name)
312                 {
313                         ec.Report.SymbolRelatedToPreviousError (type);
314                         ec.Report.Error (117, loc, "`{0}' does not contain a definition for `{1}'",
315                                 TypeManager.CSharpName (type), name);
316                 }
317
318                 protected static void Error_ValueAssignment (ResolveContext ec, Location loc)
319                 {
320                         ec.Report.Error (131, loc, "The left-hand side of an assignment must be a variable, a property or an indexer");
321                 }
322
323                 protected void Error_VoidPointerOperation (ResolveContext rc)
324                 {
325                         rc.Report.Error (242, loc, "The operation in question is undefined on void pointers");
326                 }
327
328                 public ResolveFlags ExprClassToResolveFlags {
329                         get {
330                                 switch (eclass) {
331                                 case ExprClass.Type:
332                                 case ExprClass.Namespace:
333                                         return ResolveFlags.Type;
334                                         
335                                 case ExprClass.MethodGroup:
336                                         return ResolveFlags.MethodGroup;
337                                         
338                                 case ExprClass.TypeParameter:
339                                         return ResolveFlags.TypeParameter;
340                                         
341                                 case ExprClass.Value:
342                                 case ExprClass.Variable:
343                                 case ExprClass.PropertyAccess:
344                                 case ExprClass.EventAccess:
345                                 case ExprClass.IndexerAccess:
346                                         return ResolveFlags.VariableOrValue;
347                                         
348                                 default:
349                                         throw new InternalErrorException (loc.ToString () + " " +  GetType () + " ExprClass is Invalid after resolve");
350                                 }
351                         }
352                 }
353
354                 public virtual string GetSignatureForError ()
355                 {
356                         return type.GetDefinition ().GetSignatureForError ();
357                 }
358                
359                 /// <summary>
360                 ///   Resolves an expression and performs semantic analysis on it.
361                 /// </summary>
362                 ///
363                 /// <remarks>
364                 ///   Currently Resolve wraps DoResolve to perform sanity
365                 ///   checking and assertion checking on what we expect from Resolve.
366                 /// </remarks>
367                 public Expression Resolve (ResolveContext ec, ResolveFlags flags)
368                 {
369                         if (eclass != ExprClass.Unresolved)
370                                 return this;
371                         
372                         Expression e;
373                         try {
374                                 e = DoResolve (ec);
375
376                                 if (e == null)
377                                         return null;
378
379                                 if ((flags & e.ExprClassToResolveFlags) == 0) {
380                                         e.Error_UnexpectedKind (ec, flags, loc);
381                                         return null;
382                                 }
383
384                                 if (e.type == null)
385                                         throw new InternalErrorException ("Expression `{0}' didn't set its type in DoResolve", e.GetType ());
386
387                                 return e;
388                         } catch (Exception ex) {
389                                 if (loc.IsNull || ec.Module.Compiler.Settings.DebugFlags > 0 || ex is CompletionResult || ec.Report.IsDisabled)
390                                         throw;
391
392                                 ec.Report.Error (584, loc, "Internal compiler error: {0}", ex.Message);
393                                 return EmptyExpression.Null;    // TODO: Add location
394                         }
395                 }
396
397                 /// <summary>
398                 ///   Resolves an expression and performs semantic analysis on it.
399                 /// </summary>
400                 public Expression Resolve (ResolveContext rc)
401                 {
402                         return Resolve (rc, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
403                 }
404
405                 /// <summary>
406                 ///   Resolves an expression for LValue assignment
407                 /// </summary>
408                 ///
409                 /// <remarks>
410                 ///   Currently ResolveLValue wraps DoResolveLValue to perform sanity
411                 ///   checking and assertion checking on what we expect from Resolve
412                 /// </remarks>
413                 public Expression ResolveLValue (ResolveContext ec, Expression right_side)
414                 {
415                         int errors = ec.Report.Errors;
416                         bool out_access = right_side == EmptyExpression.OutAccess;
417
418                         Expression e = DoResolveLValue (ec, right_side);
419
420                         if (e != null && out_access && !(e is IMemoryLocation)) {
421                                 // FIXME: There's no problem with correctness, the 'Expr = null' handles that.
422                                 //        Enabling this 'throw' will "only" result in deleting useless code elsewhere,
423
424                                 //throw new InternalErrorException ("ResolveLValue didn't return an IMemoryLocation: " +
425                                 //                                e.GetType () + " " + e.GetSignatureForError ());
426                                 e = null;
427                         }
428
429                         if (e == null) {
430                                 if (errors == ec.Report.Errors) {
431                                         if (out_access)
432                                                 ec.Report.Error (1510, loc, "A ref or out argument must be an assignable variable");
433                                         else
434                                                 Error_ValueAssignment (ec, loc);
435                                 }
436                                 return null;
437                         }
438
439                         if (e.eclass == ExprClass.Unresolved)
440                                 throw new Exception ("Expression " + e + " ExprClass is Invalid after resolve");
441
442                         if ((e.type == null) && !(e is GenericTypeExpr))
443                                 throw new Exception ("Expression " + e + " did not set its type after Resolve");
444
445                         return e;
446                 }
447
448                 public virtual void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
449                 {
450                         rc.Module.Compiler.Report.Error (182, loc,
451                                 "An attribute argument must be a constant expression, typeof expression or array creation expression");
452                 }
453
454                 /// <summary>
455                 ///   Emits the code for the expression
456                 /// </summary>
457                 ///
458                 /// <remarks>
459                 ///   The Emit method is invoked to generate the code
460                 ///   for the expression.  
461                 /// </remarks>
462                 public abstract void Emit (EmitContext ec);
463
464
465                 // Emit code to branch to @target if this expression is equivalent to @on_true.
466                 // The default implementation is to emit the value, and then emit a brtrue or brfalse.
467                 // Subclasses can provide more efficient implementations, but those MUST be equivalent,
468                 // including the use of conditional branches.  Note also that a branch MUST be emitted
469                 public virtual void EmitBranchable (EmitContext ec, Label target, bool on_true)
470                 {
471                         Emit (ec);
472                         ec.Emit (on_true ? OpCodes.Brtrue : OpCodes.Brfalse, target);
473                 }
474
475                 // Emit this expression for its side effects, not for its value.
476                 // The default implementation is to emit the value, and then throw it away.
477                 // Subclasses can provide more efficient implementations, but those MUST be equivalent
478                 public virtual void EmitSideEffect (EmitContext ec)
479                 {
480                         Emit (ec);
481                         ec.Emit (OpCodes.Pop);
482                 }
483
484                 //
485                 // Emits the expression into temporary field variable. The method
486                 // should be used for await expressions only
487                 //
488                 public virtual Expression EmitToField (EmitContext ec)
489                 {
490                         //
491                         // This is the await prepare Emit method. When emitting code like
492                         // a + b we emit code like
493                         //
494                         // a.Emit ()
495                         // b.Emit ()
496                         // Opcodes.Add
497                         //
498                         // For await a + await b we have to interfere the flow to keep the
499                         // stack clean because await yields from the expression. The emit
500                         // then changes to
501                         //
502                         // a = a.EmitToField () // a is changed to temporary field access
503                         // b = b.EmitToField ()
504                         // a.Emit ()
505                         // b.Emit ()
506                         // Opcodes.Add
507                         //
508                         //
509                         // The idea is to emit expression and leave the stack empty with
510                         // result value still available.
511                         //
512                         // Expressions should override this default implementation when
513                         // optimized version can be provided (e.g. FieldExpr)
514                         //
515                         //
516                         // We can optimize for side-effect free expressions, they can be
517                         // emitted out of order
518                         //
519                         if (IsSideEffectFree)
520                                 return this;
521
522                         // Emit original code
523                         EmitToFieldSource (ec);
524
525                         // Create temporary local (we cannot load this before Emit)
526                         var temp = ec.GetTemporaryLocal (type);
527                         ec.Emit (OpCodes.Stloc, temp, type);
528
529                         //
530                         // Store the result to temporary field
531                         //
532                         var field = ec.GetTemporaryField (type);
533                         ec.EmitThis ();
534                         ec.Emit (OpCodes.Ldloc, temp, type);
535                         field.EmitAssignFromStack (ec);
536
537                         ec.FreeTemporaryLocal (temp, type);
538
539                         return field;
540                 }
541
542                 protected virtual void EmitToFieldSource (EmitContext ec)
543                 {
544                         //
545                         // Default implementation calls Emit method
546                         //
547                         Emit (ec);
548                 }
549
550                 protected static void EmitExpressionsList (EmitContext ec, List<Expression> expressions)
551                 {
552                         bool contains_await = false;
553                         for (int i = 1; i < expressions.Count; ++i) {
554                                 if (expressions[i].ContainsEmitWithAwait ()) {
555                                         contains_await = true;
556                                         break;
557                                 }
558                         }
559
560                         if (contains_await) {
561                                 for (int i = 0; i < expressions.Count; ++i) {
562                                         expressions [i] = expressions[i].EmitToField (ec);
563                                 }
564                         }
565
566                         for (int i = 0; i < expressions.Count; ++i) {
567                                 expressions[i].Emit (ec);
568                         }
569                 }
570
571                 /// <summary>
572                 ///   Protected constructor.  Only derivate types should
573                 ///   be able to be created
574                 /// </summary>
575
576                 protected Expression ()
577                 {
578                 }
579
580                 /// <summary>
581                 ///   Returns a fully formed expression after a MemberLookup
582                 /// </summary>
583                 /// 
584                 static Expression ExprClassFromMemberInfo (MemberSpec spec, Location loc)
585                 {
586                         if (spec is EventSpec)
587                                 return new EventExpr ((EventSpec) spec, loc);
588                         if (spec is ConstSpec)
589                                 return new ConstantExpr ((ConstSpec) spec, loc);
590                         if (spec is FieldSpec)
591                                 return new FieldExpr ((FieldSpec) spec, loc);
592                         if (spec is PropertySpec)
593                                 return new PropertyExpr ((PropertySpec) spec, loc);
594                         if (spec is TypeSpec)
595                                 return new TypeExpression (((TypeSpec) spec), loc);
596
597                         return null;
598                 }
599
600                 protected static MethodSpec ConstructorLookup (ResolveContext rc, TypeSpec type, ref Arguments args, Location loc)
601                 {
602                         var ctors = MemberCache.FindMembers (type, Constructor.ConstructorName, true);
603                         if (ctors == null) {
604                                 rc.Report.SymbolRelatedToPreviousError (type);
605                                 if (type.IsStruct) {
606                                         // Report meaningful error for struct as they always have default ctor in C# context
607                                         OverloadResolver.Error_ConstructorMismatch (rc, type, args == null ? 0 : args.Count, loc);
608                                 } else {
609                                         rc.Report.Error (143, loc, "The class `{0}' has no constructors defined",
610                                                 type.GetSignatureForError ());
611                                 }
612
613                                 return null;
614                         }
615
616                         var r = new OverloadResolver (ctors, OverloadResolver.Restrictions.NoBaseMembers, loc);
617                         var ctor = r.ResolveMember<MethodSpec> (rc, ref args);
618                         if (ctor == null)
619                                 return null;
620
621                         if ((ctor.Modifiers & Modifiers.PROTECTED) != 0 && !rc.HasSet (ResolveContext.Options.BaseInitializer)) {
622                                 MemberExpr.CheckProtectedMemberAccess (rc, ctor, ctor.DeclaringType, loc);
623                         }
624
625                         return ctor;
626                 }
627
628                 [Flags]
629                 public enum MemberLookupRestrictions
630                 {
631                         None = 0,
632                         InvocableOnly = 1,
633                         ExactArity = 1 << 2,
634                         ReadAccess = 1 << 3
635                 }
636
637                 //
638                 // Lookup type `queried_type' for code in class `container_type' with a qualifier of
639                 // `qualifier_type' or null to lookup members in the current class.
640                 //
641                 public static Expression MemberLookup (IMemberContext rc, bool errorMode, TypeSpec queried_type, string name, int arity, MemberLookupRestrictions restrictions, Location loc)
642                 {
643                         var members = MemberCache.FindMembers (queried_type, name, false);
644                         if (members == null)
645                                 return null;
646
647                         MemberSpec non_method = null;
648                         MemberSpec ambig_non_method = null;
649                         do {
650                                 for (int i = 0; i < members.Count; ++i) {
651                                         var member = members[i];
652
653                                         // HACK: for events because +=/-= can appear at same class only, should use OverrideToBase there
654                                         if ((member.Modifiers & Modifiers.OVERRIDE) != 0 && member.Kind != MemberKind.Event)
655                                                 continue;
656
657                                         if ((arity > 0 || (restrictions & MemberLookupRestrictions.ExactArity) != 0) && member.Arity != arity)
658                                                 continue;
659
660                                         if (!errorMode) {
661                                                 if (!member.IsAccessible (rc))
662                                                         continue;
663
664                                                 //
665                                                 // With runtime binder we can have a situation where queried type is inaccessible
666                                                 // because it came via dynamic object, the check about inconsisted accessibility
667                                                 // had no effect as the type was unknown during compilation
668                                                 //
669                                                 // class A {
670                                                 //              private class N { }
671                                                 //
672                                                 //              public dynamic Foo ()
673                                                 //              {
674                                                 //                      return new N ();
675                                                 //              }
676                                                 //      }
677                                                 //
678                                                 if (rc.Module.Compiler.IsRuntimeBinder && !member.DeclaringType.IsAccessible (rc))
679                                                         continue;
680                                         }
681
682                                         if ((restrictions & MemberLookupRestrictions.InvocableOnly) != 0) {
683                                                 if (member is MethodSpec)
684                                                         return new MethodGroupExpr (members, queried_type, loc);
685
686                                                 if (!Invocation.IsMemberInvocable (member))
687                                                         continue;
688                                         }
689
690                                         if (non_method == null || member is MethodSpec || non_method.IsNotCSharpCompatible) {
691                                                 non_method = member;
692                                         } else if (!errorMode && !member.IsNotCSharpCompatible) {
693                                                 ambig_non_method = member;
694                                         }
695                                 }
696
697                                 if (non_method != null) {
698                                         if (ambig_non_method != null && rc != null) {
699                                                 var report = rc.Module.Compiler.Report;
700                                                 report.SymbolRelatedToPreviousError (non_method);
701                                                 report.SymbolRelatedToPreviousError (ambig_non_method);
702                                                 report.Error (229, loc, "Ambiguity between `{0}' and `{1}'",
703                                                         non_method.GetSignatureForError (), ambig_non_method.GetSignatureForError ());
704                                         }
705
706                                         if (non_method is MethodSpec)
707                                                 return new MethodGroupExpr (members, queried_type, loc);
708
709                                         return ExprClassFromMemberInfo (non_method, loc);
710                                 }
711
712                                 if (members[0].DeclaringType.BaseType == null)
713                                         members = null;
714                                 else
715                                         members = MemberCache.FindMembers (members[0].DeclaringType.BaseType, name, false);
716
717                         } while (members != null);
718
719                         return null;
720                 }
721
722                 protected virtual void Error_NegativeArrayIndex (ResolveContext ec, Location loc)
723                 {
724                         throw new NotImplementedException ();
725                 }
726
727                 protected void Error_PointerInsideExpressionTree (ResolveContext ec)
728                 {
729                         ec.Report.Error (1944, loc, "An expression tree cannot contain an unsafe pointer operation");
730                 }
731
732                 /// <summary>
733                 ///   Returns an expression that can be used to invoke operator true
734                 ///   on the expression if it exists.
735                 /// </summary>
736                 protected static Expression GetOperatorTrue (ResolveContext ec, Expression e, Location loc)
737                 {
738                         return GetOperatorTrueOrFalse (ec, e, true, loc);
739                 }
740
741                 /// <summary>
742                 ///   Returns an expression that can be used to invoke operator false
743                 ///   on the expression if it exists.
744                 /// </summary>
745                 protected static Expression GetOperatorFalse (ResolveContext ec, Expression e, Location loc)
746                 {
747                         return GetOperatorTrueOrFalse (ec, e, false, loc);
748                 }
749
750                 static Expression GetOperatorTrueOrFalse (ResolveContext ec, Expression e, bool is_true, Location loc)
751                 {
752                         var op = is_true ? Operator.OpType.True : Operator.OpType.False;
753                         var methods = MemberCache.GetUserOperator (e.type, op, false);
754                         if (methods == null)
755                                 return null;
756
757                         Arguments arguments = new Arguments (1);
758                         arguments.Add (new Argument (e));
759
760                         var res = new OverloadResolver (methods, OverloadResolver.Restrictions.BaseMembersIncluded | OverloadResolver.Restrictions.NoBaseMembers, loc);
761                         var oper = res.ResolveOperator (ec, ref arguments);
762
763                         if (oper == null)
764                                 return null;
765
766                         return new UserOperatorCall (oper, arguments, null, loc);
767                 }
768                 
769                 public virtual string ExprClassName
770                 {
771                         get {
772                                 switch (eclass){
773                                 case ExprClass.Unresolved:
774                                         return "Unresolved";
775                                 case ExprClass.Value:
776                                         return "value";
777                                 case ExprClass.Variable:
778                                         return "variable";
779                                 case ExprClass.Namespace:
780                                         return "namespace";
781                                 case ExprClass.Type:
782                                         return "type";
783                                 case ExprClass.MethodGroup:
784                                         return "method group";
785                                 case ExprClass.PropertyAccess:
786                                         return "property access";
787                                 case ExprClass.EventAccess:
788                                         return "event access";
789                                 case ExprClass.IndexerAccess:
790                                         return "indexer access";
791                                 case ExprClass.Nothing:
792                                         return "null";
793                                 case ExprClass.TypeParameter:
794                                         return "type parameter";
795                                 }
796                                 throw new Exception ("Should not happen");
797                         }
798                 }
799                 
800                 /// <summary>
801                 ///   Reports that we were expecting `expr' to be of class `expected'
802                 /// </summary>
803                 public void Error_UnexpectedKind (Report r, MemberCore mc, string expected, Location loc)
804                 {
805                         Error_UnexpectedKind (r, mc, expected, ExprClassName, loc);
806                 }
807
808                 public void Error_UnexpectedKind (Report r, MemberCore mc, string expected, string was, Location loc)
809                 {
810                         string name;
811                         if (mc != null)
812                                 name = mc.GetSignatureForError ();
813                         else
814                                 name = GetSignatureForError ();
815
816                         r.Error (118, loc, "`{0}' is a `{1}' but a `{2}' was expected",
817                               name, was, expected);
818                 }
819
820                 public void Error_UnexpectedKind (ResolveContext ec, ResolveFlags flags, Location loc)
821                 {
822                         string [] valid = new string [4];
823                         int count = 0;
824
825                         if ((flags & ResolveFlags.VariableOrValue) != 0) {
826                                 valid [count++] = "variable";
827                                 valid [count++] = "value";
828                         }
829
830                         if ((flags & ResolveFlags.Type) != 0)
831                                 valid [count++] = "type";
832
833                         if ((flags & ResolveFlags.MethodGroup) != 0)
834                                 valid [count++] = "method group";
835
836                         if (count == 0)
837                                 valid [count++] = "unknown";
838
839                         StringBuilder sb = new StringBuilder (valid [0]);
840                         for (int i = 1; i < count - 1; i++) {
841                                 sb.Append ("', `");
842                                 sb.Append (valid [i]);
843                         }
844                         if (count > 1) {
845                                 sb.Append ("' or `");
846                                 sb.Append (valid [count - 1]);
847                         }
848
849                         ec.Report.Error (119, loc, 
850                                 "Expression denotes a `{0}', where a `{1}' was expected", ExprClassName, sb.ToString ());
851                 }
852                 
853                 public static void UnsafeError (ResolveContext ec, Location loc)
854                 {
855                         UnsafeError (ec.Report, loc);
856                 }
857
858                 public static void UnsafeError (Report Report, Location loc)
859                 {
860                         Report.Error (214, loc, "Pointers and fixed size buffers may only be used in an unsafe context");
861                 }
862
863                 protected void Error_CannotModifyIntermediateExpressionValue (ResolveContext ec)
864                 {
865                         ec.Report.SymbolRelatedToPreviousError (type);
866                         if (ec.CurrentInitializerVariable != null) {
867                                 ec.Report.Error (1918, loc, "Members of value type `{0}' cannot be assigned using a property `{1}' object initializer",
868                                         TypeManager.CSharpName (type), GetSignatureForError ());
869                         } else {
870                                 ec.Report.Error (1612, loc, "Cannot modify a value type return value of `{0}'. Consider storing the value in a temporary variable",
871                                         GetSignatureForError ());
872                         }
873                 }
874
875                 //
876                 // Converts `source' to an int, uint, long or ulong.
877                 //
878                 protected Expression ConvertExpressionToArrayIndex (ResolveContext ec, Expression source)
879                 {
880                         var btypes = ec.BuiltinTypes;
881
882                         if (source.type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
883                                 Arguments args = new Arguments (1);
884                                 args.Add (new Argument (source));
885                                 return new DynamicConversion (btypes.Int, CSharpBinderFlags.ConvertArrayIndex, args, loc).Resolve (ec);
886                         }
887
888                         Expression converted;
889                         
890                         using (ec.Set (ResolveContext.Options.CheckedScope)) {
891                                 converted = Convert.ImplicitConversion (ec, source, btypes.Int, source.loc);
892                                 if (converted == null)
893                                         converted = Convert.ImplicitConversion (ec, source, btypes.UInt, source.loc);
894                                 if (converted == null)
895                                         converted = Convert.ImplicitConversion (ec, source, btypes.Long, source.loc);
896                                 if (converted == null)
897                                         converted = Convert.ImplicitConversion (ec, source, btypes.ULong, source.loc);
898
899                                 if (converted == null) {
900                                         source.Error_ValueCannotBeConverted (ec, source.loc, btypes.Int, false);
901                                         return null;
902                                 }
903                         }
904
905                         //
906                         // Only positive constants are allowed at compile time
907                         //
908                         Constant c = converted as Constant;
909                         if (c != null && c.IsNegative)
910                                 Error_NegativeArrayIndex (ec, source.loc);
911
912                         // No conversion needed to array index
913                         if (converted.Type.BuiltinType == BuiltinTypeSpec.Type.Int)
914                                 return converted;
915
916                         return new ArrayIndexCast (converted, btypes.Int).Resolve (ec);
917                 }
918
919                 //
920                 // Derived classes implement this method by cloning the fields that
921                 // could become altered during the Resolve stage
922                 //
923                 // Only expressions that are created for the parser need to implement
924                 // this.
925                 //
926                 protected virtual void CloneTo (CloneContext clonectx, Expression target)
927                 {
928                         throw new NotImplementedException (
929                                 String.Format (
930                                         "CloneTo not implemented for expression {0}", this.GetType ()));
931                 }
932
933                 //
934                 // Clones an expression created by the parser.
935                 //
936                 // We only support expressions created by the parser so far, not
937                 // expressions that have been resolved (many more classes would need
938                 // to implement CloneTo).
939                 //
940                 // This infrastructure is here merely for Lambda expressions which
941                 // compile the same code using different type values for the same
942                 // arguments to find the correct overload
943                 //
944                 public virtual Expression Clone (CloneContext clonectx)
945                 {
946                         Expression cloned = (Expression) MemberwiseClone ();
947                         CloneTo (clonectx, cloned);
948
949                         return cloned;
950                 }
951
952                 //
953                 // Implementation of expression to expression tree conversion
954                 //
955                 public abstract Expression CreateExpressionTree (ResolveContext ec);
956
957                 protected Expression CreateExpressionFactoryCall (ResolveContext ec, string name, Arguments args)
958                 {
959                         return CreateExpressionFactoryCall (ec, name, null, args, loc);
960                 }
961
962                 protected Expression CreateExpressionFactoryCall (ResolveContext ec, string name, TypeArguments typeArguments, Arguments args)
963                 {
964                         return CreateExpressionFactoryCall (ec, name, typeArguments, args, loc);
965                 }
966
967                 public static Expression CreateExpressionFactoryCall (ResolveContext ec, string name, TypeArguments typeArguments, Arguments args, Location loc)
968                 {
969                         return new Invocation (new MemberAccess (CreateExpressionTypeExpression (ec, loc), name, typeArguments, loc), args);
970                 }
971
972                 protected static TypeExpr CreateExpressionTypeExpression (ResolveContext ec, Location loc)
973                 {
974                         var t = ec.Module.PredefinedTypes.Expression.Resolve ();
975                         if (t == null)
976                                 return null;
977
978                         return new TypeExpression (t, loc);
979                 }
980
981                 //
982                 // Implemented by all expressions which support conversion from
983                 // compiler expression to invokable runtime expression. Used by
984                 // dynamic C# binder.
985                 //
986                 public virtual SLE.Expression MakeExpression (BuilderContext ctx)
987                 {
988                         throw new NotImplementedException ("MakeExpression for " + GetType ());
989                 }
990         }
991
992         /// <summary>
993         ///   This is just a base class for expressions that can
994         ///   appear on statements (invocations, object creation,
995         ///   assignments, post/pre increment and decrement).  The idea
996         ///   being that they would support an extra Emition interface that
997         ///   does not leave a result on the stack.
998         /// </summary>
999         public abstract class ExpressionStatement : Expression {
1000
1001                 public ExpressionStatement ResolveStatement (BlockContext ec)
1002                 {
1003                         Expression e = Resolve (ec);
1004                         if (e == null)
1005                                 return null;
1006
1007                         ExpressionStatement es = e as ExpressionStatement;
1008                         if (es == null)
1009                                 Error_InvalidExpressionStatement (ec);
1010
1011                         return es;
1012                 }
1013
1014                 /// <summary>
1015                 ///   Requests the expression to be emitted in a `statement'
1016                 ///   context.  This means that no new value is left on the
1017                 ///   stack after invoking this method (constrasted with
1018                 ///   Emit that will always leave a value on the stack).
1019                 /// </summary>
1020                 public abstract void EmitStatement (EmitContext ec);
1021
1022                 public override void EmitSideEffect (EmitContext ec)
1023                 {
1024                         EmitStatement (ec);
1025                 }
1026         }
1027
1028         /// <summary>
1029         ///   This kind of cast is used to encapsulate the child
1030         ///   whose type is child.Type into an expression that is
1031         ///   reported to return "return_type".  This is used to encapsulate
1032         ///   expressions which have compatible types, but need to be dealt
1033         ///   at higher levels with.
1034         ///
1035         ///   For example, a "byte" expression could be encapsulated in one
1036         ///   of these as an "unsigned int".  The type for the expression
1037         ///   would be "unsigned int".
1038         ///
1039         /// </summary>
1040         public abstract class TypeCast : Expression
1041         {
1042                 protected readonly Expression child;
1043
1044                 protected TypeCast (Expression child, TypeSpec return_type)
1045                 {
1046                         eclass = child.eclass;
1047                         loc = child.Location;
1048                         type = return_type;
1049                         this.child = child;
1050                 }
1051
1052                 public Expression Child {
1053                         get {
1054                                 return child;
1055                         }
1056                 }
1057
1058                 public override bool ContainsEmitWithAwait ()
1059                 {
1060                         return child.ContainsEmitWithAwait ();
1061                 }
1062
1063                 public override Expression CreateExpressionTree (ResolveContext ec)
1064                 {
1065                         Arguments args = new Arguments (2);
1066                         args.Add (new Argument (child.CreateExpressionTree (ec)));
1067                         args.Add (new Argument (new TypeOf (type, loc)));
1068
1069                         if (type.IsPointer || child.Type.IsPointer)
1070                                 Error_PointerInsideExpressionTree (ec);
1071
1072                         return CreateExpressionFactoryCall (ec, ec.HasSet (ResolveContext.Options.CheckedScope) ? "ConvertChecked" : "Convert", args);
1073                 }
1074
1075                 protected override Expression DoResolve (ResolveContext ec)
1076                 {
1077                         // This should never be invoked, we are born in fully
1078                         // initialized state.
1079
1080                         return this;
1081                 }
1082
1083                 public override void Emit (EmitContext ec)
1084                 {
1085                         child.Emit (ec);
1086                 }
1087
1088                 public override SLE.Expression MakeExpression (BuilderContext ctx)
1089                 {
1090 #if STATIC
1091                         return base.MakeExpression (ctx);
1092 #else
1093                         return ctx.HasSet (BuilderContext.Options.CheckedScope) ?
1094                                 SLE.Expression.ConvertChecked (child.MakeExpression (ctx), type.GetMetaInfo ()) :
1095                                 SLE.Expression.Convert (child.MakeExpression (ctx), type.GetMetaInfo ());
1096 #endif
1097                 }
1098
1099                 protected override void CloneTo (CloneContext clonectx, Expression t)
1100                 {
1101                         // Nothing to clone
1102                 }
1103
1104                 public override bool IsNull {
1105                         get { return child.IsNull; }
1106                 }
1107         }
1108
1109         public class EmptyCast : TypeCast {
1110                 EmptyCast (Expression child, TypeSpec target_type)
1111                         : base (child, target_type)
1112                 {
1113                 }
1114
1115                 public static Expression Create (Expression child, TypeSpec type)
1116                 {
1117                         Constant c = child as Constant;
1118                         if (c != null)
1119                                 return new EmptyConstantCast (c, type);
1120
1121                         EmptyCast e = child as EmptyCast;
1122                         if (e != null)
1123                                 return new EmptyCast (e.child, type);
1124
1125                         return new EmptyCast (child, type);
1126                 }
1127
1128                 public override void EmitBranchable (EmitContext ec, Label label, bool on_true)
1129                 {
1130                         child.EmitBranchable (ec, label, on_true);
1131                 }
1132
1133                 public override void EmitSideEffect (EmitContext ec)
1134                 {
1135                         child.EmitSideEffect (ec);
1136                 }
1137         }
1138
1139         //
1140         // Used for predefined type user operator (no obsolete check, etc.)
1141         //
1142         public class OperatorCast : TypeCast
1143         {
1144                 readonly MethodSpec conversion_operator;
1145
1146                 public OperatorCast (Expression expr, TypeSpec target_type)
1147                         : this (expr, target_type, target_type, false)
1148                 {
1149                 }
1150                 
1151                 public OperatorCast (Expression expr, TypeSpec target_type, bool find_explicit)
1152                         : this (expr, target_type, target_type, find_explicit)
1153                 {
1154                 }
1155                 
1156                 public OperatorCast (Expression expr, TypeSpec declaringType, TypeSpec returnType, bool isExplicit)
1157                         : base (expr, returnType)
1158                 {
1159                         var op = isExplicit ? Operator.OpType.Explicit : Operator.OpType.Implicit;
1160                         var mi = MemberCache.GetUserOperator (declaringType, op, true);
1161
1162                         if (mi != null) {
1163                                 foreach (MethodSpec oper in mi) {
1164                                         if (oper.ReturnType != returnType)
1165                                                 continue;
1166
1167                                         if (oper.Parameters.Types[0] == expr.Type) {
1168                                                 conversion_operator = oper;
1169                                                 return;
1170                                         }
1171                                 }
1172                         }
1173
1174                         throw new InternalErrorException ("Missing predefined user operator between `{0}' and `{1}'",
1175                                 returnType.GetSignatureForError (), expr.Type.GetSignatureForError ());
1176                 }
1177
1178                 public override void Emit (EmitContext ec)
1179                 {
1180                         child.Emit (ec);
1181                         ec.Emit (OpCodes.Call, conversion_operator);
1182                 }
1183         }
1184         
1185         //
1186         // Constant specialization of EmptyCast.
1187         // We need to special case this since an empty cast of
1188         // a constant is still a constant. 
1189         //
1190         public class EmptyConstantCast : Constant
1191         {
1192                 public readonly Constant child;
1193
1194                 public EmptyConstantCast (Constant child, TypeSpec type)
1195                         : base (child.Location)
1196                 {
1197                         if (child == null)
1198                                 throw new ArgumentNullException ("child");
1199
1200                         this.child = child;
1201                         this.eclass = child.eclass;
1202                         this.type = type;
1203                 }
1204
1205                 public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
1206                 {
1207                         if (child.Type == target_type)
1208                                 return child;
1209
1210                         // FIXME: check that 'type' can be converted to 'target_type' first
1211                         return child.ConvertExplicitly (in_checked_context, target_type);
1212                 }
1213
1214                 public override Expression CreateExpressionTree (ResolveContext ec)
1215                 {
1216                         Arguments args = Arguments.CreateForExpressionTree (ec, null,
1217                                 child.CreateExpressionTree (ec),
1218                                 new TypeOf (type, loc));
1219
1220                         if (type.IsPointer)
1221                                 Error_PointerInsideExpressionTree (ec);
1222
1223                         return CreateExpressionFactoryCall (ec, "Convert", args);
1224                 }
1225
1226                 public override bool IsDefaultValue {
1227                         get { return child.IsDefaultValue; }
1228                 }
1229
1230                 public override bool IsNegative {
1231                         get { return child.IsNegative; }
1232                 }
1233
1234                 public override bool IsNull {
1235                         get { return child.IsNull; }
1236                 }
1237                 
1238                 public override bool IsOneInteger {
1239                         get { return child.IsOneInteger; }
1240                 }
1241
1242                 public override bool IsSideEffectFree {
1243                         get {
1244                                 return child.IsSideEffectFree;
1245                         }
1246                 }
1247
1248                 public override bool IsZeroInteger {
1249                         get { return child.IsZeroInteger; }
1250                 }
1251
1252                 public override void Emit (EmitContext ec)
1253                 {
1254                         child.Emit (ec);                        
1255                 }
1256
1257                 public override void EmitBranchable (EmitContext ec, Label label, bool on_true)
1258                 {
1259                         child.EmitBranchable (ec, label, on_true);
1260
1261                         // Only to make verifier happy
1262                         if (TypeManager.IsGenericParameter (type) && child.IsNull)
1263                                 ec.Emit (OpCodes.Unbox_Any, type);
1264                 }
1265
1266                 public override void EmitSideEffect (EmitContext ec)
1267                 {
1268                         child.EmitSideEffect (ec);
1269                 }
1270
1271                 public override object GetValue ()
1272                 {
1273                         return child.GetValue ();
1274                 }
1275
1276                 public override string GetValueAsLiteral ()
1277                 {
1278                         return child.GetValueAsLiteral ();
1279                 }
1280
1281                 public override long GetValueAsLong ()
1282                 {
1283                         return child.GetValueAsLong ();
1284                 }
1285
1286                 public override Constant ConvertImplicitly (TypeSpec target_type)
1287                 {
1288                         if (type == target_type)
1289                                 return this;
1290
1291                         // FIXME: Do we need to check user conversions?
1292                         if (!Convert.ImplicitStandardConversionExists (this, target_type))
1293                                 return null;
1294
1295                         return child.ConvertImplicitly (target_type);
1296                 }
1297         }
1298
1299         /// <summary>
1300         ///  This class is used to wrap literals which belong inside Enums
1301         /// </summary>
1302         public class EnumConstant : Constant
1303         {
1304                 public Constant Child;
1305
1306                 public EnumConstant (Constant child, TypeSpec enum_type)
1307                         : base (child.Location)
1308                 {
1309                         this.Child = child;
1310
1311                         this.eclass = ExprClass.Value;
1312                         this.type = enum_type;
1313                 }
1314
1315                 protected EnumConstant (Location loc)
1316                         : base (loc)
1317                 {
1318                 }
1319
1320                 public override void Emit (EmitContext ec)
1321                 {
1322                         Child.Emit (ec);
1323                 }
1324
1325                 public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
1326                 {
1327                         Child.EncodeAttributeValue (rc, enc, Child.Type);
1328                 }
1329
1330                 public override void EmitBranchable (EmitContext ec, Label label, bool on_true)
1331                 {
1332                         Child.EmitBranchable (ec, label, on_true);
1333                 }
1334
1335                 public override void EmitSideEffect (EmitContext ec)
1336                 {
1337                         Child.EmitSideEffect (ec);
1338                 }
1339
1340                 public override string GetSignatureForError()
1341                 {
1342                         return TypeManager.CSharpName (Type);
1343                 }
1344
1345                 public override object GetValue ()
1346                 {
1347                         return Child.GetValue ();
1348                 }
1349
1350 #if !STATIC
1351                 public override object GetTypedValue ()
1352                 {
1353                         //
1354                         // The method can be used in dynamic context only (on closed types)
1355                         //
1356                         // System.Enum.ToObject cannot be called on dynamic types
1357                         // EnumBuilder has to be used, but we cannot use EnumBuilder
1358                         // because it does not properly support generics
1359                         //
1360                         return System.Enum.ToObject (type.GetMetaInfo (), Child.GetValue ());
1361                 }
1362 #endif
1363
1364                 public override string GetValueAsLiteral ()
1365                 {
1366                         return Child.GetValueAsLiteral ();
1367                 }
1368
1369                 public override long GetValueAsLong ()
1370                 {
1371                         return Child.GetValueAsLong ();
1372                 }
1373
1374                 public EnumConstant Increment()
1375                 {
1376                         return new EnumConstant (((IntegralConstant) Child).Increment (), type);
1377                 }
1378
1379                 public override bool IsDefaultValue {
1380                         get {
1381                                 return Child.IsDefaultValue;
1382                         }
1383                 }
1384
1385                 public override bool IsSideEffectFree {
1386                         get {
1387                                 return Child.IsSideEffectFree;
1388                         }
1389                 }
1390
1391                 public override bool IsZeroInteger {
1392                         get { return Child.IsZeroInteger; }
1393                 }
1394
1395                 public override bool IsNegative {
1396                         get {
1397                                 return Child.IsNegative;
1398                         }
1399                 }
1400
1401                 public override Constant ConvertExplicitly(bool in_checked_context, TypeSpec target_type)
1402                 {
1403                         if (Child.Type == target_type)
1404                                 return Child;
1405
1406                         return Child.ConvertExplicitly (in_checked_context, target_type);
1407                 }
1408
1409                 public override Constant ConvertImplicitly (TypeSpec type)
1410                 {
1411                         if (this.type == type) {
1412                                 return this;
1413                         }
1414
1415                         if (!Convert.ImplicitStandardConversionExists (this, type)){
1416                                 return null;
1417                         }
1418
1419                         return Child.ConvertImplicitly (type);
1420                 }
1421         }
1422
1423         /// <summary>
1424         ///   This kind of cast is used to encapsulate Value Types in objects.
1425         ///
1426         ///   The effect of it is to box the value type emitted by the previous
1427         ///   operation.
1428         /// </summary>
1429         public class BoxedCast : TypeCast {
1430
1431                 public BoxedCast (Expression expr, TypeSpec target_type)
1432                         : base (expr, target_type)
1433                 {
1434                         eclass = ExprClass.Value;
1435                 }
1436                 
1437                 protected override Expression DoResolve (ResolveContext ec)
1438                 {
1439                         // This should never be invoked, we are born in fully
1440                         // initialized state.
1441
1442                         return this;
1443                 }
1444
1445                 public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
1446                 {
1447                         // Only boxing to object type is supported
1448                         if (targetType.BuiltinType != BuiltinTypeSpec.Type.Object) {
1449                                 base.EncodeAttributeValue (rc, enc, targetType);
1450                                 return;
1451                         }
1452
1453                         enc.Encode (child.Type);
1454                         child.EncodeAttributeValue (rc, enc, child.Type);
1455                 }
1456
1457                 public override void Emit (EmitContext ec)
1458                 {
1459                         base.Emit (ec);
1460                         
1461                         ec.Emit (OpCodes.Box, child.Type);
1462                 }
1463
1464                 public override void EmitSideEffect (EmitContext ec)
1465                 {
1466                         // boxing is side-effectful, since it involves runtime checks, except when boxing to Object or ValueType
1467                         // so, we need to emit the box+pop instructions in most cases
1468                         if (child.Type.IsStruct &&
1469                             (type.BuiltinType == BuiltinTypeSpec.Type.Object || type.BuiltinType == BuiltinTypeSpec.Type.ValueType))
1470                                 child.EmitSideEffect (ec);
1471                         else
1472                                 base.EmitSideEffect (ec);
1473                 }
1474         }
1475
1476         public class UnboxCast : TypeCast {
1477                 public UnboxCast (Expression expr, TypeSpec return_type)
1478                         : base (expr, return_type)
1479                 {
1480                 }
1481
1482                 protected override Expression DoResolve (ResolveContext ec)
1483                 {
1484                         // This should never be invoked, we are born in fully
1485                         // initialized state.
1486
1487                         return this;
1488                 }
1489
1490                 public override void Emit (EmitContext ec)
1491                 {
1492                         base.Emit (ec);
1493
1494                         ec.Emit (OpCodes.Unbox_Any, type);
1495                 }
1496         }
1497         
1498         /// <summary>
1499         ///   This is used to perform explicit numeric conversions.
1500         ///
1501         ///   Explicit numeric conversions might trigger exceptions in a checked
1502         ///   context, so they should generate the conv.ovf opcodes instead of
1503         ///   conv opcodes.
1504         /// </summary>
1505         public class ConvCast : TypeCast {
1506                 public enum Mode : byte {
1507                         I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
1508                         U1_I1, U1_CH,
1509                         I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
1510                         U2_I1, U2_U1, U2_I2, U2_CH,
1511                         I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
1512                         U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
1513                         I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH, I8_I,
1514                         U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH, U8_I,
1515                         CH_I1, CH_U1, CH_I2,
1516                         R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
1517                         R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4,
1518                         I_I8,
1519                 }
1520
1521                 Mode mode;
1522                 
1523                 public ConvCast (Expression child, TypeSpec return_type, Mode m)
1524                         : base (child, return_type)
1525                 {
1526                         mode = m;
1527                 }
1528
1529                 protected override Expression DoResolve (ResolveContext ec)
1530                 {
1531                         // This should never be invoked, we are born in fully
1532                         // initialized state.
1533
1534                         return this;
1535                 }
1536
1537                 public override string ToString ()
1538                 {
1539                         return String.Format ("ConvCast ({0}, {1})", mode, child);
1540                 }
1541                 
1542                 public override void Emit (EmitContext ec)
1543                 {
1544                         base.Emit (ec);
1545
1546                         if (ec.HasSet (EmitContext.Options.CheckedScope)) {
1547                                 switch (mode){
1548                                 case Mode.I1_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
1549                                 case Mode.I1_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1550                                 case Mode.I1_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
1551                                 case Mode.I1_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
1552                                 case Mode.I1_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1553
1554                                 case Mode.U1_I1: ec.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1555                                 case Mode.U1_CH: /* nothing */ break;
1556
1557                                 case Mode.I2_I1: ec.Emit (OpCodes.Conv_Ovf_I1); break;
1558                                 case Mode.I2_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
1559                                 case Mode.I2_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1560                                 case Mode.I2_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
1561                                 case Mode.I2_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
1562                                 case Mode.I2_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1563
1564                                 case Mode.U2_I1: ec.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1565                                 case Mode.U2_U1: ec.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1566                                 case Mode.U2_I2: ec.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1567                                 case Mode.U2_CH: /* nothing */ break;
1568
1569                                 case Mode.I4_I1: ec.Emit (OpCodes.Conv_Ovf_I1); break;
1570                                 case Mode.I4_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
1571                                 case Mode.I4_I2: ec.Emit (OpCodes.Conv_Ovf_I2); break;
1572                                 case Mode.I4_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
1573                                 case Mode.I4_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1574                                 case Mode.I4_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
1575                                 case Mode.I4_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1576
1577                                 case Mode.U4_I1: ec.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1578                                 case Mode.U4_U1: ec.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1579                                 case Mode.U4_I2: ec.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1580                                 case Mode.U4_U2: ec.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1581                                 case Mode.U4_I4: ec.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1582                                 case Mode.U4_CH: ec.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1583
1584                                 case Mode.I8_I1: ec.Emit (OpCodes.Conv_Ovf_I1); break;
1585                                 case Mode.I8_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
1586                                 case Mode.I8_I2: ec.Emit (OpCodes.Conv_Ovf_I2); break;
1587                                 case Mode.I8_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1588                                 case Mode.I8_I4: ec.Emit (OpCodes.Conv_Ovf_I4); break;
1589                                 case Mode.I8_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
1590                                 case Mode.I8_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
1591                                 case Mode.I8_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1592                                 case Mode.I8_I: ec.Emit (OpCodes.Conv_Ovf_U); break;
1593
1594                                 case Mode.U8_I1: ec.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1595                                 case Mode.U8_U1: ec.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1596                                 case Mode.U8_I2: ec.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1597                                 case Mode.U8_U2: ec.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1598                                 case Mode.U8_I4: ec.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1599                                 case Mode.U8_U4: ec.Emit (OpCodes.Conv_Ovf_U4_Un); break;
1600                                 case Mode.U8_I8: ec.Emit (OpCodes.Conv_Ovf_I8_Un); break;
1601                                 case Mode.U8_CH: ec.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1602                                 case Mode.U8_I: ec.Emit (OpCodes.Conv_Ovf_U_Un); break;
1603
1604                                 case Mode.CH_I1: ec.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1605                                 case Mode.CH_U1: ec.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1606                                 case Mode.CH_I2: ec.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1607
1608                                 case Mode.R4_I1: ec.Emit (OpCodes.Conv_Ovf_I1); break;
1609                                 case Mode.R4_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
1610                                 case Mode.R4_I2: ec.Emit (OpCodes.Conv_Ovf_I2); break;
1611                                 case Mode.R4_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1612                                 case Mode.R4_I4: ec.Emit (OpCodes.Conv_Ovf_I4); break;
1613                                 case Mode.R4_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
1614                                 case Mode.R4_I8: ec.Emit (OpCodes.Conv_Ovf_I8); break;
1615                                 case Mode.R4_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
1616                                 case Mode.R4_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1617
1618                                 case Mode.R8_I1: ec.Emit (OpCodes.Conv_Ovf_I1); break;
1619                                 case Mode.R8_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
1620                                 case Mode.R8_I2: ec.Emit (OpCodes.Conv_Ovf_I2); break;
1621                                 case Mode.R8_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1622                                 case Mode.R8_I4: ec.Emit (OpCodes.Conv_Ovf_I4); break;
1623                                 case Mode.R8_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
1624                                 case Mode.R8_I8: ec.Emit (OpCodes.Conv_Ovf_I8); break;
1625                                 case Mode.R8_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
1626                                 case Mode.R8_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1627                                 case Mode.R8_R4: ec.Emit (OpCodes.Conv_R4); break;
1628
1629                                 case Mode.I_I8: ec.Emit (OpCodes.Conv_Ovf_I8_Un); break;
1630                                 }
1631                         } else {
1632                                 switch (mode){
1633                                 case Mode.I1_U1: ec.Emit (OpCodes.Conv_U1); break;
1634                                 case Mode.I1_U2: ec.Emit (OpCodes.Conv_U2); break;
1635                                 case Mode.I1_U4: ec.Emit (OpCodes.Conv_U4); break;
1636                                 case Mode.I1_U8: ec.Emit (OpCodes.Conv_I8); break;
1637                                 case Mode.I1_CH: ec.Emit (OpCodes.Conv_U2); break;
1638
1639                                 case Mode.U1_I1: ec.Emit (OpCodes.Conv_I1); break;
1640                                 case Mode.U1_CH: ec.Emit (OpCodes.Conv_U2); break;
1641
1642                                 case Mode.I2_I1: ec.Emit (OpCodes.Conv_I1); break;
1643                                 case Mode.I2_U1: ec.Emit (OpCodes.Conv_U1); break;
1644                                 case Mode.I2_U2: ec.Emit (OpCodes.Conv_U2); break;
1645                                 case Mode.I2_U4: ec.Emit (OpCodes.Conv_U4); break;
1646                                 case Mode.I2_U8: ec.Emit (OpCodes.Conv_I8); break;
1647                                 case Mode.I2_CH: ec.Emit (OpCodes.Conv_U2); break;
1648
1649                                 case Mode.U2_I1: ec.Emit (OpCodes.Conv_I1); break;
1650                                 case Mode.U2_U1: ec.Emit (OpCodes.Conv_U1); break;
1651                                 case Mode.U2_I2: ec.Emit (OpCodes.Conv_I2); break;
1652                                 case Mode.U2_CH: /* nothing */ break;
1653
1654                                 case Mode.I4_I1: ec.Emit (OpCodes.Conv_I1); break;
1655                                 case Mode.I4_U1: ec.Emit (OpCodes.Conv_U1); break;
1656                                 case Mode.I4_I2: ec.Emit (OpCodes.Conv_I2); break;
1657                                 case Mode.I4_U4: /* nothing */ break;
1658                                 case Mode.I4_U2: ec.Emit (OpCodes.Conv_U2); break;
1659                                 case Mode.I4_U8: ec.Emit (OpCodes.Conv_I8); break;
1660                                 case Mode.I4_CH: ec.Emit (OpCodes.Conv_U2); break;
1661
1662                                 case Mode.U4_I1: ec.Emit (OpCodes.Conv_I1); break;
1663                                 case Mode.U4_U1: ec.Emit (OpCodes.Conv_U1); break;
1664                                 case Mode.U4_I2: ec.Emit (OpCodes.Conv_I2); break;
1665                                 case Mode.U4_U2: ec.Emit (OpCodes.Conv_U2); break;
1666                                 case Mode.U4_I4: /* nothing */ break;
1667                                 case Mode.U4_CH: ec.Emit (OpCodes.Conv_U2); break;
1668
1669                                 case Mode.I8_I1: ec.Emit (OpCodes.Conv_I1); break;
1670                                 case Mode.I8_U1: ec.Emit (OpCodes.Conv_U1); break;
1671                                 case Mode.I8_I2: ec.Emit (OpCodes.Conv_I2); break;
1672                                 case Mode.I8_U2: ec.Emit (OpCodes.Conv_U2); break;
1673                                 case Mode.I8_I4: ec.Emit (OpCodes.Conv_I4); break;
1674                                 case Mode.I8_U4: ec.Emit (OpCodes.Conv_U4); break;
1675                                 case Mode.I8_U8: /* nothing */ break;
1676                                 case Mode.I8_CH: ec.Emit (OpCodes.Conv_U2); break;
1677                                 case Mode.I8_I: ec.Emit (OpCodes.Conv_U); break;
1678
1679                                 case Mode.U8_I1: ec.Emit (OpCodes.Conv_I1); break;
1680                                 case Mode.U8_U1: ec.Emit (OpCodes.Conv_U1); break;
1681                                 case Mode.U8_I2: ec.Emit (OpCodes.Conv_I2); break;
1682                                 case Mode.U8_U2: ec.Emit (OpCodes.Conv_U2); break;
1683                                 case Mode.U8_I4: ec.Emit (OpCodes.Conv_I4); break;
1684                                 case Mode.U8_U4: ec.Emit (OpCodes.Conv_U4); break;
1685                                 case Mode.U8_I8: /* nothing */ break;
1686                                 case Mode.U8_CH: ec.Emit (OpCodes.Conv_U2); break;
1687                                 case Mode.U8_I: ec.Emit (OpCodes.Conv_U); break;
1688
1689                                 case Mode.CH_I1: ec.Emit (OpCodes.Conv_I1); break;
1690                                 case Mode.CH_U1: ec.Emit (OpCodes.Conv_U1); break;
1691                                 case Mode.CH_I2: ec.Emit (OpCodes.Conv_I2); break;
1692
1693                                 case Mode.R4_I1: ec.Emit (OpCodes.Conv_I1); break;
1694                                 case Mode.R4_U1: ec.Emit (OpCodes.Conv_U1); break;
1695                                 case Mode.R4_I2: ec.Emit (OpCodes.Conv_I2); break;
1696                                 case Mode.R4_U2: ec.Emit (OpCodes.Conv_U2); break;
1697                                 case Mode.R4_I4: ec.Emit (OpCodes.Conv_I4); break;
1698                                 case Mode.R4_U4: ec.Emit (OpCodes.Conv_U4); break;
1699                                 case Mode.R4_I8: ec.Emit (OpCodes.Conv_I8); break;
1700                                 case Mode.R4_U8: ec.Emit (OpCodes.Conv_U8); break;
1701                                 case Mode.R4_CH: ec.Emit (OpCodes.Conv_U2); break;
1702
1703                                 case Mode.R8_I1: ec.Emit (OpCodes.Conv_I1); break;
1704                                 case Mode.R8_U1: ec.Emit (OpCodes.Conv_U1); break;
1705                                 case Mode.R8_I2: ec.Emit (OpCodes.Conv_I2); break;
1706                                 case Mode.R8_U2: ec.Emit (OpCodes.Conv_U2); break;
1707                                 case Mode.R8_I4: ec.Emit (OpCodes.Conv_I4); break;
1708                                 case Mode.R8_U4: ec.Emit (OpCodes.Conv_U4); break;
1709                                 case Mode.R8_I8: ec.Emit (OpCodes.Conv_I8); break;
1710                                 case Mode.R8_U8: ec.Emit (OpCodes.Conv_U8); break;
1711                                 case Mode.R8_CH: ec.Emit (OpCodes.Conv_U2); break;
1712                                 case Mode.R8_R4: ec.Emit (OpCodes.Conv_R4); break;
1713
1714                                 case Mode.I_I8: ec.Emit (OpCodes.Conv_U8); break;
1715                                 }
1716                         }
1717                 }
1718         }
1719         
1720         class OpcodeCast : TypeCast
1721         {
1722                 readonly OpCode op;
1723                 
1724                 public OpcodeCast (Expression child, TypeSpec return_type, OpCode op)
1725                         : base (child, return_type)
1726                 {
1727                         this.op = op;
1728                 }
1729
1730                 protected override Expression DoResolve (ResolveContext ec)
1731                 {
1732                         // This should never be invoked, we are born in fully
1733                         // initialized state.
1734
1735                         return this;
1736                 }
1737
1738                 public override void Emit (EmitContext ec)
1739                 {
1740                         base.Emit (ec);
1741                         ec.Emit (op);
1742                 }
1743
1744                 public TypeSpec UnderlyingType {
1745                         get { return child.Type; }
1746                 }
1747         }
1748
1749         //
1750         // Opcode casts expression with 2 opcodes but only
1751         // single expression tree node
1752         //
1753         class OpcodeCastDuplex : OpcodeCast
1754         {
1755                 readonly OpCode second;
1756
1757                 public OpcodeCastDuplex (Expression child, TypeSpec returnType, OpCode first, OpCode second)
1758                         : base (child, returnType, first)
1759                 {
1760                         this.second = second;
1761                 }
1762
1763                 public override void Emit (EmitContext ec)
1764                 {
1765                         base.Emit (ec);
1766                         ec.Emit (second);
1767                 }
1768         }
1769
1770         /// <summary>
1771         ///   This kind of cast is used to encapsulate a child and cast it
1772         ///   to the class requested
1773         /// </summary>
1774         public sealed class ClassCast : TypeCast {
1775                 readonly bool forced;
1776                 
1777                 public ClassCast (Expression child, TypeSpec return_type)
1778                         : base (child, return_type)
1779                 {
1780                 }
1781                 
1782                 public ClassCast (Expression child, TypeSpec return_type, bool forced)
1783                         : base (child, return_type)
1784                 {
1785                         this.forced = forced;
1786                 }
1787
1788                 public override void Emit (EmitContext ec)
1789                 {
1790                         base.Emit (ec);
1791
1792                         bool gen = TypeManager.IsGenericParameter (child.Type);
1793                         if (gen)
1794                                 ec.Emit (OpCodes.Box, child.Type);
1795                         
1796                         if (type.IsGenericParameter) {
1797                                 ec.Emit (OpCodes.Unbox_Any, type);
1798                                 return;
1799                         }
1800                         
1801                         if (gen && !forced)
1802                                 return;
1803                         
1804                         ec.Emit (OpCodes.Castclass, type);
1805                 }
1806         }
1807
1808         //
1809         // Created during resolving pahse when an expression is wrapped or constantified
1810         // and original expression can be used later (e.g. for expression trees)
1811         //
1812         public class ReducedExpression : Expression
1813         {
1814                 sealed class ReducedConstantExpression : EmptyConstantCast
1815                 {
1816                         readonly Expression orig_expr;
1817
1818                         public ReducedConstantExpression (Constant expr, Expression orig_expr)
1819                                 : base (expr, expr.Type)
1820                         {
1821                                 this.orig_expr = orig_expr;
1822                         }
1823
1824                         public override Constant ConvertImplicitly (TypeSpec target_type)
1825                         {
1826                                 Constant c = base.ConvertImplicitly (target_type);
1827                                 if (c != null)
1828                                         c = new ReducedConstantExpression (c, orig_expr);
1829
1830                                 return c;
1831                         }
1832
1833                         public override Expression CreateExpressionTree (ResolveContext ec)
1834                         {
1835                                 return orig_expr.CreateExpressionTree (ec);
1836                         }
1837
1838                         public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
1839                         {
1840                                 Constant c = base.ConvertExplicitly (in_checked_context, target_type);
1841                                 if (c != null)
1842                                         c = new ReducedConstantExpression (c, orig_expr);
1843                                 return c;
1844                         }
1845                 }
1846
1847                 sealed class ReducedExpressionStatement : ExpressionStatement
1848                 {
1849                         readonly Expression orig_expr;
1850                         readonly ExpressionStatement stm;
1851
1852                         public ReducedExpressionStatement (ExpressionStatement stm, Expression orig)
1853                         {
1854                                 this.orig_expr = orig;
1855                                 this.stm = stm;
1856                                 this.eclass = stm.eclass;
1857                                 this.type = stm.Type;
1858
1859                                 this.loc = orig.Location;
1860                         }
1861
1862                         public override Expression CreateExpressionTree (ResolveContext ec)
1863                         {
1864                                 return orig_expr.CreateExpressionTree (ec);
1865                         }
1866
1867                         protected override Expression DoResolve (ResolveContext ec)
1868                         {
1869                                 return this;
1870                         }
1871
1872                         public override void Emit (EmitContext ec)
1873                         {
1874                                 stm.Emit (ec);
1875                         }
1876
1877                         public override void EmitStatement (EmitContext ec)
1878                         {
1879                                 stm.EmitStatement (ec);
1880                         }
1881                 }
1882
1883                 readonly Expression expr, orig_expr;
1884
1885                 private ReducedExpression (Expression expr, Expression orig_expr)
1886                 {
1887                         this.expr = expr;
1888                         this.eclass = expr.eclass;
1889                         this.type = expr.Type;
1890                         this.orig_expr = orig_expr;
1891                         this.loc = orig_expr.Location;
1892                 }
1893
1894                 #region Properties
1895
1896                 public Expression OriginalExpression {
1897                         get {
1898                                 return orig_expr;
1899                         }
1900                 }
1901
1902                 #endregion
1903
1904                 //
1905                 // Creates fully resolved expression switcher
1906                 //
1907                 public static Constant Create (Constant expr, Expression original_expr)
1908                 {
1909                         if (expr.eclass == ExprClass.Unresolved)
1910                                 throw new ArgumentException ("Unresolved expression");
1911
1912                         return new ReducedConstantExpression (expr, original_expr);
1913                 }
1914
1915                 public static ExpressionStatement Create (ExpressionStatement s, Expression orig)
1916                 {
1917                         return new ReducedExpressionStatement (s, orig);
1918                 }
1919
1920                 public static Expression Create (Expression expr, Expression original_expr)
1921                 {
1922                         return Create (expr, original_expr, true);
1923                 }
1924
1925                 //
1926                 // Creates unresolved reduce expression. The original expression has to be
1927                 // already resolved. Created expression is constant based based on `expr'
1928                 // value unless canBeConstant is used
1929                 //
1930                 public static Expression Create (Expression expr, Expression original_expr, bool canBeConstant)
1931                 {
1932                         if (canBeConstant) {
1933                                 Constant c = expr as Constant;
1934                                 if (c != null)
1935                                         return Create (c, original_expr);
1936                         }
1937
1938                         ExpressionStatement s = expr as ExpressionStatement;
1939                         if (s != null)
1940                                 return Create (s, original_expr);
1941
1942                         if (expr.eclass == ExprClass.Unresolved)
1943                                 throw new ArgumentException ("Unresolved expression");
1944
1945                         return new ReducedExpression (expr, original_expr);
1946                 }
1947
1948                 public override Expression CreateExpressionTree (ResolveContext ec)
1949                 {
1950                         return orig_expr.CreateExpressionTree (ec);
1951                 }
1952
1953                 protected override Expression DoResolve (ResolveContext ec)
1954                 {
1955                         return this;
1956                 }
1957
1958                 public override void Emit (EmitContext ec)
1959                 {
1960                         expr.Emit (ec);
1961                 }
1962
1963                 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
1964                 {
1965                         expr.EmitBranchable (ec, target, on_true);
1966                 }
1967
1968                 public override SLE.Expression MakeExpression (BuilderContext ctx)
1969                 {
1970                         return orig_expr.MakeExpression (ctx);
1971                 }
1972         }
1973
1974         //
1975         // Standard composite pattern
1976         //
1977         public abstract class CompositeExpression : Expression
1978         {
1979                 protected Expression expr;
1980
1981                 protected CompositeExpression (Expression expr)
1982                 {
1983                         this.expr = expr;
1984                         this.loc = expr.Location;
1985                 }
1986
1987                 public override Expression CreateExpressionTree (ResolveContext rc)
1988                 {
1989                         return expr.CreateExpressionTree (rc);
1990                 }
1991
1992                 public Expression Child {
1993                         get { return expr; }
1994                 }
1995
1996                 protected override Expression DoResolve (ResolveContext rc)
1997                 {
1998                         expr = expr.Resolve (rc);
1999                         if (expr != null) {
2000                                 type = expr.Type;
2001                                 eclass = expr.eclass;
2002                         }
2003
2004                         return this;
2005                 }
2006
2007                 public override void Emit (EmitContext ec)
2008                 {
2009                         expr.Emit (ec);
2010                 }
2011
2012                 public override bool IsNull {
2013                         get { return expr.IsNull; }
2014                 }
2015         }
2016
2017         //
2018         // Base of expressions used only to narrow resolve flow
2019         //
2020         public abstract class ShimExpression : Expression
2021         {
2022                 protected Expression expr;
2023
2024                 protected ShimExpression (Expression expr)
2025                 {
2026                         this.expr = expr;
2027                 }
2028
2029                 public Expression Expr {
2030                         get {
2031                                 return expr;
2032                         }
2033                 }
2034
2035                 protected override void CloneTo (CloneContext clonectx, Expression t)
2036                 {
2037                         if (expr == null)
2038                                 return;
2039
2040                         ShimExpression target = (ShimExpression) t;
2041                         target.expr = expr.Clone (clonectx);
2042                 }
2043
2044                 public override Expression CreateExpressionTree (ResolveContext ec)
2045                 {
2046                         throw new NotSupportedException ("ET");
2047                 }
2048
2049                 public override void Emit (EmitContext ec)
2050                 {
2051                         throw new InternalErrorException ("Missing Resolve call");
2052                 }
2053
2054         }
2055
2056         //
2057         // Unresolved type name expressions
2058         //
2059         public abstract class ATypeNameExpression : FullNamedExpression
2060         {
2061                 string name;
2062                 protected TypeArguments targs;
2063
2064                 protected ATypeNameExpression (string name, Location l)
2065                 {
2066                         this.name = name;
2067                         loc = l;
2068                 }
2069
2070                 protected ATypeNameExpression (string name, TypeArguments targs, Location l)
2071                 {
2072                         this.name = name;
2073                         this.targs = targs;
2074                         loc = l;
2075                 }
2076
2077                 protected ATypeNameExpression (string name, int arity, Location l)
2078                         : this (name, new UnboundTypeArguments (arity), l)
2079                 {
2080                 }
2081
2082                 #region Properties
2083
2084                 protected int Arity {
2085                         get {
2086                                 return targs == null ? 0 : targs.Count;
2087                         }
2088                 }
2089
2090                 public bool HasTypeArguments {
2091                         get {
2092                                 return targs != null && !targs.IsEmpty;
2093                         }
2094                 }
2095
2096                 public string Name {
2097                         get {
2098                                 return name;
2099                         }
2100                         set {
2101                                 name = value;
2102                         }
2103                 }
2104
2105                 public TypeArguments TypeArguments {
2106                         get {
2107                                 return targs;
2108                         }
2109                 }
2110
2111                 #endregion
2112
2113                 public override bool Equals (object obj)
2114                 {
2115                         ATypeNameExpression atne = obj as ATypeNameExpression;
2116                         return atne != null && atne.Name == Name &&
2117                                 (targs == null || targs.Equals (atne.targs));
2118                 }
2119
2120                 public override int GetHashCode ()
2121                 {
2122                         return Name.GetHashCode ();
2123                 }
2124
2125                 // TODO: Move it to MemberCore
2126                 public static string GetMemberType (MemberCore mc)
2127                 {
2128                         if (mc is Property)
2129                                 return "property";
2130                         if (mc is Indexer)
2131                                 return "indexer";
2132                         if (mc is FieldBase)
2133                                 return "field";
2134                         if (mc is MethodCore)
2135                                 return "method";
2136                         if (mc is EnumMember)
2137                                 return "enum";
2138                         if (mc is Event)
2139                                 return "event";
2140
2141                         return "type";
2142                 }
2143
2144                 public override string GetSignatureForError ()
2145                 {
2146                         if (targs != null) {
2147                                 return Name + "<" + targs.GetSignatureForError () + ">";
2148                         }
2149
2150                         return Name;
2151                 }
2152
2153                 public abstract Expression LookupNameExpression (ResolveContext rc, MemberLookupRestrictions restriction);
2154         }
2155         
2156         /// <summary>
2157         ///   SimpleName expressions are formed of a single word and only happen at the beginning 
2158         ///   of a dotted-name.
2159         /// </summary>
2160         public class SimpleName : ATypeNameExpression
2161         {
2162                 public SimpleName (string name, Location l)
2163                         : base (name, l)
2164                 {
2165                 }
2166
2167                 public SimpleName (string name, TypeArguments args, Location l)
2168                         : base (name, args, l)
2169                 {
2170                 }
2171
2172                 public SimpleName (string name, int arity, Location l)
2173                         : base (name, arity, l)
2174                 {
2175                 }
2176
2177                 public SimpleName GetMethodGroup ()
2178                 {
2179                         return new SimpleName (Name, targs, loc);
2180                 }
2181
2182                 protected override Expression DoResolve (ResolveContext ec)
2183                 {
2184                         return SimpleNameResolve (ec, null, false);
2185                 }
2186
2187                 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
2188                 {
2189                         return SimpleNameResolve (ec, right_side, false);
2190                 }
2191
2192                 protected virtual void Error_TypeOrNamespaceNotFound (IMemberContext ctx)
2193                 {
2194                         if (ctx.CurrentType != null) {
2195                                 if (ctx.CurrentMemberDefinition != null) {
2196                                         MemberCore mc = ctx.CurrentMemberDefinition.Parent.GetDefinition (Name);
2197                                         if (mc != null) {
2198                                                 Error_UnexpectedKind (ctx.Module.Compiler.Report, mc, "type", GetMemberType (mc), loc);
2199                                                 return;
2200                                         }
2201                                 }
2202                         }
2203
2204                         var retval = ctx.LookupNamespaceOrType (Name, Arity, LookupMode.IgnoreAccessibility, loc);
2205                         if (retval != null) {
2206                                 ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (retval.Type);
2207                                 ErrorIsInaccesible (ctx, retval.GetSignatureForError (), loc);
2208                                 return;
2209                         }
2210
2211                         retval = ctx.LookupNamespaceOrType (Name, -System.Math.Max (1, Arity), LookupMode.Probing, loc);
2212                         if (retval != null) {
2213                                 Error_TypeArgumentsCannotBeUsed (ctx, retval.Type, Arity, loc);
2214                                 return;
2215                         }
2216
2217                         NamespaceContainer.Error_NamespaceNotFound (loc, Name, ctx.Module.Compiler.Report);
2218                 }
2219
2220                 public override FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext ec)
2221                 {
2222                         FullNamedExpression fne = ec.LookupNamespaceOrType (Name, Arity, LookupMode.Normal, loc);
2223
2224                         if (fne != null) {
2225                                 if (fne.Type != null && Arity > 0) {
2226                                         if (HasTypeArguments) {
2227                                                 GenericTypeExpr ct = new GenericTypeExpr (fne.Type, targs, loc);
2228                                                 if (ct.ResolveAsType (ec) == null)
2229                                                         return null;
2230
2231                                                 return ct;
2232                                         }
2233
2234                                         return new GenericOpenTypeExpr (fne.Type, loc);
2235                                 }
2236
2237                                 //
2238                                 // dynamic namespace is ignored when dynamic is allowed (does not apply to types)
2239                                 //
2240                                 if (!(fne is Namespace))
2241                                         return fne;
2242                         }
2243
2244                         if (Arity == 0 && Name == "dynamic" && ec.Module.Compiler.Settings.Version > LanguageVersion.V_3) {
2245                                 if (!ec.Module.PredefinedAttributes.Dynamic.IsDefined) {
2246                                         ec.Module.Compiler.Report.Error (1980, Location,
2247                                                 "Dynamic keyword requires `{0}' to be defined. Are you missing System.Core.dll assembly reference?",
2248                                                 ec.Module.PredefinedAttributes.Dynamic.GetSignatureForError ());
2249                                 }
2250
2251                                 fne = new DynamicTypeExpr (loc);
2252                                 fne.ResolveAsType (ec);
2253                         }
2254
2255                         if (fne != null)
2256                                 return fne;
2257
2258                         Error_TypeOrNamespaceNotFound (ec);
2259                         return null;
2260                 }
2261
2262                 public bool IsPossibleTypeOrNamespace (IMemberContext mc)
2263                 {
2264                         return mc.LookupNamespaceOrType (Name, Arity, LookupMode.Probing, loc) != null;
2265                 }
2266
2267                 public override Expression LookupNameExpression (ResolveContext rc, MemberLookupRestrictions restrictions)
2268                 {
2269                         int lookup_arity = Arity;
2270                         bool errorMode = false;
2271                         Expression e;
2272                         Block current_block = rc.CurrentBlock;
2273                         INamedBlockVariable variable = null;
2274                         bool variable_found = false;
2275
2276                         while (true) {
2277                                 //
2278                                 // Stage 1: binding to local variables or parameters
2279                                 //
2280                                 // LAMESPEC: It should take invocableOnly into account but that would break csc compatibility
2281                                 //
2282                                 if (current_block != null && lookup_arity == 0) {
2283                                         if (current_block.ParametersBlock.TopBlock.GetLocalName (Name, current_block.Original, ref variable)) {
2284                                                 if (!variable.IsDeclared) {
2285                                                         // We found local name in accessible block but it's not
2286                                                         // initialized yet, maybe the user wanted to bind to something else
2287                                                         errorMode = true;
2288                                                         variable_found = true;
2289                                                 } else {
2290                                                         e = variable.CreateReferenceExpression (rc, loc);
2291                                                         if (e != null) {
2292                                                                 if (Arity > 0)
2293                                                                         Error_TypeArgumentsCannotBeUsed (rc, "variable", Name, loc);
2294
2295                                                                 return e;
2296                                                         }
2297                                                 }
2298                                         }
2299                                 }
2300
2301                                 //
2302                                 // Stage 2: Lookup members if we are inside a type up to top level type for nested types
2303                                 //
2304                                 TypeSpec member_type = rc.CurrentType;
2305                                 for (; member_type != null; member_type = member_type.DeclaringType) {
2306                                         e = MemberLookup (rc, errorMode, member_type, Name, lookup_arity, restrictions, loc);
2307                                         if (e == null)
2308                                                 continue;
2309
2310                                         var me = e as MemberExpr;
2311                                         if (me == null) {
2312                                                 // The name matches a type, defer to ResolveAsTypeStep
2313                                                 if (e is TypeExpr)
2314                                                         break;
2315
2316                                                 continue;
2317                                         }
2318
2319                                         if (errorMode) {
2320                                                 if (variable != null) {
2321                                                         if (me is FieldExpr || me is ConstantExpr || me is EventExpr || me is PropertyExpr) {
2322                                                                 rc.Report.Error (844, loc,
2323                                                                         "A local variable `{0}' cannot be used before it is declared. Consider renaming the local variable when it hides the member `{1}'",
2324                                                                         Name, me.GetSignatureForError ());
2325                                                         } else {
2326                                                                 break;
2327                                                         }
2328                                                 } else if (me is MethodGroupExpr) {
2329                                                         // Leave it to overload resolution to report correct error
2330                                                 } else {
2331                                                         // TODO: rc.Report.SymbolRelatedToPreviousError ()
2332                                                         ErrorIsInaccesible (rc, me.GetSignatureForError (), loc);
2333                                                 }
2334                                         } else {
2335                                                 // LAMESPEC: again, ignores InvocableOnly
2336                                                 if (variable != null) {
2337                                                         rc.Report.SymbolRelatedToPreviousError (variable.Location, Name);
2338                                                         rc.Report.Error (135, loc, "`{0}' conflicts with a declaration in a child block", Name);
2339                                                 }
2340
2341                                                 //
2342                                                 // MemberLookup does not check accessors availability, this is actually needed for properties only
2343                                                 //
2344                                                 var pe = me as PropertyExpr;
2345                                                 if (pe != null) {
2346
2347                                                         // Break as there is no other overload available anyway
2348                                                         if ((restrictions & MemberLookupRestrictions.ReadAccess) != 0) {
2349                                                                 if (!pe.PropertyInfo.HasGet || !pe.PropertyInfo.Get.IsAccessible (rc))
2350                                                                         break;
2351
2352                                                                 pe.Getter = pe.PropertyInfo.Get;
2353                                                         } else {
2354                                                                 if (!pe.PropertyInfo.HasSet || !pe.PropertyInfo.Set.IsAccessible (rc))
2355                                                                         break;
2356
2357                                                                 pe.Setter = pe.PropertyInfo.Set;
2358                                                         }
2359                                                 }
2360                                         }
2361
2362                                         // TODO: It's used by EventExpr -> FieldExpr transformation only
2363                                         // TODO: Should go to MemberAccess
2364                                         me = me.ResolveMemberAccess (rc, null, null);
2365
2366                                         if (Arity > 0) {
2367                                                 targs.Resolve (rc);
2368                                                 me.SetTypeArguments (rc, targs);
2369                                         }
2370
2371                                         return me;
2372                                 }
2373
2374                                 //
2375                                 // Stage 3: Lookup nested types, namespaces and type parameters in the context
2376                                 //
2377                                 if ((restrictions & MemberLookupRestrictions.InvocableOnly) == 0 && !variable_found) {
2378                                         if (IsPossibleTypeOrNamespace (rc)) {
2379                                                 if (variable != null) {
2380                                                         rc.Report.SymbolRelatedToPreviousError (variable.Location, Name);
2381                                                         rc.Report.Error (135, loc, "`{0}' conflicts with a declaration in a child block", Name);
2382                                                 }
2383
2384                                                 return ResolveAsTypeOrNamespace (rc);
2385                                         }
2386                                 }
2387
2388                                 if (errorMode) {
2389                                         if (variable_found) {
2390                                                 rc.Report.Error (841, loc, "A local variable `{0}' cannot be used before it is declared", Name);
2391                                         } else {
2392                                                 if (Arity > 0) {
2393                                                         TypeParameter[] tparams = rc.CurrentTypeParameters;
2394                                                         if (tparams != null) {
2395                                                                 foreach (var ctp in tparams) {
2396                                                                         if (ctp.Name == Name) {
2397                                                                                 Error_TypeArgumentsCannotBeUsed (rc, "type parameter", Name, loc);
2398                                                                                 return null;
2399                                                                         }
2400                                                                 }
2401                                                         }
2402
2403                                                         var ct = rc.CurrentType;
2404                                                         do {
2405                                                                 if (ct.MemberDefinition.TypeParametersCount > 0) {
2406                                                                         foreach (var ctp in ct.MemberDefinition.TypeParameters) {
2407                                                                                 if (ctp.Name == Name) {
2408                                                                                         Error_TypeArgumentsCannotBeUsed (rc, "type parameter", Name, loc);
2409                                                                                         return null;
2410                                                                                 }
2411                                                                         }
2412                                                                 }
2413
2414                                                                 ct = ct.DeclaringType;
2415                                                         } while (ct != null);
2416                                                 }
2417
2418                                                 if ((restrictions & MemberLookupRestrictions.InvocableOnly) == 0) {
2419                                                         e = rc.LookupNamespaceOrType (Name, Arity, LookupMode.IgnoreAccessibility, loc);
2420                                                         if (e != null) {
2421                                                                 rc.Report.SymbolRelatedToPreviousError (e.Type);
2422                                                                 ErrorIsInaccesible (rc, e.GetSignatureForError (), loc);
2423                                                                 return e;
2424                                                         }
2425                                                 }
2426
2427                                                 e = rc.LookupNamespaceOrType (Name, -System.Math.Max (1, Arity), LookupMode.Probing, loc);
2428                                                 if (e != null) {
2429                                                         if (!(e is TypeExpr) || (restrictions & MemberLookupRestrictions.InvocableOnly) == 0 || !e.Type.IsDelegate) {
2430                                                                 Error_TypeArgumentsCannotBeUsed (rc, e.Type, Arity, loc);
2431                                                                 return e;
2432                                                         }
2433                                                 }
2434
2435                                                 rc.Report.Error (103, loc, "The name `{0}' does not exist in the current context", Name);
2436                                         }
2437
2438                                         return ErrorExpression.Instance;
2439                                 }
2440
2441                                 if (rc.Module.Evaluator != null) {
2442                                         var fi = rc.Module.Evaluator.LookupField (Name);
2443                                         if (fi != null)
2444                                                 return new FieldExpr (fi.Item1, loc);
2445                                 }
2446
2447                                 lookup_arity = 0;
2448                                 errorMode = true;
2449                         }
2450                 }
2451                 
2452                 Expression SimpleNameResolve (ResolveContext ec, Expression right_side, bool intermediate)
2453                 {
2454                         Expression e = LookupNameExpression (ec, right_side == null ? MemberLookupRestrictions.ReadAccess : MemberLookupRestrictions.None);
2455
2456                         if (e == null)
2457                                 return null;
2458
2459                         if (right_side != null) {
2460                                 if (e is TypeExpr) {
2461                                     e.Error_UnexpectedKind (ec, ResolveFlags.VariableOrValue, loc);
2462                                     return null;
2463                                 }
2464
2465                                 e = e.ResolveLValue (ec, right_side);
2466                         } else {
2467                                 e = e.Resolve (ec);
2468                         }
2469
2470                         //if (ec.CurrentBlock == null || ec.CurrentBlock.CheckInvariantMeaningInBlock (Name, e, Location))
2471                         return e;
2472                 }
2473         }
2474
2475         /// <summary>
2476         ///   Represents a namespace or a type.  The name of the class was inspired by
2477         ///   section 10.8.1 (Fully Qualified Names).
2478         /// </summary>
2479         public abstract class FullNamedExpression : Expression
2480         {
2481                 protected override void CloneTo (CloneContext clonectx, Expression target)
2482                 {
2483                         // Do nothing, most unresolved type expressions cannot be
2484                         // resolved to different type
2485                 }
2486
2487                 public override Expression CreateExpressionTree (ResolveContext ec)
2488                 {
2489                         throw new NotSupportedException ("ET");
2490                 }
2491
2492                 public abstract FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext mc);
2493
2494                 //
2495                 // This is used to resolve the expression as a type, a null
2496                 // value will be returned if the expression is not a type
2497                 // reference
2498                 //
2499                 public override TypeSpec ResolveAsType (IMemberContext mc)
2500                 {
2501                         FullNamedExpression fne = ResolveAsTypeOrNamespace (mc);
2502
2503                         if (fne == null)
2504                                 return null;
2505
2506                         TypeExpr te = fne as TypeExpr;
2507                         if (te == null) {
2508                                 fne.Error_UnexpectedKind (mc.Module.Compiler.Report, null, "type", loc);
2509                                 return null;
2510                         }
2511
2512                         te.loc = loc;
2513
2514                         type = te.Type;
2515
2516                         var dep = type.GetMissingDependencies ();
2517                         if (dep != null) {
2518                                 ImportedTypeDefinition.Error_MissingDependency (mc, dep, loc);
2519                         }
2520
2521                         //
2522                         // Obsolete checks cannot be done when resolving base context as they
2523                         // require type dependencies to be set but we are in process of resolving them
2524                         //
2525                         if (!(mc is TypeContainer.BaseContext)) {
2526                                 ObsoleteAttribute obsolete_attr = type.GetAttributeObsolete ();
2527                                 if (obsolete_attr != null && !mc.IsObsolete) {
2528                                         AttributeTester.Report_ObsoleteMessage (obsolete_attr, te.GetSignatureForError (), Location, mc.Module.Compiler.Report);
2529                                 }
2530                         }
2531
2532                         return type;
2533                 }
2534
2535
2536                 public override void Emit (EmitContext ec)
2537                 {
2538                         throw new InternalErrorException ("FullNamedExpression `{0}' found in resolved tree",
2539                                 GetSignatureForError ());
2540                 }
2541         }
2542         
2543         /// <summary>
2544         ///   Expression that evaluates to a type
2545         /// </summary>
2546         public abstract class TypeExpr : FullNamedExpression
2547         {
2548                 public sealed override FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext mc)
2549                 {
2550                         ResolveAsType (mc);
2551                         return this;
2552                 }
2553
2554                 protected sealed override Expression DoResolve (ResolveContext ec)
2555                 {
2556                         ResolveAsType (ec);
2557                         return this;
2558                 }
2559
2560                 public override bool Equals (object obj)
2561                 {
2562                         TypeExpr tobj = obj as TypeExpr;
2563                         if (tobj == null)
2564                                 return false;
2565
2566                         return Type == tobj.Type;
2567                 }
2568
2569                 public override int GetHashCode ()
2570                 {
2571                         return Type.GetHashCode ();
2572                 }
2573         }
2574
2575         /// <summary>
2576         ///   Fully resolved Expression that already evaluated to a type
2577         /// </summary>
2578         public class TypeExpression : TypeExpr
2579         {
2580                 public TypeExpression (TypeSpec t, Location l)
2581                 {
2582                         Type = t;
2583                         eclass = ExprClass.Type;
2584                         loc = l;
2585                 }
2586
2587                 public sealed override TypeSpec ResolveAsType (IMemberContext ec)
2588                 {
2589                         return type;
2590                 }
2591         }
2592
2593         /// <summary>
2594         ///   This class denotes an expression which evaluates to a member
2595         ///   of a struct or a class.
2596         /// </summary>
2597         public abstract class MemberExpr : Expression
2598         {
2599                 //
2600                 // An instance expression associated with this member, if it's a
2601                 // non-static member
2602                 //
2603                 public Expression InstanceExpression;
2604
2605                 /// <summary>
2606                 ///   The name of this member.
2607                 /// </summary>
2608                 public abstract string Name {
2609                         get;
2610                 }
2611
2612                 //
2613                 // When base.member is used
2614                 //
2615                 public bool IsBase {
2616                         get { return InstanceExpression is BaseThis; }
2617                 }
2618
2619                 /// <summary>
2620                 ///   Whether this is an instance member.
2621                 /// </summary>
2622                 public abstract bool IsInstance {
2623                         get;
2624                 }
2625
2626                 /// <summary>
2627                 ///   Whether this is a static member.
2628                 /// </summary>
2629                 public abstract bool IsStatic {
2630                         get;
2631                 }
2632
2633                 protected abstract TypeSpec DeclaringType {
2634                         get;
2635                 }
2636
2637                 //
2638                 // Converts best base candidate for virtual method starting from QueriedBaseType
2639                 //
2640                 protected MethodSpec CandidateToBaseOverride (ResolveContext rc, MethodSpec method)
2641                 {
2642                         //
2643                         // Only when base.member is used and method is virtual
2644                         //
2645                         if (!IsBase)
2646                                 return method;
2647
2648                         //
2649                         // Overload resulution works on virtual or non-virtual members only (no overrides). That
2650                         // means for base.member access we have to find the closest match after we found best candidate
2651                         //
2652                         if ((method.Modifiers & (Modifiers.ABSTRACT | Modifiers.VIRTUAL | Modifiers.OVERRIDE)) != 0) {
2653                                 //
2654                                 // The method could already be what we are looking for
2655                                 //
2656                                 TypeSpec[] targs = null;
2657                                 if (method.DeclaringType != InstanceExpression.Type) {
2658                                         var base_override = MemberCache.FindMember (InstanceExpression.Type, new MemberFilter (method), BindingRestriction.InstanceOnly) as MethodSpec;
2659                                         if (base_override != null && base_override.DeclaringType != method.DeclaringType) {
2660                                                 if (base_override.IsGeneric)
2661                                                         targs = method.TypeArguments;
2662
2663                                                 method = base_override;
2664                                         }
2665                                 }
2666
2667                                 // TODO: For now we do it for any hoisted call even if it's needed for
2668                                 // hoisted stories only but that requires a new expression wrapper
2669                                 if (rc.CurrentAnonymousMethod != null) {
2670                                         if (targs == null && method.IsGeneric) {
2671                                                 targs = method.TypeArguments;
2672                                                 method = method.GetGenericMethodDefinition ();
2673                                         }
2674
2675                                         if (method.Parameters.HasArglist)
2676                                                 throw new NotImplementedException ("__arglist base call proxy");
2677
2678                                         method = rc.CurrentMemberDefinition.Parent.PartialContainer.CreateHoistedBaseCallProxy (rc, method);
2679
2680                                         // Ideally this should apply to any proxy rewrite but in the case of unary mutators on
2681                                         // get/set member expressions second call would fail to proxy because left expression
2682                                         // would be of 'this' and not 'base'
2683                                         if (rc.CurrentType.IsStruct)
2684                                                 InstanceExpression = new This (loc).Resolve (rc);
2685                                 }
2686
2687                                 if (targs != null)
2688                                         method = method.MakeGenericMethod (rc, targs);
2689                         }
2690
2691                         //
2692                         // Only base will allow this invocation to happen.
2693                         //
2694                         if (method.IsAbstract) {
2695                                 Error_CannotCallAbstractBase (rc, method.GetSignatureForError ());
2696                         }
2697
2698                         return method;
2699                 }
2700
2701                 protected void CheckProtectedMemberAccess<T> (ResolveContext rc, T member) where T : MemberSpec
2702                 {
2703                         if (InstanceExpression == null)
2704                                 return;
2705
2706                         if ((member.Modifiers & Modifiers.PROTECTED) != 0 && !(InstanceExpression is This)) {
2707                                 CheckProtectedMemberAccess (rc, member, InstanceExpression.Type, loc);
2708                         }
2709                 }
2710
2711                 public static void CheckProtectedMemberAccess<T> (ResolveContext rc, T member, TypeSpec qualifier, Location loc) where T : MemberSpec
2712                 {
2713                         var ct = rc.CurrentType;
2714                         if (ct == qualifier)
2715                                 return;
2716
2717                         if ((member.Modifiers & Modifiers.INTERNAL) != 0 && member.DeclaringType.MemberDefinition.IsInternalAsPublic (ct.MemberDefinition.DeclaringAssembly))
2718                                 return;
2719
2720                         qualifier = qualifier.GetDefinition ();
2721                         if (ct != qualifier && !IsSameOrBaseQualifier (ct, qualifier)) {
2722                                 rc.Report.SymbolRelatedToPreviousError (member);
2723                                 rc.Report.Error (1540, loc,
2724                                         "Cannot access protected member `{0}' via a qualifier of type `{1}'. The qualifier must be of type `{2}' or derived from it",
2725                                         member.GetSignatureForError (), qualifier.GetSignatureForError (), ct.GetSignatureForError ());
2726                         }
2727                 }
2728
2729                 public override bool ContainsEmitWithAwait ()
2730                 {
2731                         return InstanceExpression != null && InstanceExpression.ContainsEmitWithAwait ();
2732                 }
2733
2734                 static bool IsSameOrBaseQualifier (TypeSpec type, TypeSpec qtype)
2735                 {
2736                         do {
2737                                 type = type.GetDefinition ();
2738
2739                                 if (type == qtype || TypeManager.IsFamilyAccessible (qtype, type))
2740                                         return true;
2741
2742                                 type = type.DeclaringType;
2743                         } while (type != null);
2744
2745                         return false;
2746                 }
2747
2748                 protected void DoBestMemberChecks<T> (ResolveContext rc, T member) where T : MemberSpec, IInterfaceMemberSpec
2749                 {
2750                         if (InstanceExpression != null) {
2751                                 InstanceExpression = InstanceExpression.Resolve (rc);
2752                                 CheckProtectedMemberAccess (rc, member);
2753                         }
2754
2755                         if (member.MemberType.IsPointer && !rc.IsUnsafe) {
2756                                 UnsafeError (rc, loc);
2757                         }
2758
2759                         var dep = member.GetMissingDependencies ();
2760                         if (dep != null) {
2761                                 ImportedTypeDefinition.Error_MissingDependency (rc, dep, loc);
2762                         }
2763
2764                         if (!rc.IsObsolete) {
2765                                 ObsoleteAttribute oa = member.GetAttributeObsolete ();
2766                                 if (oa != null)
2767                                         AttributeTester.Report_ObsoleteMessage (oa, member.GetSignatureForError (), loc, rc.Report);
2768                         }
2769
2770                         if (!(member is FieldSpec))
2771                                 member.MemberDefinition.SetIsUsed ();
2772                 }
2773
2774                 protected virtual void Error_CannotCallAbstractBase (ResolveContext rc, string name)
2775                 {
2776                         rc.Report.Error (205, loc, "Cannot call an abstract base member `{0}'", name);
2777                 }
2778
2779                 //
2780                 // Implements identicial simple name and type-name
2781                 //
2782                 public Expression ProbeIdenticalTypeName (ResolveContext rc, Expression left, SimpleName name)
2783                 {
2784                         var t = left.Type;
2785                         if (t.Kind == MemberKind.InternalCompilerType || t is ElementTypeSpec || t.Arity > 0)
2786                                 return left;
2787
2788                         // In a member access of the form E.I, if E is a single identifier, and if the meaning of E as a simple-name is
2789                         // a constant, field, property, local variable, or parameter with the same type as the meaning of E as a type-name
2790
2791                         if (left is MemberExpr || left is VariableReference) {
2792                                 var identical_type = rc.LookupNamespaceOrType (name.Name, 0, LookupMode.Probing, loc) as TypeExpr;
2793                                 if (identical_type != null && identical_type.Type == left.Type)
2794                                         return identical_type;
2795                         }
2796
2797                         return left;
2798                 }
2799
2800                 public bool ResolveInstanceExpression (ResolveContext rc, Expression rhs)
2801                 {
2802                         if (IsStatic) {
2803                                 if (InstanceExpression != null) {
2804                                         if (InstanceExpression is TypeExpr) {
2805                                                 var t = InstanceExpression.Type;
2806                                                 do {
2807                                                         ObsoleteAttribute oa = t.GetAttributeObsolete ();
2808                                                         if (oa != null && !rc.IsObsolete) {
2809                                                                 AttributeTester.Report_ObsoleteMessage (oa, t.GetSignatureForError (), loc, rc.Report);
2810                                                         }
2811
2812                                                         t = t.DeclaringType;
2813                                                 } while (t != null);
2814                                         } else {
2815                                                 var runtime_expr = InstanceExpression as RuntimeValueExpression;
2816                                                 if (runtime_expr == null || !runtime_expr.IsSuggestionOnly) {
2817                                                         rc.Report.Error (176, loc,
2818                                                                 "Static member `{0}' cannot be accessed with an instance reference, qualify it with a type name instead",
2819                                                                 GetSignatureForError ());
2820                                                 }
2821                                         }
2822
2823                                         InstanceExpression = null;
2824                                 }
2825
2826                                 return false;
2827                         }
2828
2829                         if (InstanceExpression == null || InstanceExpression is TypeExpr) {
2830                                 if (InstanceExpression != null || !This.IsThisAvailable (rc, true)) {
2831                                         if (rc.HasSet (ResolveContext.Options.FieldInitializerScope))
2832                                                 rc.Report.Error (236, loc,
2833                                                         "A field initializer cannot reference the nonstatic field, method, or property `{0}'",
2834                                                         GetSignatureForError ());
2835                                         else
2836                                                 rc.Report.Error (120, loc,
2837                                                         "An object reference is required to access non-static member `{0}'",
2838                                                         GetSignatureForError ());
2839
2840                                         return false;
2841                                 }
2842
2843                                 if (!TypeManager.IsFamilyAccessible (rc.CurrentType, DeclaringType)) {
2844                                         rc.Report.Error (38, loc,
2845                                                 "Cannot access a nonstatic member of outer type `{0}' via nested type `{1}'",
2846                                                 DeclaringType.GetSignatureForError (), rc.CurrentType.GetSignatureForError ());
2847                                 }
2848
2849                                 InstanceExpression = new This (loc);
2850                                 if (this is FieldExpr && rc.CurrentType.IsStruct) {
2851                                         using (rc.Set (ResolveContext.Options.OmitStructFlowAnalysis)) {
2852                                                 InstanceExpression = InstanceExpression.Resolve (rc);
2853                                         }
2854                                 } else {
2855                                         InstanceExpression = InstanceExpression.Resolve (rc);
2856                                 }
2857
2858                                 return false;
2859                         }
2860
2861                         var me = InstanceExpression as MemberExpr;
2862                         if (me != null) {
2863                                 me.ResolveInstanceExpression (rc, rhs);
2864
2865                                 var fe = me as FieldExpr;
2866                                 if (fe != null && fe.IsMarshalByRefAccess (rc)) {
2867                                         rc.Report.SymbolRelatedToPreviousError (me.DeclaringType);
2868                                         rc.Report.Warning (1690, 1, loc,
2869                                                 "Cannot call methods, properties, or indexers on `{0}' because it is a value type member of a marshal-by-reference class",
2870                                                 me.GetSignatureForError ());
2871                                 }
2872
2873                                 return true;
2874                         }
2875
2876                         //
2877                         // Run member-access postponed check once we know that
2878                         // the expression is not field expression which is the only
2879                         // expression which can use uninitialized this
2880                         //
2881                         if (InstanceExpression is This && !(this is FieldExpr) && rc.CurrentType.IsStruct) {
2882                                 ((This)InstanceExpression).CheckStructThisDefiniteAssignment (rc);
2883                         }
2884
2885                         //
2886                         // Additional checks for l-value member access
2887                         //
2888                         if (rhs != null) {
2889                                 //
2890                                 // TODO: It should be recursive but that would break csc compatibility
2891                                 //
2892                                 if (InstanceExpression is UnboxCast) {
2893                                         rc.Report.Error (445, InstanceExpression.Location, "Cannot modify the result of an unboxing conversion");
2894                                 }
2895                         }
2896
2897                         return true;
2898                 }
2899
2900                 public virtual MemberExpr ResolveMemberAccess (ResolveContext ec, Expression left, SimpleName original)
2901                 {
2902                         if (left != null && left.IsNull && TypeSpec.IsReferenceType (left.Type)) {
2903                                 ec.Report.Warning (1720, 1, left.Location,
2904                                         "Expression will always cause a `{0}'", "System.NullReferenceException");
2905                         }
2906
2907                         InstanceExpression = left;
2908                         return this;
2909                 }
2910
2911                 protected void EmitInstance (EmitContext ec, bool prepare_for_load)
2912                 {
2913                         TypeSpec instance_type = InstanceExpression.Type;
2914                         if (TypeSpec.IsValueType (instance_type)) {
2915                                 if (InstanceExpression is IMemoryLocation) {
2916                                         ((IMemoryLocation) InstanceExpression).AddressOf (ec, AddressOp.Load);
2917                                 } else {
2918                                         LocalTemporary t = new LocalTemporary (instance_type);
2919                                         InstanceExpression.Emit (ec);
2920                                         t.Store (ec);
2921                                         t.AddressOf (ec, AddressOp.Store);
2922                                 }
2923                         } else {
2924                                 InstanceExpression.Emit (ec);
2925
2926                                 // Only to make verifier happy
2927                                 if (instance_type.IsGenericParameter && !(InstanceExpression is This) && TypeSpec.IsReferenceType (instance_type))
2928                                         ec.Emit (OpCodes.Box, instance_type);
2929                         }
2930
2931                         if (prepare_for_load)
2932                                 ec.Emit (OpCodes.Dup);
2933                 }
2934
2935                 public abstract void SetTypeArguments (ResolveContext ec, TypeArguments ta);
2936         }
2937
2938         // 
2939         // Represents a group of extension method candidates for whole namespace
2940         // 
2941         class ExtensionMethodGroupExpr : MethodGroupExpr, OverloadResolver.IErrorHandler
2942         {
2943                 NamespaceContainer namespace_entry;
2944                 public readonly Expression ExtensionExpression;
2945
2946                 public ExtensionMethodGroupExpr (IList<MethodSpec> list, NamespaceContainer n, Expression extensionExpr, Location l)
2947                         : base (list.Cast<MemberSpec>().ToList (), extensionExpr.Type, l)
2948                 {
2949                         this.namespace_entry = n;
2950                         this.ExtensionExpression = extensionExpr;
2951                 }
2952
2953                 public override bool IsStatic {
2954                         get { return true; }
2955                 }
2956
2957                 public override IList<MemberSpec> GetBaseMembers (TypeSpec baseType)
2958                 {
2959                         if (namespace_entry == null)
2960                                 return null;
2961
2962                         //
2963                         // For extension methodgroup we are not looking for base members but parent
2964                         // namespace extension methods
2965                         //
2966                         int arity = type_arguments == null ? 0 : type_arguments.Count;
2967                         var found = namespace_entry.LookupExtensionMethod (DeclaringType, Name, arity, ref namespace_entry);
2968                         if (found == null)
2969                                 return null;
2970
2971                         return found.Cast<MemberSpec> ().ToList ();
2972                 }
2973
2974                 public override MethodGroupExpr LookupExtensionMethod (ResolveContext rc)
2975                 {
2976                         // We are already here
2977                         return null;
2978                 }
2979
2980                 public override MethodGroupExpr OverloadResolve (ResolveContext ec, ref Arguments arguments, OverloadResolver.IErrorHandler ehandler, OverloadResolver.Restrictions restr)
2981                 {
2982                         if (arguments == null)
2983                                 arguments = new Arguments (1);
2984
2985                         arguments.Insert (0, new Argument (ExtensionExpression, Argument.AType.ExtensionType));
2986                         var res = base.OverloadResolve (ec, ref arguments, ehandler ?? this, restr);
2987
2988                         // Store resolved argument and restore original arguments
2989                         if (res == null) {
2990                                 // Clean-up modified arguments for error reporting
2991                                 arguments.RemoveAt (0);
2992                                 return null;
2993                         }
2994
2995                         var me = ExtensionExpression as MemberExpr;
2996                         if (me != null)
2997                                 me.ResolveInstanceExpression (ec, null);
2998
2999                         InstanceExpression = null;
3000                         return this;
3001                 }
3002
3003                 #region IErrorHandler Members
3004
3005                 bool OverloadResolver.IErrorHandler.AmbiguousCandidates (ResolveContext rc, MemberSpec best, MemberSpec ambiguous)
3006                 {
3007                         return false;
3008                 }
3009
3010                 bool OverloadResolver.IErrorHandler.ArgumentMismatch (ResolveContext rc, MemberSpec best, Argument arg, int index)
3011                 {
3012                         rc.Report.SymbolRelatedToPreviousError (best);
3013                         rc.Report.Error (1928, loc,
3014                                 "Type `{0}' does not contain a member `{1}' and the best extension method overload `{2}' has some invalid arguments",
3015                                 queried_type.GetSignatureForError (), Name, best.GetSignatureForError ());
3016
3017                         if (index == 0) {
3018                                 rc.Report.Error (1929, loc,
3019                                         "Extension method instance type `{0}' cannot be converted to `{1}'",
3020                                         arg.Type.GetSignatureForError (), ((MethodSpec)best).Parameters.ExtensionMethodType.GetSignatureForError ());
3021                         }
3022
3023                         return true;
3024                 }
3025
3026                 bool OverloadResolver.IErrorHandler.NoArgumentMatch (ResolveContext rc, MemberSpec best)
3027                 {
3028                         return false;
3029                 }
3030
3031                 bool OverloadResolver.IErrorHandler.TypeInferenceFailed (ResolveContext rc, MemberSpec best)
3032                 {
3033                         return false;
3034                 }
3035
3036                 #endregion
3037         }
3038
3039         /// <summary>
3040         ///   MethodGroupExpr represents a group of method candidates which
3041         ///   can be resolved to the best method overload
3042         /// </summary>
3043         public class MethodGroupExpr : MemberExpr, OverloadResolver.IBaseMembersProvider
3044         {
3045                 protected IList<MemberSpec> Methods;
3046                 MethodSpec best_candidate;
3047                 TypeSpec best_candidate_return;
3048                 protected TypeArguments type_arguments;
3049
3050                 SimpleName simple_name;
3051                 protected TypeSpec queried_type;
3052
3053                 public MethodGroupExpr (IList<MemberSpec> mi, TypeSpec type, Location loc)
3054                 {
3055                         Methods = mi;
3056                         this.loc = loc;
3057                         this.type = InternalType.MethodGroup;
3058
3059                         eclass = ExprClass.MethodGroup;
3060                         queried_type = type;
3061                 }
3062
3063                 public MethodGroupExpr (MethodSpec m, TypeSpec type, Location loc)
3064                         : this (new MemberSpec[] { m }, type, loc)
3065                 {
3066                 }
3067
3068                 #region Properties
3069
3070                 public MethodSpec BestCandidate {
3071                         get {
3072                                 return best_candidate;
3073                         }
3074                 }
3075
3076                 public TypeSpec BestCandidateReturnType {
3077                         get {
3078                                 return best_candidate_return;
3079                         }
3080                 }
3081
3082                 public IList<MemberSpec> Candidates {
3083                         get {
3084                                 return Methods;
3085                         }
3086                 }
3087
3088                 protected override TypeSpec DeclaringType {
3089                         get {
3090                                 return queried_type;
3091                         }
3092                 }
3093
3094                 public override bool IsInstance {
3095                         get {
3096                                 if (best_candidate != null)
3097                                         return !best_candidate.IsStatic;
3098
3099                                 return false;
3100                         }
3101                 }
3102
3103                 public override bool IsStatic {
3104                         get {
3105                                 if (best_candidate != null)
3106                                         return best_candidate.IsStatic;
3107
3108                                 return false;
3109                         }
3110                 }
3111
3112                 public override string Name {
3113                         get {
3114                                 if (best_candidate != null)
3115                                         return best_candidate.Name;
3116
3117                                 // TODO: throw ?
3118                                 return Methods.First ().Name;
3119                         }
3120                 }
3121
3122                 #endregion
3123
3124                 //
3125                 // When best candidate is already know this factory can be used
3126                 // to avoid expensive overload resolution to be called
3127                 //
3128                 // NOTE: InstanceExpression has to be set manually
3129                 //
3130                 public static MethodGroupExpr CreatePredefined (MethodSpec best, TypeSpec queriedType, Location loc)
3131                 {
3132                         return new MethodGroupExpr (best, queriedType, loc) {
3133                                 best_candidate = best,
3134                                 best_candidate_return = best.ReturnType
3135                         };
3136                 }
3137
3138                 public override string GetSignatureForError ()
3139                 {
3140                         if (best_candidate != null)
3141                                 return best_candidate.GetSignatureForError ();
3142
3143                         return Methods.First ().GetSignatureForError ();
3144                 }
3145
3146                 public override Expression CreateExpressionTree (ResolveContext ec)
3147                 {
3148                         if (best_candidate == null) {
3149                                 ec.Report.Error (1953, loc, "An expression tree cannot contain an expression with method group");
3150                                 return null;
3151                         }
3152
3153                         if (best_candidate.IsConditionallyExcluded (ec.Module.Compiler, loc))
3154                                 ec.Report.Error (765, loc,
3155                                         "Partial methods with only a defining declaration or removed conditional methods cannot be used in an expression tree");
3156                         
3157                         return new TypeOfMethod (best_candidate, loc);
3158                 }
3159                 
3160                 protected override Expression DoResolve (ResolveContext ec)
3161                 {
3162                         this.eclass = ExprClass.MethodGroup;
3163
3164                         if (InstanceExpression != null) {
3165                                 InstanceExpression = InstanceExpression.Resolve (ec);
3166                                 if (InstanceExpression == null)
3167                                         return null;
3168                         }
3169
3170                         return this;
3171                 }
3172
3173                 public override void Emit (EmitContext ec)
3174                 {
3175                         throw new NotSupportedException ();
3176                 }
3177                 
3178                 public void EmitCall (EmitContext ec, Arguments arguments)
3179                 {
3180                         var call = new CallEmitter ();
3181                         call.InstanceExpression = InstanceExpression;
3182                         call.Emit (ec, best_candidate, arguments, loc);                 
3183                 }
3184
3185                 public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec target, bool expl)
3186                 {
3187                         ec.Report.Error (428, loc, "Cannot convert method group `{0}' to non-delegate type `{1}'. Consider using parentheses to invoke the method",
3188                                 Name, TypeManager.CSharpName (target));
3189                 }
3190
3191                 public static bool IsExtensionMethodArgument (Expression expr)
3192                 {
3193                         //
3194                         // LAMESPEC: No details about which expressions are not allowed
3195                         //
3196                         return !(expr is TypeExpr) && !(expr is BaseThis);
3197                 }
3198
3199                 /// <summary>
3200                 ///   Find the Applicable Function Members (7.4.2.1)
3201                 ///
3202                 ///   me: Method Group expression with the members to select.
3203                 ///       it might contain constructors or methods (or anything
3204                 ///       that maps to a method).
3205                 ///
3206                 ///   Arguments: ArrayList containing resolved Argument objects.
3207                 ///
3208                 ///   loc: The location if we want an error to be reported, or a Null
3209                 ///        location for "probing" purposes.
3210                 ///
3211                 ///   Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
3212                 ///            that is the best match of me on Arguments.
3213                 ///
3214                 /// </summary>
3215                 public virtual MethodGroupExpr OverloadResolve (ResolveContext ec, ref Arguments args, OverloadResolver.IErrorHandler cerrors, OverloadResolver.Restrictions restr)
3216                 {
3217                         // TODO: causes issues with probing mode, remove explicit Kind check
3218                         if (best_candidate != null && best_candidate.Kind == MemberKind.Destructor)
3219                                 return this;
3220
3221                         var r = new OverloadResolver (Methods, type_arguments, restr, loc);
3222                         if ((restr & OverloadResolver.Restrictions.NoBaseMembers) == 0) {
3223                                 r.BaseMembersProvider = this;
3224                         }
3225
3226                         if (cerrors != null)
3227                                 r.CustomErrors = cerrors;
3228
3229                         // TODO: When in probing mode do IsApplicable only and when called again do VerifyArguments for full error reporting
3230                         best_candidate = r.ResolveMember<MethodSpec> (ec, ref args);
3231                         if (best_candidate == null)
3232                                 return r.BestCandidateIsDynamic ? this : null;
3233
3234                         // Overload resolver had to create a new method group, all checks bellow have already been executed
3235                         if (r.BestCandidateNewMethodGroup != null)
3236                                 return r.BestCandidateNewMethodGroup;
3237
3238                         if (best_candidate.Kind == MemberKind.Method && (restr & OverloadResolver.Restrictions.ProbingOnly) == 0) {
3239                                 if (InstanceExpression != null) {
3240                                         if (best_candidate.IsExtensionMethod && args[0].Expr == InstanceExpression) {
3241                                                 InstanceExpression = null;
3242                                         } else {
3243                                                 if (best_candidate.IsStatic && simple_name != null) {
3244                                                         InstanceExpression = ProbeIdenticalTypeName (ec, InstanceExpression, simple_name);
3245                                                 }
3246
3247                                                 InstanceExpression.Resolve (ec);
3248                                         }
3249                                 }
3250
3251                                 ResolveInstanceExpression (ec, null);
3252                                 if (InstanceExpression != null)
3253                                         CheckProtectedMemberAccess (ec, best_candidate);
3254                         }
3255
3256                         var base_override = CandidateToBaseOverride (ec, best_candidate);
3257                         if (base_override == best_candidate) {
3258                                 best_candidate_return = r.BestCandidateReturnType;
3259                         } else {
3260                                 best_candidate = base_override;
3261                                 best_candidate_return = best_candidate.ReturnType;
3262                         }
3263
3264                         return this;
3265                 }
3266
3267                 public override MemberExpr ResolveMemberAccess (ResolveContext ec, Expression left, SimpleName original)
3268                 {
3269                         simple_name = original;
3270                         return base.ResolveMemberAccess (ec, left, original);
3271                 }
3272
3273                 public override void SetTypeArguments (ResolveContext ec, TypeArguments ta)
3274                 {
3275                         type_arguments = ta;
3276                 }
3277
3278                 #region IBaseMembersProvider Members
3279
3280                 public virtual IList<MemberSpec> GetBaseMembers (TypeSpec baseType)
3281                 {
3282                         return baseType == null ? null : MemberCache.FindMembers (baseType, Methods [0].Name, false);
3283                 }
3284
3285                 public IParametersMember GetOverrideMemberParameters (MemberSpec member)
3286                 {
3287                         if (queried_type == member.DeclaringType)
3288                                 return null;
3289
3290                         return MemberCache.FindMember (queried_type, new MemberFilter ((MethodSpec) member),
3291                                 BindingRestriction.InstanceOnly | BindingRestriction.OverrideOnly) as IParametersMember;
3292                 }
3293
3294                 //
3295                 // Extension methods lookup after ordinary methods candidates failed to apply
3296                 //
3297                 public virtual MethodGroupExpr LookupExtensionMethod (ResolveContext rc)
3298                 {
3299                         if (InstanceExpression == null)
3300                                 return null;
3301
3302                         InstanceExpression = InstanceExpression.Resolve (rc);
3303                         if (!IsExtensionMethodArgument (InstanceExpression))
3304                                 return null;
3305
3306                         int arity = type_arguments == null ? 0 : type_arguments.Count;
3307                         NamespaceContainer methods_scope = null;
3308                         var methods = rc.LookupExtensionMethod (InstanceExpression.Type, Methods[0].Name, arity, ref methods_scope);
3309                         if (methods == null)
3310                                 return null;
3311
3312                         var emg = new ExtensionMethodGroupExpr (methods, methods_scope, InstanceExpression, loc);
3313                         emg.SetTypeArguments (rc, type_arguments);
3314                         return emg;
3315                 }
3316
3317                 #endregion
3318         }
3319
3320         public struct OverloadResolver
3321         {
3322                 [Flags]
3323                 public enum Restrictions
3324                 {
3325                         None = 0,
3326                         DelegateInvoke = 1,
3327                         ProbingOnly     = 1 << 1,
3328                         CovariantDelegate = 1 << 2,
3329                         NoBaseMembers = 1 << 3,
3330                         BaseMembersIncluded = 1 << 4
3331                 }
3332
3333                 public interface IBaseMembersProvider
3334                 {
3335                         IList<MemberSpec> GetBaseMembers (TypeSpec baseType);
3336                         IParametersMember GetOverrideMemberParameters (MemberSpec member);
3337                         MethodGroupExpr LookupExtensionMethod (ResolveContext rc);
3338                 }
3339
3340                 public interface IErrorHandler
3341                 {
3342                         bool AmbiguousCandidates (ResolveContext rc, MemberSpec best, MemberSpec ambiguous);
3343                         bool ArgumentMismatch (ResolveContext rc, MemberSpec best, Argument a, int index);
3344                         bool NoArgumentMatch (ResolveContext rc, MemberSpec best);
3345                         bool TypeInferenceFailed (ResolveContext rc, MemberSpec best);
3346                 }
3347
3348                 sealed class NoBaseMembers : IBaseMembersProvider
3349                 {
3350                         public static readonly IBaseMembersProvider Instance = new NoBaseMembers ();
3351
3352                         public IList<MemberSpec> GetBaseMembers (TypeSpec baseType)
3353                         {
3354                                 return null;
3355                         }
3356
3357                         public IParametersMember GetOverrideMemberParameters (MemberSpec member)
3358                         {
3359                                 return null;
3360                         }
3361
3362                         public MethodGroupExpr LookupExtensionMethod (ResolveContext rc)
3363                         {
3364                                 return null;
3365                         }
3366                 }
3367
3368                 struct AmbiguousCandidate
3369                 {
3370                         public readonly MemberSpec Member;
3371                         public readonly bool Expanded;
3372                         public readonly AParametersCollection Parameters;
3373
3374                         public AmbiguousCandidate (MemberSpec member, AParametersCollection parameters, bool expanded)
3375                         {
3376                                 Member = member;
3377                                 Parameters = parameters;
3378                                 Expanded = expanded;
3379                         }
3380                 }
3381
3382                 Location loc;
3383                 IList<MemberSpec> members;
3384                 TypeArguments type_arguments;
3385                 IBaseMembersProvider base_provider;
3386                 IErrorHandler custom_errors;
3387                 Restrictions restrictions;
3388                 MethodGroupExpr best_candidate_extension_group;
3389                 TypeSpec best_candidate_return_type;
3390
3391                 SessionReportPrinter lambda_conv_msgs;
3392                 ReportPrinter prev_recorder;
3393
3394                 public OverloadResolver (IList<MemberSpec> members, Restrictions restrictions, Location loc)
3395                         : this (members, null, restrictions, loc)
3396                 {
3397                 }
3398
3399                 public OverloadResolver (IList<MemberSpec> members, TypeArguments targs, Restrictions restrictions, Location loc)
3400                         : this ()
3401                 {
3402                         if (members == null || members.Count == 0)
3403                                 throw new ArgumentException ("empty members set");
3404
3405                         this.members = members;
3406                         this.loc = loc;
3407                         type_arguments = targs;
3408                         this.restrictions = restrictions;
3409                         if (IsDelegateInvoke)
3410                                 this.restrictions |= Restrictions.NoBaseMembers;
3411
3412                         base_provider = NoBaseMembers.Instance;
3413                 }
3414
3415                 #region Properties
3416
3417                 public IBaseMembersProvider BaseMembersProvider {
3418                         get {
3419                                 return base_provider;
3420                         }
3421                         set {
3422                                 base_provider = value;
3423                         }
3424                 }
3425
3426                 public bool BestCandidateIsDynamic { get; set; }
3427
3428                 //
3429                 // Best candidate was found in newly created MethodGroupExpr, used by extension methods
3430                 //
3431                 public MethodGroupExpr BestCandidateNewMethodGroup {
3432                         get {
3433                                 return best_candidate_extension_group;
3434                         }
3435                 }
3436
3437                 //
3438                 // Return type can be different between best candidate and closest override
3439                 //
3440                 public TypeSpec BestCandidateReturnType {
3441                         get {
3442                                 return best_candidate_return_type;
3443                         }
3444                 }
3445
3446                 public IErrorHandler CustomErrors {
3447                         get {
3448                                 return custom_errors;
3449                         }
3450                         set {
3451                                 custom_errors = value;
3452                         }
3453                 }
3454
3455                 TypeSpec DelegateType {
3456                         get {
3457                                 if ((restrictions & Restrictions.DelegateInvoke) == 0)
3458                                         throw new InternalErrorException ("Not running in delegate mode", loc);
3459
3460                                 return members [0].DeclaringType;
3461                         }
3462                 }
3463
3464                 bool IsProbingOnly {
3465                         get {
3466                                 return (restrictions & Restrictions.ProbingOnly) != 0;
3467                         }
3468                 }
3469
3470                 bool IsDelegateInvoke {
3471                         get {
3472                                 return (restrictions & Restrictions.DelegateInvoke) != 0;
3473                         }
3474                 }
3475
3476                 #endregion
3477
3478                 //
3479                 //  7.4.3.3  Better conversion from expression
3480                 //  Returns :   1    if a->p is better,
3481                 //              2    if a->q is better,
3482                 //              0 if neither is better
3483                 //
3484                 static int BetterExpressionConversion (ResolveContext ec, Argument a, TypeSpec p, TypeSpec q)
3485                 {
3486                         TypeSpec argument_type = a.Type;
3487
3488                         //
3489                         // If argument is an anonymous function
3490                         //
3491                         if (argument_type == InternalType.AnonymousMethod && ec.Module.Compiler.Settings.Version > LanguageVersion.ISO_2) {
3492                                 //
3493                                 // p and q are delegate types or expression tree types
3494                                 //
3495                                 if (p.IsExpressionTreeType || q.IsExpressionTreeType) {
3496                                         if (q.MemberDefinition != p.MemberDefinition) {
3497                                                 return 0;
3498                                         }
3499
3500                                         //
3501                                         // Uwrap delegate from Expression<T>
3502                                         //
3503                                         q = TypeManager.GetTypeArguments (q)[0];
3504                                         p = TypeManager.GetTypeArguments (p)[0];
3505                                 }
3506
3507                                 var p_m = Delegate.GetInvokeMethod (p);
3508                                 var q_m = Delegate.GetInvokeMethod (q);
3509
3510                                 //
3511                                 // With identical parameter lists
3512                                 //
3513                                 if (!TypeSpecComparer.Equals (p_m.Parameters.Types, q_m.Parameters.Types))
3514                                         return 0;
3515
3516                                 p = p_m.ReturnType;
3517                                 q = q_m.ReturnType;
3518
3519                                 //
3520                                 // if p is void returning, and q has a return type Y, then C2 is the better conversion.
3521                                 //
3522                                 if (p.Kind == MemberKind.Void) {
3523                                         return q.Kind != MemberKind.Void ? 2 : 0;
3524                                 }
3525
3526                                 //
3527                                 // if p has a return type Y, and q is void returning, then C1 is the better conversion.
3528                                 //
3529                                 if (q.Kind == MemberKind.Void) {
3530                                         return p.Kind != MemberKind.Void ? 1: 0;
3531                                 }
3532
3533                                 //
3534                                 // When anonymous method is an asynchronous, and P has a return type Task<Y1>, and Q has a return type Task<Y2>
3535                                 // better conversion is performed between underlying types Y1 and Y2
3536                                 //
3537                                 if (p.IsGenericTask || q.IsGenericTask) {
3538                                         if (p.IsGenericTask != q.IsGenericTask) {
3539                                                 return 0;
3540                                         }
3541
3542                                         var async_am = a.Expr as AnonymousMethodExpression;
3543                                         if (async_am == null || !async_am.IsAsync)
3544                                                 return 0;
3545
3546                                         q = q.TypeArguments[0];
3547                                         p = p.TypeArguments[0];
3548                                 }
3549
3550                                 //
3551                                 // The parameters are identicial and return type is not void, use better type conversion
3552                                 // on return type to determine better one
3553                                 //
3554                         } else {
3555                                 if (argument_type == p)
3556                                         return 1;
3557
3558                                 if (argument_type == q)
3559                                         return 2;
3560                         }
3561
3562                         return BetterTypeConversion (ec, p, q);
3563                 }
3564
3565                 //
3566                 // 7.4.3.4  Better conversion from type
3567                 //
3568                 public static int BetterTypeConversion (ResolveContext ec, TypeSpec p, TypeSpec q)
3569                 {
3570                         if (p == null || q == null)
3571                                 throw new InternalErrorException ("BetterTypeConversion got a null conversion");
3572
3573                         switch (p.BuiltinType) {
3574                         case BuiltinTypeSpec.Type.Int:
3575                                 if (q.BuiltinType == BuiltinTypeSpec.Type.UInt || q.BuiltinType == BuiltinTypeSpec.Type.ULong)
3576                                         return 1;
3577                                 break;
3578                         case BuiltinTypeSpec.Type.Long:
3579                                 if (q.BuiltinType == BuiltinTypeSpec.Type.ULong)
3580                                         return 1;
3581                                 break;
3582                         case BuiltinTypeSpec.Type.SByte:
3583                                 switch (q.BuiltinType) {
3584                                 case BuiltinTypeSpec.Type.Byte:
3585                                 case BuiltinTypeSpec.Type.UShort:
3586                                 case BuiltinTypeSpec.Type.UInt:
3587                                 case BuiltinTypeSpec.Type.ULong:
3588                                         return 1;
3589                                 }
3590                                 break;
3591                         case BuiltinTypeSpec.Type.Short:
3592                                 switch (q.BuiltinType) {
3593                                 case BuiltinTypeSpec.Type.UShort:
3594                                 case BuiltinTypeSpec.Type.UInt:
3595                                 case BuiltinTypeSpec.Type.ULong:
3596                                         return 1;
3597                                 }
3598                                 break;
3599                         case BuiltinTypeSpec.Type.Dynamic:
3600                                 // Dynamic is never better
3601                                 return 2;
3602                         }
3603
3604                         switch (q.BuiltinType) {
3605                         case BuiltinTypeSpec.Type.Int:
3606                                 if (p.BuiltinType == BuiltinTypeSpec.Type.UInt || p.BuiltinType == BuiltinTypeSpec.Type.ULong)
3607                                         return 2;
3608                                 break;
3609                         case BuiltinTypeSpec.Type.Long:
3610                                 if (p.BuiltinType == BuiltinTypeSpec.Type.ULong)
3611                                         return 2;
3612                                 break;
3613                         case BuiltinTypeSpec.Type.SByte:
3614                                 switch (p.BuiltinType) {
3615                                 case BuiltinTypeSpec.Type.Byte:
3616                                 case BuiltinTypeSpec.Type.UShort:
3617                                 case BuiltinTypeSpec.Type.UInt:
3618                                 case BuiltinTypeSpec.Type.ULong:
3619                                         return 2;
3620                                 }
3621                                 break;
3622                         case BuiltinTypeSpec.Type.Short:
3623                                 switch (p.BuiltinType) {
3624                                 case BuiltinTypeSpec.Type.UShort:
3625                                 case BuiltinTypeSpec.Type.UInt:
3626                                 case BuiltinTypeSpec.Type.ULong:
3627                                         return 2;
3628                                 }
3629                                 break;
3630                         case BuiltinTypeSpec.Type.Dynamic:
3631                                 // Dynamic is never better
3632                                 return 1;
3633                         }
3634
3635                         // FIXME: handle lifted operators
3636
3637                         // TODO: this is expensive
3638                         Expression p_tmp = new EmptyExpression (p);
3639                         Expression q_tmp = new EmptyExpression (q);
3640
3641                         bool p_to_q = Convert.ImplicitConversionExists (ec, p_tmp, q);
3642                         bool q_to_p = Convert.ImplicitConversionExists (ec, q_tmp, p);
3643
3644                         if (p_to_q && !q_to_p)
3645                                 return 1;
3646
3647                         if (q_to_p && !p_to_q)
3648                                 return 2;
3649
3650                         return 0;
3651                 }
3652
3653                 /// <summary>
3654                 ///   Determines "Better function" between candidate
3655                 ///   and the current best match
3656                 /// </summary>
3657                 /// <remarks>
3658                 ///    Returns a boolean indicating :
3659                 ///     false if candidate ain't better
3660                 ///     true  if candidate is better than the current best match
3661                 /// </remarks>
3662                 static bool BetterFunction (ResolveContext ec, Arguments args, MemberSpec candidate, AParametersCollection cparam, bool candidate_params,
3663                         MemberSpec best, AParametersCollection bparam, bool best_params)
3664                 {
3665                         AParametersCollection candidate_pd = ((IParametersMember) candidate).Parameters;
3666                         AParametersCollection best_pd = ((IParametersMember) best).Parameters;
3667
3668                         bool better_at_least_one = false;
3669                         bool same = true;
3670                         int args_count = args == null ? 0 : args.Count;
3671                         int j = 0;
3672                         Argument a = null;
3673                         TypeSpec ct, bt;
3674                         for (int c_idx = 0, b_idx = 0; j < args_count; ++j, ++c_idx, ++b_idx) {
3675                                 a = args[j];
3676
3677                                 // Default arguments are ignored for better decision
3678                                 if (a.IsDefaultArgument)
3679                                         break;
3680
3681                                 //
3682                                 // When comparing named argument the parameter type index has to be looked up
3683                                 // in original parameter set (override version for virtual members)
3684                                 //
3685                                 NamedArgument na = a as NamedArgument;
3686                                 if (na != null) {
3687                                         int idx = cparam.GetParameterIndexByName (na.Name);
3688                                         ct = candidate_pd.Types[idx];
3689                                         if (candidate_params && candidate_pd.FixedParameters[idx].ModFlags == Parameter.Modifier.PARAMS)
3690                                                 ct = TypeManager.GetElementType (ct);
3691
3692                                         idx = bparam.GetParameterIndexByName (na.Name);
3693                                         bt = best_pd.Types[idx];
3694                                         if (best_params && best_pd.FixedParameters[idx].ModFlags == Parameter.Modifier.PARAMS)
3695                                                 bt = TypeManager.GetElementType (bt);
3696                                 } else {
3697                                         ct = candidate_pd.Types[c_idx];
3698                                         bt = best_pd.Types[b_idx];
3699
3700                                         if (candidate_params && candidate_pd.FixedParameters[c_idx].ModFlags == Parameter.Modifier.PARAMS) {
3701                                                 ct = TypeManager.GetElementType (ct);
3702                                                 --c_idx;
3703                                         }
3704
3705                                         if (best_params && best_pd.FixedParameters[b_idx].ModFlags == Parameter.Modifier.PARAMS) {
3706                                                 bt = TypeManager.GetElementType (bt);
3707                                                 --b_idx;
3708                                         }
3709                                 }
3710
3711                                 if (TypeSpecComparer.IsEqual (ct, bt))
3712                                         continue;
3713
3714                                 same = false;
3715                                 int result = BetterExpressionConversion (ec, a, ct, bt);
3716
3717                                 // for each argument, the conversion to 'ct' should be no worse than 
3718                                 // the conversion to 'bt'.
3719                                 if (result == 2)
3720                                         return false;
3721
3722                                 // for at least one argument, the conversion to 'ct' should be better than 
3723                                 // the conversion to 'bt'.
3724                                 if (result != 0)
3725                                         better_at_least_one = true;
3726                         }
3727
3728                         if (better_at_least_one)
3729                                 return true;
3730
3731                         //
3732                         // This handles the case
3733                         //
3734                         //   Add (float f1, float f2, float f3);
3735                         //   Add (params decimal [] foo);
3736                         //
3737                         // The call Add (3, 4, 5) should be ambiguous.  Without this check, the
3738                         // first candidate would've chosen as better.
3739                         //
3740                         if (!same && !a.IsDefaultArgument)
3741                                 return false;
3742
3743                         //
3744                         // The two methods have equal non-optional parameter types, apply tie-breaking rules
3745                         //
3746
3747                         //
3748                         // This handles the following cases:
3749                         //
3750                         //  Foo (int i) is better than Foo (int i, long l = 0)
3751                         //  Foo (params int[] args) is better than Foo (int i = 0, params int[] args)
3752                         //
3753                         // Prefer non-optional version
3754                         //
3755                         // LAMESPEC: Specification claims this should be done at last but the opposite is true
3756                         //
3757                         if (candidate_params == best_params && candidate_pd.Count != best_pd.Count) {
3758                                 if (candidate_pd.Count >= best_pd.Count)
3759                                         return false;
3760
3761                                 if (j < candidate_pd.Count && candidate_pd.FixedParameters[j].HasDefaultValue)
3762                                         return false;
3763
3764                                 return true;
3765                         }
3766
3767                         //
3768                         // One is a non-generic method and second is a generic method, then non-generic is better
3769                         //
3770                         if (best.IsGeneric != candidate.IsGeneric)
3771                                 return best.IsGeneric;
3772
3773                         //
3774                         // This handles the following cases:
3775                         //
3776                         //   Trim () is better than Trim (params char[] chars)
3777                         //   Concat (string s1, string s2, string s3) is better than
3778                         //     Concat (string s1, params string [] srest)
3779                         //   Foo (int, params int [] rest) is better than Foo (params int [] rest)
3780                         //
3781                         // Prefer non-expanded version
3782                         //
3783                         if (candidate_params != best_params)
3784                                 return best_params;
3785
3786                         int candidate_param_count = candidate_pd.Count;
3787                         int best_param_count = best_pd.Count;
3788
3789                         if (candidate_param_count != best_param_count)
3790                                 // can only happen if (candidate_params && best_params)
3791                                 return candidate_param_count > best_param_count && best_pd.HasParams;
3792
3793                         //
3794                         // Both methods have the same number of parameters, and the parameters have equal types
3795                         // Pick the "more specific" signature using rules over original (non-inflated) types
3796                         //
3797                         var candidate_def_pd = ((IParametersMember) candidate.MemberDefinition).Parameters;
3798                         var best_def_pd = ((IParametersMember) best.MemberDefinition).Parameters;
3799
3800                         bool specific_at_least_once = false;
3801                         for (j = 0; j < args_count; ++j) {
3802                                 NamedArgument na = args_count == 0 ? null : args [j] as NamedArgument;
3803                                 if (na != null) {
3804                                         ct = candidate_def_pd.Types[cparam.GetParameterIndexByName (na.Name)];
3805                                         bt = best_def_pd.Types[bparam.GetParameterIndexByName (na.Name)];
3806                                 } else {
3807                                         ct = candidate_def_pd.Types[j];
3808                                         bt = best_def_pd.Types[j];
3809                                 }
3810
3811                                 if (ct == bt)
3812                                         continue;
3813                                 TypeSpec specific = MoreSpecific (ct, bt);
3814                                 if (specific == bt)
3815                                         return false;
3816                                 if (specific == ct)
3817                                         specific_at_least_once = true;
3818                         }
3819
3820                         if (specific_at_least_once)
3821                                 return true;
3822
3823                         return false;
3824                 }
3825
3826                 public static void Error_ConstructorMismatch (ResolveContext rc, TypeSpec type, int argCount, Location loc)
3827                 {
3828                         rc.Report.Error (1729, loc,
3829                                 "The type `{0}' does not contain a constructor that takes `{1}' arguments",
3830                                 type.GetSignatureForError (), argCount.ToString ());
3831                 }
3832
3833                 //
3834                 // Determines if the candidate method is applicable to the given set of arguments
3835                 // There could be two different set of parameters for same candidate where one
3836                 // is the closest override for default values and named arguments checks and second
3837                 // one being the virtual base for the parameter types and modifiers.
3838                 //
3839                 // A return value rates candidate method compatibility,
3840                 // 0 = the best, int.MaxValue = the worst
3841                 //
3842                 int IsApplicable (ResolveContext ec, ref Arguments arguments, int arg_count, ref MemberSpec candidate, IParametersMember pm, ref bool params_expanded_form, ref bool dynamicArgument, ref TypeSpec returnType)
3843                 {
3844                         // Parameters of most-derived type used mainly for named and optional parameters
3845                         var pd = pm.Parameters;
3846
3847                         // Used for params modifier only, that's legacy of C# 1.0 which uses base type for
3848                         // params modifier instead of most-derived type
3849                         var cpd = ((IParametersMember) candidate).Parameters;
3850                         int param_count = pd.Count;
3851                         int optional_count = 0;
3852                         int score;
3853                         Arguments orig_args = arguments;
3854
3855                         if (arg_count != param_count) {
3856                                 for (int i = 0; i < pd.Count; ++i) {
3857                                         if (pd.FixedParameters[i].HasDefaultValue) {
3858                                                 optional_count = pd.Count - i;
3859                                                 break;
3860                                         }
3861                                 }
3862
3863                                 if (optional_count != 0) {
3864                                         // Readjust expected number when params used
3865                                         if (cpd.HasParams) {
3866                                                 optional_count--;
3867                                                 if (arg_count < param_count)
3868                                                         param_count--;
3869                                         } else if (arg_count > param_count) {
3870                                                 int args_gap = System.Math.Abs (arg_count - param_count);
3871                                                 return int.MaxValue - 10000 + args_gap;
3872                                         }
3873                                 } else if (arg_count != param_count) {
3874                                         int args_gap = System.Math.Abs (arg_count - param_count);
3875                                         if (!cpd.HasParams)
3876                                                 return int.MaxValue - 10000 + args_gap;
3877                                         if (arg_count < param_count - 1)
3878                                                 return int.MaxValue - 10000 + args_gap;
3879                                 }
3880
3881                                 // Resize to fit optional arguments
3882                                 if (optional_count != 0) {
3883                                         if (arguments == null) {
3884                                                 arguments = new Arguments (optional_count);
3885                                         } else {
3886                                                 // Have to create a new container, so the next run can do same
3887                                                 var resized = new Arguments (param_count);
3888                                                 resized.AddRange (arguments);
3889                                                 arguments = resized;
3890                                         }
3891
3892                                         for (int i = arg_count; i < param_count; ++i)
3893                                                 arguments.Add (null);
3894                                 }
3895                         }
3896
3897                         if (arg_count > 0) {
3898                                 //
3899                                 // Shuffle named arguments to the right positions if there are any
3900                                 //
3901                                 if (arguments[arg_count - 1] is NamedArgument) {
3902                                         arg_count = arguments.Count;
3903
3904                                         for (int i = 0; i < arg_count; ++i) {
3905                                                 bool arg_moved = false;
3906                                                 while (true) {
3907                                                         NamedArgument na = arguments[i] as NamedArgument;
3908                                                         if (na == null)
3909                                                                 break;
3910
3911                                                         int index = pd.GetParameterIndexByName (na.Name);
3912
3913                                                         // Named parameter not found
3914                                                         if (index < 0)
3915                                                                 return (i + 1) * 3;
3916
3917                                                         // already reordered
3918                                                         if (index == i)
3919                                                                 break;
3920
3921                                                         Argument temp;
3922                                                         if (index >= param_count) {
3923                                                                 // When using parameters which should not be available to the user
3924                                                                 if ((cpd.FixedParameters[index].ModFlags & Parameter.Modifier.PARAMS) == 0)
3925                                                                         break;
3926
3927                                                                 arguments.Add (null);
3928                                                                 ++arg_count;
3929                                                                 temp = null;
3930                                                         } else {
3931                                                                 temp = arguments[index];
3932
3933                                                                 // The slot has been taken by positional argument
3934                                                                 if (temp != null && !(temp is NamedArgument))
3935                                                                         break;
3936                                                         }
3937
3938                                                         if (!arg_moved) {
3939                                                                 arguments = arguments.MarkOrderedArgument (na);
3940                                                                 arg_moved = true;
3941                                                         }
3942
3943                                                         arguments[index] = arguments[i];
3944                                                         arguments[i] = temp;
3945
3946                                                         if (temp == null)
3947                                                                 break;
3948                                                 }
3949                                         }
3950                                 } else {
3951                                         arg_count = arguments.Count;
3952                                 }
3953                         } else if (arguments != null) {
3954                                 arg_count = arguments.Count;
3955                         }
3956
3957                         //
3958                         // 1. Handle generic method using type arguments when specified or type inference
3959                         //
3960                         TypeSpec[] ptypes;
3961                         var ms = candidate as MethodSpec;
3962                         if (ms != null && ms.IsGeneric) {
3963                                 // Setup constraint checker for probing only
3964                                 ConstraintChecker cc = new ConstraintChecker (null);
3965
3966                                 if (type_arguments != null) {
3967                                         var g_args_count = ms.Arity;
3968                                         if (g_args_count != type_arguments.Count)
3969                                                 return int.MaxValue - 20000 + System.Math.Abs (type_arguments.Count - g_args_count);
3970
3971                                         ms = ms.MakeGenericMethod (ec, type_arguments.Arguments);
3972                                 } else {
3973                                         // TODO: It should not be here (we don't know yet whether any argument is lambda) but
3974                                         // for now it simplifies things. I should probably add a callback to ResolveContext
3975                                         if (lambda_conv_msgs == null) {
3976                                                 lambda_conv_msgs = new SessionReportPrinter ();
3977                                                 prev_recorder = ec.Report.SetPrinter (lambda_conv_msgs);
3978                                         }
3979
3980                                         var ti = new TypeInference (arguments);
3981                                         TypeSpec[] i_args = ti.InferMethodArguments (ec, ms);
3982                                         lambda_conv_msgs.EndSession ();
3983
3984                                         if (i_args == null)
3985                                                 return ti.InferenceScore - 20000;
3986
3987                                         if (i_args.Length != 0) {
3988                                                 ms = ms.MakeGenericMethod (ec, i_args);
3989                                         }
3990
3991                                         cc.IgnoreInferredDynamic = true;
3992                                 }
3993
3994                                 //
3995                                 // Type arguments constraints have to match for the method to be applicable
3996                                 //
3997                                 if (!cc.CheckAll (ms.GetGenericMethodDefinition (), ms.TypeArguments, ms.Constraints, loc)) {
3998                                         candidate = ms;
3999                                         return int.MaxValue - 25000;
4000                                 }
4001
4002                                 //
4003                                 // We have a generic return type and at same time the method is override which
4004                                 // means we have to also inflate override return type in case the candidate is
4005                                 // best candidate and override return type is different to base return type.
4006                                 // 
4007                                 // virtual Foo<T, object> with override Foo<T, dynamic>
4008                                 //
4009                                 if (candidate != pm) {
4010                                         MethodSpec override_ms = (MethodSpec) pm;
4011                                         var inflator = new TypeParameterInflator (ec, ms.DeclaringType, override_ms.GenericDefinition.TypeParameters, ms.TypeArguments);
4012                                         returnType = inflator.Inflate (returnType);
4013                                 } else {
4014                                         returnType = ms.ReturnType;
4015                                 }
4016
4017                                 candidate = ms;
4018                                 ptypes = ms.Parameters.Types;
4019                         } else {
4020                                 if (type_arguments != null)
4021                                         return int.MaxValue - 15000;
4022
4023                                 ptypes = cpd.Types;
4024                         }
4025
4026                         //
4027                         // 2. Each argument has to be implicitly convertible to method parameter
4028                         //
4029                         Parameter.Modifier p_mod = 0;
4030                         TypeSpec pt = null;
4031
4032                         for (int i = 0; i < arg_count; i++) {
4033                                 Argument a = arguments[i];
4034                                 if (a == null) {
4035                                         if (!pd.FixedParameters[i].HasDefaultValue) {
4036                                                 arguments = orig_args;
4037                                                 return arg_count * 2 + 2;
4038                                         }
4039
4040                                         //
4041                                         // Get the default value expression, we can use the same expression
4042                                         // if the type matches
4043                                         //
4044                                         Expression e = pd.FixedParameters[i].DefaultValue;
4045                                         if (!(e is Constant) || e.Type.IsGenericOrParentIsGeneric) {
4046                                                 //
4047                                                 // LAMESPEC: No idea what the exact rules are for System.Reflection.Missing.Value instead of null
4048                                                 //
4049                                                 if (e == EmptyExpression.MissingValue && ptypes[i].BuiltinType == BuiltinTypeSpec.Type.Object || ptypes[i].BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
4050                                                         e = new MemberAccess (new MemberAccess (new MemberAccess (
4051                                                                 new QualifiedAliasMember (QualifiedAliasMember.GlobalAlias, "System", loc), "Reflection", loc), "Missing", loc), "Value", loc);
4052                                                 } else {
4053                                                         e = new DefaultValueExpression (new TypeExpression (ptypes [i], loc), loc);
4054                                                 }
4055
4056                                                 e = e.Resolve (ec);
4057                                         }
4058
4059                                         arguments[i] = new Argument (e, Argument.AType.Default);
4060                                         continue;
4061                                 }
4062
4063                                 if (p_mod != Parameter.Modifier.PARAMS) {
4064                                         p_mod = (pd.FixedParameters[i].ModFlags & ~Parameter.Modifier.PARAMS) | (cpd.FixedParameters[i].ModFlags & Parameter.Modifier.PARAMS);
4065                                         pt = ptypes [i];
4066                                 } else if (!params_expanded_form) {
4067                                         params_expanded_form = true;
4068                                         pt = ((ElementTypeSpec) pt).Element;
4069                                         i -= 2;
4070                                         continue;
4071                                 }
4072
4073                                 score = 1;
4074                                 if (!params_expanded_form) {
4075                                         if (a.ArgType == Argument.AType.ExtensionType) {
4076                                                 //
4077                                                 // Indentity, implicit reference or boxing conversion must exist for the extension parameter
4078                                                 //
4079                                                 var at = a.Type;
4080                                                 if (at == pt || TypeSpecComparer.IsEqual (at, pt) ||
4081                                                         Convert.ImplicitReferenceConversionExists (at, pt) ||
4082                                                         Convert.ImplicitBoxingConversion (null, at, pt) != null) {
4083                                                         score = 0;
4084                                                         continue;
4085                                                 }
4086                                         } else {
4087                                                 score = IsArgumentCompatible (ec, a, p_mod & ~Parameter.Modifier.PARAMS, pt);
4088
4089                                                 if (score < 0)
4090                                                         dynamicArgument = true;
4091                                         }
4092                                 }
4093
4094                                 //
4095                                 // It can be applicable in expanded form (when not doing exact match like for delegates)
4096                                 //
4097                                 if (score != 0 && (p_mod & Parameter.Modifier.PARAMS) != 0 && (restrictions & Restrictions.CovariantDelegate) == 0) {
4098                                         if (!params_expanded_form)
4099                                                 pt = ((ElementTypeSpec) pt).Element;
4100
4101                                         if (score > 0)
4102                                                 score = IsArgumentCompatible (ec, a, Parameter.Modifier.NONE, pt);
4103
4104                                         if (score == 0) {
4105                                                 params_expanded_form = true;
4106                                         } else if (score < 0) {
4107                                                 params_expanded_form = true;
4108                                                 dynamicArgument = true;
4109                                         }
4110                                 }
4111
4112                                 if (score > 0) {
4113                                         if (params_expanded_form)
4114                                                 ++score;
4115                                         return (arg_count - i) * 2 + score;
4116                                 }
4117                         }
4118
4119                         //
4120                         // When params parameter has no argument it will be provided later if the method is the best candidate
4121                         //
4122                         if (arg_count + 1 == pd.Count && (cpd.FixedParameters [arg_count].ModFlags & Parameter.Modifier.PARAMS) != 0)
4123                                 params_expanded_form = true;
4124
4125                         //
4126                         // Restore original arguments for dynamic binder to keep the intention of original source code
4127                         //
4128                         if (dynamicArgument)
4129                                 arguments = orig_args;
4130
4131                         return 0;
4132                 }
4133
4134                 //
4135                 // Tests argument compatibility with the parameter
4136                 // The possible return values are
4137                 // 0 - success
4138                 // 1 - modifier mismatch
4139                 // 2 - type mismatch
4140                 // -1 - dynamic binding required
4141                 //
4142                 int IsArgumentCompatible (ResolveContext ec, Argument argument, Parameter.Modifier param_mod, TypeSpec parameter)
4143                 {
4144                         //
4145                         // Types have to be identical when ref or out modifer
4146                         // is used and argument is not of dynamic type
4147                         //
4148                         if ((argument.Modifier | param_mod) != 0) {
4149                                 if (argument.Type != parameter) {
4150                                         //
4151                                         // Do full equality check after quick path
4152                                         //
4153                                         if (!TypeSpecComparer.IsEqual (argument.Type, parameter)) {
4154                                                 //
4155                                                 // Using dynamic for ref/out parameter can still succeed at runtime
4156                                                 //
4157                                                 if (argument.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic && argument.Modifier == 0 && (restrictions & Restrictions.CovariantDelegate) == 0)
4158                                                         return -1;
4159
4160                                                 return 2;
4161                                         }
4162                                 }
4163
4164                                 if (argument.Modifier != param_mod) {
4165                                         //
4166                                         // Using dynamic for ref/out parameter can still succeed at runtime
4167                                         //
4168                                         if (argument.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic && argument.Modifier == 0 && (restrictions & Restrictions.CovariantDelegate) == 0)
4169                                                 return -1;
4170
4171                                         return 1;
4172                                 }
4173
4174                         } else {
4175                                 if (argument.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic && (restrictions & Restrictions.CovariantDelegate) == 0)
4176                                         return -1;
4177
4178                                 //
4179                                 // Deploy custom error reporting for lambda methods. When probing lambda methods
4180                                 // keep all errors reported in separate set and once we are done and no best
4181                                 // candidate was found, this set is used to report more details about what was wrong
4182                                 // with lambda body
4183                                 //
4184                                 if (argument.Expr.Type == InternalType.AnonymousMethod) {
4185                                         if (lambda_conv_msgs == null) {
4186                                                 lambda_conv_msgs = new SessionReportPrinter ();
4187                                                 prev_recorder = ec.Report.SetPrinter (lambda_conv_msgs);
4188                                         }
4189                                 }
4190
4191                                 //
4192                                 // Use implicit conversion in all modes to return same candidates when the expression
4193                                 // is used as argument or delegate conversion
4194                                 //
4195                                 if (!Convert.ImplicitConversionExists (ec, argument.Expr, parameter)) {
4196                                         if (lambda_conv_msgs != null) {
4197                                                 lambda_conv_msgs.EndSession ();
4198                                         }
4199
4200                                         return 2;
4201                                 }
4202                         }
4203
4204                         return 0;
4205                 }
4206
4207                 static TypeSpec MoreSpecific (TypeSpec p, TypeSpec q)
4208                 {
4209                         if (TypeManager.IsGenericParameter (p) && !TypeManager.IsGenericParameter (q))
4210                                 return q;
4211                         if (!TypeManager.IsGenericParameter (p) && TypeManager.IsGenericParameter (q))
4212                                 return p;
4213
4214                         var ac_p = p as ArrayContainer;
4215                         if (ac_p != null) {
4216                                 var ac_q = ((ArrayContainer) q);
4217                                 TypeSpec specific = MoreSpecific (ac_p.Element, ac_q.Element);
4218                                 if (specific == ac_p.Element)
4219                                         return p;
4220                                 if (specific == ac_q.Element)
4221                                         return q;
4222                         } else if (TypeManager.IsGenericType (p)) {
4223                                 var pargs = TypeManager.GetTypeArguments (p);
4224                                 var qargs = TypeManager.GetTypeArguments (q);
4225
4226                                 bool p_specific_at_least_once = false;
4227                                 bool q_specific_at_least_once = false;
4228
4229                                 for (int i = 0; i < pargs.Length; i++) {
4230                                         TypeSpec specific = MoreSpecific (pargs[i], qargs[i]);
4231                                         if (specific == pargs[i])
4232                                                 p_specific_at_least_once = true;
4233                                         if (specific == qargs[i])
4234                                                 q_specific_at_least_once = true;
4235                                 }
4236
4237                                 if (p_specific_at_least_once && !q_specific_at_least_once)
4238                                         return p;
4239                                 if (!p_specific_at_least_once && q_specific_at_least_once)
4240                                         return q;
4241                         }
4242
4243                         return null;
4244                 }
4245
4246                 //
4247                 // Find the best method from candidate list
4248                 //
4249                 public T ResolveMember<T> (ResolveContext rc, ref Arguments args) where T : MemberSpec, IParametersMember
4250                 {
4251                         List<AmbiguousCandidate> ambiguous_candidates = null;
4252
4253                         MemberSpec best_candidate;
4254                         Arguments best_candidate_args = null;
4255                         bool best_candidate_params = false;
4256                         bool best_candidate_dynamic = false;
4257                         int best_candidate_rate;
4258                         IParametersMember best_parameter_member = null;
4259
4260                         int args_count = args != null ? args.Count : 0;
4261
4262                         Arguments candidate_args = args;
4263                         bool error_mode = false;
4264                         MemberSpec invocable_member = null;
4265
4266                         // Be careful, cannot return until error reporter is restored
4267                         while (true) {
4268                                 best_candidate = null;
4269                                 best_candidate_rate = int.MaxValue;
4270
4271                                 var type_members = members;
4272                                 try {
4273
4274                                         do {
4275                                                 for (int i = 0; i < type_members.Count; ++i) {
4276                                                         var member = type_members[i];
4277
4278                                                         //
4279                                                         // Methods in a base class are not candidates if any method in a derived
4280                                                         // class is applicable
4281                                                         //
4282                                                         if ((member.Modifiers & Modifiers.OVERRIDE) != 0)
4283                                                                 continue;
4284
4285                                                         if (!error_mode) {
4286                                                                 if (!member.IsAccessible (rc))
4287                                                                         continue;
4288
4289                                                                 if (rc.IsRuntimeBinder && !member.DeclaringType.IsAccessible (rc))
4290                                                                         continue;
4291                                                         }
4292
4293                                                         IParametersMember pm = member as IParametersMember;
4294                                                         if (pm == null) {
4295                                                                 //
4296                                                                 // Will use it later to report ambiguity between best method and invocable member
4297                                                                 //
4298                                                                 if (Invocation.IsMemberInvocable (member))
4299                                                                         invocable_member = member;
4300
4301                                                                 continue;
4302                                                         }
4303
4304                                                         //
4305                                                         // Overload resolution is looking for base member but using parameter names
4306                                                         // and default values from the closest member. That means to do expensive lookup
4307                                                         // for the closest override for virtual or abstract members
4308                                                         //
4309                                                         if ((member.Modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT)) != 0) {
4310                                                                 var override_params = base_provider.GetOverrideMemberParameters (member);
4311                                                                 if (override_params != null)
4312                                                                         pm = override_params;
4313                                                         }
4314
4315                                                         //
4316                                                         // Check if the member candidate is applicable
4317                                                         //
4318                                                         bool params_expanded_form = false;
4319                                                         bool dynamic_argument = false;
4320                                                         TypeSpec rt = pm.MemberType;
4321                                                         int candidate_rate = IsApplicable (rc, ref candidate_args, args_count, ref member, pm, ref params_expanded_form, ref dynamic_argument, ref rt);
4322
4323                                                         //
4324                                                         // How does it score compare to others
4325                                                         //
4326                                                         if (candidate_rate < best_candidate_rate) {
4327                                                                 best_candidate_rate = candidate_rate;
4328                                                                 best_candidate = member;
4329                                                                 best_candidate_args = candidate_args;
4330                                                                 best_candidate_params = params_expanded_form;
4331                                                                 best_candidate_dynamic = dynamic_argument;
4332                                                                 best_parameter_member = pm;
4333                                                                 best_candidate_return_type = rt;
4334                                                         } else if (candidate_rate == 0) {
4335                                                                 //
4336                                                                 // The member look is done per type for most operations but sometimes
4337                                                                 // it's not possible like for binary operators overload because they
4338                                                                 // are unioned between 2 sides
4339                                                                 //
4340                                                                 if ((restrictions & Restrictions.BaseMembersIncluded) != 0) {
4341                                                                         if (TypeSpec.IsBaseClass (best_candidate.DeclaringType, member.DeclaringType, true))
4342                                                                                 continue;
4343                                                                 }
4344
4345                                                                 bool is_better;
4346                                                                 if (best_candidate.DeclaringType.IsInterface && member.DeclaringType.ImplementsInterface (best_candidate.DeclaringType, false)) {
4347                                                                         //
4348                                                                         // We pack all interface members into top level type which makes the overload resolution
4349                                                                         // more complicated for interfaces. We compensate it by removing methods with same
4350                                                                         // signature when building the cache hence this path should not really be hit often
4351                                                                         //
4352                                                                         // Example:
4353                                                                         // interface IA { void Foo (int arg); }
4354                                                                         // interface IB : IA { void Foo (params int[] args); }
4355                                                                         //
4356                                                                         // IB::Foo is the best overload when calling IB.Foo (1)
4357                                                                         //
4358                                                                         is_better = true;
4359                                                                         if (ambiguous_candidates != null) {
4360                                                                                 foreach (var amb_cand in ambiguous_candidates) {
4361                                                                                         if (member.DeclaringType.ImplementsInterface (best_candidate.DeclaringType, false)) {
4362                                                                                                 continue;
4363                                                                                         }
4364
4365                                                                                         is_better = false;
4366                                                                                         break;
4367                                                                                 }
4368
4369                                                                                 if (is_better)
4370                                                                                         ambiguous_candidates = null;
4371                                                                         }
4372                                                                 } else {
4373                                                                         // Is the new candidate better
4374                                                                         is_better = BetterFunction (rc, candidate_args, member, pm.Parameters, params_expanded_form, best_candidate, best_parameter_member.Parameters, best_candidate_params);
4375                                                                 }
4376
4377                                                                 if (is_better) {
4378                                                                         best_candidate = member;
4379                                                                         best_candidate_args = candidate_args;
4380                                                                         best_candidate_params = params_expanded_form;
4381                                                                         best_candidate_dynamic = dynamic_argument;
4382                                                                         best_parameter_member = pm;
4383                                                                         best_candidate_return_type = rt;
4384                                                                 } else {
4385                                                                         // It's not better but any other found later could be but we are not sure yet
4386                                                                         if (ambiguous_candidates == null)
4387                                                                                 ambiguous_candidates = new List<AmbiguousCandidate> ();
4388
4389                                                                         ambiguous_candidates.Add (new AmbiguousCandidate (member, pm.Parameters, params_expanded_form));
4390                                                                 }
4391                                                         }
4392
4393                                                         // Restore expanded arguments
4394                                                         if (candidate_args != args)
4395                                                                 candidate_args = args;
4396                                                 }
4397                                         } while (best_candidate_rate != 0 && (type_members = base_provider.GetBaseMembers (type_members[0].DeclaringType.BaseType)) != null);
4398                                 } finally {
4399                                         if (prev_recorder != null)
4400                                                 rc.Report.SetPrinter (prev_recorder);
4401                                 }
4402
4403                                 //
4404                                 // We've found exact match
4405                                 //
4406                                 if (best_candidate_rate == 0)
4407                                         break;
4408
4409                                 //
4410                                 // Try extension methods lookup when no ordinary method match was found and provider enables it
4411                                 //
4412                                 if (!error_mode) {
4413                                         var emg = base_provider.LookupExtensionMethod (rc);
4414                                         if (emg != null) {
4415                                                 emg = emg.OverloadResolve (rc, ref args, null, restrictions);
4416                                                 if (emg != null) {
4417                                                         best_candidate_extension_group = emg;
4418                                                         return (T) (MemberSpec) emg.BestCandidate;
4419                                                 }
4420                                         }
4421                                 }
4422
4423                                 // Don't run expensive error reporting mode for probing
4424                                 if (IsProbingOnly)
4425                                         return null;
4426
4427                                 if (error_mode)
4428                                         break;
4429
4430                                 lambda_conv_msgs = null;
4431                                 error_mode = true;
4432                         }
4433
4434                         //
4435                         // No best member match found, report an error
4436                         //
4437                         if (best_candidate_rate != 0 || error_mode) {
4438                                 ReportOverloadError (rc, best_candidate, best_parameter_member, best_candidate_args, best_candidate_params);
4439                                 return null;
4440                         }
4441
4442                         if (best_candidate_dynamic) {
4443                                 if (args[0].ArgType == Argument.AType.ExtensionType) {
4444                                         rc.Report.Error (1973, loc,
4445                                                 "Type `{0}' does not contain a member `{1}' and the best extension method overload `{2}' cannot be dynamically dispatched. Consider calling the method without the extension method syntax",
4446                                                 args [0].Type.GetSignatureForError (), best_candidate.Name, best_candidate.GetSignatureForError ());
4447                                 }
4448
4449                                 BestCandidateIsDynamic = true;
4450                                 return null;
4451                         }
4452
4453                         //
4454                         // These flags indicates we are running delegate probing conversion. No need to
4455                         // do more expensive checks
4456                         // 
4457                         if ((restrictions & (Restrictions.ProbingOnly | Restrictions.CovariantDelegate)) == (Restrictions.CovariantDelegate | Restrictions.ProbingOnly))
4458                                 return (T) best_candidate;
4459
4460                         if (ambiguous_candidates != null) {
4461                                 //
4462                                 // Now check that there are no ambiguities i.e the selected method
4463                                 // should be better than all the others
4464                                 //
4465                                 for (int ix = 0; ix < ambiguous_candidates.Count; ix++) {
4466                                         var candidate = ambiguous_candidates [ix];
4467
4468                                         if (!BetterFunction (rc, best_candidate_args, best_candidate, best_parameter_member.Parameters, best_candidate_params, candidate.Member, candidate.Parameters, candidate.Expanded)) {
4469                                                 var ambiguous = candidate.Member;
4470                                                 if (custom_errors == null || !custom_errors.AmbiguousCandidates (rc, best_candidate, ambiguous)) {
4471                                                         rc.Report.SymbolRelatedToPreviousError (best_candidate);
4472                                                         rc.Report.SymbolRelatedToPreviousError (ambiguous);
4473                                                         rc.Report.Error (121, loc, "The call is ambiguous between the following methods or properties: `{0}' and `{1}'",
4474                                                                 best_candidate.GetSignatureForError (), ambiguous.GetSignatureForError ());
4475                                                 }
4476
4477                                                 return (T) best_candidate;
4478                                         }
4479                                 }
4480                         }
4481
4482                         if (invocable_member != null) {
4483                                 rc.Report.SymbolRelatedToPreviousError (best_candidate);
4484                                 rc.Report.SymbolRelatedToPreviousError (invocable_member);
4485                                 rc.Report.Warning (467, 2, loc, "Ambiguity between method `{0}' and invocable non-method `{1}'. Using method group",
4486                                         best_candidate.GetSignatureForError (), invocable_member.GetSignatureForError ());
4487                         }
4488
4489                         //
4490                         // And now check if the arguments are all
4491                         // compatible, perform conversions if
4492                         // necessary etc. and return if everything is
4493                         // all right
4494                         //
4495                         if (!VerifyArguments (rc, ref best_candidate_args, best_candidate, best_parameter_member, best_candidate_params))
4496                                 return null;
4497
4498                         if (best_candidate == null)
4499                                 return null;
4500
4501                         //
4502                         // Check ObsoleteAttribute on the best method
4503                         //
4504                         ObsoleteAttribute oa = best_candidate.GetAttributeObsolete ();
4505                         if (oa != null && !rc.IsObsolete)
4506                                 AttributeTester.Report_ObsoleteMessage (oa, best_candidate.GetSignatureForError (), loc, rc.Report);
4507
4508                         var dep = best_candidate.GetMissingDependencies ();
4509                         if (dep != null) {
4510                                 ImportedTypeDefinition.Error_MissingDependency (rc, dep, loc);
4511                         }
4512
4513                         best_candidate.MemberDefinition.SetIsUsed ();
4514
4515                         args = best_candidate_args;
4516                         return (T) best_candidate;
4517                 }
4518
4519                 public MethodSpec ResolveOperator (ResolveContext rc, ref Arguments args)
4520                 {
4521                         return ResolveMember<MethodSpec> (rc, ref args);
4522                 }
4523
4524                 void ReportArgumentMismatch (ResolveContext ec, int idx, MemberSpec method,
4525                                                                                                         Argument a, AParametersCollection expected_par, TypeSpec paramType)
4526                 {
4527                         if (custom_errors != null && custom_errors.ArgumentMismatch (ec, method, a, idx))
4528                                 return;
4529
4530                         if (a is CollectionElementInitializer.ElementInitializerArgument) {
4531                                 ec.Report.SymbolRelatedToPreviousError (method);
4532                                 if ((expected_par.FixedParameters[idx].ModFlags & Parameter.Modifier.ISBYREF) != 0) {
4533                                         ec.Report.Error (1954, loc, "The best overloaded collection initalizer method `{0}' cannot have 'ref', or `out' modifier",
4534                                                 TypeManager.CSharpSignature (method));
4535                                         return;
4536                                 }
4537                                 ec.Report.Error (1950, loc, "The best overloaded collection initalizer method `{0}' has some invalid arguments",
4538                                           TypeManager.CSharpSignature (method));
4539                         } else if (IsDelegateInvoke) {
4540                                 ec.Report.Error (1594, loc, "Delegate `{0}' has some invalid arguments",
4541                                         DelegateType.GetSignatureForError ());
4542                         } else {
4543                                 ec.Report.SymbolRelatedToPreviousError (method);
4544                                 ec.Report.Error (1502, loc, "The best overloaded method match for `{0}' has some invalid arguments",
4545                                         method.GetSignatureForError ());
4546                         }
4547
4548                         Parameter.Modifier mod = idx >= expected_par.Count ? 0 : expected_par.FixedParameters[idx].ModFlags;
4549
4550                         string index = (idx + 1).ToString ();
4551                         if (((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) ^
4552                                 (a.Modifier & (Parameter.Modifier.REF | Parameter.Modifier.OUT))) != 0) {
4553                                 if ((mod & Parameter.Modifier.ISBYREF) == 0)
4554                                         ec.Report.Error (1615, loc, "Argument `#{0}' does not require `{1}' modifier. Consider removing `{1}' modifier",
4555                                                 index, Parameter.GetModifierSignature (a.Modifier));
4556                                 else
4557                                         ec.Report.Error (1620, loc, "Argument `#{0}' is missing `{1}' modifier",
4558                                                 index, Parameter.GetModifierSignature (mod));
4559                         } else if (a.Expr != ErrorExpression.Instance) {
4560                                 string p1 = a.GetSignatureForError ();
4561                                 string p2 = TypeManager.CSharpName (paramType);
4562
4563                                 if (p1 == p2) {
4564                                         p1 = a.Type.GetSignatureForErrorIncludingAssemblyName ();
4565                                         p2 = paramType.GetSignatureForErrorIncludingAssemblyName ();
4566                                 }
4567
4568                                 ec.Report.Error (1503, loc,
4569                                         "Argument `#{0}' cannot convert `{1}' expression to type `{2}'", index, p1, p2);
4570                         }
4571                 }
4572
4573                 //
4574                 // We have failed to find exact match so we return error info about the closest match
4575                 //
4576                 void ReportOverloadError (ResolveContext rc, MemberSpec best_candidate, IParametersMember pm, Arguments args, bool params_expanded)
4577                 {
4578                         int ta_count = type_arguments == null ? 0 : type_arguments.Count;
4579                         int arg_count = args == null ? 0 : args.Count;
4580
4581                         if (ta_count != best_candidate.Arity && (ta_count > 0 || ((IParametersMember) best_candidate).Parameters.IsEmpty)) {
4582                                 var mg = new MethodGroupExpr (new [] { best_candidate }, best_candidate.DeclaringType, loc);
4583                                 mg.Error_TypeArgumentsCannotBeUsed (rc, best_candidate, ta_count, loc);
4584                                 return;
4585                         }
4586
4587                         if (lambda_conv_msgs != null) {
4588                                 if (lambda_conv_msgs.Merge (rc.Report.Printer))
4589                                         return;
4590                         }
4591
4592                         //
4593                         // For candidates which match on parameters count report more details about incorrect arguments
4594                         //
4595                         if (pm != null) {
4596                                 int unexpanded_count = ((IParametersMember) best_candidate).Parameters.HasParams ? pm.Parameters.Count - 1 : pm.Parameters.Count;
4597                                 if (pm.Parameters.Count == arg_count || params_expanded || unexpanded_count == arg_count) {
4598                                         // Reject any inaccessible member
4599                                         if (!best_candidate.IsAccessible (rc) || !best_candidate.DeclaringType.IsAccessible (rc)) {
4600                                                 rc.Report.SymbolRelatedToPreviousError (best_candidate);
4601                                                 Expression.ErrorIsInaccesible (rc, best_candidate.GetSignatureForError (), loc);
4602                                                 return;
4603                                         }
4604
4605                                         var ms = best_candidate as MethodSpec;
4606                                         if (ms != null && ms.IsGeneric) {
4607                                                 bool constr_ok = true;
4608                                                 if (ms.TypeArguments != null)
4609                                                         constr_ok = new ConstraintChecker (rc.MemberContext).CheckAll (ms.GetGenericMethodDefinition (), ms.TypeArguments, ms.Constraints, loc);
4610
4611                                                 if (ta_count == 0) {
4612                                                         if (custom_errors != null && custom_errors.TypeInferenceFailed (rc, best_candidate))
4613                                                                 return;
4614
4615                                                         if (constr_ok) {
4616                                                                 rc.Report.Error (411, loc,
4617                                                                         "The type arguments for method `{0}' cannot be inferred from the usage. Try specifying the type arguments explicitly",
4618                                                                         ms.GetGenericMethodDefinition ().GetSignatureForError ());
4619                                                         }
4620
4621                                                         return;
4622                                                 }
4623                                         }
4624
4625                                         VerifyArguments (rc, ref args, best_candidate, pm, params_expanded);
4626                                         return;
4627                                 }
4628                         }
4629
4630                         //
4631                         // We failed to find any method with correct argument count, report best candidate
4632                         //
4633                         if (custom_errors != null && custom_errors.NoArgumentMatch (rc, best_candidate))
4634                                 return;
4635
4636                         if (best_candidate.Kind == MemberKind.Constructor) {
4637                                 rc.Report.SymbolRelatedToPreviousError (best_candidate);
4638                                 Error_ConstructorMismatch (rc, best_candidate.DeclaringType, arg_count, loc);
4639                         } else if (IsDelegateInvoke) {
4640                                 rc.Report.SymbolRelatedToPreviousError (DelegateType);
4641                                 rc.Report.Error (1593, loc, "Delegate `{0}' does not take `{1}' arguments",
4642                                         DelegateType.GetSignatureForError (), arg_count.ToString ());
4643                         } else {
4644                                 string name = best_candidate.Kind == MemberKind.Indexer ? "this" : best_candidate.Name;
4645                                 rc.Report.SymbolRelatedToPreviousError (best_candidate);
4646                                 rc.Report.Error (1501, loc, "No overload for method `{0}' takes `{1}' arguments",
4647                                         name, arg_count.ToString ());
4648                         }
4649                 }
4650
4651                 bool VerifyArguments (ResolveContext ec, ref Arguments args, MemberSpec member, IParametersMember pm, bool chose_params_expanded)
4652                 {
4653                         var pd = pm.Parameters;
4654                         TypeSpec[] ptypes = ((IParametersMember) member).Parameters.Types;
4655
4656                         Parameter.Modifier p_mod = 0;
4657                         TypeSpec pt = null;
4658                         int a_idx = 0, a_pos = 0;
4659                         Argument a = null;
4660                         ArrayInitializer params_initializers = null;
4661                         bool has_unsafe_arg = pm.MemberType.IsPointer;
4662                         int arg_count = args == null ? 0 : args.Count;
4663
4664                         for (; a_idx < arg_count; a_idx++, ++a_pos) {
4665                                 a = args[a_idx];
4666                                 if (p_mod != Parameter.Modifier.PARAMS) {
4667                                         p_mod = pd.FixedParameters[a_idx].ModFlags;
4668                                         pt = ptypes[a_idx];
4669                                         has_unsafe_arg |= pt.IsPointer;
4670
4671                                         if (p_mod == Parameter.Modifier.PARAMS) {
4672                                                 if (chose_params_expanded) {
4673                                                         params_initializers = new ArrayInitializer (arg_count - a_idx, a.Expr.Location);
4674                                                         pt = TypeManager.GetElementType (pt);
4675                                                 }
4676                                         }
4677                                 }
4678
4679                                 //
4680                                 // Types have to be identical when ref or out modifer is used 
4681                                 //
4682                                 if (a.Modifier != 0 || (p_mod & ~Parameter.Modifier.PARAMS) != 0) {
4683                                         if ((p_mod & ~Parameter.Modifier.PARAMS) != a.Modifier)
4684                                                 break;
4685
4686                                         if (a.Expr.Type == pt || TypeSpecComparer.IsEqual (a.Expr.Type, pt))
4687                                                 continue;
4688
4689                                         break;
4690                                 }
4691
4692                                 NamedArgument na = a as NamedArgument;
4693                                 if (na != null) {
4694                                         int name_index = pd.GetParameterIndexByName (na.Name);
4695                                         if (name_index < 0 || name_index >= pd.Count) {
4696                                                 if (IsDelegateInvoke) {
4697                                                         ec.Report.SymbolRelatedToPreviousError (DelegateType);
4698                                                         ec.Report.Error (1746, na.Location,
4699                                                                 "The delegate `{0}' does not contain a parameter named `{1}'",
4700                                                                 DelegateType.GetSignatureForError (), na.Name);
4701                                                 } else {
4702                                                         ec.Report.SymbolRelatedToPreviousError (member);
4703                                                         ec.Report.Error (1739, na.Location,
4704                                                                 "The best overloaded method match for `{0}' does not contain a parameter named `{1}'",
4705                                                                 TypeManager.CSharpSignature (member), na.Name);
4706                                                 }
4707                                         } else if (args[name_index] != a) {
4708                                                 if (IsDelegateInvoke)
4709                                                         ec.Report.SymbolRelatedToPreviousError (DelegateType);
4710                                                 else
4711                                                         ec.Report.SymbolRelatedToPreviousError (member);
4712
4713                                                 ec.Report.Error (1744, na.Location,
4714                                                         "Named argument `{0}' cannot be used for a parameter which has positional argument specified",
4715                                                         na.Name);
4716                                         }
4717                                 }
4718                                 
4719                                 if (a.Expr.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
4720                                         continue;
4721
4722                                 if ((restrictions & Restrictions.CovariantDelegate) != 0 && !Delegate.IsTypeCovariant (ec, a.Expr.Type, pt)) {
4723                                         custom_errors.NoArgumentMatch (ec, member);
4724                                         return false;
4725                                 }
4726
4727                                 Expression conv = null;
4728                                 if (a.ArgType == Argument.AType.ExtensionType) {
4729                                         if (a.Expr.Type == pt || TypeSpecComparer.IsEqual (a.Expr.Type, pt)) {
4730                                                 conv = a.Expr;
4731                                         } else {
4732                                                 conv = Convert.ImplicitReferenceConversion (a.Expr, pt, false);
4733                                                 if (conv == null)
4734                                                         conv = Convert.ImplicitBoxingConversion (a.Expr, a.Expr.Type, pt);
4735                                         }
4736                                 } else {
4737                                         conv = Convert.ImplicitConversion (ec, a.Expr, pt, loc);
4738                                 }
4739
4740                                 if (conv == null)
4741                                         break;
4742
4743                                 //
4744                                 // Convert params arguments to an array initializer
4745                                 //
4746                                 if (params_initializers != null) {
4747                                         // we choose to use 'a.Expr' rather than 'conv' so that
4748                                         // we don't hide the kind of expression we have (esp. CompoundAssign.Helper)
4749                                         params_initializers.Add (a.Expr);
4750                                         args.RemoveAt (a_idx--);
4751                                         --arg_count;
4752                                         continue;
4753                                 }
4754
4755                                 // Update the argument with the implicit conversion
4756                                 a.Expr = conv;
4757                         }
4758
4759                         if (a_idx != arg_count) {
4760                                 ReportArgumentMismatch (ec, a_pos, member, a, pd, pt);
4761                                 return false;
4762                         }
4763
4764                         //
4765                         // Fill not provided arguments required by params modifier
4766                         //
4767                         if (params_initializers == null && pd.HasParams && arg_count + 1 == pd.Count) {
4768                                 if (args == null)
4769                                         args = new Arguments (1);
4770
4771                                 pt = ptypes[pd.Count - 1];
4772                                 pt = TypeManager.GetElementType (pt);
4773                                 has_unsafe_arg |= pt.IsPointer;
4774                                 params_initializers = new ArrayInitializer (0, loc);
4775                         }
4776
4777                         //
4778                         // Append an array argument with all params arguments
4779                         //
4780                         if (params_initializers != null) {
4781                                 args.Add (new Argument (
4782                                         new ArrayCreation (new TypeExpression (pt, loc), params_initializers, loc).Resolve (ec)));
4783                                 arg_count++;
4784                         }
4785
4786                         if (has_unsafe_arg && !ec.IsUnsafe) {
4787                                 Expression.UnsafeError (ec, loc);
4788                         }
4789
4790                         //
4791                         // We could infer inaccesible type arguments
4792                         //
4793                         if (type_arguments == null && member.IsGeneric) {
4794                                 var ms = (MethodSpec) member;
4795                                 foreach (var ta in ms.TypeArguments) {
4796                                         if (!ta.IsAccessible (ec)) {
4797                                                 ec.Report.SymbolRelatedToPreviousError (ta);
4798                                                 Expression.ErrorIsInaccesible (ec, member.GetSignatureForError (), loc);
4799                                                 break;
4800                                         }
4801                                 }
4802                         }
4803
4804                         return true;
4805                 }
4806         }
4807
4808         public class ConstantExpr : MemberExpr
4809         {
4810                 readonly ConstSpec constant;
4811
4812                 public ConstantExpr (ConstSpec constant, Location loc)
4813                 {
4814                         this.constant = constant;
4815                         this.loc = loc;
4816                 }
4817
4818                 public override string Name {
4819                         get { throw new NotImplementedException (); }
4820                 }
4821
4822                 public override bool IsInstance {
4823                         get { return !IsStatic; }
4824                 }
4825
4826                 public override bool IsStatic {
4827                         get { return true; }
4828                 }
4829
4830                 protected override TypeSpec DeclaringType {
4831                         get { return constant.DeclaringType; }
4832                 }
4833
4834                 public override Expression CreateExpressionTree (ResolveContext ec)
4835                 {
4836                         throw new NotSupportedException ("ET");
4837                 }
4838
4839                 protected override Expression DoResolve (ResolveContext rc)
4840                 {
4841                         ResolveInstanceExpression (rc, null);
4842                         DoBestMemberChecks (rc, constant);
4843
4844                         var c = constant.GetConstant (rc);
4845
4846                         // Creates reference expression to the constant value
4847                         return Constant.CreateConstant (constant.MemberType, c.GetValue (), loc);
4848                 }
4849
4850                 public override void Emit (EmitContext ec)
4851                 {
4852                         throw new NotSupportedException ();
4853                 }
4854
4855                 public override string GetSignatureForError ()
4856                 {
4857                         return constant.GetSignatureForError ();
4858                 }
4859
4860                 public override void SetTypeArguments (ResolveContext ec, TypeArguments ta)
4861                 {
4862                         Error_TypeArgumentsCannotBeUsed (ec, "constant", GetSignatureForError (), loc);
4863                 }
4864         }
4865
4866         //
4867         // Fully resolved expression that references a Field
4868         //
4869         public class FieldExpr : MemberExpr, IDynamicAssign, IMemoryLocation, IVariableReference
4870         {
4871                 protected FieldSpec spec;
4872                 VariableInfo variable_info;
4873                 
4874                 LocalTemporary temp;
4875                 bool prepared;
4876                 
4877                 protected FieldExpr (Location l)
4878                 {
4879                         loc = l;
4880                 }
4881
4882                 public FieldExpr (FieldSpec spec, Location loc)
4883                 {
4884                         this.spec = spec;
4885                         this.loc = loc;
4886
4887                         type = spec.MemberType;
4888                 }
4889                 
4890                 public FieldExpr (FieldBase fi, Location l)
4891                         : this (fi.Spec, l)
4892                 {
4893                 }
4894
4895                 #region Properties
4896
4897                 public override string Name {
4898                         get {
4899                                 return spec.Name;
4900                         }
4901                 }
4902
4903                 public bool IsHoisted {
4904                         get {
4905                                 IVariableReference hv = InstanceExpression as IVariableReference;
4906                                 return hv != null && hv.IsHoisted;
4907                         }
4908                 }
4909
4910                 public override bool IsInstance {
4911                         get {
4912                                 return !spec.IsStatic;
4913                         }
4914                 }
4915
4916                 public override bool IsStatic {
4917                         get {
4918                                 return spec.IsStatic;
4919                         }
4920                 }
4921
4922                 public FieldSpec Spec {
4923                         get {
4924                                 return spec;
4925                         }
4926                 }
4927
4928                 protected override TypeSpec DeclaringType {
4929                         get {
4930                                 return spec.DeclaringType;
4931                         }
4932                 }
4933
4934                 public VariableInfo VariableInfo {
4935                         get {
4936                                 return variable_info;
4937                         }
4938                 }
4939
4940 #endregion
4941
4942                 public override string GetSignatureForError ()
4943                 {
4944                         return TypeManager.GetFullNameSignature (spec);
4945                 }
4946
4947                 public bool IsMarshalByRefAccess (ResolveContext rc)
4948                 {
4949                         // Checks possible ldflda of field access expression
4950                         return !spec.IsStatic && TypeSpec.IsValueType (spec.MemberType) && !(InstanceExpression is This) &&
4951                                 rc.Module.PredefinedTypes.MarshalByRefObject.Define () &&
4952                                 TypeSpec.IsBaseClass (spec.DeclaringType, rc.Module.PredefinedTypes.MarshalByRefObject.TypeSpec, false);
4953                 }
4954
4955                 public void SetHasAddressTaken ()
4956                 {
4957                         IVariableReference vr = InstanceExpression as IVariableReference;
4958                         if (vr != null)
4959                                 vr.SetHasAddressTaken ();
4960                 }
4961
4962                 public override Expression CreateExpressionTree (ResolveContext ec)
4963                 {
4964                         Expression instance;
4965                         if (InstanceExpression == null) {
4966                                 instance = new NullLiteral (loc);
4967                         } else {
4968                                 instance = InstanceExpression.CreateExpressionTree (ec);
4969                         }
4970
4971                         Arguments args = Arguments.CreateForExpressionTree (ec, null,
4972                                 instance,
4973                                 CreateTypeOfExpression ());
4974
4975                         return CreateExpressionFactoryCall (ec, "Field", args);
4976                 }
4977
4978                 public Expression CreateTypeOfExpression ()
4979                 {
4980                         return new TypeOfField (spec, loc);
4981                 }
4982
4983                 protected override Expression DoResolve (ResolveContext ec)
4984                 {
4985                         return DoResolve (ec, null);
4986                 }
4987
4988                 Expression DoResolve (ResolveContext ec, Expression rhs)
4989                 {
4990                         bool lvalue_instance = rhs != null && IsInstance && spec.DeclaringType.IsStruct;
4991
4992                         if (rhs != this) {
4993                                 if (ResolveInstanceExpression (ec, rhs)) {
4994                                         // Resolve the field's instance expression while flow analysis is turned
4995                                         // off: when accessing a field "a.b", we must check whether the field
4996                                         // "a.b" is initialized, not whether the whole struct "a" is initialized.
4997
4998                                         if (lvalue_instance) {
4999                                                 using (ec.With (ResolveContext.Options.DoFlowAnalysis, false)) {
5000                                                         bool out_access = rhs == EmptyExpression.OutAccess || rhs == EmptyExpression.LValueMemberOutAccess;
5001
5002                                                         Expression right_side =
5003                                                                 out_access ? EmptyExpression.LValueMemberOutAccess : EmptyExpression.LValueMemberAccess;
5004
5005                                                         InstanceExpression = InstanceExpression.ResolveLValue (ec, right_side);
5006                                                 }
5007                                         } else {
5008                                                 using (ec.With (ResolveContext.Options.DoFlowAnalysis, false)) {
5009                                                         InstanceExpression = InstanceExpression.Resolve (ec, ResolveFlags.VariableOrValue);
5010                                                 }
5011                                         }
5012
5013                                         if (InstanceExpression == null)
5014                                                 return null;
5015                                 }
5016
5017                                 DoBestMemberChecks (ec, spec);
5018                         }
5019
5020                         var fb = spec as FixedFieldSpec;
5021                         IVariableReference var = InstanceExpression as IVariableReference;
5022
5023                         if (lvalue_instance && var != null && var.VariableInfo != null) {
5024                                 var.VariableInfo.SetFieldAssigned (ec, Name);
5025                         }
5026                         
5027                         if (fb != null) {
5028                                 IFixedExpression fe = InstanceExpression as IFixedExpression;
5029                                 if (!ec.HasSet (ResolveContext.Options.FixedInitializerScope) && (fe == null || !fe.IsFixed)) {
5030                                         ec.Report.Error (1666, loc, "You cannot use fixed size buffers contained in unfixed expressions. Try using the fixed statement");
5031                                 }
5032
5033                                 if (InstanceExpression.eclass != ExprClass.Variable) {
5034                                         ec.Report.SymbolRelatedToPreviousError (spec);
5035                                         ec.Report.Error (1708, loc, "`{0}': Fixed size buffers can only be accessed through locals or fields",
5036                                                 TypeManager.GetFullNameSignature (spec));
5037                                 } else if (var != null && var.IsHoisted) {
5038                                         AnonymousMethodExpression.Error_AddressOfCapturedVar (ec, var, loc);
5039                                 }
5040                                 
5041                                 return new FixedBufferPtr (this, fb.ElementType, loc).Resolve (ec);
5042                         }
5043
5044                         eclass = ExprClass.Variable;
5045
5046                         // If the instance expression is a local variable or parameter.
5047                         if (var == null || var.VariableInfo == null)
5048                                 return this;
5049
5050                         VariableInfo vi = var.VariableInfo;
5051                         if (!vi.IsFieldAssigned (ec, Name, loc))
5052                                 return null;
5053
5054                         variable_info = vi.GetSubStruct (Name);
5055                         return this;
5056                 }
5057
5058                 static readonly int [] codes = {
5059                         191,    // instance, write access
5060                         192,    // instance, out access
5061                         198,    // static, write access
5062                         199,    // static, out access
5063                         1648,   // member of value instance, write access
5064                         1649,   // member of value instance, out access
5065                         1650,   // member of value static, write access
5066                         1651    // member of value static, out access
5067                 };
5068
5069                 static readonly string [] msgs = {
5070                         /*0191*/ "A readonly field `{0}' cannot be assigned to (except in a constructor or a variable initializer)",
5071                         /*0192*/ "A readonly field `{0}' cannot be passed ref or out (except in a constructor)",
5072                         /*0198*/ "A static readonly field `{0}' cannot be assigned to (except in a static constructor or a variable initializer)",
5073                         /*0199*/ "A static readonly field `{0}' cannot be passed ref or out (except in a static constructor)",
5074                         /*1648*/ "Members of readonly field `{0}' cannot be modified (except in a constructor or a variable initializer)",
5075                         /*1649*/ "Members of readonly field `{0}' cannot be passed ref or out (except in a constructor)",
5076                         /*1650*/ "Fields of static readonly field `{0}' cannot be assigned to (except in a static constructor or a variable initializer)",
5077                         /*1651*/ "Fields of static readonly field `{0}' cannot be passed ref or out (except in a static constructor)"
5078                 };
5079
5080                 // The return value is always null.  Returning a value simplifies calling code.
5081                 Expression Report_AssignToReadonly (ResolveContext ec, Expression right_side)
5082                 {
5083                         int i = 0;
5084                         if (right_side == EmptyExpression.OutAccess || right_side == EmptyExpression.LValueMemberOutAccess)
5085                                 i += 1;
5086                         if (IsStatic)
5087                                 i += 2;
5088                         if (right_side == EmptyExpression.LValueMemberAccess || right_side == EmptyExpression.LValueMemberOutAccess)
5089                                 i += 4;
5090                         ec.Report.Error (codes [i], loc, msgs [i], GetSignatureForError ());
5091
5092                         return null;
5093                 }
5094                 
5095                 override public Expression DoResolveLValue (ResolveContext ec, Expression right_side)
5096                 {
5097                         Expression e = DoResolve (ec, right_side);
5098
5099                         if (e == null)
5100                                 return null;
5101
5102                         spec.MemberDefinition.SetIsAssigned ();
5103
5104                         if ((right_side == EmptyExpression.UnaryAddress || right_side == EmptyExpression.OutAccess) &&
5105                                         (spec.Modifiers & Modifiers.VOLATILE) != 0) {
5106                                 ec.Report.Warning (420, 1, loc,
5107                                         "`{0}': A volatile field references will not be treated as volatile",
5108                                         spec.GetSignatureForError ());
5109                         }
5110
5111                         if (spec.IsReadOnly) {
5112                                 // InitOnly fields can only be assigned in constructors or initializers
5113                                 if (!ec.HasAny (ResolveContext.Options.FieldInitializerScope | ResolveContext.Options.ConstructorScope))
5114                                         return Report_AssignToReadonly (ec, right_side);
5115
5116                                 if (ec.HasSet (ResolveContext.Options.ConstructorScope)) {
5117
5118                                         // InitOnly fields cannot be assigned-to in a different constructor from their declaring type
5119                                         if (ec.CurrentMemberDefinition.Parent.Definition != spec.DeclaringType.GetDefinition ())
5120                                                 return Report_AssignToReadonly (ec, right_side);
5121                                         // static InitOnly fields cannot be assigned-to in an instance constructor
5122                                         if (IsStatic && !ec.IsStatic)
5123                                                 return Report_AssignToReadonly (ec, right_side);
5124                                         // instance constructors can't modify InitOnly fields of other instances of the same type
5125                                         if (!IsStatic && !(InstanceExpression is This))
5126                                                 return Report_AssignToReadonly (ec, right_side);
5127                                 }
5128                         }
5129
5130                         if (right_side == EmptyExpression.OutAccess && IsMarshalByRefAccess (ec)) {
5131                                 ec.Report.SymbolRelatedToPreviousError (spec.DeclaringType);
5132                                 ec.Report.Warning (197, 1, loc,
5133                                                 "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",
5134                                                 GetSignatureForError ());
5135                         }
5136
5137                         eclass = ExprClass.Variable;
5138                         return this;
5139                 }
5140
5141                 public override int GetHashCode ()
5142                 {
5143                         return spec.GetHashCode ();
5144                 }
5145                 
5146                 public bool IsFixed {
5147                         get {
5148                                 //
5149                                 // A variable of the form V.I is fixed when V is a fixed variable of a struct type
5150                                 //
5151                                 IVariableReference variable = InstanceExpression as IVariableReference;
5152                                 if (variable != null)
5153                                         return InstanceExpression.Type.IsStruct && variable.IsFixed;
5154
5155                                 IFixedExpression fe = InstanceExpression as IFixedExpression;
5156                                 return fe != null && fe.IsFixed;
5157                         }
5158                 }
5159
5160                 public override bool Equals (object obj)
5161                 {
5162                         FieldExpr fe = obj as FieldExpr;
5163                         if (fe == null)
5164                                 return false;
5165
5166                         if (spec != fe.spec)
5167                                 return false;
5168
5169                         if (InstanceExpression == null || fe.InstanceExpression == null)
5170                                 return true;
5171
5172                         return InstanceExpression.Equals (fe.InstanceExpression);
5173                 }
5174                 
5175                 public void Emit (EmitContext ec, bool leave_copy)
5176                 {
5177                         bool is_volatile = false;
5178
5179                         if ((spec.Modifiers & Modifiers.VOLATILE) != 0)
5180                                 is_volatile = true;
5181
5182                         spec.MemberDefinition.SetIsUsed ();
5183                         
5184                         if (IsStatic){
5185                                 if (is_volatile)
5186                                         ec.Emit (OpCodes.Volatile);
5187
5188                                 ec.Emit (OpCodes.Ldsfld, spec);
5189                         } else {
5190                                 if (!prepared)
5191                                         EmitInstance (ec, false);
5192
5193                                 // Optimization for build-in types
5194                                 if (type.IsStruct && type == ec.CurrentType && InstanceExpression.Type == type) {
5195                                         ec.EmitLoadFromPtr (type);
5196                                 } else {
5197                                         var ff = spec as FixedFieldSpec;
5198                                         if (ff != null) {
5199                                                 ec.Emit (OpCodes.Ldflda, spec);
5200                                                 ec.Emit (OpCodes.Ldflda, ff.Element);
5201                                         } else {
5202                                                 if (is_volatile)
5203                                                         ec.Emit (OpCodes.Volatile);
5204
5205                                                 ec.Emit (OpCodes.Ldfld, spec);
5206                                         }
5207                                 }
5208                         }
5209
5210                         if (leave_copy) {
5211                                 ec.Emit (OpCodes.Dup);
5212                                 if (!IsStatic) {
5213                                         temp = new LocalTemporary (this.Type);
5214                                         temp.Store (ec);
5215                                 }
5216                         }
5217                 }
5218
5219                 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
5220                 {
5221                         if (source.ContainsEmitWithAwait ()) {
5222                                 source = source.EmitToField (ec);
5223                         }
5224
5225                         prepared = isCompound && !(source is DynamicExpressionStatement);
5226                         if (IsInstance)
5227                                 EmitInstance (ec, prepared);
5228
5229                         source.Emit (ec);
5230
5231                         if (leave_copy) {
5232                                 ec.Emit (OpCodes.Dup);
5233                                 if (!IsStatic) {
5234                                         temp = new LocalTemporary (this.Type);
5235                                         temp.Store (ec);
5236                                 }
5237                         }
5238
5239                         if ((spec.Modifiers & Modifiers.VOLATILE) != 0)
5240                                 ec.Emit (OpCodes.Volatile);
5241                                         
5242                         spec.MemberDefinition.SetIsAssigned ();
5243
5244                         if (IsStatic)
5245                                 ec.Emit (OpCodes.Stsfld, spec);
5246                         else
5247                                 ec.Emit (OpCodes.Stfld, spec);
5248                         
5249                         if (temp != null) {
5250                                 temp.Emit (ec);
5251                                 temp.Release (ec);
5252                                 temp = null;
5253                         }
5254                 }
5255
5256                 //
5257                 // Emits store to field with prepared values on stack
5258                 //
5259                 public void EmitAssignFromStack (EmitContext ec)
5260                 {
5261                         if (IsStatic) {
5262                                 ec.Emit (OpCodes.Stsfld, spec);
5263                         } else {
5264                                 ec.Emit (OpCodes.Stfld, spec);
5265                         }
5266                 }
5267
5268                 public override void Emit (EmitContext ec)
5269                 {
5270                         Emit (ec, false);
5271                 }
5272
5273                 public override void EmitSideEffect (EmitContext ec)
5274                 {
5275                         bool is_volatile = (spec.Modifiers & Modifiers.VOLATILE) != 0;
5276
5277                         if (is_volatile) // || is_marshal_by_ref ())
5278                                 base.EmitSideEffect (ec);
5279                 }
5280
5281                 public void AddressOf (EmitContext ec, AddressOp mode)
5282                 {
5283                         if ((mode & AddressOp.Store) != 0)
5284                                 spec.MemberDefinition.SetIsAssigned ();
5285                         if ((mode & AddressOp.Load) != 0)
5286                                 spec.MemberDefinition.SetIsUsed ();
5287
5288                         //
5289                         // Handle initonly fields specially: make a copy and then
5290                         // get the address of the copy.
5291                         //
5292                         bool need_copy;
5293                         if (spec.IsReadOnly){
5294                                 need_copy = true;
5295                                 if (ec.HasSet (EmitContext.Options.ConstructorScope) && spec.DeclaringType == ec.CurrentType) {
5296                                         if (IsStatic){
5297                                                 if (ec.IsStatic)
5298                                                         need_copy = false;
5299                                         } else
5300                                                 need_copy = false;
5301                                 }
5302                         } else
5303                                 need_copy = false;
5304                         
5305                         if (need_copy) {
5306                                 Emit (ec);
5307                                 var temp = ec.GetTemporaryLocal (type);
5308                                 ec.Emit (OpCodes.Stloc, temp, type);
5309                                 ec.Emit (OpCodes.Ldloca, temp, type);
5310                                 ec.FreeTemporaryLocal (temp, type);
5311                                 return;
5312                         }
5313
5314
5315                         if (IsStatic){
5316                                 ec.Emit (OpCodes.Ldsflda, spec);
5317                         } else {
5318                                 if (!prepared)
5319                                         EmitInstance (ec, false);
5320                                 ec.Emit (OpCodes.Ldflda, spec);
5321                         }
5322                 }
5323
5324                 public SLE.Expression MakeAssignExpression (BuilderContext ctx, Expression source)
5325                 {
5326                         return MakeExpression (ctx);
5327                 }
5328
5329                 public override SLE.Expression MakeExpression (BuilderContext ctx)
5330                 {
5331 #if STATIC
5332                         return base.MakeExpression (ctx);
5333 #else
5334                         return SLE.Expression.Field (
5335                                 IsStatic ? null : InstanceExpression.MakeExpression (ctx),
5336                                 spec.GetMetaInfo ());
5337 #endif
5338                 }
5339
5340                 public override void SetTypeArguments (ResolveContext ec, TypeArguments ta)
5341                 {
5342                         Error_TypeArgumentsCannotBeUsed (ec, "field", GetSignatureForError (), loc);
5343                 }
5344         }
5345
5346         
5347         //
5348         // Expression that evaluates to a Property.
5349         //
5350         // This is not an LValue because we need to re-write the expression. We
5351         // can not take data from the stack and store it.
5352         //
5353         sealed class PropertyExpr : PropertyOrIndexerExpr<PropertySpec>
5354         {
5355                 public PropertyExpr (PropertySpec spec, Location l)
5356                         : base (l)
5357                 {
5358                         best_candidate = spec;
5359                         type = spec.MemberType;
5360                 }
5361
5362                 #region Properties
5363
5364                 protected override Arguments Arguments {
5365                         get {
5366                                 return null;
5367                         }
5368                         set {
5369                         }
5370                 }
5371
5372                 protected override TypeSpec DeclaringType {
5373                         get {
5374                                 return best_candidate.DeclaringType;
5375                         }
5376                 }
5377
5378                 public override string Name {
5379                         get {
5380                                 return best_candidate.Name;
5381                         }
5382                 }
5383
5384                 public override bool IsInstance {
5385                         get {
5386                                 return !IsStatic;
5387                         }
5388                 }
5389
5390                 public override bool IsStatic {
5391                         get {
5392                                 return best_candidate.IsStatic;
5393                         }
5394                 }
5395
5396                 public PropertySpec PropertyInfo {
5397                         get {
5398                                 return best_candidate;
5399                         }
5400                 }
5401
5402                 #endregion
5403
5404                 public override Expression CreateExpressionTree (ResolveContext ec)
5405                 {
5406                         Arguments args;
5407                         if (IsSingleDimensionalArrayLength ()) {
5408                                 args = new Arguments (1);
5409                                 args.Add (new Argument (InstanceExpression.CreateExpressionTree (ec)));
5410                                 return CreateExpressionFactoryCall (ec, "ArrayLength", args);
5411                         }
5412
5413                         args = new Arguments (2);
5414                         if (InstanceExpression == null)
5415                                 args.Add (new Argument (new NullLiteral (loc)));
5416                         else
5417                                 args.Add (new Argument (InstanceExpression.CreateExpressionTree (ec)));
5418                         args.Add (new Argument (new TypeOfMethod (Getter, loc)));
5419                         return CreateExpressionFactoryCall (ec, "Property", args);
5420                 }
5421
5422                 public Expression CreateSetterTypeOfExpression ()
5423                 {
5424                         return new TypeOfMethod (Setter, loc);
5425                 }
5426
5427                 public override string GetSignatureForError ()
5428                 {
5429                         return best_candidate.GetSignatureForError ();
5430                 }
5431
5432                 public override SLE.Expression MakeAssignExpression (BuilderContext ctx, Expression source)
5433                 {
5434 #if STATIC
5435                         return base.MakeExpression (ctx);
5436 #else
5437                         return SLE.Expression.Property (InstanceExpression.MakeExpression (ctx), (MethodInfo) Setter.GetMetaInfo ());
5438 #endif
5439                 }
5440
5441                 public override SLE.Expression MakeExpression (BuilderContext ctx)
5442                 {
5443 #if STATIC
5444                         return base.MakeExpression (ctx);
5445 #else
5446                         return SLE.Expression.Property (InstanceExpression.MakeExpression (ctx), (MethodInfo) Getter.GetMetaInfo ());
5447 #endif
5448                 }
5449
5450                 void Error_PropertyNotValid (ResolveContext ec)
5451                 {
5452                         ec.Report.SymbolRelatedToPreviousError (best_candidate);
5453                         ec.Report.Error (1546, loc, "Property or event `{0}' is not supported by the C# language",
5454                                 GetSignatureForError ());
5455                 }
5456
5457                 bool IsSingleDimensionalArrayLength ()
5458                 {
5459                         if (best_candidate.DeclaringType.BuiltinType != BuiltinTypeSpec.Type.Array || !best_candidate.HasGet || Name != "Length")
5460                                 return false;
5461
5462                         ArrayContainer ac = InstanceExpression.Type as ArrayContainer;
5463                         return ac != null && ac.Rank == 1;
5464                 }
5465
5466                 public override void Emit (EmitContext ec, bool leave_copy)
5467                 {
5468                         //
5469                         // Special case: length of single dimension array property is turned into ldlen
5470                         //
5471                         if (IsSingleDimensionalArrayLength ()) {
5472                                 EmitInstance (ec, false);
5473                                 ec.Emit (OpCodes.Ldlen);
5474                                 ec.Emit (OpCodes.Conv_I4);
5475                                 return;
5476                         }
5477
5478                         base.Emit (ec, leave_copy);
5479                 }
5480
5481                 public override void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
5482                 {
5483                         Arguments args;
5484                         LocalTemporary await_source_arg = null;
5485
5486                         if (isCompound && !(source is DynamicExpressionStatement)) {
5487                                 emitting_compound_assignment = true;
5488                                 source.Emit (ec);
5489
5490                                 if (has_await_arguments) {
5491                                         await_source_arg = new LocalTemporary (Type);
5492                                         await_source_arg.Store (ec);
5493
5494                                         has_await_arguments = false;
5495
5496                                         args = new Arguments (1);
5497                                         args.Add (new Argument (await_source_arg));
5498                                 } else {
5499                                         args = null;
5500                                 }
5501
5502                                 if (leave_copy) {
5503                                         ec.Emit (OpCodes.Dup);
5504                                         if (!IsStatic) {
5505                                                 temp = new LocalTemporary (this.Type);
5506                                                 temp.Store (ec);
5507                                         }
5508                                 }
5509                         } else {
5510                                 args = new Arguments (1);
5511
5512                                 if (leave_copy) {
5513                                         source.Emit (ec);
5514                                         temp = new LocalTemporary (this.Type);
5515                                         temp.Store (ec);
5516                                         args.Add (new Argument (temp));
5517                                 } else {
5518                                         args.Add (new Argument (source));
5519                                 }
5520                         }
5521
5522                         emitting_compound_assignment = false;
5523
5524                         var call = new CallEmitter ();
5525                         call.InstanceExpression = InstanceExpression;
5526                         if (args == null)
5527                                 call.InstanceExpressionOnStack = true;
5528
5529                         call.Emit (ec, Setter, args, loc);
5530
5531                         if (temp != null) {
5532                                 temp.Emit (ec);
5533                                 temp.Release (ec);
5534                         }
5535
5536                         if (await_source_arg != null) {
5537                                 await_source_arg.Release (ec);
5538                         }
5539                 }
5540
5541                 protected override Expression OverloadResolve (ResolveContext rc, Expression right_side)
5542                 {
5543                         eclass = ExprClass.PropertyAccess;
5544
5545                         if (best_candidate.IsNotCSharpCompatible) {
5546                                 Error_PropertyNotValid (rc);
5547                         }
5548
5549                         ResolveInstanceExpression (rc, right_side);
5550
5551                         if ((best_candidate.Modifiers & (Modifiers.ABSTRACT | Modifiers.VIRTUAL)) != 0 && best_candidate.DeclaringType != InstanceExpression.Type) {
5552                                 var filter = new MemberFilter (best_candidate.Name, 0, MemberKind.Property, null, null);
5553                                 var p = MemberCache.FindMember (InstanceExpression.Type, filter, BindingRestriction.InstanceOnly | BindingRestriction.OverrideOnly) as PropertySpec;
5554                                 if (p != null) {
5555                                         type = p.MemberType;
5556                                 }
5557                         }
5558
5559                         DoBestMemberChecks (rc, best_candidate);
5560                         return this;
5561                 }
5562
5563                 public override void SetTypeArguments (ResolveContext ec, TypeArguments ta)
5564                 {
5565                         Error_TypeArgumentsCannotBeUsed (ec, "property", GetSignatureForError (), loc);
5566                 }
5567         }
5568
5569         abstract class PropertyOrIndexerExpr<T> : MemberExpr, IDynamicAssign where T : PropertySpec
5570         {
5571                 // getter and setter can be different for base calls
5572                 MethodSpec getter, setter;
5573                 protected T best_candidate;
5574
5575                 protected LocalTemporary temp;
5576                 protected bool emitting_compound_assignment;
5577                 protected bool has_await_arguments;
5578
5579                 protected PropertyOrIndexerExpr (Location l)
5580                 {
5581                         loc = l;
5582                 }
5583
5584                 #region Properties
5585
5586                 protected abstract Arguments Arguments { get; set; }
5587
5588                 public MethodSpec Getter {
5589                         get {
5590                                 return getter;
5591                         }
5592                         set {
5593                                 getter = value;
5594                         }
5595                 }
5596
5597                 public MethodSpec Setter {
5598                         get {
5599                                 return setter;
5600                         }
5601                         set {
5602                                 setter = value;
5603                         }
5604                 }
5605
5606                 #endregion
5607
5608                 protected override Expression DoResolve (ResolveContext ec)
5609                 {
5610                         if (eclass == ExprClass.Unresolved) {
5611                                 var expr = OverloadResolve (ec, null);
5612                                 if (expr == null)
5613                                         return null;
5614
5615                                 if (expr != this)
5616                                         return expr.Resolve (ec);
5617                         }
5618
5619                         if (!ResolveGetter (ec))
5620                                 return null;
5621
5622                         return this;
5623                 }
5624
5625                 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
5626                 {
5627                         if (right_side == EmptyExpression.OutAccess) {
5628                                 // TODO: best_candidate can be null at this point
5629                                 INamedBlockVariable variable = null;
5630                                 if (best_candidate != null && ec.CurrentBlock.ParametersBlock.TopBlock.GetLocalName (best_candidate.Name, ec.CurrentBlock, ref variable) && variable is Linq.RangeVariable) {
5631                                         ec.Report.Error (1939, loc, "A range variable `{0}' may not be passes as `ref' or `out' parameter",
5632                                                 best_candidate.Name);
5633                                 } else {
5634                                         right_side.DoResolveLValue (ec, this);
5635                                 }
5636                                 return null;
5637                         }
5638
5639                         // if the property/indexer returns a value type, and we try to set a field in it
5640                         if (right_side == EmptyExpression.LValueMemberAccess || right_side == EmptyExpression.LValueMemberOutAccess) {
5641                                 Error_CannotModifyIntermediateExpressionValue (ec);
5642                         }
5643
5644                         if (eclass == ExprClass.Unresolved) {
5645                                 var expr = OverloadResolve (ec, right_side);
5646                                 if (expr == null)
5647                                         return null;
5648
5649                                 if (expr != this)
5650                                         return expr.ResolveLValue (ec, right_side);
5651                         }
5652
5653                         if (!ResolveSetter (ec))
5654                                 return null;
5655
5656                         return this;
5657                 }
5658
5659                 //
5660                 // Implements the IAssignMethod interface for assignments
5661                 //
5662                 public virtual void Emit (EmitContext ec, bool leave_copy)
5663                 {
5664                         var call = new CallEmitter ();
5665                         call.InstanceExpression = InstanceExpression;
5666                         if (has_await_arguments)
5667                                 call.HasAwaitArguments = true;
5668                         else
5669                                 call.DuplicateArguments = emitting_compound_assignment;
5670
5671                         call.Emit (ec, Getter, Arguments, loc);
5672
5673                         if (call.HasAwaitArguments) {
5674                                 InstanceExpression = call.InstanceExpression;
5675                                 Arguments = call.EmittedArguments;
5676                                 has_await_arguments = true;
5677                         }
5678
5679                         if (leave_copy) {
5680                                 ec.Emit (OpCodes.Dup);
5681                                 temp = new LocalTemporary (Type);
5682                                 temp.Store (ec);
5683                         }
5684                 }
5685
5686                 public abstract void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound);
5687
5688                 public override void Emit (EmitContext ec)
5689                 {
5690                         Emit (ec, false);
5691                 }
5692
5693                 protected override void EmitToFieldSource (EmitContext ec)
5694                 {
5695                         has_await_arguments = true;
5696                         Emit (ec, false);
5697                 }
5698
5699                 public abstract SLE.Expression MakeAssignExpression (BuilderContext ctx, Expression source);
5700
5701                 protected abstract Expression OverloadResolve (ResolveContext rc, Expression right_side);
5702
5703                 bool ResolveGetter (ResolveContext rc)
5704                 {
5705                         if (!best_candidate.HasGet) {
5706                                 if (InstanceExpression != EmptyExpression.Null) {
5707                                         rc.Report.SymbolRelatedToPreviousError (best_candidate);
5708                                         rc.Report.Error (154, loc, "The property or indexer `{0}' cannot be used in this context because it lacks the `get' accessor",
5709                                                 best_candidate.GetSignatureForError ());
5710                                         return false;
5711                                 }
5712                         } else if (!best_candidate.Get.IsAccessible (rc)) {
5713                                 if (best_candidate.HasDifferentAccessibility) {
5714                                         rc.Report.SymbolRelatedToPreviousError (best_candidate.Get);
5715                                         rc.Report.Error (271, loc, "The property or indexer `{0}' cannot be used in this context because the get accessor is inaccessible",
5716                                                 TypeManager.CSharpSignature (best_candidate));
5717                                 } else {
5718                                         rc.Report.SymbolRelatedToPreviousError (best_candidate.Get);
5719                                         ErrorIsInaccesible (rc, best_candidate.Get.GetSignatureForError (), loc);
5720                                 }
5721                         }
5722
5723                         if (best_candidate.HasDifferentAccessibility) {
5724                                 CheckProtectedMemberAccess (rc, best_candidate.Get);
5725                         }
5726
5727                         getter = CandidateToBaseOverride (rc, best_candidate.Get);
5728                         return true;
5729                 }
5730
5731                 bool ResolveSetter (ResolveContext rc)
5732                 {
5733                         if (!best_candidate.HasSet) {
5734                                 rc.Report.Error (200, loc, "Property or indexer `{0}' cannot be assigned to (it is read-only)",
5735                                         GetSignatureForError ());
5736                                 return false;
5737                         }
5738
5739                         if (!best_candidate.Set.IsAccessible (rc)) {
5740                                 if (best_candidate.HasDifferentAccessibility) {
5741                                         rc.Report.SymbolRelatedToPreviousError (best_candidate.Set);
5742                                         rc.Report.Error (272, loc, "The property or indexer `{0}' cannot be used in this context because the set accessor is inaccessible",
5743                                                 GetSignatureForError ());
5744                                 } else {
5745                                         rc.Report.SymbolRelatedToPreviousError (best_candidate.Set);
5746                                         ErrorIsInaccesible (rc, best_candidate.Set.GetSignatureForError (), loc);
5747                                 }
5748                         }
5749
5750                         if (best_candidate.HasDifferentAccessibility)
5751                                 CheckProtectedMemberAccess (rc, best_candidate.Set);
5752
5753                         setter = CandidateToBaseOverride (rc, best_candidate.Set);
5754                         return true;
5755                 }
5756         }
5757
5758         /// <summary>
5759         ///   Fully resolved expression that evaluates to an Event
5760         /// </summary>
5761         public class EventExpr : MemberExpr, IAssignMethod
5762         {
5763                 readonly EventSpec spec;
5764                 MethodSpec op;
5765
5766                 public EventExpr (EventSpec spec, Location loc)
5767                 {
5768                         this.spec = spec;
5769                         this.loc = loc;
5770                 }
5771
5772                 #region Properties
5773
5774                 protected override TypeSpec DeclaringType {
5775                         get {
5776                                 return spec.DeclaringType;
5777                         }
5778                 }
5779
5780                 public override string Name {
5781                         get {
5782                                 return spec.Name;
5783                         }
5784                 }
5785
5786                 public override bool IsInstance {
5787                         get {
5788                                 return !spec.IsStatic;
5789                         }
5790                 }
5791
5792                 public override bool IsStatic {
5793                         get {
5794                                 return spec.IsStatic;
5795                         }
5796                 }
5797
5798                 public MethodSpec Operator {
5799                         get {
5800                                 return op;
5801                         }
5802                 }
5803
5804                 #endregion
5805
5806                 public override MemberExpr ResolveMemberAccess (ResolveContext ec, Expression left, SimpleName original)
5807                 {
5808                         //
5809                         // If the event is local to this class and we are not lhs of +=/-= we transform ourselves into a FieldExpr
5810                         //
5811                         if (!ec.HasSet (ResolveContext.Options.CompoundAssignmentScope)) {
5812                                 if (spec.BackingField != null &&
5813                                         (spec.DeclaringType == ec.CurrentType || TypeManager.IsNestedChildOf (ec.CurrentType, spec.DeclaringType.MemberDefinition))) {
5814
5815                                         spec.MemberDefinition.SetIsUsed ();
5816
5817                                         if (!ec.IsObsolete) {
5818                                                 ObsoleteAttribute oa = spec.GetAttributeObsolete ();
5819                                                 if (oa != null)
5820                                                         AttributeTester.Report_ObsoleteMessage (oa, spec.GetSignatureForError (), loc, ec.Report);
5821                                         }
5822
5823                                         if ((spec.Modifiers & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
5824                                                 Error_AssignmentEventOnly (ec);
5825
5826                                         FieldExpr ml = new FieldExpr (spec.BackingField, loc);
5827
5828                                         InstanceExpression = null;
5829
5830                                         return ml.ResolveMemberAccess (ec, left, original);
5831                                 }
5832                         }
5833
5834                         return base.ResolveMemberAccess (ec, left, original);
5835                 }
5836
5837                 public override Expression CreateExpressionTree (ResolveContext ec)
5838                 {
5839                         throw new NotSupportedException ("ET");
5840                 }
5841
5842                 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
5843                 {
5844                         if (right_side == EmptyExpression.EventAddition) {
5845                                 op = spec.AccessorAdd;
5846                         } else if (right_side == EmptyExpression.EventSubtraction) {
5847                                 op = spec.AccessorRemove;
5848                         }
5849
5850                         if (op == null) {
5851                                 Error_AssignmentEventOnly (ec);
5852                                 return null;
5853                         }
5854
5855                         op = CandidateToBaseOverride (ec, op);
5856                         return this;
5857                 }
5858
5859                 protected override Expression DoResolve (ResolveContext ec)
5860                 {
5861                         eclass = ExprClass.EventAccess;
5862                         type = spec.MemberType;
5863
5864                         ResolveInstanceExpression (ec, null);
5865
5866                         if (!ec.HasSet (ResolveContext.Options.CompoundAssignmentScope)) {
5867                                 Error_AssignmentEventOnly (ec);
5868                         }
5869
5870                         DoBestMemberChecks (ec, spec);
5871                         return this;
5872                 }               
5873
5874                 public override void Emit (EmitContext ec)
5875                 {
5876                         throw new NotSupportedException ();
5877                         //Error_CannotAssign ();
5878                 }
5879
5880                 #region IAssignMethod Members
5881
5882                 public void Emit (EmitContext ec, bool leave_copy)
5883                 {
5884                         throw new NotImplementedException ();
5885                 }
5886
5887                 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
5888                 {
5889                         if (leave_copy || !isCompound)
5890                                 throw new NotImplementedException ("EventExpr::EmitAssign");
5891
5892                         Arguments args = new Arguments (1);
5893                         args.Add (new Argument (source));
5894
5895                         var call = new CallEmitter ();
5896                         call.InstanceExpression = InstanceExpression;
5897                         call.Emit (ec, op, args, loc);
5898                 }
5899
5900                 #endregion
5901
5902                 void Error_AssignmentEventOnly (ResolveContext ec)
5903                 {
5904                         if (spec.DeclaringType == ec.CurrentType || TypeManager.IsNestedChildOf (ec.CurrentType, spec.DeclaringType.MemberDefinition)) {
5905                                 ec.Report.Error (79, loc,
5906                                         "The event `{0}' can only appear on the left hand side of `+=' or `-=' operator",
5907                                         GetSignatureForError ());
5908                         } else {
5909                                 ec.Report.Error (70, loc,
5910                                         "The event `{0}' can only appear on the left hand side of += or -= when used outside of the type `{1}'",
5911                                         GetSignatureForError (), spec.DeclaringType.GetSignatureForError ());
5912                         }
5913                 }
5914
5915                 protected override void Error_CannotCallAbstractBase (ResolveContext rc, string name)
5916                 {
5917                         name = name.Substring (0, name.LastIndexOf ('.'));
5918                         base.Error_CannotCallAbstractBase (rc, name);
5919                 }
5920
5921                 public override string GetSignatureForError ()
5922                 {
5923                         return TypeManager.CSharpSignature (spec);
5924                 }
5925
5926                 public override void SetTypeArguments (ResolveContext ec, TypeArguments ta)
5927                 {
5928                         Error_TypeArgumentsCannotBeUsed (ec, "event", GetSignatureForError (), loc);
5929                 }
5930         }
5931
5932         public class TemporaryVariableReference : VariableReference
5933         {
5934                 public class Declarator : Statement
5935                 {
5936                         TemporaryVariableReference variable;
5937
5938                         public Declarator (TemporaryVariableReference variable)
5939                         {
5940                                 this.variable = variable;
5941                                 loc = variable.loc;
5942                         }
5943
5944                         protected override void DoEmit (EmitContext ec)
5945                         {
5946                                 variable.li.CreateBuilder (ec);
5947                         }
5948
5949                         protected override void CloneTo (CloneContext clonectx, Statement target)
5950                         {
5951                                 // Nothing
5952                         }
5953                 }
5954
5955                 LocalVariable li;
5956
5957                 public TemporaryVariableReference (LocalVariable li, Location loc)
5958                 {
5959                         this.li = li;
5960                         this.type = li.Type;
5961                         this.loc = loc;
5962                 }
5963
5964                 public override bool IsLockedByStatement {
5965                         get {
5966                                 return false;
5967                         }
5968                         set {
5969                         }
5970                 }
5971
5972                 public LocalVariable LocalInfo {
5973                     get {
5974                         return li;
5975                     }
5976                 }
5977
5978                 public static TemporaryVariableReference Create (TypeSpec type, Block block, Location loc)
5979                 {
5980                         var li = LocalVariable.CreateCompilerGenerated (type, block, loc);
5981                         return new TemporaryVariableReference (li, loc);
5982                 }
5983
5984                 public override Expression CreateExpressionTree (ResolveContext ec)
5985                 {
5986                         throw new NotSupportedException ("ET");
5987                 }
5988
5989                 protected override Expression DoResolve (ResolveContext ec)
5990                 {
5991                         eclass = ExprClass.Variable;
5992
5993                         //
5994                         // Don't capture temporary variables except when using
5995                         // iterator redirection
5996                         //
5997                         if (ec.CurrentAnonymousMethod != null && ec.CurrentAnonymousMethod.IsIterator && ec.IsVariableCapturingRequired) {
5998                                 AnonymousMethodStorey storey = li.Block.Explicit.CreateAnonymousMethodStorey (ec);
5999                                 storey.CaptureLocalVariable (ec, li);
6000                         }
6001
6002                         return this;
6003                 }
6004
6005                 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
6006                 {
6007                         return Resolve (ec);
6008                 }
6009                 
6010                 public override void Emit (EmitContext ec)
6011                 {
6012                         li.CreateBuilder (ec);
6013
6014                         Emit (ec, false);
6015                 }
6016
6017                 public void EmitAssign (EmitContext ec, Expression source)
6018                 {
6019                         li.CreateBuilder (ec);
6020
6021                         EmitAssign (ec, source, false, false);
6022                 }
6023
6024                 public override HoistedVariable GetHoistedVariable (AnonymousExpression ae)
6025                 {
6026                         return li.HoistedVariant;
6027                 }
6028
6029                 public override bool IsFixed {
6030                         get { return true; }
6031                 }
6032
6033                 public override bool IsRef {
6034                         get { return false; }
6035                 }
6036
6037                 public override string Name {
6038                         get { throw new NotImplementedException (); }
6039                 }
6040
6041                 public override void SetHasAddressTaken ()
6042                 {
6043                         throw new NotImplementedException ();
6044                 }
6045
6046                 protected override ILocalVariable Variable {
6047                         get { return li; }
6048                 }
6049
6050                 public override VariableInfo VariableInfo {
6051                         get { throw new NotImplementedException (); }
6052                 }
6053         }
6054
6055         /// 
6056         /// Handles `var' contextual keyword; var becomes a keyword only
6057         /// if no type called var exists in a variable scope
6058         /// 
6059         class VarExpr : SimpleName
6060         {
6061                 public VarExpr (Location loc)
6062                         : base ("var", loc)
6063                 {
6064                 }
6065
6066                 public bool InferType (ResolveContext ec, Expression right_side)
6067                 {
6068                         if (type != null)
6069                                 throw new InternalErrorException ("An implicitly typed local variable could not be redefined");
6070                         
6071                         type = right_side.Type;
6072                         if (type == InternalType.NullLiteral || type.Kind == MemberKind.Void || type == InternalType.AnonymousMethod || type == InternalType.MethodGroup) {
6073                                 ec.Report.Error (815, loc,
6074                                         "An implicitly typed local variable declaration cannot be initialized with `{0}'",
6075                                         type.GetSignatureForError ());
6076                                 return false;
6077                         }
6078
6079                         eclass = ExprClass.Variable;
6080                         return true;
6081                 }
6082
6083                 protected override void Error_TypeOrNamespaceNotFound (IMemberContext ec)
6084                 {
6085                         if (ec.Module.Compiler.Settings.Version < LanguageVersion.V_3)
6086                                 base.Error_TypeOrNamespaceNotFound (ec);
6087                         else
6088                                 ec.Module.Compiler.Report.Error (825, loc, "The contextual keyword `var' may only appear within a local variable declaration");
6089                 }
6090         }
6091 }