[sgen] Fix logging of major heap size with concurrent sweep
[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@gmail.com)
7 //
8 // Copyright 2001, 2002, 2003 Ximian, Inc.
9 // Copyright 2003-2008 Novell, Inc.
10 // Copyright 2011-2012 Xamarin Inc.
11 //
12 //
13
14 using System;
15 using System.Collections.Generic;
16 using System.Text;
17 using SLE = System.Linq.Expressions;
18 using System.Linq;
19
20 #if STATIC
21 using IKVM.Reflection;
22 using IKVM.Reflection.Emit;
23 #else
24 using System.Reflection;
25 using System.Reflection.Emit;
26 #endif
27
28 namespace Mono.CSharp {
29
30         /// <remarks>
31         ///   The ExprClass class contains the is used to pass the 
32         ///   classification of an expression (value, variable, namespace,
33         ///   type, method group, property access, event access, indexer access,
34         ///   nothing).
35         /// </remarks>
36         public enum ExprClass : byte {
37                 Unresolved      = 0,
38                 
39                 Value,
40                 Variable,
41                 Namespace,
42                 Type,
43                 TypeParameter,
44                 MethodGroup,
45                 PropertyAccess,
46                 EventAccess,
47                 IndexerAccess,
48                 Nothing, 
49         }
50
51         /// <remarks>
52         ///   This is used to tell Resolve in which types of expressions we're
53         ///   interested.
54         /// </remarks>
55         [Flags]
56         public enum ResolveFlags {
57                 // Returns Value, Variable, PropertyAccess, EventAccess or IndexerAccess.
58                 VariableOrValue         = 1,
59
60                 // Returns a type expression.
61                 Type                    = 1 << 1,
62
63                 // Returns a method group.
64                 MethodGroup             = 1 << 2,
65
66                 TypeParameter   = 1 << 3,
67
68                 // Mask of all the expression class flags.
69                 MaskExprClass = VariableOrValue | Type | MethodGroup | TypeParameter,
70         }
71
72         //
73         // This is just as a hint to AddressOf of what will be done with the
74         // address.
75         [Flags]
76         public enum AddressOp {
77                 Store = 1,
78                 Load  = 2,
79                 LoadStore = 3
80         };
81         
82         /// <summary>
83         ///   This interface is implemented by variables
84         /// </summary>
85         public interface IMemoryLocation {
86                 /// <summary>
87                 ///   The AddressOf method should generate code that loads
88                 ///   the address of the object and leaves it on the stack.
89                 ///
90                 ///   The `mode' argument is used to notify the expression
91                 ///   of whether this will be used to read from the address or
92                 ///   write to the address.
93                 ///
94                 ///   This is just a hint that can be used to provide good error
95                 ///   reporting, and should have no other side effects. 
96                 /// </summary>
97                 void AddressOf (EmitContext ec, AddressOp mode);
98         }
99
100         //
101         // An expressions resolved as a direct variable reference
102         //
103         public interface IVariableReference : IFixedExpression
104         {
105                 bool IsHoisted { get; }
106                 string Name { get; }
107                 VariableInfo VariableInfo { get; }
108
109                 void SetHasAddressTaken ();
110         }
111
112         //
113         // Implemented by an expression which could be or is always
114         // fixed
115         //
116         public interface IFixedExpression
117         {
118                 bool IsFixed { get; }
119         }
120
121         public interface IExpressionCleanup
122         {
123                 void EmitCleanup (EmitContext ec);
124         }
125
126         /// <remarks>
127         ///   Base class for expressions
128         /// </remarks>
129         public abstract class Expression {
130                 public ExprClass eclass;
131                 protected TypeSpec type;
132                 protected Location loc;
133                 
134                 public TypeSpec Type {
135                         get { return type; }
136                         set { type = value; }
137                 }
138
139                 public virtual bool IsSideEffectFree {
140                         get {
141                                 return false;
142                         }
143                 }
144
145                 public Location Location {
146                         get { return loc; }
147                 }
148
149                 public virtual bool IsNull {
150                         get {
151                                 return false;
152                         }
153                 }
154
155                 //
156                 // Used to workaround parser limitation where we cannot get
157                 // start of statement expression location
158                 //
159                 public virtual Location StartLocation {
160                         get {
161                                 return loc;
162                         }
163                 }
164
165                 public virtual MethodGroupExpr CanReduceLambda (AnonymousMethodBody body)
166                 {
167                         //
168                         // Return method-group expression when the expression can be used as
169                         // lambda replacement. A good example is array sorting where instead of
170                         // code like
171                         //
172                         //  Array.Sort (s, (a, b) => String.Compare (a, b));
173                         //
174                         // we can use method group directly
175                         //
176                         //  Array.Sort (s, String.Compare);
177                         //
178                         // Correct overload will be used because we do the reduction after
179                         // best candidate was found.
180                         //
181                         return null;
182                 }
183
184                 //
185                 // Returns true when the expression during Emit phase breaks stack
186                 // by using await expression
187                 //
188                 public virtual bool ContainsEmitWithAwait ()
189                 {
190                         return false;
191                 }
192
193                 /// <summary>
194                 ///   Performs semantic analysis on the Expression
195                 /// </summary>
196                 ///
197                 /// <remarks>
198                 ///   The Resolve method is invoked to perform the semantic analysis
199                 ///   on the node.
200                 ///
201                 ///   The return value is an expression (it can be the
202                 ///   same expression in some cases) or a new
203                 ///   expression that better represents this node.
204                 ///   
205                 ///   For example, optimizations of Unary (LiteralInt)
206                 ///   would return a new LiteralInt with a negated
207                 ///   value.
208                 ///   
209                 ///   If there is an error during semantic analysis,
210                 ///   then an error should be reported (using Report)
211                 ///   and a null value should be returned.
212                 ///   
213                 ///   There are two side effects expected from calling
214                 ///   Resolve(): the the field variable "eclass" should
215                 ///   be set to any value of the enumeration
216                 ///   `ExprClass' and the type variable should be set
217                 ///   to a valid type (this is the type of the
218                 ///   expression).
219                 /// </remarks>
220                 protected abstract Expression DoResolve (ResolveContext rc);
221
222                 public virtual Expression DoResolveLValue (ResolveContext rc, Expression right_side)
223                 {
224                         return null;
225                 }
226
227                 //
228                 // This is used if the expression should be resolved as a type or namespace name.
229                 // the default implementation fails.   
230                 //
231                 public virtual TypeSpec ResolveAsType (IMemberContext mc, bool allowUnboundTypeArguments = false)
232                 {
233                         var rc = mc as ResolveContext ?? new ResolveContext (mc);
234                         Expression e = Resolve (rc);
235                         if (e != null)
236                                 e.Error_UnexpectedKind (rc, ResolveFlags.Type, loc);
237
238                         return null;
239                 }
240
241                 public static void ErrorIsInaccesible (IMemberContext rc, string member, Location loc)
242                 {
243                         rc.Module.Compiler.Report.Error (122, loc, "`{0}' is inaccessible due to its protection level", member);
244                 }
245
246                 public void Error_ExpressionMustBeConstant (ResolveContext rc, Location loc, string e_name)
247                 {
248                         rc.Report.Error (133, loc, "The expression being assigned to `{0}' must be constant", e_name);
249                 }
250
251                 public void Error_ConstantCanBeInitializedWithNullOnly (ResolveContext rc, TypeSpec type, Location loc, string name)
252                 {
253                         rc.Report.Error (134, loc, "A constant `{0}' of reference type `{1}' can only be initialized with null",
254                                 name, type.GetSignatureForError ());
255                 }
256
257                 protected virtual void Error_InvalidExpressionStatement (Report report, Location loc)
258                 {
259                         report.Error (201, loc, "Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement");
260                 }
261                 
262                 public void Error_InvalidExpressionStatement (BlockContext bc)
263                 {
264                         Error_InvalidExpressionStatement (bc.Report, loc);
265                 }
266
267                 public void Error_InvalidExpressionStatement (Report report)
268                 {
269                         Error_InvalidExpressionStatement (report, loc);
270                 }
271
272                 public static void Error_VoidInvalidInTheContext (Location loc, Report Report)
273                 {
274                         Report.Error (1547, loc, "Keyword `void' cannot be used in this context");
275                 }
276
277                 public virtual void Error_ValueCannotBeConverted (ResolveContext ec, TypeSpec target, bool expl)
278                 {
279                         Error_ValueCannotBeConvertedCore (ec, loc, target, expl);
280                 }
281
282                 protected void Error_ValueCannotBeConvertedCore (ResolveContext ec, Location loc, TypeSpec target, bool expl)
283                 {
284                         // The error was already reported as CS1660
285                         if (type == InternalType.AnonymousMethod)
286                                 return;
287
288                         if (type == InternalType.ErrorType || target == InternalType.ErrorType)
289                                 return;
290
291                         string from_type = type.GetSignatureForError ();
292                         string to_type = target.GetSignatureForError ();
293                         if (from_type == to_type) {
294                                 from_type = type.GetSignatureForErrorIncludingAssemblyName ();
295                                 to_type = target.GetSignatureForErrorIncludingAssemblyName ();
296                         }
297
298                         if (expl) {
299                                 ec.Report.Error (30, loc, "Cannot convert type `{0}' to `{1}'",
300                                         from_type, to_type);
301                                 return;
302                         }
303
304                         ec.Report.DisableReporting ();
305                         bool expl_exists = Convert.ExplicitConversion (ec, this, target, Location.Null) != null;
306                         ec.Report.EnableReporting ();
307
308                         if (expl_exists) {
309                                 ec.Report.Error (266, loc,
310                                         "Cannot implicitly convert type `{0}' to `{1}'. An explicit conversion exists (are you missing a cast?)",
311                                         from_type, to_type);
312                         } else {
313                                 ec.Report.Error (29, loc, "Cannot implicitly convert type `{0}' to `{1}'",
314                                         from_type, to_type);
315                         }
316                 }
317
318                 public void Error_TypeArgumentsCannotBeUsed (IMemberContext context, MemberSpec member, Location loc)
319                 {
320                         // Better message for possible generic expressions
321                         if (member != null && (member.Kind & MemberKind.GenericMask) != 0) {
322                                 var report = context.Module.Compiler.Report;
323                                 report.SymbolRelatedToPreviousError (member);
324                                 if (member is TypeSpec)
325                                         member = ((TypeSpec) member).GetDefinition ();
326                                 else
327                                         member = ((MethodSpec) member).GetGenericMethodDefinition ();
328
329                                 string name = member.Kind == MemberKind.Method ? "method" : "type";
330                                 if (member.IsGeneric) {
331                                         report.Error (305, loc, "Using the generic {0} `{1}' requires `{2}' type argument(s)",
332                                                 name, member.GetSignatureForError (), member.Arity.ToString ());
333                                 } else {
334                                         report.Error (308, loc, "The non-generic {0} `{1}' cannot be used with the type arguments",
335                                                 name, member.GetSignatureForError ());
336                                 }
337                         } else {
338                                 Error_TypeArgumentsCannotBeUsed (context, ExprClassName, GetSignatureForError (), loc);
339                         }
340                 }
341
342                 public static void Error_TypeArgumentsCannotBeUsed (IMemberContext context, string exprType, string name, Location loc)
343                 {
344                         context.Module.Compiler.Report.Error (307, loc, "The {0} `{1}' cannot be used with type arguments",
345                                 exprType, name);
346                 }
347
348                 public virtual void Error_TypeDoesNotContainDefinition (ResolveContext ec, TypeSpec type, string name)
349                 {
350                         Error_TypeDoesNotContainDefinition (ec, loc, type, name);
351                 }
352
353                 public static void Error_TypeDoesNotContainDefinition (ResolveContext ec, Location loc, TypeSpec type, string name)
354                 {
355                         ec.Report.SymbolRelatedToPreviousError (type);
356                         ec.Report.Error (117, loc, "`{0}' does not contain a definition for `{1}'",
357                                 type.GetSignatureForError (), name);
358                 }
359
360                 public virtual void Error_ValueAssignment (ResolveContext rc, Expression rhs)
361                 {
362                         if (rhs == EmptyExpression.LValueMemberAccess || rhs == EmptyExpression.LValueMemberOutAccess) {
363                                 // Already reported as CS1612
364                         } else if (rhs == EmptyExpression.OutAccess) {
365                                 rc.Report.Error (1510, loc, "A ref or out argument must be an assignable variable");
366                         } else {
367                                 rc.Report.Error (131, loc, "The left-hand side of an assignment must be a variable, a property or an indexer");
368                         }
369                 }
370
371                 protected void Error_VoidPointerOperation (ResolveContext rc)
372                 {
373                         rc.Report.Error (242, loc, "The operation in question is undefined on void pointers");
374                 }
375
376                 public static void Warning_UnreachableExpression (ResolveContext rc, Location loc)
377                 {
378                         rc.Report.Warning (429, 4, loc, "Unreachable expression code detected");
379                 }
380
381                 public ResolveFlags ExprClassToResolveFlags {
382                         get {
383                                 switch (eclass) {
384                                 case ExprClass.Type:
385                                 case ExprClass.Namespace:
386                                         return ResolveFlags.Type;
387                                         
388                                 case ExprClass.MethodGroup:
389                                         return ResolveFlags.MethodGroup;
390                                         
391                                 case ExprClass.TypeParameter:
392                                         return ResolveFlags.TypeParameter;
393                                         
394                                 case ExprClass.Value:
395                                 case ExprClass.Variable:
396                                 case ExprClass.PropertyAccess:
397                                 case ExprClass.EventAccess:
398                                 case ExprClass.IndexerAccess:
399                                         return ResolveFlags.VariableOrValue;
400                                         
401                                 default:
402                                         throw new InternalErrorException (loc.ToString () + " " +  GetType () + " ExprClass is Invalid after resolve");
403                                 }
404                         }
405                 }
406
407                 //
408                 // Implements identical simple name and type-name resolution
409                 //
410                 public Expression ProbeIdenticalTypeName (ResolveContext rc, Expression left, SimpleName name)
411                 {
412                         var t = left.Type;
413                         if (t.Kind == MemberKind.InternalCompilerType || t is ElementTypeSpec || t.Arity > 0)
414                                 return left;
415
416                         // 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
417                         // a constant, field, property, local variable, or parameter with the same type as the meaning of E as a type-name
418
419                         if (left is MemberExpr || left is VariableReference) {
420                                 var identical_type = rc.LookupNamespaceOrType (name.Name, 0, LookupMode.Probing, loc) as TypeExpr;
421                                 if (identical_type != null && identical_type.Type == left.Type)
422                                         return identical_type;
423                         }
424
425                         return left;
426                 }
427
428                 public virtual string GetSignatureForError ()
429                 {
430                         return type.GetDefinition ().GetSignatureForError ();
431                 }
432
433                 public static bool IsNeverNull (Expression expr)
434                 {
435                         if (expr is This || expr is New || expr is ArrayCreation || expr is DelegateCreation || expr is ConditionalMemberAccess)
436                                 return true;
437
438                         var c = expr as Constant;
439                         if (c != null)
440                                 return !c.IsNull;
441
442                         var tc = expr as TypeCast;
443                         if (tc != null)
444                                 return IsNeverNull (tc.Child);
445
446                         return false;
447                 }
448
449                 protected static bool IsNullPropagatingValid (TypeSpec type)
450                 {
451                         switch (type.Kind) {
452                         case MemberKind.Struct:
453                                 return type.IsNullableType;
454                         case MemberKind.Enum:
455                         case MemberKind.Void:
456                         case MemberKind.PointerType:
457                                 return false;
458                         case MemberKind.InternalCompilerType:
459                                 return type.BuiltinType == BuiltinTypeSpec.Type.Dynamic;
460                         case MemberKind.TypeParameter:
461                                 return !((TypeParameterSpec) type).IsValueType;
462                         default:
463                                 return true;
464                         }
465                 }
466
467                 public virtual bool HasConditionalAccess ()
468                 {
469                         return false;
470                 }
471
472                 protected static TypeSpec LiftMemberType (ResolveContext rc, TypeSpec type)
473                 {
474                         return TypeSpec.IsValueType (type) && !type.IsNullableType ?
475                                 Nullable.NullableInfo.MakeType (rc.Module, type) :
476                                 type;
477                 }
478                
479                 /// <summary>
480                 ///   Resolves an expression and performs semantic analysis on it.
481                 /// </summary>
482                 ///
483                 /// <remarks>
484                 ///   Currently Resolve wraps DoResolve to perform sanity
485                 ///   checking and assertion checking on what we expect from Resolve.
486                 /// </remarks>
487                 public Expression Resolve (ResolveContext ec, ResolveFlags flags)
488                 {
489                         if (eclass != ExprClass.Unresolved) {
490                                 if ((flags & ExprClassToResolveFlags) == 0) {
491                                         Error_UnexpectedKind (ec, flags, loc);
492                                         return null;
493                                 }
494
495                                 return this;
496                         }
497                         
498                         Expression e;
499                         try {
500                                 e = DoResolve (ec);
501
502                                 if (e == null)
503                                         return null;
504
505                                 if ((flags & e.ExprClassToResolveFlags) == 0) {
506                                         e.Error_UnexpectedKind (ec, flags, loc);
507                                         return null;
508                                 }
509
510                                 if (e.type == null)
511                                         throw new InternalErrorException ("Expression `{0}' didn't set its type in DoResolve", e.GetType ());
512
513                                 return e;
514                         } catch (Exception ex) {
515                                 if (loc.IsNull || ec.Module.Compiler.Settings.BreakOnInternalError || ex is CompletionResult || ec.Report.IsDisabled || ex is FatalException ||
516                                         ec.Report.Printer is NullReportPrinter)
517                                         throw;
518
519                                 ec.Report.Error (584, loc, "Internal compiler error: {0}", ex.Message);
520                                 return ErrorExpression.Instance;        // TODO: Add location
521                         }
522                 }
523
524                 /// <summary>
525                 ///   Resolves an expression and performs semantic analysis on it.
526                 /// </summary>
527                 public Expression Resolve (ResolveContext rc)
528                 {
529                         return Resolve (rc, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
530                 }
531
532                 /// <summary>
533                 ///   Resolves an expression for LValue assignment
534                 /// </summary>
535                 ///
536                 /// <remarks>
537                 ///   Currently ResolveLValue wraps DoResolveLValue to perform sanity
538                 ///   checking and assertion checking on what we expect from Resolve
539                 /// </remarks>
540                 public Expression ResolveLValue (ResolveContext ec, Expression right_side)
541                 {
542                         int errors = ec.Report.Errors;
543                         bool out_access = right_side == EmptyExpression.OutAccess;
544
545                         Expression e = DoResolveLValue (ec, right_side);
546
547                         if (e != null && out_access && !(e is IMemoryLocation)) {
548                                 // FIXME: There's no problem with correctness, the 'Expr = null' handles that.
549                                 //        Enabling this 'throw' will "only" result in deleting useless code elsewhere,
550
551                                 //throw new InternalErrorException ("ResolveLValue didn't return an IMemoryLocation: " +
552                                 //                                e.GetType () + " " + e.GetSignatureForError ());
553                                 e = null;
554                         }
555
556                         if (e == null) {
557                                 if (errors == ec.Report.Errors) {
558                                         Error_ValueAssignment (ec, right_side);
559                                 }
560                                 return null;
561                         }
562
563                         if (e.eclass == ExprClass.Unresolved)
564                                 throw new Exception ("Expression " + e + " ExprClass is Invalid after resolve");
565
566                         if ((e.type == null) && !(e is GenericTypeExpr))
567                                 throw new Exception ("Expression " + e + " did not set its type after Resolve");
568
569                         return e;
570                 }
571
572                 public Constant ResolveLabelConstant (ResolveContext rc)
573                 {
574                         var expr = Resolve (rc);
575                         if (expr == null)
576                                 return null;
577
578                         Constant c = expr as Constant;
579                         if (c == null) {
580                                 if (expr.type != InternalType.ErrorType)
581                                         rc.Report.Error (150, expr.StartLocation, "A constant value is expected");
582
583                                 return null;
584                         }
585
586                         return c;
587                 }
588
589                 public virtual void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
590                 {
591                         if (Attribute.IsValidArgumentType (parameterType)) {
592                                 rc.Module.Compiler.Report.Error (182, loc,
593                                         "An attribute argument must be a constant expression, typeof expression or array creation expression");
594                         } else {
595                                 rc.Module.Compiler.Report.Error (181, loc,
596                                         "Attribute constructor parameter has type `{0}', which is not a valid attribute parameter type",
597                                         targetType.GetSignatureForError ());
598                         }
599                 }
600
601                 /// <summary>
602                 ///   Emits the code for the expression
603                 /// </summary>
604                 ///
605                 /// <remarks>
606                 ///   The Emit method is invoked to generate the code
607                 ///   for the expression.  
608                 /// </remarks>
609                 public abstract void Emit (EmitContext ec);
610
611
612                 // Emit code to branch to @target if this expression is equivalent to @on_true.
613                 // The default implementation is to emit the value, and then emit a brtrue or brfalse.
614                 // Subclasses can provide more efficient implementations, but those MUST be equivalent,
615                 // including the use of conditional branches.  Note also that a branch MUST be emitted
616                 public virtual void EmitBranchable (EmitContext ec, Label target, bool on_true)
617                 {
618                         Emit (ec);
619                         ec.Emit (on_true ? OpCodes.Brtrue : OpCodes.Brfalse, target);
620                 }
621
622                 // Emit this expression for its side effects, not for its value.
623                 // The default implementation is to emit the value, and then throw it away.
624                 // Subclasses can provide more efficient implementations, but those MUST be equivalent
625                 public virtual void EmitSideEffect (EmitContext ec)
626                 {
627                         Emit (ec);
628                         ec.Emit (OpCodes.Pop);
629                 }
630
631                 //
632                 // Emits the expression into temporary field variable. The method
633                 // should be used for await expressions only
634                 //
635                 public virtual Expression EmitToField (EmitContext ec)
636                 {
637                         //
638                         // This is the await prepare Emit method. When emitting code like
639                         // a + b we emit code like
640                         //
641                         // a.Emit ()
642                         // b.Emit ()
643                         // Opcodes.Add
644                         //
645                         // For await a + await b we have to interfere the flow to keep the
646                         // stack clean because await yields from the expression. The emit
647                         // then changes to
648                         //
649                         // a = a.EmitToField () // a is changed to temporary field access
650                         // b = b.EmitToField ()
651                         // a.Emit ()
652                         // b.Emit ()
653                         // Opcodes.Add
654                         //
655                         //
656                         // The idea is to emit expression and leave the stack empty with
657                         // result value still available.
658                         //
659                         // Expressions should override this default implementation when
660                         // optimized version can be provided (e.g. FieldExpr)
661                         //
662                         //
663                         // We can optimize for side-effect free expressions, they can be
664                         // emitted out of order
665                         //
666                         if (IsSideEffectFree)
667                                 return this;
668
669                         bool needs_temporary = ContainsEmitWithAwait ();
670                         if (!needs_temporary)
671                                 ec.EmitThis ();
672
673                         // Emit original code
674                         var field = EmitToFieldSource (ec);
675                         if (field == null) {
676                                 //
677                                 // Store the result to temporary field when we
678                                 // cannot load `this' directly
679                                 //
680                                 field = ec.GetTemporaryField (type);
681                                 if (needs_temporary) {
682                                         //
683                                         // Create temporary local (we cannot load `this' before Emit)
684                                         //
685                                         var temp = ec.GetTemporaryLocal (type);
686                                         ec.Emit (OpCodes.Stloc, temp);
687
688                                         ec.EmitThis ();
689                                         ec.Emit (OpCodes.Ldloc, temp);
690                                         field.EmitAssignFromStack (ec);
691
692                                         ec.FreeTemporaryLocal (temp, type);
693                                 } else {
694                                         field.EmitAssignFromStack (ec);
695                                 }
696                         }
697
698                         return field;
699                 }
700
701                 protected virtual FieldExpr EmitToFieldSource (EmitContext ec)
702                 {
703                         //
704                         // Default implementation calls Emit method
705                         //
706                         Emit (ec);
707                         return null;
708                 }
709
710                 protected static void EmitExpressionsList (EmitContext ec, List<Expression> expressions)
711                 {
712                         if (ec.HasSet (BuilderContext.Options.AsyncBody)) {
713                                 bool contains_await = false;
714
715                                 for (int i = 1; i < expressions.Count; ++i) {
716                                         if (expressions[i].ContainsEmitWithAwait ()) {
717                                                 contains_await = true;
718                                                 break;
719                                         }
720                                 }
721
722                                 if (contains_await) {
723                                         for (int i = 0; i < expressions.Count; ++i) {
724                                                 expressions[i] = expressions[i].EmitToField (ec);
725                                         }
726                                 }
727                         }
728
729                         for (int i = 0; i < expressions.Count; ++i) {
730                                 expressions[i].Emit (ec);
731                         }
732                 }
733
734                 /// <summary>
735                 ///   Protected constructor.  Only derivate types should
736                 ///   be able to be created
737                 /// </summary>
738
739                 protected Expression ()
740                 {
741                 }
742
743                 /// <summary>
744                 ///   Returns a fully formed expression after a MemberLookup
745                 /// </summary>
746                 /// 
747                 static Expression ExprClassFromMemberInfo (MemberSpec spec, Location loc)
748                 {
749                         if (spec is EventSpec)
750                                 return new EventExpr ((EventSpec) spec, loc);
751                         if (spec is ConstSpec)
752                                 return new ConstantExpr ((ConstSpec) spec, loc);
753                         if (spec is FieldSpec)
754                                 return new FieldExpr ((FieldSpec) spec, loc);
755                         if (spec is PropertySpec)
756                                 return new PropertyExpr ((PropertySpec) spec, loc);
757                         if (spec is TypeSpec)
758                                 return new TypeExpression (((TypeSpec) spec), loc);
759
760                         return null;
761                 }
762
763                 public static MethodSpec ConstructorLookup (ResolveContext rc, TypeSpec type, ref Arguments args, Location loc)
764                 {
765                         var ctors = MemberCache.FindMembers (type, Constructor.ConstructorName, true);
766                         if (ctors == null) {
767                                 switch (type.Kind) {
768                                 case MemberKind.Struct:
769                                         // Every struct has implicit default constructor if not provided by user
770                                         if (args == null)
771                                                 return null;
772
773                                         rc.Report.SymbolRelatedToPreviousError (type);
774                                         // Report meaningful error for struct as they always have default ctor in C# context
775                                         OverloadResolver.Error_ConstructorMismatch (rc, type, args == null ? 0 : args.Count, loc);
776                                         break;
777                                 case MemberKind.MissingType:
778                                 case MemberKind.InternalCompilerType:
779 // LAMESPEC: dynamic is not really object
780 //                                      if (type.BuiltinType == BuiltinTypeSpec.Type.Object)
781 //                                              goto default;
782                                         break;
783                                 default:
784                                         rc.Report.SymbolRelatedToPreviousError (type);
785                                         rc.Report.Error (143, loc, "The class `{0}' has no constructors defined",
786                                                 type.GetSignatureForError ());
787                                         break;
788                                 }
789
790                                 return null;
791                         }
792
793                         if (args == null && type.IsStruct) {
794                                 bool includes_empty = false;
795                                 foreach (MethodSpec ctor in ctors) {
796                                         if (ctor.Parameters.IsEmpty) {
797                                                 includes_empty = true;
798                                         }
799                                 }
800
801                                 if (!includes_empty)
802                                         return null;
803                         }
804
805                         var r = new OverloadResolver (ctors, OverloadResolver.Restrictions.NoBaseMembers, loc);
806                         if (!rc.HasSet (ResolveContext.Options.BaseInitializer)) {
807                                 r.InstanceQualifier = new ConstructorInstanceQualifier (type);
808                         }
809
810                         return r.ResolveMember<MethodSpec> (rc, ref args);
811                 }
812
813                 [Flags]
814                 public enum MemberLookupRestrictions
815                 {
816                         None = 0,
817                         InvocableOnly = 1,
818                         ExactArity = 1 << 2,
819                         ReadAccess = 1 << 3,
820                         EmptyArguments = 1 << 4,
821                         IgnoreArity = 1 << 5,
822                         IgnoreAmbiguity = 1 << 6,
823                         NameOfExcluded = 1 << 7,
824                         DontSetConditionalAccess = 1 << 8
825                 }
826
827                 //
828                 // Lookup type `queried_type' for code in class `container_type' with a qualifier of
829                 // `qualifier_type' or null to lookup members in the current class.
830                 //
831                 public static Expression MemberLookup (IMemberContext rc, bool errorMode, TypeSpec queried_type, string name, int arity, MemberLookupRestrictions restrictions, Location loc)
832                 {
833                         var members = MemberCache.FindMembers (queried_type, name, false);
834                         if (members == null)
835                                 return null;
836
837                         Expression expr;
838                         do {
839                                 expr = MemberLookupToExpression (rc, members, errorMode, queried_type, name, arity, restrictions, loc);
840                                 if (expr != null)
841                                         return expr;
842
843                                 if (members [0].DeclaringType.BaseType == null)
844                                         members = null;
845                                 else
846                                         members = MemberCache.FindMembers (members [0].DeclaringType.BaseType, name, false);
847                         } while (members != null);
848
849                         return expr;
850                 }
851
852                 public static Expression MemberLookupToExpression (IMemberContext rc, IList<MemberSpec> members, bool errorMode, TypeSpec queried_type, string name, int arity, MemberLookupRestrictions restrictions, Location loc)
853                 {
854                         MemberSpec non_method = null;
855                         MemberSpec ambig_non_method = null;
856
857                         for (int i = 0; i < members.Count; ++i) {
858                                 var member = members [i];
859
860                                 // HACK: for events because +=/-= can appear at same class only, should use OverrideToBase there
861                                 if ((member.Modifiers & Modifiers.OVERRIDE) != 0 && member.Kind != MemberKind.Event)
862                                         continue;
863
864                                 if ((member.Modifiers & Modifiers.BACKING_FIELD) != 0 || member.Kind == MemberKind.Operator)
865                                         continue;
866
867                                 if ((arity > 0 || (restrictions & MemberLookupRestrictions.ExactArity) != 0) && member.Arity != arity)
868                                         continue;
869
870                                 if (!errorMode) {
871                                         if (!member.IsAccessible (rc))
872                                                 continue;
873
874                                         //
875                                         // With runtime binder we can have a situation where queried type is inaccessible
876                                         // because it came via dynamic object, the check about inconsisted accessibility
877                                         // had no effect as the type was unknown during compilation
878                                         //
879                                         // class A {
880                                         //              private class N { }
881                                         //
882                                         //              public dynamic Foo ()
883                                         //              {
884                                         //                      return new N ();
885                                         //              }
886                                         //      }
887                                         //
888                                         if (rc.Module.Compiler.IsRuntimeBinder && !member.DeclaringType.IsAccessible (rc))
889                                                 continue;
890                                 }
891
892                                 if ((restrictions & MemberLookupRestrictions.InvocableOnly) != 0) {
893                                         if (member is MethodSpec) {
894                                                 //
895                                                 // Interface members that are hidden by class members are removed from the set. This
896                                                 // step only has an effect if T is a type parameter and T has both an effective base 
897                                                 // class other than object and a non-empty effective interface set
898                                                 //
899                                                 var tps = queried_type as TypeParameterSpec;
900                                                 if (tps != null && tps.HasTypeConstraint)
901                                                         members = RemoveHiddenTypeParameterMethods (members);
902
903                                                 return new MethodGroupExpr (members, queried_type, loc);
904                                         }
905
906                                         if (!Invocation.IsMemberInvocable (member))
907                                                 continue;
908                                 }
909
910                                 if (non_method == null || member is MethodSpec || non_method.IsNotCSharpCompatible) {
911                                         non_method = member;
912                                 } else if (!errorMode && !member.IsNotCSharpCompatible) {
913                                         //
914                                         // Interface members that are hidden by class members are removed from the set when T is a type parameter and
915                                         // T has both an effective base class other than object and a non-empty effective interface set.
916                                         //
917                                         // The spec has more complex rules but we simply remove all members declared in an interface declaration.
918                                         //
919                                         var tps = queried_type as TypeParameterSpec;
920                                         if (tps != null && tps.HasTypeConstraint) {
921                                                 if (non_method.DeclaringType.IsClass && member.DeclaringType.IsInterface)
922                                                         continue;
923
924                                                 if (non_method.DeclaringType.IsInterface && member.DeclaringType.IsInterface) {
925                                                         non_method = member;
926                                                         continue;
927                                                 }
928                                         }
929
930                                         ambig_non_method = member;
931                                 }
932                         }
933
934                         if (non_method != null) {
935                                 if (ambig_non_method != null && rc != null && (restrictions & MemberLookupRestrictions.IgnoreAmbiguity) == 0) {
936                                         var report = rc.Module.Compiler.Report;
937                                         report.SymbolRelatedToPreviousError (non_method);
938                                         report.SymbolRelatedToPreviousError (ambig_non_method);
939                                         report.Error (229, loc, "Ambiguity between `{0}' and `{1}'",
940                                                 non_method.GetSignatureForError (), ambig_non_method.GetSignatureForError ());
941                                 }
942
943                                 if (non_method is MethodSpec)
944                                         return new MethodGroupExpr (members, queried_type, loc);
945
946                                 return ExprClassFromMemberInfo (non_method, loc);
947                         }
948
949                         return null;
950                 }
951
952                 static IList<MemberSpec> RemoveHiddenTypeParameterMethods (IList<MemberSpec> members)
953                 {
954                         if (members.Count < 2)
955                                 return members;
956
957                         //
958                         // If M is a method, then all non-method members declared in an interface declaration
959                         // are removed from the set, and all methods with the same signature as M declared in
960                         // an interface declaration are removed from the set
961                         //
962
963                         bool copied = false;
964                         for (int i = 0; i < members.Count; ++i) {
965                                 var method = members[i] as MethodSpec;
966                                 if (method == null) {
967                                         if (!copied) {
968                                                 copied = true;
969                                                 members = new List<MemberSpec> (members);
970                                         } 
971                                         
972                                         members.RemoveAt (i--);
973                                         continue;
974                                 }
975
976                                 if (!method.DeclaringType.IsInterface)
977                                         continue;
978
979                                 for (int ii = 0; ii < members.Count; ++ii) {
980                                         var candidate = members[ii] as MethodSpec;
981                                         if (candidate == null || !candidate.DeclaringType.IsClass)
982                                                 continue;
983
984                                         if (!TypeSpecComparer.Override.IsEqual (candidate.Parameters, method.Parameters))
985                                                 continue;
986
987                                         if (!AParametersCollection.HasSameParameterDefaults (candidate.Parameters, method.Parameters))
988                                                 continue;
989
990                                         if (!copied) {
991                                                 copied = true;
992                                                 members = new List<MemberSpec> (members);
993                                         }
994
995                                         members.RemoveAt (i--);
996                                         break;
997                                 }
998                         }
999
1000                         return members;
1001                 }
1002
1003                 protected static void Error_NamedArgument (NamedArgument na, Report Report)
1004                 {
1005                         Report.Error (1742, na.Location, "An element access expression cannot use named argument");
1006                 }
1007
1008                 protected virtual void Error_NegativeArrayIndex (ResolveContext ec, Location loc)
1009                 {
1010                         throw new NotImplementedException ();
1011                 }
1012
1013                 public virtual void Error_OperatorCannotBeApplied (ResolveContext rc, Location loc, string oper, TypeSpec t)
1014                 {
1015                         if (t == InternalType.ErrorType)
1016                                 return;
1017
1018                         rc.Report.Error (23, loc, "The `{0}' operator cannot be applied to operand of type `{1}'",
1019                                 oper, t.GetSignatureForError ());
1020                 }
1021
1022                 protected void Error_PointerInsideExpressionTree (ResolveContext ec)
1023                 {
1024                         ec.Report.Error (1944, loc, "An expression tree cannot contain an unsafe pointer operation");
1025                 }
1026
1027                 protected void Error_NullShortCircuitInsideExpressionTree (ResolveContext rc)
1028                 {
1029                         rc.Report.Error (8072, loc, "An expression tree cannot contain a null propagating operator");
1030                 }
1031
1032                 protected void Error_NullPropagatingLValue (ResolveContext rc)
1033                 {
1034                         rc.Report.Error (-1030, loc, "The left-hand side of an assignment cannot contain a null propagating operator");
1035                 }
1036
1037                 public virtual void FlowAnalysis (FlowAnalysisContext fc)
1038                 {
1039                 }
1040
1041                 //
1042                 // Special version of flow analysis for expressions which can return different
1043                 // on-true and on-false result. Used by &&, ||, ?: expressions
1044                 //
1045                 public virtual void FlowAnalysisConditional (FlowAnalysisContext fc)
1046                 {
1047                         FlowAnalysis (fc);
1048                         fc.DefiniteAssignmentOnTrue = fc.DefiniteAssignmentOnFalse = fc.DefiniteAssignment;
1049                 }
1050
1051                 /// <summary>
1052                 ///   Returns an expression that can be used to invoke operator true
1053                 ///   on the expression if it exists.
1054                 /// </summary>
1055                 protected static Expression GetOperatorTrue (ResolveContext ec, Expression e, Location loc)
1056                 {
1057                         return GetOperatorTrueOrFalse (ec, e, true, loc);
1058                 }
1059
1060                 /// <summary>
1061                 ///   Returns an expression that can be used to invoke operator false
1062                 ///   on the expression if it exists.
1063                 /// </summary>
1064                 protected static Expression GetOperatorFalse (ResolveContext ec, Expression e, Location loc)
1065                 {
1066                         return GetOperatorTrueOrFalse (ec, e, false, loc);
1067                 }
1068
1069                 static Expression GetOperatorTrueOrFalse (ResolveContext ec, Expression e, bool is_true, Location loc)
1070                 {
1071                         var op = is_true ? Operator.OpType.True : Operator.OpType.False;
1072                         var type = e.type;
1073                         if (type.IsNullableType)
1074                                 type = Nullable.NullableInfo.GetUnderlyingType (type);
1075
1076                         var methods = MemberCache.GetUserOperator (type, op, false);
1077                         if (methods == null)
1078                                 return null;
1079
1080                         Arguments arguments = new Arguments (1);
1081                         arguments.Add (new Argument (e));
1082
1083                         var res = new OverloadResolver (methods, OverloadResolver.Restrictions.BaseMembersIncluded | OverloadResolver.Restrictions.NoBaseMembers, loc);
1084                         var oper = res.ResolveOperator (ec, ref arguments);
1085
1086                         if (oper == null)
1087                                 return null;
1088
1089                         return new UserOperatorCall (oper, arguments, null, loc);
1090                 }
1091                 
1092                 public virtual string ExprClassName
1093                 {
1094                         get {
1095                                 switch (eclass){
1096                                 case ExprClass.Unresolved:
1097                                         return "Unresolved";
1098                                 case ExprClass.Value:
1099                                         return "value";
1100                                 case ExprClass.Variable:
1101                                         return "variable";
1102                                 case ExprClass.Namespace:
1103                                         return "namespace";
1104                                 case ExprClass.Type:
1105                                         return "type";
1106                                 case ExprClass.MethodGroup:
1107                                         return "method group";
1108                                 case ExprClass.PropertyAccess:
1109                                         return "property access";
1110                                 case ExprClass.EventAccess:
1111                                         return "event access";
1112                                 case ExprClass.IndexerAccess:
1113                                         return "indexer access";
1114                                 case ExprClass.Nothing:
1115                                         return "null";
1116                                 case ExprClass.TypeParameter:
1117                                         return "type parameter";
1118                                 }
1119                                 throw new Exception ("Should not happen");
1120                         }
1121                 }
1122                 
1123                 /// <summary>
1124                 ///   Reports that we were expecting `expr' to be of class `expected'
1125                 /// </summary>
1126                 public static void Error_UnexpectedKind (IMemberContext ctx, Expression memberExpr, string expected, string was, Location loc)
1127                 {
1128                         var name = memberExpr.GetSignatureForError ();
1129
1130                         ctx.Module.Compiler.Report.Error (118, loc, "`{0}' is a `{1}' but a `{2}' was expected", name, was, expected);
1131                 }
1132
1133                 public virtual void Error_UnexpectedKind (ResolveContext ec, ResolveFlags flags, Location loc)
1134                 {
1135                         string [] valid = new string [4];
1136                         int count = 0;
1137
1138                         if ((flags & ResolveFlags.VariableOrValue) != 0) {
1139                                 valid [count++] = "variable";
1140                                 valid [count++] = "value";
1141                         }
1142
1143                         if ((flags & ResolveFlags.Type) != 0)
1144                                 valid [count++] = "type";
1145
1146                         if ((flags & ResolveFlags.MethodGroup) != 0)
1147                                 valid [count++] = "method group";
1148
1149                         if (count == 0)
1150                                 valid [count++] = "unknown";
1151
1152                         StringBuilder sb = new StringBuilder (valid [0]);
1153                         for (int i = 1; i < count - 1; i++) {
1154                                 sb.Append ("', `");
1155                                 sb.Append (valid [i]);
1156                         }
1157                         if (count > 1) {
1158                                 sb.Append ("' or `");
1159                                 sb.Append (valid [count - 1]);
1160                         }
1161
1162                         ec.Report.Error (119, loc, 
1163                                 "Expression denotes a `{0}', where a `{1}' was expected", ExprClassName, sb.ToString ());
1164                 }
1165                 
1166                 public static void UnsafeError (ResolveContext ec, Location loc)
1167                 {
1168                         UnsafeError (ec.Report, loc);
1169                 }
1170
1171                 public static void UnsafeError (Report Report, Location loc)
1172                 {
1173                         Report.Error (214, loc, "Pointers and fixed size buffers may only be used in an unsafe context");
1174                 }
1175
1176                 //
1177                 // Converts `source' to an int, uint, long or ulong.
1178                 //
1179                 protected Expression ConvertExpressionToArrayIndex (ResolveContext ec, Expression source, bool pointerArray = false)
1180                 {
1181                         var btypes = ec.BuiltinTypes;
1182
1183                         if (source.type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
1184                                 Arguments args = new Arguments (1);
1185                                 args.Add (new Argument (source));
1186                                 return new DynamicConversion (btypes.Int, CSharpBinderFlags.ConvertArrayIndex, args, source.loc).Resolve (ec);
1187                         }
1188
1189                         Expression converted;
1190                         
1191                         using (ec.Set (ResolveContext.Options.CheckedScope)) {
1192                                 converted = Convert.ImplicitConversion (ec, source, btypes.Int, source.loc);
1193                                 if (converted == null)
1194                                         converted = Convert.ImplicitConversion (ec, source, btypes.UInt, source.loc);
1195                                 if (converted == null)
1196                                         converted = Convert.ImplicitConversion (ec, source, btypes.Long, source.loc);
1197                                 if (converted == null)
1198                                         converted = Convert.ImplicitConversion (ec, source, btypes.ULong, source.loc);
1199
1200                                 if (converted == null) {
1201                                         source.Error_ValueCannotBeConverted (ec, btypes.Int, false);
1202                                         return null;
1203                                 }
1204                         }
1205
1206                         if (pointerArray)
1207                                 return converted;
1208
1209                         //
1210                         // Only positive constants are allowed at compile time
1211                         //
1212                         Constant c = converted as Constant;
1213                         if (c != null && c.IsNegative)
1214                                 Error_NegativeArrayIndex (ec, source.loc);
1215
1216                         // No conversion needed to array index
1217                         if (converted.Type.BuiltinType == BuiltinTypeSpec.Type.Int)
1218                                 return converted;
1219
1220                         return new ArrayIndexCast (converted, btypes.Int).Resolve (ec);
1221                 }
1222
1223                 public Expression MakePointerAccess (ResolveContext rc, TypeSpec type, Arguments args)
1224                 {
1225                         if (args.Count != 1){
1226                                 rc.Report.Error (196, loc, "A pointer must be indexed by only one value");
1227                                 return null;
1228                         }
1229
1230                         var arg = args [0];
1231                         if (arg is NamedArgument)
1232                                 Error_NamedArgument ((NamedArgument) arg, rc.Report);
1233
1234                         var index = arg.Expr.Resolve (rc);
1235                         if (index == null)
1236                                 return null;
1237
1238                         index = ConvertExpressionToArrayIndex (rc, index, true);
1239
1240                         Expression p = new PointerArithmetic (Binary.Operator.Addition, this, index, type, loc);
1241                         return new Indirection (p, loc);
1242                 }
1243
1244                 //
1245                 // Derived classes implement this method by cloning the fields that
1246                 // could become altered during the Resolve stage
1247                 //
1248                 // Only expressions that are created for the parser need to implement
1249                 // this.
1250                 //
1251                 protected virtual void CloneTo (CloneContext clonectx, Expression target)
1252                 {
1253                         throw new NotImplementedException (
1254                                 String.Format (
1255                                         "CloneTo not implemented for expression {0}", this.GetType ()));
1256                 }
1257
1258                 //
1259                 // Clones an expression created by the parser.
1260                 //
1261                 // We only support expressions created by the parser so far, not
1262                 // expressions that have been resolved (many more classes would need
1263                 // to implement CloneTo).
1264                 //
1265                 // This infrastructure is here merely for Lambda expressions which
1266                 // compile the same code using different type values for the same
1267                 // arguments to find the correct overload
1268                 //
1269                 public virtual Expression Clone (CloneContext clonectx)
1270                 {
1271                         Expression cloned = (Expression) MemberwiseClone ();
1272                         CloneTo (clonectx, cloned);
1273
1274                         return cloned;
1275                 }
1276
1277                 //
1278                 // Implementation of expression to expression tree conversion
1279                 //
1280                 public abstract Expression CreateExpressionTree (ResolveContext ec);
1281
1282                 protected Expression CreateExpressionFactoryCall (ResolveContext ec, string name, Arguments args)
1283                 {
1284                         return CreateExpressionFactoryCall (ec, name, null, args, loc);
1285                 }
1286
1287                 protected Expression CreateExpressionFactoryCall (ResolveContext ec, string name, TypeArguments typeArguments, Arguments args)
1288                 {
1289                         return CreateExpressionFactoryCall (ec, name, typeArguments, args, loc);
1290                 }
1291
1292                 public static Expression CreateExpressionFactoryCall (ResolveContext ec, string name, TypeArguments typeArguments, Arguments args, Location loc)
1293                 {
1294                         return new Invocation (new MemberAccess (CreateExpressionTypeExpression (ec, loc), name, typeArguments, loc), args);
1295                 }
1296
1297                 protected static TypeExpr CreateExpressionTypeExpression (ResolveContext ec, Location loc)
1298                 {
1299                         var t = ec.Module.PredefinedTypes.Expression.Resolve ();
1300                         if (t == null)
1301                                 return null;
1302
1303                         return new TypeExpression (t, loc);
1304                 }
1305
1306                 //
1307                 // Implemented by all expressions which support conversion from
1308                 // compiler expression to invokable runtime expression. Used by
1309                 // dynamic C# binder.
1310                 //
1311                 public virtual SLE.Expression MakeExpression (BuilderContext ctx)
1312                 {
1313                         throw new NotImplementedException ("MakeExpression for " + GetType ());
1314                 }
1315                         
1316                 public virtual object Accept (StructuralVisitor visitor)
1317                 {
1318                         return visitor.Visit (this);
1319                 }
1320         }
1321
1322         /// <summary>
1323         ///   This is just a base class for expressions that can
1324         ///   appear on statements (invocations, object creation,
1325         ///   assignments, post/pre increment and decrement).  The idea
1326         ///   being that they would support an extra Emition interface that
1327         ///   does not leave a result on the stack.
1328         /// </summary>
1329         public abstract class ExpressionStatement : Expression
1330         {
1331                 public virtual void MarkReachable (Reachability rc)
1332                 {
1333                 }
1334
1335                 public ExpressionStatement ResolveStatement (BlockContext ec)
1336                 {
1337                         Expression e = Resolve (ec);
1338                         if (e == null)
1339                                 return null;
1340
1341                         ExpressionStatement es = e as ExpressionStatement;
1342                         if (es == null || e is AnonymousMethodBody) {
1343                                 var reduced = e as IReducedExpressionStatement;
1344                                 if (reduced != null) {
1345                                         return EmptyExpressionStatement.Instance;
1346                                 }
1347
1348                                 Error_InvalidExpressionStatement (ec);
1349                         }
1350
1351                         //
1352                         // This is quite expensive warning, try to limit the damage
1353                         //
1354                         if (MemberAccess.IsValidDotExpression (e.Type) && !(e is Assign || e is Await)) {
1355                                 WarningAsyncWithoutWait (ec, e);
1356                         }
1357
1358                         return es;
1359                 }
1360
1361                 static void WarningAsyncWithoutWait (BlockContext bc, Expression e)
1362                 {
1363                         if (bc.CurrentAnonymousMethod is AsyncInitializer) {
1364                                 var awaiter = new AwaitStatement.AwaitableMemberAccess (e) {
1365                                         ProbingMode = true
1366                                 };
1367
1368                                 //
1369                                 // Need to do full resolve because GetAwaiter can be extension method
1370                                 // available only in this context
1371                                 //
1372                                 var mg = awaiter.Resolve (bc) as MethodGroupExpr;
1373                                 if (mg == null)
1374                                         return;
1375
1376                                 var arguments = new Arguments (0);
1377                                 mg = mg.OverloadResolve (bc, ref arguments, null, OverloadResolver.Restrictions.ProbingOnly);
1378                                 if (mg == null)
1379                                         return;
1380
1381                                 //
1382                                 // Use same check rules as for real await
1383                                 //
1384                                 var awaiter_definition = bc.Module.GetAwaiter (mg.BestCandidateReturnType);
1385                                 if (!awaiter_definition.IsValidPattern || !awaiter_definition.INotifyCompletion)
1386                                         return;
1387
1388                                 bc.Report.Warning (4014, 1, e.Location,
1389                                         "The statement is not awaited and execution of current method continues before the call is completed. Consider using `await' operator");
1390                                 return;
1391                         }
1392
1393                         var inv = e as Invocation;
1394                         if (inv != null && inv.MethodGroup != null && inv.MethodGroup.BestCandidate.IsAsync) {
1395                                 // The warning won't be reported for imported methods to maintain warning compatiblity with csc 
1396                                 bc.Report.Warning (4014, 1, e.Location,
1397                                         "The statement is not awaited and execution of current method continues before the call is completed. Consider using `await' operator or calling `Wait' method");
1398                                 return;
1399                         }
1400                 }
1401
1402                 /// <summary>
1403                 ///   Requests the expression to be emitted in a `statement'
1404                 ///   context.  This means that no new value is left on the
1405                 ///   stack after invoking this method (constrasted with
1406                 ///   Emit that will always leave a value on the stack).
1407                 /// </summary>
1408                 public abstract void EmitStatement (EmitContext ec);
1409
1410                 public override void EmitSideEffect (EmitContext ec)
1411                 {
1412                         EmitStatement (ec);
1413                 }
1414         }
1415
1416         interface IReducedExpressionStatement
1417         {
1418         }
1419
1420         /// <summary>
1421         ///   This kind of cast is used to encapsulate the child
1422         ///   whose type is child.Type into an expression that is
1423         ///   reported to return "return_type".  This is used to encapsulate
1424         ///   expressions which have compatible types, but need to be dealt
1425         ///   at higher levels with.
1426         ///
1427         ///   For example, a "byte" expression could be encapsulated in one
1428         ///   of these as an "unsigned int".  The type for the expression
1429         ///   would be "unsigned int".
1430         ///
1431         /// </summary>
1432         public abstract class TypeCast : Expression
1433         {
1434                 protected readonly Expression child;
1435
1436                 protected TypeCast (Expression child, TypeSpec return_type)
1437                 {
1438                         eclass = child.eclass;
1439                         loc = child.Location;
1440                         type = return_type;
1441                         this.child = child;
1442                 }
1443
1444                 public Expression Child {
1445                         get {
1446                                 return child;
1447                         }
1448                 }
1449
1450                 public override bool ContainsEmitWithAwait ()
1451                 {
1452                         return child.ContainsEmitWithAwait ();
1453                 }
1454
1455                 public override Expression CreateExpressionTree (ResolveContext ec)
1456                 {
1457                         Arguments args = new Arguments (2);
1458                         args.Add (new Argument (child.CreateExpressionTree (ec)));
1459                         args.Add (new Argument (new TypeOf (type, loc)));
1460
1461                         if (type.IsPointer || child.Type.IsPointer)
1462                                 Error_PointerInsideExpressionTree (ec);
1463
1464                         return CreateExpressionFactoryCall (ec, ec.HasSet (ResolveContext.Options.CheckedScope) ? "ConvertChecked" : "Convert", args);
1465                 }
1466
1467                 protected override Expression DoResolve (ResolveContext ec)
1468                 {
1469                         // This should never be invoked, we are born in fully
1470                         // initialized state.
1471
1472                         return this;
1473                 }
1474
1475                 public override void Emit (EmitContext ec)
1476                 {
1477                         child.Emit (ec);
1478                 }
1479
1480                 public override void FlowAnalysis (FlowAnalysisContext fc)
1481                 {
1482                         child.FlowAnalysis (fc);
1483                 }
1484
1485                 public override SLE.Expression MakeExpression (BuilderContext ctx)
1486                 {
1487 #if STATIC
1488                         return base.MakeExpression (ctx);
1489 #else
1490                         return ctx.HasSet (BuilderContext.Options.CheckedScope) ?
1491                                 SLE.Expression.ConvertChecked (child.MakeExpression (ctx), type.GetMetaInfo ()) :
1492                                 SLE.Expression.Convert (child.MakeExpression (ctx), type.GetMetaInfo ());
1493 #endif
1494                 }
1495
1496                 protected override void CloneTo (CloneContext clonectx, Expression t)
1497                 {
1498                         // Nothing to clone
1499                 }
1500
1501                 public override bool IsNull {
1502                         get { return child.IsNull; }
1503                 }
1504         }
1505
1506         public class EmptyCast : TypeCast {
1507                 EmptyCast (Expression child, TypeSpec target_type)
1508                         : base (child, target_type)
1509                 {
1510                 }
1511
1512                 public static Expression Create (Expression child, TypeSpec type)
1513                 {
1514                         Constant c = child as Constant;
1515                         if (c != null) {
1516                                 var enum_constant = c as EnumConstant;
1517                                 if (enum_constant != null)
1518                                         c = enum_constant.Child;
1519
1520                                 if (!(c is ReducedExpression.ReducedConstantExpression)) {
1521                                         if (c.Type == type)
1522                                                 return c;
1523
1524                                         var res = c.ConvertImplicitly (type);
1525                                         if (res != null)
1526                                                 return res;
1527                                 }
1528                         }
1529
1530                         EmptyCast e = child as EmptyCast;
1531                         if (e != null)
1532                                 return new EmptyCast (e.child, type);
1533
1534                         return new EmptyCast (child, type);
1535                 }
1536
1537                 public override void EmitBranchable (EmitContext ec, Label label, bool on_true)
1538                 {
1539                         child.EmitBranchable (ec, label, on_true);
1540                 }
1541
1542                 public override void EmitSideEffect (EmitContext ec)
1543                 {
1544                         child.EmitSideEffect (ec);
1545                 }
1546         }
1547
1548         //
1549         // Used for predefined type user operator (no obsolete check, etc.)
1550         //
1551         public class OperatorCast : TypeCast
1552         {
1553                 readonly MethodSpec conversion_operator;
1554
1555                 public OperatorCast (Expression expr, TypeSpec target_type)
1556                         : this (expr, target_type, target_type, false)
1557                 {
1558                 }
1559                 
1560                 public OperatorCast (Expression expr, TypeSpec target_type, bool find_explicit)
1561                         : this (expr, target_type, target_type, find_explicit)
1562                 {
1563                 }
1564                 
1565                 public OperatorCast (Expression expr, TypeSpec declaringType, TypeSpec returnType, bool isExplicit)
1566                         : base (expr, returnType)
1567                 {
1568                         var op = isExplicit ? Operator.OpType.Explicit : Operator.OpType.Implicit;
1569                         var mi = MemberCache.GetUserOperator (declaringType, op, true);
1570
1571                         if (mi != null) {
1572                                 foreach (MethodSpec oper in mi) {
1573                                         if (oper.ReturnType != returnType)
1574                                                 continue;
1575
1576                                         if (oper.Parameters.Types[0] == expr.Type) {
1577                                                 conversion_operator = oper;
1578                                                 return;
1579                                         }
1580                                 }
1581                         }
1582
1583                         throw new InternalErrorException ("Missing predefined user operator between `{0}' and `{1}'",
1584                                 returnType.GetSignatureForError (), expr.Type.GetSignatureForError ());
1585                 }
1586
1587                 public override void Emit (EmitContext ec)
1588                 {
1589                         child.Emit (ec);
1590                         ec.Emit (OpCodes.Call, conversion_operator);
1591                 }
1592         }
1593         
1594         //
1595         // Constant specialization of EmptyCast.
1596         // We need to special case this since an empty cast of
1597         // a constant is still a constant. 
1598         //
1599         public class EmptyConstantCast : Constant
1600         {
1601                 public readonly Constant child;
1602
1603                 public EmptyConstantCast (Constant child, TypeSpec type)
1604                         : base (child.Location)
1605                 {
1606                         if (child == null)
1607                                 throw new ArgumentNullException ("child");
1608
1609                         this.child = child;
1610                         this.eclass = child.eclass;
1611                         this.type = type;
1612                 }
1613
1614                 public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
1615                 {
1616                         if (child.Type == target_type)
1617                                 return child;
1618
1619                         // FIXME: check that 'type' can be converted to 'target_type' first
1620                         return child.ConvertExplicitly (in_checked_context, target_type);
1621                 }
1622
1623                 public override Expression CreateExpressionTree (ResolveContext ec)
1624                 {
1625                         Arguments args = Arguments.CreateForExpressionTree (ec, null,
1626                                 child.CreateExpressionTree (ec),
1627                                 new TypeOf (type, loc));
1628
1629                         if (type.IsPointer)
1630                                 Error_PointerInsideExpressionTree (ec);
1631
1632                         return CreateExpressionFactoryCall (ec, "Convert", args);
1633                 }
1634
1635                 public override bool IsDefaultValue {
1636                         get { return child.IsDefaultValue; }
1637                 }
1638
1639                 public override bool IsNegative {
1640                         get { return child.IsNegative; }
1641                 }
1642
1643                 public override bool IsNull {
1644                         get { return child.IsNull; }
1645                 }
1646                 
1647                 public override bool IsOneInteger {
1648                         get { return child.IsOneInteger; }
1649                 }
1650
1651                 public override bool IsSideEffectFree {
1652                         get {
1653                                 return child.IsSideEffectFree;
1654                         }
1655                 }
1656
1657                 public override bool IsZeroInteger {
1658                         get { return child.IsZeroInteger; }
1659                 }
1660
1661                 public override void Emit (EmitContext ec)
1662                 {
1663                         child.Emit (ec);                        
1664                 }
1665
1666                 public override void EmitBranchable (EmitContext ec, Label label, bool on_true)
1667                 {
1668                         child.EmitBranchable (ec, label, on_true);
1669
1670                         // Only to make verifier happy
1671                         if (TypeManager.IsGenericParameter (type) && child.IsNull)
1672                                 ec.Emit (OpCodes.Unbox_Any, type);
1673                 }
1674
1675                 public override void EmitSideEffect (EmitContext ec)
1676                 {
1677                         child.EmitSideEffect (ec);
1678                 }
1679
1680                 public override object GetValue ()
1681                 {
1682                         return child.GetValue ();
1683                 }
1684
1685                 public override string GetValueAsLiteral ()
1686                 {
1687                         return child.GetValueAsLiteral ();
1688                 }
1689
1690                 public override long GetValueAsLong ()
1691                 {
1692                         return child.GetValueAsLong ();
1693                 }
1694
1695                 public override Constant ConvertImplicitly (TypeSpec target_type)
1696                 {
1697                         if (type == target_type)
1698                                 return this;
1699
1700                         // FIXME: Do we need to check user conversions?
1701                         if (!Convert.ImplicitStandardConversionExists (this, target_type))
1702                                 return null;
1703
1704                         return child.ConvertImplicitly (target_type);
1705                 }
1706         }
1707
1708         /// <summary>
1709         ///  This class is used to wrap literals which belong inside Enums
1710         /// </summary>
1711         public class EnumConstant : Constant
1712         {
1713                 public Constant Child;
1714
1715                 public EnumConstant (Constant child, TypeSpec enum_type)
1716                         : base (child.Location)
1717                 {
1718                         this.Child = child;
1719
1720                         this.eclass = ExprClass.Value;
1721                         this.type = enum_type;
1722                 }
1723
1724                 protected EnumConstant (Location loc)
1725                         : base (loc)
1726                 {
1727                 }
1728
1729                 public override void Emit (EmitContext ec)
1730                 {
1731                         Child.Emit (ec);
1732                 }
1733
1734                 public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
1735                 {
1736                         Child.EncodeAttributeValue (rc, enc, Child.Type, parameterType);
1737                 }
1738
1739                 public override void EmitBranchable (EmitContext ec, Label label, bool on_true)
1740                 {
1741                         Child.EmitBranchable (ec, label, on_true);
1742                 }
1743
1744                 public override void EmitSideEffect (EmitContext ec)
1745                 {
1746                         Child.EmitSideEffect (ec);
1747                 }
1748
1749                 public override string GetSignatureForError()
1750                 {
1751                         return Type.GetSignatureForError ();
1752                 }
1753
1754                 public override object GetValue ()
1755                 {
1756                         return Child.GetValue ();
1757                 }
1758
1759 #if !STATIC
1760                 public override object GetTypedValue ()
1761                 {
1762                         //
1763                         // The method can be used in dynamic context only (on closed types)
1764                         //
1765                         // System.Enum.ToObject cannot be called on dynamic types
1766                         // EnumBuilder has to be used, but we cannot use EnumBuilder
1767                         // because it does not properly support generics
1768                         //
1769                         return System.Enum.ToObject (type.GetMetaInfo (), Child.GetValue ());
1770                 }
1771 #endif
1772
1773                 public override string GetValueAsLiteral ()
1774                 {
1775                         return Child.GetValueAsLiteral ();
1776                 }
1777
1778                 public override long GetValueAsLong ()
1779                 {
1780                         return Child.GetValueAsLong ();
1781                 }
1782
1783                 public EnumConstant Increment()
1784                 {
1785                         return new EnumConstant (((IntegralConstant) Child).Increment (), type);
1786                 }
1787
1788                 public override bool IsDefaultValue {
1789                         get {
1790                                 return Child.IsDefaultValue;
1791                         }
1792                 }
1793
1794                 public override bool IsSideEffectFree {
1795                         get {
1796                                 return Child.IsSideEffectFree;
1797                         }
1798                 }
1799
1800                 public override bool IsZeroInteger {
1801                         get { return Child.IsZeroInteger; }
1802                 }
1803
1804                 public override bool IsNegative {
1805                         get {
1806                                 return Child.IsNegative;
1807                         }
1808                 }
1809
1810                 public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
1811                 {
1812                         if (Child.Type == target_type)
1813                                 return Child;
1814
1815                         return Child.ConvertExplicitly (in_checked_context, target_type);
1816                 }
1817
1818                 public override Constant ConvertImplicitly (TypeSpec type)
1819                 {
1820                         if (this.type == type) {
1821                                 return this;
1822                         }
1823
1824                         if (!Convert.ImplicitStandardConversionExists (this, type)){
1825                                 return null;
1826                         }
1827
1828                         return Child.ConvertImplicitly (type);
1829                 }
1830         }
1831
1832         /// <summary>
1833         ///   This kind of cast is used to encapsulate Value Types in objects.
1834         ///
1835         ///   The effect of it is to box the value type emitted by the previous
1836         ///   operation.
1837         /// </summary>
1838         public class BoxedCast : TypeCast {
1839
1840                 public BoxedCast (Expression expr, TypeSpec target_type)
1841                         : base (expr, target_type)
1842                 {
1843                         eclass = ExprClass.Value;
1844                 }
1845                 
1846                 protected override Expression DoResolve (ResolveContext ec)
1847                 {
1848                         // This should never be invoked, we are born in fully
1849                         // initialized state.
1850
1851                         return this;
1852                 }
1853
1854                 public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
1855                 {
1856                         // Only boxing to object type is supported
1857                         if (targetType.BuiltinType != BuiltinTypeSpec.Type.Object) {
1858                                 base.EncodeAttributeValue (rc, enc, targetType, parameterType);
1859                                 return;
1860                         }
1861
1862                         enc.Encode (child.Type);
1863                         child.EncodeAttributeValue (rc, enc, child.Type, parameterType);
1864                 }
1865
1866                 public override void Emit (EmitContext ec)
1867                 {
1868                         base.Emit (ec);
1869                         
1870                         ec.Emit (OpCodes.Box, child.Type);
1871                 }
1872
1873                 public override void EmitSideEffect (EmitContext ec)
1874                 {
1875                         // boxing is side-effectful, since it involves runtime checks, except when boxing to Object or ValueType
1876                         // so, we need to emit the box+pop instructions in most cases
1877                         if (child.Type.IsStruct &&
1878                             (type.BuiltinType == BuiltinTypeSpec.Type.Object || type.BuiltinType == BuiltinTypeSpec.Type.ValueType))
1879                                 child.EmitSideEffect (ec);
1880                         else
1881                                 base.EmitSideEffect (ec);
1882                 }
1883         }
1884
1885         public class UnboxCast : TypeCast {
1886                 public UnboxCast (Expression expr, TypeSpec return_type)
1887                         : base (expr, return_type)
1888                 {
1889                 }
1890
1891                 protected override Expression DoResolve (ResolveContext ec)
1892                 {
1893                         // This should never be invoked, we are born in fully
1894                         // initialized state.
1895
1896                         return this;
1897                 }
1898
1899                 public override void Emit (EmitContext ec)
1900                 {
1901                         base.Emit (ec);
1902
1903                         ec.Emit (OpCodes.Unbox_Any, type);
1904                 }
1905         }
1906         
1907         /// <summary>
1908         ///   This is used to perform explicit numeric conversions.
1909         ///
1910         ///   Explicit numeric conversions might trigger exceptions in a checked
1911         ///   context, so they should generate the conv.ovf opcodes instead of
1912         ///   conv opcodes.
1913         /// </summary>
1914         public class ConvCast : TypeCast {
1915                 public enum Mode : byte {
1916                         I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
1917                         U1_I1, U1_CH,
1918                         I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
1919                         U2_I1, U2_U1, U2_I2, U2_CH,
1920                         I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
1921                         U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
1922                         I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH, I8_I,
1923                         U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH, U8_I,
1924                         CH_I1, CH_U1, CH_I2,
1925                         R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
1926                         R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4,
1927                         I_I8,
1928                 }
1929
1930                 Mode mode;
1931                 
1932                 public ConvCast (Expression child, TypeSpec return_type, Mode m)
1933                         : base (child, return_type)
1934                 {
1935                         mode = m;
1936                 }
1937
1938                 protected override Expression DoResolve (ResolveContext ec)
1939                 {
1940                         // This should never be invoked, we are born in fully
1941                         // initialized state.
1942
1943                         return this;
1944                 }
1945
1946                 public override string ToString ()
1947                 {
1948                         return String.Format ("ConvCast ({0}, {1})", mode, child);
1949                 }
1950                 
1951                 public override void Emit (EmitContext ec)
1952                 {
1953                         base.Emit (ec);
1954                         Emit (ec, mode);
1955                 }
1956
1957                 public static void Emit (EmitContext ec, Mode mode)
1958                 {
1959                         if (ec.HasSet (EmitContext.Options.CheckedScope)) {
1960                                 switch (mode){
1961                                 case Mode.I1_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
1962                                 case Mode.I1_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1963                                 case Mode.I1_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
1964                                 case Mode.I1_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
1965                                 case Mode.I1_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1966
1967                                 case Mode.U1_I1: ec.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1968                                 case Mode.U1_CH: /* nothing */ break;
1969
1970                                 case Mode.I2_I1: ec.Emit (OpCodes.Conv_Ovf_I1); break;
1971                                 case Mode.I2_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
1972                                 case Mode.I2_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1973                                 case Mode.I2_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
1974                                 case Mode.I2_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
1975                                 case Mode.I2_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1976
1977                                 case Mode.U2_I1: ec.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1978                                 case Mode.U2_U1: ec.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1979                                 case Mode.U2_I2: ec.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1980                                 case Mode.U2_CH: /* nothing */ break;
1981
1982                                 case Mode.I4_I1: ec.Emit (OpCodes.Conv_Ovf_I1); break;
1983                                 case Mode.I4_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
1984                                 case Mode.I4_I2: ec.Emit (OpCodes.Conv_Ovf_I2); break;
1985                                 case Mode.I4_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
1986                                 case Mode.I4_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1987                                 case Mode.I4_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
1988                                 case Mode.I4_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1989
1990                                 case Mode.U4_I1: ec.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1991                                 case Mode.U4_U1: ec.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1992                                 case Mode.U4_I2: ec.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1993                                 case Mode.U4_U2: ec.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1994                                 case Mode.U4_I4: ec.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1995                                 case Mode.U4_CH: ec.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1996
1997                                 case Mode.I8_I1: ec.Emit (OpCodes.Conv_Ovf_I1); break;
1998                                 case Mode.I8_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
1999                                 case Mode.I8_I2: ec.Emit (OpCodes.Conv_Ovf_I2); break;
2000                                 case Mode.I8_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
2001                                 case Mode.I8_I4: ec.Emit (OpCodes.Conv_Ovf_I4); break;
2002                                 case Mode.I8_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
2003                                 case Mode.I8_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
2004                                 case Mode.I8_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
2005                                 case Mode.I8_I: ec.Emit (OpCodes.Conv_Ovf_U); break;
2006
2007                                 case Mode.U8_I1: ec.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2008                                 case Mode.U8_U1: ec.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2009                                 case Mode.U8_I2: ec.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2010                                 case Mode.U8_U2: ec.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2011                                 case Mode.U8_I4: ec.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2012                                 case Mode.U8_U4: ec.Emit (OpCodes.Conv_Ovf_U4_Un); break;
2013                                 case Mode.U8_I8: ec.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2014                                 case Mode.U8_CH: ec.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2015                                 case Mode.U8_I: ec.Emit (OpCodes.Conv_Ovf_U_Un); break;
2016
2017                                 case Mode.CH_I1: ec.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2018                                 case Mode.CH_U1: ec.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2019                                 case Mode.CH_I2: ec.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2020
2021                                 case Mode.R4_I1: ec.Emit (OpCodes.Conv_Ovf_I1); break;
2022                                 case Mode.R4_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
2023                                 case Mode.R4_I2: ec.Emit (OpCodes.Conv_Ovf_I2); break;
2024                                 case Mode.R4_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
2025                                 case Mode.R4_I4: ec.Emit (OpCodes.Conv_Ovf_I4); break;
2026                                 case Mode.R4_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
2027                                 case Mode.R4_I8: ec.Emit (OpCodes.Conv_Ovf_I8); break;
2028                                 case Mode.R4_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
2029                                 case Mode.R4_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
2030
2031                                 case Mode.R8_I1: ec.Emit (OpCodes.Conv_Ovf_I1); break;
2032                                 case Mode.R8_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
2033                                 case Mode.R8_I2: ec.Emit (OpCodes.Conv_Ovf_I2); break;
2034                                 case Mode.R8_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
2035                                 case Mode.R8_I4: ec.Emit (OpCodes.Conv_Ovf_I4); break;
2036                                 case Mode.R8_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
2037                                 case Mode.R8_I8: ec.Emit (OpCodes.Conv_Ovf_I8); break;
2038                                 case Mode.R8_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
2039                                 case Mode.R8_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
2040                                 case Mode.R8_R4: ec.Emit (OpCodes.Conv_R4); break;
2041
2042                                 case Mode.I_I8: ec.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2043                                 }
2044                         } else {
2045                                 switch (mode){
2046                                 case Mode.I1_U1: ec.Emit (OpCodes.Conv_U1); break;
2047                                 case Mode.I1_U2: ec.Emit (OpCodes.Conv_U2); break;
2048                                 case Mode.I1_U4: ec.Emit (OpCodes.Conv_U4); break;
2049                                 case Mode.I1_U8: ec.Emit (OpCodes.Conv_I8); break;
2050                                 case Mode.I1_CH: ec.Emit (OpCodes.Conv_U2); break;
2051
2052                                 case Mode.U1_I1: ec.Emit (OpCodes.Conv_I1); break;
2053                                 case Mode.U1_CH: ec.Emit (OpCodes.Conv_U2); break;
2054
2055                                 case Mode.I2_I1: ec.Emit (OpCodes.Conv_I1); break;
2056                                 case Mode.I2_U1: ec.Emit (OpCodes.Conv_U1); break;
2057                                 case Mode.I2_U2: ec.Emit (OpCodes.Conv_U2); break;
2058                                 case Mode.I2_U4: ec.Emit (OpCodes.Conv_U4); break;
2059                                 case Mode.I2_U8: ec.Emit (OpCodes.Conv_I8); break;
2060                                 case Mode.I2_CH: ec.Emit (OpCodes.Conv_U2); break;
2061
2062                                 case Mode.U2_I1: ec.Emit (OpCodes.Conv_I1); break;
2063                                 case Mode.U2_U1: ec.Emit (OpCodes.Conv_U1); break;
2064                                 case Mode.U2_I2: ec.Emit (OpCodes.Conv_I2); break;
2065                                 case Mode.U2_CH: /* nothing */ break;
2066
2067                                 case Mode.I4_I1: ec.Emit (OpCodes.Conv_I1); break;
2068                                 case Mode.I4_U1: ec.Emit (OpCodes.Conv_U1); break;
2069                                 case Mode.I4_I2: ec.Emit (OpCodes.Conv_I2); break;
2070                                 case Mode.I4_U4: /* nothing */ break;
2071                                 case Mode.I4_U2: ec.Emit (OpCodes.Conv_U2); break;
2072                                 case Mode.I4_U8: ec.Emit (OpCodes.Conv_I8); break;
2073                                 case Mode.I4_CH: ec.Emit (OpCodes.Conv_U2); break;
2074
2075                                 case Mode.U4_I1: ec.Emit (OpCodes.Conv_I1); break;
2076                                 case Mode.U4_U1: ec.Emit (OpCodes.Conv_U1); break;
2077                                 case Mode.U4_I2: ec.Emit (OpCodes.Conv_I2); break;
2078                                 case Mode.U4_U2: ec.Emit (OpCodes.Conv_U2); break;
2079                                 case Mode.U4_I4: /* nothing */ break;
2080                                 case Mode.U4_CH: ec.Emit (OpCodes.Conv_U2); break;
2081
2082                                 case Mode.I8_I1: ec.Emit (OpCodes.Conv_I1); break;
2083                                 case Mode.I8_U1: ec.Emit (OpCodes.Conv_U1); break;
2084                                 case Mode.I8_I2: ec.Emit (OpCodes.Conv_I2); break;
2085                                 case Mode.I8_U2: ec.Emit (OpCodes.Conv_U2); break;
2086                                 case Mode.I8_I4: ec.Emit (OpCodes.Conv_I4); break;
2087                                 case Mode.I8_U4: ec.Emit (OpCodes.Conv_U4); break;
2088                                 case Mode.I8_U8: /* nothing */ break;
2089                                 case Mode.I8_CH: ec.Emit (OpCodes.Conv_U2); break;
2090                                 case Mode.I8_I: ec.Emit (OpCodes.Conv_U); break;
2091
2092                                 case Mode.U8_I1: ec.Emit (OpCodes.Conv_I1); break;
2093                                 case Mode.U8_U1: ec.Emit (OpCodes.Conv_U1); break;
2094                                 case Mode.U8_I2: ec.Emit (OpCodes.Conv_I2); break;
2095                                 case Mode.U8_U2: ec.Emit (OpCodes.Conv_U2); break;
2096                                 case Mode.U8_I4: ec.Emit (OpCodes.Conv_I4); break;
2097                                 case Mode.U8_U4: ec.Emit (OpCodes.Conv_U4); break;
2098                                 case Mode.U8_I8: /* nothing */ break;
2099                                 case Mode.U8_CH: ec.Emit (OpCodes.Conv_U2); break;
2100                                 case Mode.U8_I: ec.Emit (OpCodes.Conv_U); break;
2101
2102                                 case Mode.CH_I1: ec.Emit (OpCodes.Conv_I1); break;
2103                                 case Mode.CH_U1: ec.Emit (OpCodes.Conv_U1); break;
2104                                 case Mode.CH_I2: ec.Emit (OpCodes.Conv_I2); break;
2105
2106                                 case Mode.R4_I1: ec.Emit (OpCodes.Conv_I1); break;
2107                                 case Mode.R4_U1: ec.Emit (OpCodes.Conv_U1); break;
2108                                 case Mode.R4_I2: ec.Emit (OpCodes.Conv_I2); break;
2109                                 case Mode.R4_U2: ec.Emit (OpCodes.Conv_U2); break;
2110                                 case Mode.R4_I4: ec.Emit (OpCodes.Conv_I4); break;
2111                                 case Mode.R4_U4: ec.Emit (OpCodes.Conv_U4); break;
2112                                 case Mode.R4_I8: ec.Emit (OpCodes.Conv_I8); break;
2113                                 case Mode.R4_U8: ec.Emit (OpCodes.Conv_U8); break;
2114                                 case Mode.R4_CH: ec.Emit (OpCodes.Conv_U2); break;
2115
2116                                 case Mode.R8_I1: ec.Emit (OpCodes.Conv_I1); break;
2117                                 case Mode.R8_U1: ec.Emit (OpCodes.Conv_U1); break;
2118                                 case Mode.R8_I2: ec.Emit (OpCodes.Conv_I2); break;
2119                                 case Mode.R8_U2: ec.Emit (OpCodes.Conv_U2); break;
2120                                 case Mode.R8_I4: ec.Emit (OpCodes.Conv_I4); break;
2121                                 case Mode.R8_U4: ec.Emit (OpCodes.Conv_U4); break;
2122                                 case Mode.R8_I8: ec.Emit (OpCodes.Conv_I8); break;
2123                                 case Mode.R8_U8: ec.Emit (OpCodes.Conv_U8); break;
2124                                 case Mode.R8_CH: ec.Emit (OpCodes.Conv_U2); break;
2125                                 case Mode.R8_R4: ec.Emit (OpCodes.Conv_R4); break;
2126
2127                                 case Mode.I_I8: ec.Emit (OpCodes.Conv_U8); break;
2128                                 }
2129                         }
2130                 }
2131         }
2132         
2133         class OpcodeCast : TypeCast
2134         {
2135                 readonly OpCode op;
2136                 
2137                 public OpcodeCast (Expression child, TypeSpec return_type, OpCode op)
2138                         : base (child, return_type)
2139                 {
2140                         this.op = op;
2141                 }
2142
2143                 protected override Expression DoResolve (ResolveContext ec)
2144                 {
2145                         // This should never be invoked, we are born in fully
2146                         // initialized state.
2147
2148                         return this;
2149                 }
2150
2151                 public override void Emit (EmitContext ec)
2152                 {
2153                         base.Emit (ec);
2154                         ec.Emit (op);
2155                 }
2156
2157                 public TypeSpec UnderlyingType {
2158                         get { return child.Type; }
2159                 }
2160         }
2161
2162         //
2163         // Opcode casts expression with 2 opcodes but only
2164         // single expression tree node
2165         //
2166         class OpcodeCastDuplex : OpcodeCast
2167         {
2168                 readonly OpCode second;
2169
2170                 public OpcodeCastDuplex (Expression child, TypeSpec returnType, OpCode first, OpCode second)
2171                         : base (child, returnType, first)
2172                 {
2173                         this.second = second;
2174                 }
2175
2176                 public override void Emit (EmitContext ec)
2177                 {
2178                         base.Emit (ec);
2179                         ec.Emit (second);
2180                 }
2181         }
2182
2183         /// <summary>
2184         ///   This kind of cast is used to encapsulate a child and cast it
2185         ///   to the class requested
2186         /// </summary>
2187         public sealed class ClassCast : TypeCast {
2188                 readonly bool forced;
2189                 
2190                 public ClassCast (Expression child, TypeSpec return_type)
2191                         : base (child, return_type)
2192                 {
2193                 }
2194                 
2195                 public ClassCast (Expression child, TypeSpec return_type, bool forced)
2196                         : base (child, return_type)
2197                 {
2198                         this.forced = forced;
2199                 }
2200
2201                 public override void Emit (EmitContext ec)
2202                 {
2203                         base.Emit (ec);
2204
2205                         bool gen = TypeManager.IsGenericParameter (child.Type);
2206                         if (gen)
2207                                 ec.Emit (OpCodes.Box, child.Type);
2208                         
2209                         if (type.IsGenericParameter) {
2210                                 ec.Emit (OpCodes.Unbox_Any, type);
2211                                 return;
2212                         }
2213                         
2214                         if (gen && !forced)
2215                                 return;
2216                         
2217                         ec.Emit (OpCodes.Castclass, type);
2218                 }
2219         }
2220
2221         //
2222         // Created during resolving pahse when an expression is wrapped or constantified
2223         // and original expression can be used later (e.g. for expression trees)
2224         //
2225         public class ReducedExpression : Expression
2226         {
2227                 public class ReducedConstantExpression : EmptyConstantCast
2228                 {
2229                         readonly Expression orig_expr;
2230
2231                         public ReducedConstantExpression (Constant expr, Expression orig_expr)
2232                                 : base (expr, expr.Type)
2233                         {
2234                                 this.orig_expr = orig_expr;
2235                         }
2236
2237                         public Expression OriginalExpression {
2238                                 get {
2239                                         return orig_expr;
2240                                 }
2241                         }
2242
2243                         public override Constant ConvertImplicitly (TypeSpec target_type)
2244                         {
2245                                 Constant c = base.ConvertImplicitly (target_type);
2246                                 if (c != null)
2247                                         c = new ReducedConstantExpression (c, orig_expr);
2248
2249                                 return c;
2250                         }
2251
2252                         public override Expression CreateExpressionTree (ResolveContext ec)
2253                         {
2254                                 return orig_expr.CreateExpressionTree (ec);
2255                         }
2256
2257                         public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
2258                         {
2259                                 Constant c = base.ConvertExplicitly (in_checked_context, target_type);
2260                                 if (c != null)
2261                                         c = new ReducedConstantExpression (c, orig_expr);
2262                                 return c;
2263                         }
2264
2265                         public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
2266                         {
2267                                 //
2268                                 // LAMESPEC: Reduced conditional expression is allowed as an attribute argument
2269                                 //
2270                                 if (orig_expr is Conditional)
2271                                         child.EncodeAttributeValue (rc, enc, targetType,parameterType);
2272                                 else
2273                                         base.EncodeAttributeValue (rc, enc, targetType, parameterType);
2274                         }
2275                 }
2276
2277                 sealed class ReducedConstantStatement : ReducedConstantExpression, IReducedExpressionStatement
2278                 {
2279                         public ReducedConstantStatement (Constant expr, Expression origExpr)
2280                                 : base (expr, origExpr)
2281                         {
2282                         }
2283                 }
2284
2285                 sealed class ReducedExpressionStatement : ExpressionStatement
2286                 {
2287                         readonly Expression orig_expr;
2288                         readonly ExpressionStatement stm;
2289
2290                         public ReducedExpressionStatement (ExpressionStatement stm, Expression orig)
2291                         {
2292                                 this.orig_expr = orig;
2293                                 this.stm = stm;
2294                                 this.eclass = stm.eclass;
2295                                 this.type = stm.Type;
2296
2297                                 this.loc = orig.Location;
2298                         }
2299
2300                         public override bool ContainsEmitWithAwait ()
2301                         {
2302                                 return stm.ContainsEmitWithAwait ();
2303                         }
2304
2305                         public override Expression CreateExpressionTree (ResolveContext ec)
2306                         {
2307                                 return orig_expr.CreateExpressionTree (ec);
2308                         }
2309
2310                         protected override Expression DoResolve (ResolveContext ec)
2311                         {
2312                                 return this;
2313                         }
2314
2315                         public override void Emit (EmitContext ec)
2316                         {
2317                                 stm.Emit (ec);
2318                         }
2319
2320                         public override void EmitStatement (EmitContext ec)
2321                         {
2322                                 stm.EmitStatement (ec);
2323                         }
2324
2325                         public override void FlowAnalysis (FlowAnalysisContext fc)
2326                         {
2327                                 stm.FlowAnalysis (fc);
2328                         }
2329                 }
2330
2331                 readonly Expression expr, orig_expr;
2332
2333                 private ReducedExpression (Expression expr, Expression orig_expr)
2334                 {
2335                         this.expr = expr;
2336                         this.eclass = expr.eclass;
2337                         this.type = expr.Type;
2338                         this.orig_expr = orig_expr;
2339                         this.loc = orig_expr.Location;
2340                 }
2341
2342                 #region Properties
2343
2344                 public override bool IsSideEffectFree {
2345                         get {
2346                                 return expr.IsSideEffectFree;
2347                         }
2348                 }
2349
2350                 public Expression OriginalExpression {
2351                         get {
2352                                 return orig_expr;
2353                         }
2354                 }
2355
2356                 #endregion
2357
2358                 public override bool ContainsEmitWithAwait ()
2359                 {
2360                         return expr.ContainsEmitWithAwait ();
2361                 }
2362
2363                 //
2364                 // Creates fully resolved expression switcher
2365                 //
2366                 public static Constant Create (Constant expr, Expression originalExpr)
2367                 {
2368                         if (expr.eclass == ExprClass.Unresolved)
2369                                 throw new ArgumentException ("Unresolved expression");
2370
2371                         if (originalExpr is ExpressionStatement)
2372                                 return new ReducedConstantStatement (expr, originalExpr);
2373
2374                         return new ReducedConstantExpression (expr, originalExpr);
2375                 }
2376
2377                 public static ExpressionStatement Create (ExpressionStatement s, Expression orig)
2378                 {
2379                         return new ReducedExpressionStatement (s, orig);
2380                 }
2381
2382                 public static Expression Create (Expression expr, Expression original_expr)
2383                 {
2384                         return Create (expr, original_expr, true);
2385                 }
2386
2387                 //
2388                 // Creates unresolved reduce expression. The original expression has to be
2389                 // already resolved. Created expression is constant based based on `expr'
2390                 // value unless canBeConstant is used
2391                 //
2392                 public static Expression Create (Expression expr, Expression original_expr, bool canBeConstant)
2393                 {
2394                         if (canBeConstant) {
2395                                 Constant c = expr as Constant;
2396                                 if (c != null)
2397                                         return Create (c, original_expr);
2398                         }
2399
2400                         ExpressionStatement s = expr as ExpressionStatement;
2401                         if (s != null)
2402                                 return Create (s, original_expr);
2403
2404                         if (expr.eclass == ExprClass.Unresolved)
2405                                 throw new ArgumentException ("Unresolved expression");
2406
2407                         return new ReducedExpression (expr, original_expr);
2408                 }
2409
2410                 public override Expression CreateExpressionTree (ResolveContext ec)
2411                 {
2412                         return orig_expr.CreateExpressionTree (ec);
2413                 }
2414
2415                 protected override Expression DoResolve (ResolveContext ec)
2416                 {
2417                         return this;
2418                 }
2419
2420                 public override void Emit (EmitContext ec)
2421                 {
2422                         expr.Emit (ec);
2423                 }
2424
2425                 public override Expression EmitToField (EmitContext ec)
2426                 {
2427                         return expr.EmitToField(ec);
2428                 }
2429
2430                 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
2431                 {
2432                         expr.EmitBranchable (ec, target, on_true);
2433                 }
2434
2435                 public override void FlowAnalysis (FlowAnalysisContext fc)
2436                 {
2437                         expr.FlowAnalysis (fc);
2438                 }
2439
2440                 public override SLE.Expression MakeExpression (BuilderContext ctx)
2441                 {
2442                         return orig_expr.MakeExpression (ctx);
2443                 }
2444         }
2445
2446         //
2447         // Standard composite pattern
2448         //
2449         public abstract class CompositeExpression : Expression
2450         {
2451                 protected Expression expr;
2452
2453                 protected CompositeExpression (Expression expr)
2454                 {
2455                         this.expr = expr;
2456                         this.loc = expr.Location;
2457                 }
2458
2459                 public override bool ContainsEmitWithAwait ()
2460                 {
2461                         return expr.ContainsEmitWithAwait ();
2462                 }
2463
2464                 public override Expression CreateExpressionTree (ResolveContext rc)
2465                 {
2466                         return expr.CreateExpressionTree (rc);
2467                 }
2468
2469                 public Expression Child {
2470                         get { return expr; }
2471                 }
2472
2473                 protected override Expression DoResolve (ResolveContext rc)
2474                 {
2475                         expr = expr.Resolve (rc);
2476                         if (expr == null)
2477                                 return null;
2478
2479                         type = expr.Type;
2480                         eclass = expr.eclass;
2481                         return this;
2482                 }
2483
2484                 public override void Emit (EmitContext ec)
2485                 {
2486                         expr.Emit (ec);
2487                 }
2488
2489                 public override bool IsNull {
2490                         get { return expr.IsNull; }
2491                 }
2492         }
2493
2494         //
2495         // Base of expressions used only to narrow resolve flow
2496         //
2497         public abstract class ShimExpression : Expression
2498         {
2499                 protected Expression expr;
2500
2501                 protected ShimExpression (Expression expr)
2502                 {
2503                         this.expr = expr;
2504                 }
2505
2506                 public Expression Expr {
2507                         get {
2508                                 return expr;
2509                         }
2510                 }
2511
2512                 protected override void CloneTo (CloneContext clonectx, Expression t)
2513                 {
2514                         if (expr == null)
2515                                 return;
2516
2517                         ShimExpression target = (ShimExpression) t;
2518                         target.expr = expr.Clone (clonectx);
2519                 }
2520
2521                 public override bool ContainsEmitWithAwait ()
2522                 {
2523                         return expr.ContainsEmitWithAwait ();
2524                 }
2525
2526                 public override Expression CreateExpressionTree (ResolveContext ec)
2527                 {
2528                         throw new NotSupportedException ("ET");
2529                 }
2530
2531                 public override void Emit (EmitContext ec)
2532                 {
2533                         throw new InternalErrorException ("Missing Resolve call");
2534                 }
2535         }
2536
2537         public class UnreachableExpression : Expression
2538         {
2539                 public UnreachableExpression (Expression expr)
2540                 {
2541                         this.loc = expr.Location;
2542                 }
2543
2544                 public override Expression CreateExpressionTree (ResolveContext ec)
2545                 {
2546                         // TODO: is it ok
2547                         throw new NotImplementedException ();
2548                 }
2549
2550                 protected override Expression DoResolve (ResolveContext rc)
2551                 {
2552                         throw new NotSupportedException ();
2553                 }
2554
2555                 public override void FlowAnalysis (FlowAnalysisContext fc)
2556                 {
2557                         fc.Report.Warning (429, 4, loc, "Unreachable expression code detected");
2558                 }
2559
2560                 public override void Emit (EmitContext ec)
2561                 {
2562                 }
2563
2564                 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
2565                 {
2566                 }
2567         }
2568
2569         //
2570         // Unresolved type name expressions
2571         //
2572         public abstract class ATypeNameExpression : FullNamedExpression
2573         {
2574                 string name;
2575                 protected TypeArguments targs;
2576
2577                 protected ATypeNameExpression (string name, Location l)
2578                 {
2579                         this.name = name;
2580                         loc = l;
2581                 }
2582
2583                 protected ATypeNameExpression (string name, TypeArguments targs, Location l)
2584                 {
2585                         this.name = name;
2586                         this.targs = targs;
2587                         loc = l;
2588                 }
2589
2590                 protected ATypeNameExpression (string name, int arity, Location l)
2591                         : this (name, new UnboundTypeArguments (arity, l), l)
2592                 {
2593                 }
2594
2595                 #region Properties
2596
2597                 public int Arity {
2598                         get {
2599                                 return targs == null ? 0 : targs.Count;
2600                         }
2601                 }
2602
2603                 public bool HasTypeArguments {
2604                         get {
2605                                 return targs != null && !targs.IsEmpty;
2606                         }
2607                 }
2608
2609                 public string Name {
2610                         get {
2611                                 return name;
2612                         }
2613                         set {
2614                                 name = value;
2615                         }
2616                 }
2617
2618                 public TypeArguments TypeArguments {
2619                         get {
2620                                 return targs;
2621                         }
2622                 }
2623
2624                 #endregion
2625
2626                 public override bool Equals (object obj)
2627                 {
2628                         ATypeNameExpression atne = obj as ATypeNameExpression;
2629                         return atne != null && atne.Name == Name &&
2630                                 (targs == null || targs.Equals (atne.targs));
2631                 }
2632
2633                 public override int GetHashCode ()
2634                 {
2635                         return Name.GetHashCode ();
2636                 }
2637
2638                 // TODO: Move it to MemberCore
2639                 public static string GetMemberType (MemberCore mc)
2640                 {
2641                         if (mc is Property)
2642                                 return "property";
2643                         if (mc is Indexer)
2644                                 return "indexer";
2645                         if (mc is FieldBase)
2646                                 return "field";
2647                         if (mc is MethodCore)
2648                                 return "method";
2649                         if (mc is EnumMember)
2650                                 return "enum";
2651                         if (mc is Event)
2652                                 return "event";
2653
2654                         return "type";
2655                 }
2656
2657                 public override string GetSignatureForError ()
2658                 {
2659                         if (targs != null) {
2660                                 return Name + "<" + targs.GetSignatureForError () + ">";
2661                         }
2662
2663                         return Name;
2664                 }
2665
2666                 public abstract Expression LookupNameExpression (ResolveContext rc, MemberLookupRestrictions restriction);
2667         }
2668         
2669         /// <summary>
2670         ///   SimpleName expressions are formed of a single word and only happen at the beginning 
2671         ///   of a dotted-name.
2672         /// </summary>
2673         public class SimpleName : ATypeNameExpression
2674         {
2675                 public SimpleName (string name, Location l)
2676                         : base (name, l)
2677                 {
2678                 }
2679
2680                 public SimpleName (string name, TypeArguments args, Location l)
2681                         : base (name, args, l)
2682                 {
2683                 }
2684
2685                 public SimpleName (string name, int arity, Location l)
2686                         : base (name, arity, l)
2687                 {
2688                 }
2689
2690                 public SimpleName GetMethodGroup ()
2691                 {
2692                         return new SimpleName (Name, targs, loc);
2693                 }
2694
2695                 protected override Expression DoResolve (ResolveContext rc)
2696                 {
2697                         return SimpleNameResolve (rc, null);
2698                 }
2699
2700                 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
2701                 {
2702                         return SimpleNameResolve (ec, right_side);
2703                 }
2704
2705                 public void Error_NameDoesNotExist (ResolveContext rc)
2706                 {
2707                         rc.Report.Error (103, loc, "The name `{0}' does not exist in the current context", Name);
2708                 }
2709
2710                 protected virtual void Error_TypeOrNamespaceNotFound (IMemberContext ctx)
2711                 {
2712                         if (ctx.CurrentType != null) {
2713                                 var member = MemberLookup (ctx, false, ctx.CurrentType, Name, 0, MemberLookupRestrictions.ExactArity, loc) as MemberExpr;
2714                                 if (member != null) {
2715                                         Error_UnexpectedKind (ctx, member, "type", member.KindName, loc);
2716                                         return;
2717                                 }
2718                         }
2719
2720                         var report = ctx.Module.Compiler.Report;
2721
2722                         var retval = ctx.LookupNamespaceOrType (Name, Arity, LookupMode.IgnoreAccessibility, loc);
2723                         if (retval != null) {
2724                                 report.SymbolRelatedToPreviousError (retval.Type);
2725                                 ErrorIsInaccesible (ctx, retval.GetSignatureForError (), loc);
2726                                 return;
2727                         }
2728
2729                         retval = ctx.LookupNamespaceOrType (Name, -System.Math.Max (1, Arity), LookupMode.Probing, loc);
2730                         if (retval != null) {
2731                                 Error_TypeArgumentsCannotBeUsed (ctx, retval.Type, loc);
2732                                 return;
2733                         }
2734
2735                         var ns_candidates = ctx.Module.GlobalRootNamespace.FindTypeNamespaces (ctx, Name, Arity);
2736                         if (ns_candidates != null) {
2737                                 if (ctx is UsingAliasNamespace.AliasContext) {
2738                                         report.Error (246, loc,
2739                                                 "The type or namespace name `{1}' could not be found. Consider using fully qualified name `{0}.{1}'",
2740                                                 ns_candidates[0], Name);
2741                                 } else {
2742                                         string usings = string.Join ("' or `", ns_candidates.ToArray ());
2743                                         report.Error (246, loc,
2744                                                 "The type or namespace name `{0}' could not be found. Are you missing `{1}' using directive?",
2745                                                 Name, usings);
2746                                 }
2747                         } else {
2748                                 report.Error (246, loc,
2749                                         "The type or namespace name `{0}' could not be found. Are you missing an assembly reference?",
2750                                         Name);
2751                         }
2752                 }
2753
2754                 public override FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext mc, bool allowUnboundTypeArguments)
2755                 {
2756                         FullNamedExpression fne = mc.LookupNamespaceOrType (Name, Arity, LookupMode.Normal, loc);
2757
2758                         if (fne != null) {
2759                                 if (fne.Type != null && Arity > 0) {
2760                                         if (HasTypeArguments) {
2761                                                 GenericTypeExpr ct = new GenericTypeExpr (fne.Type, targs, loc);
2762                                                 if (ct.ResolveAsType (mc) == null)
2763                                                         return null;
2764
2765                                                 return ct;
2766                                         }
2767
2768                                         targs.Resolve (mc, allowUnboundTypeArguments);
2769
2770                                         return new GenericOpenTypeExpr (fne.Type, loc);
2771                                 }
2772
2773                                 //
2774                                 // dynamic namespace is ignored when dynamic is allowed (does not apply to types)
2775                                 //
2776                                 if (!(fne is NamespaceExpression))
2777                                         return fne;
2778                         }
2779
2780                         if (Arity == 0 && Name == "dynamic" && !(mc is NamespaceContainer) && mc.Module.Compiler.Settings.Version > LanguageVersion.V_3) {
2781                                 if (!mc.Module.PredefinedAttributes.Dynamic.IsDefined) {
2782                                         mc.Module.Compiler.Report.Error (1980, Location,
2783                                                 "Dynamic keyword requires `{0}' to be defined. Are you missing System.Core.dll assembly reference?",
2784                                                 mc.Module.PredefinedAttributes.Dynamic.GetSignatureForError ());
2785                                 }
2786
2787                                 fne = new DynamicTypeExpr (loc);
2788                                 fne.ResolveAsType (mc);
2789                         }
2790
2791                         if (fne != null)
2792                                 return fne;
2793
2794                         Error_TypeOrNamespaceNotFound (mc);
2795                         return null;
2796                 }
2797
2798                 public bool IsPossibleTypeOrNamespace (IMemberContext mc)
2799                 {
2800                         return mc.LookupNamespaceOrType (Name, Arity, LookupMode.Probing, loc) != null;
2801                 }
2802
2803                 public bool IsPossibleType (IMemberContext mc)
2804                 {
2805                         return mc.LookupNamespaceOrType (Name, Arity, LookupMode.Probing, loc) is TypeExpr;
2806                 }
2807
2808                 public override Expression LookupNameExpression (ResolveContext rc, MemberLookupRestrictions restrictions)
2809                 {
2810                         int lookup_arity = Arity;
2811                         bool errorMode = false;
2812                         Expression e;
2813                         Block current_block = rc.CurrentBlock;
2814                         INamedBlockVariable variable = null;
2815                         bool variable_found = false;
2816
2817                         while (true) {
2818                                 //
2819                                 // Stage 1: binding to local variables or parameters
2820                                 //
2821                                 // LAMESPEC: It should take invocableOnly into account but that would break csc compatibility
2822                                 //
2823                                 if (current_block != null && lookup_arity == 0) {
2824                                         if (current_block.ParametersBlock.TopBlock.GetLocalName (Name, current_block.Original, ref variable)) {
2825                                                 if (!variable.IsDeclared) {
2826                                                         // We found local name in accessible block but it's not
2827                                                         // initialized yet, maybe the user wanted to bind to something else
2828                                                         errorMode = true;
2829                                                         variable_found = true;
2830                                                 } else {
2831                                                         e = variable.CreateReferenceExpression (rc, loc);
2832                                                         if (e != null) {
2833                                                                 if (Arity > 0)
2834                                                                         Error_TypeArgumentsCannotBeUsed (rc, "variable", Name, loc);
2835
2836                                                                 return e;
2837                                                         }
2838                                                 }
2839                                         }
2840                                 }
2841
2842                                 //
2843                                 // Stage 2: Lookup members if we are inside a type up to top level type for nested types
2844                                 //
2845                                 TypeSpec member_type = rc.CurrentType;
2846                                 for (; member_type != null; member_type = member_type.DeclaringType) {
2847                                         e = MemberLookup (rc, errorMode, member_type, Name, lookup_arity, restrictions, loc);
2848                                         if (e == null)
2849                                                 continue;
2850
2851                                         var me = e as MemberExpr;
2852                                         if (me == null) {
2853                                                 // The name matches a type, defer to ResolveAsTypeStep
2854                                                 if (e is TypeExpr)
2855                                                         break;
2856
2857                                                 continue;
2858                                         }
2859
2860                                         if (errorMode) {
2861                                                 if (variable != null) {
2862                                                         if (me is FieldExpr || me is ConstantExpr || me is EventExpr || me is PropertyExpr) {
2863                                                                 rc.Report.Error (844, loc,
2864                                                                         "A local variable `{0}' cannot be used before it is declared. Consider renaming the local variable when it hides the member `{1}'",
2865                                                                         Name, me.GetSignatureForError ());
2866                                                         } else {
2867                                                                 break;
2868                                                         }
2869                                                 } else if (me is MethodGroupExpr || me is PropertyExpr || me is IndexerExpr) {
2870                                                         // Leave it to overload resolution to report correct error
2871                                                 } else {
2872                                                         // TODO: rc.Report.SymbolRelatedToPreviousError ()
2873                                                         ErrorIsInaccesible (rc, me.GetSignatureForError (), loc);
2874                                                 }
2875                                         } else {
2876                                                 //
2877                                                 // MemberLookup does not check accessors availability, this is actually needed for properties only
2878                                                 //
2879                                                 var pe = me as PropertyExpr;
2880                                                 if (pe != null) {
2881
2882                                                         // Break as there is no other overload available anyway
2883                                                         if ((restrictions & MemberLookupRestrictions.ReadAccess) != 0) {
2884                                                                 if (!pe.PropertyInfo.HasGet || !pe.PropertyInfo.Get.IsAccessible (rc))
2885                                                                         break;
2886
2887                                                                 pe.Getter = pe.PropertyInfo.Get;
2888                                                         } else {
2889                                                                 if (!pe.PropertyInfo.HasSet) {
2890                                                                         if (rc.HasSet (ResolveContext.Options.ConstructorScope) && pe.IsAutoPropertyAccess &&
2891                                                                                 pe.PropertyInfo.DeclaringType == rc.CurrentType && pe.IsStatic == rc.IsStatic) {
2892                                                                                 var p = (Property) pe.PropertyInfo.MemberDefinition;
2893                                                                                 return new FieldExpr (p.BackingField, loc);
2894                                                                         }
2895
2896                                                                         variable_found = true;
2897                                                                         break;
2898                                                                 }
2899
2900                                                                 if (!pe.PropertyInfo.Set.IsAccessible (rc)) {
2901                                                                         variable_found = true;
2902                                                                         break;
2903                                                                 }
2904
2905                                                                 pe.Setter = pe.PropertyInfo.Set;
2906                                                         }
2907                                                 }
2908                                         }
2909
2910                                         // TODO: It's used by EventExpr -> FieldExpr transformation only
2911                                         // TODO: Should go to MemberAccess
2912                                         me = me.ResolveMemberAccess (rc, null, null);
2913
2914                                         if (Arity > 0) {
2915                                                 targs.Resolve (rc, false);
2916                                                 me.SetTypeArguments (rc, targs);
2917                                         }
2918
2919                                         return me;
2920                                 }
2921
2922                                 //
2923                                 // Stage 3: Lookup nested types, namespaces and type parameters in the context
2924                                 //
2925                                 if ((restrictions & MemberLookupRestrictions.InvocableOnly) == 0 && !variable_found) {
2926                                         if (IsPossibleTypeOrNamespace (rc)) {
2927                                                 return ResolveAsTypeOrNamespace (rc, false);
2928                                         }
2929                                 }
2930
2931                                 var expr = NamespaceContainer.LookupStaticUsings (rc, Name, Arity, loc);
2932                                 if (expr != null) {
2933                                         if (Arity > 0) {
2934                                                 targs.Resolve (rc, false);
2935
2936                                                 var me = expr as MemberExpr;
2937                                                 if (me != null)
2938                                                         me.SetTypeArguments (rc, targs);
2939                                         }
2940                                         return expr;
2941                                 }
2942
2943                                 if ((restrictions & MemberLookupRestrictions.NameOfExcluded) == 0 && Name == "nameof")
2944                                         return new NameOf (this);
2945
2946                                 if (errorMode) {
2947                                         if (variable_found) {
2948                                                 rc.Report.Error (841, loc, "A local variable `{0}' cannot be used before it is declared", Name);
2949                                         } else {
2950                                                 if (Arity > 0) {
2951                                                         var tparams = rc.CurrentTypeParameters;
2952                                                         if (tparams != null) {
2953                                                                 if (tparams.Find (Name) != null) {
2954                                                                         Error_TypeArgumentsCannotBeUsed (rc, "type parameter", Name, loc);
2955                                                                         return null;
2956                                                                 }
2957                                                         }
2958
2959                                                         var ct = rc.CurrentType;
2960                                                         do {
2961                                                                 if (ct.MemberDefinition.TypeParametersCount > 0) {
2962                                                                         foreach (var ctp in ct.MemberDefinition.TypeParameters) {
2963                                                                                 if (ctp.Name == Name) {
2964                                                                                         Error_TypeArgumentsCannotBeUsed (rc, "type parameter", Name, loc);
2965                                                                                         return null;
2966                                                                                 }
2967                                                                         }
2968                                                                 }
2969
2970                                                                 ct = ct.DeclaringType;
2971                                                         } while (ct != null);
2972                                                 }
2973
2974                                                 if ((restrictions & MemberLookupRestrictions.InvocableOnly) == 0) {
2975                                                         e = rc.LookupNamespaceOrType (Name, Arity, LookupMode.IgnoreAccessibility, loc);
2976                                                         if (e != null) {
2977                                                                 rc.Report.SymbolRelatedToPreviousError (e.Type);
2978                                                                 ErrorIsInaccesible (rc, e.GetSignatureForError (), loc);
2979                                                                 return e;
2980                                                         }
2981                                                 } else {
2982                                                         var me = MemberLookup (rc, false, rc.CurrentType, Name, Arity, restrictions & ~MemberLookupRestrictions.InvocableOnly, loc) as MemberExpr;
2983                                                         if (me != null) {
2984                                                                 Error_UnexpectedKind (rc, me, "method group", me.KindName, loc);
2985                                                                 return ErrorExpression.Instance;
2986                                                         }
2987                                                 }
2988
2989                                                 e = rc.LookupNamespaceOrType (Name, -System.Math.Max (1, Arity), LookupMode.Probing, loc);
2990                                                 if (e != null) {
2991                                                         if (e.Type.Arity != Arity && (restrictions & MemberLookupRestrictions.IgnoreArity) == 0) {
2992                                                                 Error_TypeArgumentsCannotBeUsed (rc, e.Type, loc);
2993                                                                 return e;
2994                                                         }
2995
2996                                                         if (e is TypeExpr) {
2997                                                                 // TypeExpression does not have correct location
2998                                                                 if (e is TypeExpression)
2999                                                                         e = new TypeExpression (e.Type, loc);
3000
3001                                                                 return e;
3002                                                         }
3003                                                 }
3004
3005                                                 Error_NameDoesNotExist (rc);
3006                                         }
3007
3008                                         return ErrorExpression.Instance;
3009                                 }
3010
3011                                 if (rc.Module.Evaluator != null) {
3012                                         var fi = rc.Module.Evaluator.LookupField (Name);
3013                                         if (fi != null)
3014                                                 return new FieldExpr (fi.Item1, loc);
3015                                 }
3016
3017                                 lookup_arity = 0;
3018                                 errorMode = true;
3019                         }
3020                 }
3021                 
3022                 Expression SimpleNameResolve (ResolveContext ec, Expression right_side)
3023                 {
3024                         Expression e = LookupNameExpression (ec, right_side == null ? MemberLookupRestrictions.ReadAccess : MemberLookupRestrictions.None);
3025
3026                         if (e == null)
3027                                 return null;
3028
3029                         if (e is FullNamedExpression && e.eclass != ExprClass.Unresolved) {
3030                                 Error_UnexpectedKind (ec, e, "variable", e.ExprClassName, loc);
3031                                 return e;
3032                         }
3033
3034                         if (right_side != null) {
3035                                 e = e.ResolveLValue (ec, right_side);
3036                         } else {
3037                                 e = e.Resolve (ec);
3038                         }
3039
3040                         return e;
3041                 }
3042                 
3043                 public override object Accept (StructuralVisitor visitor)
3044                 {
3045                         return visitor.Visit (this);
3046                 }
3047         }
3048
3049         /// <summary>
3050         ///   Represents a namespace or a type.  The name of the class was inspired by
3051         ///   section 10.8.1 (Fully Qualified Names).
3052         /// </summary>
3053         public abstract class FullNamedExpression : Expression
3054         {
3055                 protected override void CloneTo (CloneContext clonectx, Expression target)
3056                 {
3057                         // Do nothing, most unresolved type expressions cannot be
3058                         // resolved to different type
3059                 }
3060
3061                 public override bool ContainsEmitWithAwait ()
3062                 {
3063                         return false;
3064                 }
3065
3066                 public override Expression CreateExpressionTree (ResolveContext ec)
3067                 {
3068                         throw new NotSupportedException ("ET");
3069                 }
3070
3071                 public abstract FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext mc, bool allowUnboundTypeArguments);
3072
3073                 //
3074                 // This is used to resolve the expression as a type, a null
3075                 // value will be returned if the expression is not a type
3076                 // reference
3077                 //
3078                 public override TypeSpec ResolveAsType (IMemberContext mc, bool allowUnboundTypeArguments = false)
3079                 {
3080                         FullNamedExpression fne = ResolveAsTypeOrNamespace (mc, allowUnboundTypeArguments);
3081
3082                         if (fne == null)
3083                                 return null;
3084
3085                         TypeExpr te = fne as TypeExpr;
3086                         if (te == null) {
3087                                 Error_UnexpectedKind (mc, fne, "type", fne.ExprClassName, loc);
3088                                 return null;
3089                         }
3090
3091                         te.loc = loc;
3092
3093                         type = te.Type;
3094
3095                         var dep = type.GetMissingDependencies ();
3096                         if (dep != null) {
3097                                 ImportedTypeDefinition.Error_MissingDependency (mc, dep, loc);
3098                         }
3099
3100                         if (type.Kind == MemberKind.Void) {
3101                                 mc.Module.Compiler.Report.Error (673, loc, "System.Void cannot be used from C#. Consider using `void'");
3102                         }
3103
3104                         //
3105                         // Obsolete checks cannot be done when resolving base context as they
3106                         // require type dependencies to be set but we are in process of resolving them
3107                         //
3108                         if (mc is ResolveContext) {
3109                                 var oa = type.GetAttributeObsolete ();
3110                                 if (oa != null && !mc.IsObsolete)
3111                                         AttributeTester.Report_ObsoleteMessage (oa, type.GetSignatureForError (), fne.Location, mc.Module.Compiler.Report);
3112                         }
3113
3114                         return type;
3115                 }
3116
3117
3118                 public override void Emit (EmitContext ec)
3119                 {
3120                         throw new InternalErrorException ("FullNamedExpression `{0}' found in resolved tree",
3121                                 GetSignatureForError ());
3122                 }
3123         }
3124         
3125         /// <summary>
3126         ///   Expression that evaluates to a type
3127         /// </summary>
3128         public abstract class TypeExpr : FullNamedExpression
3129         {
3130                 public sealed override FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext mc, bool allowUnboundTypeArguments)
3131                 {
3132                         ResolveAsType (mc);
3133                         return this;
3134                 }
3135
3136                 protected sealed override Expression DoResolve (ResolveContext ec)
3137                 {
3138                         ResolveAsType (ec);
3139                         return this;
3140                 }
3141
3142                 public override bool Equals (object obj)
3143                 {
3144                         TypeExpr tobj = obj as TypeExpr;
3145                         if (tobj == null)
3146                                 return false;
3147
3148                         return Type == tobj.Type;
3149                 }
3150
3151                 public override int GetHashCode ()
3152                 {
3153                         return Type.GetHashCode ();
3154                 }
3155         }
3156
3157         /// <summary>
3158         ///   Fully resolved Expression that already evaluated to a type
3159         /// </summary>
3160         public class TypeExpression : TypeExpr
3161         {
3162                 public TypeExpression (TypeSpec t, Location l)
3163                 {
3164                         Type = t;
3165                         eclass = ExprClass.Type;
3166                         loc = l;
3167                 }
3168
3169                 public sealed override TypeSpec ResolveAsType (IMemberContext mc, bool allowUnboundTypeArguments = false)
3170                 {
3171                         return type;
3172                 }
3173         }
3174
3175         public class NamespaceExpression : FullNamedExpression
3176         {
3177                 readonly Namespace ns;
3178
3179                 public NamespaceExpression (Namespace ns, Location loc)
3180                 {
3181                         this.ns = ns;
3182                         this.Type = InternalType.Namespace;
3183                         this.eclass = ExprClass.Namespace;
3184                         this.loc = loc;
3185                 }
3186
3187                 public Namespace Namespace {
3188                         get {
3189                                 return ns;
3190                         }
3191                 }
3192
3193                 protected override Expression DoResolve (ResolveContext rc)
3194                 {
3195                         throw new NotImplementedException ();
3196                 }
3197
3198                 public override FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext mc, bool allowUnboundTypeArguments)
3199                 {
3200                         return this;
3201                 }
3202
3203                 public void Error_NamespaceDoesNotExist (IMemberContext ctx, string name, int arity, Location loc)
3204                 {
3205                         var retval = Namespace.LookupType (ctx, name, arity, LookupMode.IgnoreAccessibility, loc);
3206                         if (retval != null) {
3207 //                              ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (retval.MemberDefinition);
3208                                 ErrorIsInaccesible (ctx, retval.GetSignatureForError (), loc);
3209                                 return;
3210                         }
3211
3212                         retval = Namespace.LookupType (ctx, name, -System.Math.Max (1, arity), LookupMode.Probing, loc);
3213                         if (retval != null) {
3214                                 Error_TypeArgumentsCannotBeUsed (ctx, retval, loc);
3215                                 return;
3216                         }
3217
3218                         Namespace ns;
3219                         if (arity > 0 && Namespace.TryGetNamespace (name, out ns)) {
3220                                 Error_TypeArgumentsCannotBeUsed (ctx, ExprClassName, ns.GetSignatureForError (), loc);
3221                                 return;
3222                         }
3223
3224                         string assembly = null;
3225                         string possible_name = Namespace.GetSignatureForError () + "." + name;
3226
3227                         // Only assembly unique name should be added
3228                         switch (possible_name) {
3229                         case "System.Drawing":
3230                         case "System.Web.Services":
3231                         case "System.Web":
3232                         case "System.Data":
3233                         case "System.Configuration":
3234                         case "System.Data.Services":
3235                         case "System.DirectoryServices":
3236                         case "System.Json":
3237                         case "System.Net.Http":
3238                         case "System.Numerics":
3239                         case "System.Runtime.Caching":
3240                         case "System.ServiceModel":
3241                         case "System.Transactions":
3242                         case "System.Web.Routing":
3243                         case "System.Xml.Linq":
3244                         case "System.Xml":
3245                                 assembly = possible_name;
3246                                 break;
3247
3248                         case "System.Linq":
3249                         case "System.Linq.Expressions":
3250                                 assembly = "System.Core";
3251                                 break;
3252
3253                         case "System.Windows.Forms":
3254                         case "System.Windows.Forms.Layout":
3255                                 assembly = "System.Windows.Forms";
3256                                 break;
3257                         }
3258
3259                         assembly = assembly == null ? "an" : "`" + assembly + "'";
3260
3261                         if (Namespace is GlobalRootNamespace) {
3262                                 ctx.Module.Compiler.Report.Error (400, loc,
3263                                         "The type or namespace name `{0}' could not be found in the global namespace. Are you missing {1} assembly reference?",
3264                                         name, assembly);
3265                         } else {
3266                                 ctx.Module.Compiler.Report.Error (234, loc,
3267                                         "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing {2} assembly reference?",
3268                                         name, GetSignatureForError (), assembly);
3269                         }
3270                 }
3271
3272                 public override string GetSignatureForError ()
3273                 {
3274                         return ns.GetSignatureForError ();
3275                 }
3276
3277                 public FullNamedExpression LookupTypeOrNamespace (IMemberContext ctx, string name, int arity, LookupMode mode, Location loc)
3278                 {
3279                         return ns.LookupTypeOrNamespace (ctx, name, arity, mode, loc);
3280                 }
3281
3282                 public override string ToString ()
3283                 {
3284                         return Namespace.Name;
3285                 }
3286     }
3287
3288         /// <summary>
3289         ///   This class denotes an expression which evaluates to a member
3290         ///   of a struct or a class.
3291         /// </summary>
3292         public abstract class MemberExpr : Expression, OverloadResolver.IInstanceQualifier
3293         {
3294                 protected bool conditional_access_receiver;
3295
3296                 //
3297                 // An instance expression associated with this member, if it's a
3298                 // non-static member
3299                 //
3300                 public Expression InstanceExpression;
3301
3302                 /// <summary>
3303                 ///   The name of this member.
3304                 /// </summary>
3305                 public abstract string Name {
3306                         get;
3307                 }
3308
3309                 //
3310                 // When base.member is used
3311                 //
3312                 public bool IsBase {
3313                         get { return InstanceExpression is BaseThis; }
3314                 }
3315
3316                 /// <summary>
3317                 ///   Whether this is an instance member.
3318                 /// </summary>
3319                 public abstract bool IsInstance {
3320                         get;
3321                 }
3322
3323                 /// <summary>
3324                 ///   Whether this is a static member.
3325                 /// </summary>
3326                 public abstract bool IsStatic {
3327                         get;
3328                 }
3329
3330                 public abstract string KindName {
3331                         get;
3332                 }
3333
3334                 public bool ConditionalAccess { get; set; }
3335
3336                 protected abstract TypeSpec DeclaringType {
3337                         get;
3338                 }
3339
3340                 TypeSpec OverloadResolver.IInstanceQualifier.InstanceType {
3341                         get {
3342                                 return InstanceExpression.Type;
3343                         }
3344                 }
3345
3346                 //
3347                 // Converts best base candidate for virtual method starting from QueriedBaseType
3348                 //
3349                 protected MethodSpec CandidateToBaseOverride (ResolveContext rc, MethodSpec method)
3350                 {
3351                         //
3352                         // Only when base.member is used and method is virtual
3353                         //
3354                         if (!IsBase)
3355                                 return method;
3356
3357                         //
3358                         // Overload resulution works on virtual or non-virtual members only (no overrides). That
3359                         // means for base.member access we have to find the closest match after we found best candidate
3360                         //
3361                         if ((method.Modifiers & (Modifiers.ABSTRACT | Modifiers.VIRTUAL | Modifiers.OVERRIDE)) != 0) {
3362                                 //
3363                                 // The method could already be what we are looking for
3364                                 //
3365                                 TypeSpec[] targs = null;
3366                                 if (method.DeclaringType != InstanceExpression.Type) {
3367                                         //
3368                                         // Candidate can have inflated MVAR parameters and we need to find
3369                                         // base match for original definition not inflated parameter types
3370                                         //
3371                                         var parameters = method.Parameters;
3372                                         if (method.Arity > 0) {
3373                                                 parameters = ((IParametersMember) method.MemberDefinition).Parameters;
3374                                                 var inflated = method.DeclaringType as InflatedTypeSpec;
3375                                                 if (inflated != null) {
3376                                                         parameters = parameters.Inflate (inflated.CreateLocalInflator (rc));
3377                                                 }
3378                                         }
3379
3380                                         var filter = new MemberFilter (method.Name, method.Arity, MemberKind.Method, parameters, null);
3381                                         var base_override = MemberCache.FindMember (InstanceExpression.Type, filter, BindingRestriction.InstanceOnly | BindingRestriction.OverrideOnly) as MethodSpec;
3382                                         if (base_override != null && base_override.DeclaringType != method.DeclaringType) {
3383                                                 if (base_override.IsGeneric)
3384                                                         targs = method.TypeArguments;
3385
3386                                                 method = base_override;
3387                                         }
3388                                 }
3389
3390                                 //
3391                                 // When base access is used inside anonymous method/iterator/etc we need to
3392                                 // get back to the context of original type. We do it by emiting proxy
3393                                 // method in original class and rewriting base call to this compiler
3394                                 // generated method call which does the actual base invocation. This may
3395                                 // introduce redundant storey but with `this' only but it's tricky to avoid
3396                                 // at this stage as we don't know what expressions follow base
3397                                 //
3398                                 if (rc.CurrentAnonymousMethod != null) {
3399                                         if (targs == null && method.IsGeneric) {
3400                                                 targs = method.TypeArguments;
3401                                                 method = method.GetGenericMethodDefinition ();
3402                                         }
3403
3404                                         if (method.Parameters.HasArglist)
3405                                                 throw new NotImplementedException ("__arglist base call proxy");
3406
3407                                         method = rc.CurrentMemberDefinition.Parent.PartialContainer.CreateHoistedBaseCallProxy (rc, method);
3408
3409                                         // Ideally this should apply to any proxy rewrite but in the case of unary mutators on
3410                                         // get/set member expressions second call would fail to proxy because left expression
3411                                         // would be of 'this' and not 'base' because we share InstanceExpression for get/set
3412                                         // FIXME: The async check is another hack but will probably fail with mutators
3413                                         if (rc.CurrentType.IsStruct || rc.CurrentAnonymousMethod.Storey is AsyncTaskStorey)
3414                                                 InstanceExpression = new This (loc).Resolve (rc);
3415                                 }
3416
3417                                 if (targs != null)
3418                                         method = method.MakeGenericMethod (rc, targs);
3419                         }
3420
3421                         //
3422                         // Only base will allow this invocation to happen.
3423                         //
3424                         if (method.IsAbstract) {
3425                                 rc.Report.SymbolRelatedToPreviousError (method);
3426                                 Error_CannotCallAbstractBase (rc, method.GetSignatureForError ());
3427                         }
3428
3429                         return method;
3430                 }
3431
3432                 protected void CheckProtectedMemberAccess (ResolveContext rc, MemberSpec member)
3433                 {
3434                         if (InstanceExpression == null)
3435                                 return;
3436
3437                         if ((member.Modifiers & Modifiers.PROTECTED) != 0 && !(InstanceExpression is This)) {
3438                                 if (!CheckProtectedMemberAccess (rc, member, InstanceExpression.Type)) {
3439                                         Error_ProtectedMemberAccess (rc, member, InstanceExpression.Type, loc);
3440                                 }
3441                         }
3442                 }
3443
3444                 bool OverloadResolver.IInstanceQualifier.CheckProtectedMemberAccess (ResolveContext rc, MemberSpec member)
3445                 {
3446                         if (InstanceExpression == null)
3447                                 return true;
3448
3449                         return InstanceExpression is This || CheckProtectedMemberAccess (rc, member, InstanceExpression.Type);
3450                 }
3451
3452                 public static bool CheckProtectedMemberAccess<T> (ResolveContext rc, T member, TypeSpec qualifier) where T : MemberSpec
3453                 {
3454                         var ct = rc.CurrentType;
3455                         if (ct == qualifier)
3456                                 return true;
3457
3458                         if ((member.Modifiers & Modifiers.INTERNAL) != 0 && member.DeclaringType.MemberDefinition.IsInternalAsPublic (ct.MemberDefinition.DeclaringAssembly))
3459                                 return true;
3460
3461                         qualifier = qualifier.GetDefinition ();
3462                         if (ct != qualifier && !IsSameOrBaseQualifier (ct, qualifier)) {
3463                                 return false;
3464                         }
3465
3466                         return true;
3467                 }
3468
3469                 public override bool ContainsEmitWithAwait ()
3470                 {
3471                         return InstanceExpression != null && InstanceExpression.ContainsEmitWithAwait ();
3472                 }
3473
3474                 public override bool HasConditionalAccess ()
3475                 {
3476                         return ConditionalAccess || (InstanceExpression != null && InstanceExpression.HasConditionalAccess ());
3477                 }
3478
3479                 static bool IsSameOrBaseQualifier (TypeSpec type, TypeSpec qtype)
3480                 {
3481                         do {
3482                                 type = type.GetDefinition ();
3483
3484                                 if (type == qtype || TypeManager.IsFamilyAccessible (qtype, type))
3485                                         return true;
3486
3487                                 type = type.DeclaringType;
3488                         } while (type != null);
3489
3490                         return false;
3491                 }
3492
3493                 protected void DoBestMemberChecks<T> (ResolveContext rc, T member) where T : MemberSpec, IInterfaceMemberSpec
3494                 {
3495                         if (InstanceExpression != null) {
3496                                 InstanceExpression = InstanceExpression.Resolve (rc);
3497                                 CheckProtectedMemberAccess (rc, member);
3498                         }
3499
3500                         if (member.MemberType.IsPointer && !rc.IsUnsafe) {
3501                                 UnsafeError (rc, loc);
3502                         }
3503
3504                         var dep = member.GetMissingDependencies ();
3505                         if (dep != null) {
3506                                 ImportedTypeDefinition.Error_MissingDependency (rc, dep, loc);
3507                         }
3508
3509                         member.CheckObsoleteness (rc, loc);
3510
3511                         if (!(member is FieldSpec))
3512                                 member.MemberDefinition.SetIsUsed ();
3513                 }
3514
3515                 protected virtual void Error_CannotCallAbstractBase (ResolveContext rc, string name)
3516                 {
3517                         rc.Report.Error (205, loc, "Cannot call an abstract base member `{0}'", name);
3518                 }
3519
3520                 public static void Error_ProtectedMemberAccess (ResolveContext rc, MemberSpec member, TypeSpec qualifier, Location loc)
3521                 {
3522                         rc.Report.SymbolRelatedToPreviousError (member);
3523                         rc.Report.Error (1540, loc,
3524                                 "Cannot access protected member `{0}' via a qualifier of type `{1}'. The qualifier must be of type `{2}' or derived from it",
3525                                 member.GetSignatureForError (), qualifier.GetSignatureForError (), rc.CurrentType.GetSignatureForError ());
3526                 }
3527
3528                 public override void FlowAnalysis (FlowAnalysisContext fc)
3529                 {
3530                         if (InstanceExpression != null) {
3531                                 InstanceExpression.FlowAnalysis (fc);
3532                         }
3533                 }
3534
3535                 protected void ResolveConditionalAccessReceiver (ResolveContext rc)
3536                 {
3537                         if (!rc.HasSet (ResolveContext.Options.DontSetConditionalAccessReceiver) && HasConditionalAccess ()) {
3538                                 conditional_access_receiver = true;
3539                         }
3540                 }
3541
3542                 public bool ResolveInstanceExpression (ResolveContext rc, Expression rhs)
3543                 {
3544                         if (!ResolveInstanceExpressionCore (rc, rhs))
3545                                 return false;
3546
3547                         //
3548                         // Check intermediate value modification which won't have any effect
3549                         //
3550                         if (rhs != null && TypeSpec.IsValueType (InstanceExpression.Type)) {
3551                                 var fexpr = InstanceExpression as FieldExpr;
3552                                 if (fexpr != null) {
3553                                         if (!fexpr.Spec.IsReadOnly || rc.HasAny (ResolveContext.Options.FieldInitializerScope | ResolveContext.Options.ConstructorScope))
3554                                                 return true;
3555
3556                                         if (fexpr.IsStatic) {
3557                                                 rc.Report.Error (1650, loc, "Fields of static readonly field `{0}' cannot be assigned to (except in a static constructor or a variable initializer)",
3558                                                         fexpr.GetSignatureForError ());
3559                                         } else {
3560                                                 rc.Report.Error (1648, loc, "Members of readonly field `{0}' cannot be modified (except in a constructor or a variable initializer)",
3561                                                         fexpr.GetSignatureForError ());
3562                                         }
3563
3564                                         return true;
3565                                 }
3566
3567                                 if (InstanceExpression is PropertyExpr || InstanceExpression is IndexerExpr || InstanceExpression is Invocation) {
3568                                         if (rc.CurrentInitializerVariable != null) {
3569                                                 rc.Report.Error (1918, loc, "Members of value type `{0}' cannot be assigned using a property `{1}' object initializer",
3570                                                         InstanceExpression.Type.GetSignatureForError (), InstanceExpression.GetSignatureForError ());
3571                                         } else {
3572                                                 rc.Report.Error (1612, loc,
3573                                                         "Cannot modify a value type return value of `{0}'. Consider storing the value in a temporary variable",
3574                                                         InstanceExpression.GetSignatureForError ());
3575                                         }
3576
3577                                         return true;
3578                                 }
3579
3580                                 var lvr = InstanceExpression as LocalVariableReference;
3581                                 if (lvr != null) {
3582
3583                                         if (!lvr.local_info.IsReadonly)
3584                                                 return true;
3585
3586                                         rc.Report.Error (1654, loc, "Cannot assign to members of `{0}' because it is a `{1}'",
3587                                                 InstanceExpression.GetSignatureForError (), lvr.local_info.GetReadOnlyContext ());
3588                                 }
3589                         }
3590
3591                         return true;
3592                 }
3593
3594                 bool ResolveInstanceExpressionCore (ResolveContext rc, Expression rhs)
3595                 {
3596                         if (IsStatic) {
3597                                 if (InstanceExpression != null) {
3598                                         if (InstanceExpression is TypeExpr) {
3599                                                 var t = InstanceExpression.Type;
3600                                                 do {
3601                                                         t.CheckObsoleteness (rc, loc);
3602
3603                                                         t = t.DeclaringType;
3604                                                 } while (t != null);
3605                                         } else {
3606                                                 var runtime_expr = InstanceExpression as RuntimeValueExpression;
3607                                                 if (runtime_expr == null || !runtime_expr.IsSuggestionOnly) {
3608                                                         rc.Report.Error (176, loc,
3609                                                                 "Static member `{0}' cannot be accessed with an instance reference, qualify it with a type name instead",
3610                                                                 GetSignatureForError ());
3611                                                 }
3612                                         }
3613
3614                                         InstanceExpression = null;
3615                                 }
3616
3617                                 return false;
3618                         }
3619
3620                         if (InstanceExpression == null || InstanceExpression is TypeExpr) {
3621                                 if (InstanceExpression != null || !This.IsThisAvailable (rc, true)) {
3622                                         if (rc.HasSet (ResolveContext.Options.FieldInitializerScope)) {
3623                                                 rc.Report.Error (236, loc,
3624                                                         "A field initializer cannot reference the nonstatic field, method, or property `{0}'",
3625                                                         GetSignatureForError ());
3626                                         } else {
3627                                                 var fe = this as FieldExpr;
3628                                                 if (fe != null && fe.Spec.MemberDefinition is PrimaryConstructorField) {
3629                                                         if (rc.HasSet (ResolveContext.Options.BaseInitializer)) {
3630                                                                 rc.Report.Error (9005, loc, "Constructor initializer cannot access primary constructor parameters");
3631                                                         } else  {
3632                                                                 rc.Report.Error (9006, loc, "An object reference is required to access primary constructor parameter `{0}'",
3633                                                                         fe.Name);
3634                                                         }
3635                                                 } else {
3636                                                         rc.Report.Error (120, loc,
3637                                                                 "An object reference is required to access non-static member `{0}'",
3638                                                                 GetSignatureForError ());
3639                                                 }
3640                                         }
3641
3642                                         InstanceExpression = new CompilerGeneratedThis (rc.CurrentType, loc).Resolve (rc);
3643                                         return false;
3644                                 }
3645
3646                                 if (!TypeManager.IsFamilyAccessible (rc.CurrentType, DeclaringType)) {
3647                                         rc.Report.Error (38, loc,
3648                                                 "Cannot access a nonstatic member of outer type `{0}' via nested type `{1}'",
3649                                                 DeclaringType.GetSignatureForError (), rc.CurrentType.GetSignatureForError ());
3650                                 }
3651
3652                                 InstanceExpression = new This (loc).Resolve (rc);
3653                                 return false;
3654                         }
3655
3656                         var me = InstanceExpression as MemberExpr;
3657                         if (me != null) {
3658                                 me.ResolveInstanceExpressionCore (rc, rhs);
3659
3660                                 var fe = me as FieldExpr;
3661                                 if (fe != null && fe.IsMarshalByRefAccess (rc)) {
3662                                         rc.Report.SymbolRelatedToPreviousError (me.DeclaringType);
3663                                         rc.Report.Warning (1690, 1, loc,
3664                                                 "Cannot call methods, properties, or indexers on `{0}' because it is a value type member of a marshal-by-reference class",
3665                                                 me.GetSignatureForError ());
3666                                 }
3667
3668                                 return true;
3669                         }
3670
3671                         //
3672                         // Additional checks for l-value member access
3673                         //
3674                         if (rhs != null) {
3675                                 if (InstanceExpression is UnboxCast) {
3676                                         rc.Report.Error (445, InstanceExpression.Location, "Cannot modify the result of an unboxing conversion");
3677                                 }
3678                         }
3679
3680                         return true;
3681                 }
3682
3683                 public virtual MemberExpr ResolveMemberAccess (ResolveContext ec, Expression left, SimpleName original)
3684                 {
3685                         if (left != null && !ConditionalAccess && !ec.HasSet (ResolveContext.Options.NameOfScope) && left.IsNull && TypeSpec.IsReferenceType (left.Type)) {
3686                                 ec.Report.Warning (1720, 1, left.Location,
3687                                         "Expression will always cause a `{0}'", "System.NullReferenceException");
3688                         }
3689
3690                         InstanceExpression = left;
3691                         return this;
3692                 }
3693
3694                 protected void EmitInstance (EmitContext ec, bool prepare_for_load)
3695                 {
3696                         var inst = new InstanceEmitter (InstanceExpression, TypeSpec.IsValueType (InstanceExpression.Type));
3697                         inst.Emit (ec, ConditionalAccess);
3698
3699                         if (prepare_for_load)
3700                                 ec.Emit (OpCodes.Dup);
3701                 }
3702
3703                 public abstract void SetTypeArguments (ResolveContext ec, TypeArguments ta);
3704         }
3705
3706         public class ExtensionMethodCandidates
3707         {
3708                 readonly NamespaceContainer container;
3709                 readonly IList<MethodSpec> methods;
3710                 readonly int index;
3711                 readonly IMemberContext context;
3712
3713                 public ExtensionMethodCandidates (IMemberContext context, IList<MethodSpec> methods, NamespaceContainer nsContainer, int lookupIndex)
3714                 {
3715                         this.context = context;
3716                         this.methods = methods;
3717                         this.container = nsContainer;
3718                         this.index = lookupIndex;
3719                 }
3720
3721                 public NamespaceContainer Container {
3722                         get {
3723                                 return container;
3724                         }
3725                 }
3726
3727                 public IMemberContext Context {
3728                         get {
3729                                 return context;
3730                         }
3731                 }
3732
3733                 public int LookupIndex {
3734                         get {
3735                                 return index;
3736                         }
3737                 }
3738
3739                 public IList<MethodSpec> Methods {
3740                         get {
3741                                 return methods;
3742                         }
3743                 }
3744         }
3745
3746         // 
3747         // Represents a group of extension method candidates for whole namespace
3748         // 
3749         class ExtensionMethodGroupExpr : MethodGroupExpr, OverloadResolver.IErrorHandler
3750         {
3751                 ExtensionMethodCandidates candidates;
3752                 public Expression ExtensionExpression;
3753
3754                 public ExtensionMethodGroupExpr (ExtensionMethodCandidates candidates, Expression extensionExpr, Location loc)
3755                         : base (candidates.Methods.Cast<MemberSpec>().ToList (), extensionExpr.Type, loc)
3756                 {
3757                         this.candidates = candidates;
3758                         this.ExtensionExpression = extensionExpr;
3759                 }
3760
3761                 public override bool IsStatic {
3762                         get { return true; }
3763                 }
3764
3765                 //
3766                 // For extension methodgroup we are not looking for base members but parent
3767                 // namespace extension methods
3768                 //
3769                 public override IList<MemberSpec> GetBaseMembers (TypeSpec baseType)
3770                 {
3771                         // TODO: candidates are null only when doing error reporting, that's
3772                         // incorrect. We have to discover same extension methods in error mode
3773                         if (candidates == null)
3774                                 return null;
3775
3776                         int arity = type_arguments == null ? 0 : type_arguments.Count;
3777
3778                         candidates = candidates.Container.LookupExtensionMethod (candidates.Context, Name, arity, candidates.LookupIndex);
3779                         if (candidates == null)
3780                                 return null;
3781
3782                         return candidates.Methods.Cast<MemberSpec> ().ToList ();
3783                 }
3784
3785                 public static bool IsExtensionTypeCompatible (TypeSpec argType, TypeSpec extensionType)
3786                 {
3787                         //
3788                         // Indentity, implicit reference or boxing conversion must exist for the extension parameter
3789                         //
3790                         // LAMESPEC: or implicit type parameter conversion
3791                         //
3792                         return argType == extensionType ||
3793                                 TypeSpecComparer.IsEqual (argType, extensionType) ||
3794                                 Convert.ImplicitReferenceConversionExists (argType, extensionType, false) ||
3795                                 Convert.ImplicitBoxingConversion (null, argType, extensionType) != null;
3796                 }
3797
3798                 public bool ResolveNameOf (ResolveContext rc, MemberAccess ma)
3799                 {
3800                         rc.Report.Error (8093, ma.Location, "An argument to nameof operator cannot be extension method group");
3801
3802                         // Not included in C#6
3803                         /*
3804                         ExtensionExpression = ExtensionExpression.Resolve (rc);
3805                         if (ExtensionExpression == null)
3806                                 return false;
3807
3808                         var argType = ExtensionExpression.Type;
3809                         foreach (MethodSpec candidate in Candidates) {
3810                                 if (ExtensionMethodGroupExpr.IsExtensionTypeCompatible (argType, candidate.Parameters.ExtensionMethodType))
3811                                         return true;
3812                         }
3813
3814                         // TODO: Scan full hierarchy
3815
3816                         ma.Error_TypeDoesNotContainDefinition (rc, argType, ma.Name);
3817                         */
3818                         return false;
3819                 }
3820
3821                 public override MethodGroupExpr LookupExtensionMethod (ResolveContext rc)
3822                 {
3823                         // We are already here
3824                         return null;
3825                 }
3826
3827                 public override MethodGroupExpr OverloadResolve (ResolveContext ec, ref Arguments arguments, OverloadResolver.IErrorHandler ehandler, OverloadResolver.Restrictions restr)
3828                 {
3829                         if (arguments == null)
3830                                 arguments = new Arguments (1);
3831
3832                         ExtensionExpression = ExtensionExpression.Resolve (ec);
3833                         if (ExtensionExpression == null)
3834                                 return null;
3835
3836                         var cand = candidates;
3837                         var atype = ConditionalAccess ? Argument.AType.ExtensionTypeConditionalAccess : Argument.AType.ExtensionType;
3838                         arguments.Insert (0, new Argument (ExtensionExpression, atype));
3839                         var res = base.OverloadResolve (ec, ref arguments, ehandler ?? this, restr);
3840                         
3841                         // Restore candidates in case we are running in probing mode 
3842                         candidates = cand;
3843
3844                         // Store resolved argument and restore original arguments
3845                         if (res == null) {
3846                                 // Clean-up modified arguments for error reporting
3847                                 arguments.RemoveAt (0);
3848                                 return null;
3849                         }
3850
3851                         var me = ExtensionExpression as MemberExpr;
3852                         if (me != null) {
3853                                 me.ResolveInstanceExpression (ec, null);
3854                                 var fe = me as FieldExpr;
3855                                 if (fe != null)
3856                                         fe.Spec.MemberDefinition.SetIsUsed ();
3857                         }
3858
3859                         InstanceExpression = null;
3860                         return this;
3861                 }
3862
3863                 #region IErrorHandler Members
3864
3865                 bool OverloadResolver.IErrorHandler.AmbiguousCandidates (ResolveContext rc, MemberSpec best, MemberSpec ambiguous)
3866                 {
3867                         return false;
3868                 }
3869
3870                 bool OverloadResolver.IErrorHandler.ArgumentMismatch (ResolveContext rc, MemberSpec best, Argument arg, int index)
3871                 {
3872                         rc.Report.SymbolRelatedToPreviousError (best);
3873
3874                         if (index == 0) {
3875                                 rc.Report.Error (1929, loc,
3876                                         "Type `{0}' does not contain a member `{1}' and the best extension method overload `{2}' requires an instance of type `{3}'",
3877                                         queried_type.GetSignatureForError (), Name, best.GetSignatureForError (), ((MethodSpec)best).Parameters.ExtensionMethodType.GetSignatureForError ());
3878                         } else {
3879                                 rc.Report.Error (1928, loc,
3880                                         "Type `{0}' does not contain a member `{1}' and the best extension method overload `{2}' has some invalid arguments",
3881                                         queried_type.GetSignatureForError (), Name, best.GetSignatureForError ());
3882                         }
3883
3884                         return true;
3885                 }
3886
3887                 bool OverloadResolver.IErrorHandler.NoArgumentMatch (ResolveContext rc, MemberSpec best)
3888                 {
3889                         return false;
3890                 }
3891
3892                 bool OverloadResolver.IErrorHandler.TypeInferenceFailed (ResolveContext rc, MemberSpec best)
3893                 {
3894                         return false;
3895                 }
3896
3897                 #endregion
3898         }
3899
3900         /// <summary>
3901         ///   MethodGroupExpr represents a group of method candidates which
3902         ///   can be resolved to the best method overload
3903         /// </summary>
3904         public class MethodGroupExpr : MemberExpr, OverloadResolver.IBaseMembersProvider
3905         {
3906                 static readonly MemberSpec[] Excluded = new MemberSpec[0];
3907
3908                 protected IList<MemberSpec> Methods;
3909                 MethodSpec best_candidate;
3910                 TypeSpec best_candidate_return;
3911                 protected TypeArguments type_arguments;
3912
3913                 SimpleName simple_name;
3914                 protected TypeSpec queried_type;
3915
3916                 public MethodGroupExpr (IList<MemberSpec> mi, TypeSpec type, Location loc)
3917                 {
3918                         Methods = mi;
3919                         this.loc = loc;
3920                         this.type = InternalType.MethodGroup;
3921
3922                         eclass = ExprClass.MethodGroup;
3923                         queried_type = type;
3924                 }
3925
3926                 public MethodGroupExpr (MethodSpec m, TypeSpec type, Location loc)
3927                         : this (new MemberSpec[] { m }, type, loc)
3928                 {
3929                 }
3930
3931                 #region Properties
3932
3933                 public MethodSpec BestCandidate {
3934                         get {
3935                                 return best_candidate;
3936                         }
3937                 }
3938
3939                 public TypeSpec BestCandidateReturnType {
3940                         get {
3941                                 return best_candidate_return;
3942                         }
3943                 }
3944
3945                 public IList<MemberSpec> Candidates {
3946                         get {
3947                                 return Methods;
3948                         }
3949                 }
3950
3951                 protected override TypeSpec DeclaringType {
3952                         get {
3953                                 return queried_type;
3954                         }
3955                 }
3956
3957                 public bool IsConditionallyExcluded {
3958                         get {
3959                                 return Methods == Excluded;
3960                         }
3961                 }
3962
3963                 public override bool IsInstance {
3964                         get {
3965                                 if (best_candidate != null)
3966                                         return !best_candidate.IsStatic;
3967
3968                                 return false;
3969                         }
3970                 }
3971
3972                 public override bool IsSideEffectFree {
3973                         get {
3974                                 return InstanceExpression == null || InstanceExpression.IsSideEffectFree;
3975                         }
3976                 }
3977
3978                 public override bool IsStatic {
3979                         get {
3980                                 if (best_candidate != null)
3981                                         return best_candidate.IsStatic;
3982
3983                                 return false;
3984                         }
3985                 }
3986
3987                 public override string KindName {
3988                         get { return "method"; }
3989                 }
3990
3991                 public override string Name {
3992                         get {
3993                                 if (best_candidate != null)
3994                                         return best_candidate.Name;
3995
3996                                 // TODO: throw ?
3997                                 return Methods.First ().Name;
3998                         }
3999                 }
4000
4001                 #endregion
4002
4003                 //
4004                 // When best candidate is already know this factory can be used
4005                 // to avoid expensive overload resolution to be called
4006                 //
4007                 // NOTE: InstanceExpression has to be set manually
4008                 //
4009                 public static MethodGroupExpr CreatePredefined (MethodSpec best, TypeSpec queriedType, Location loc)
4010                 {
4011                         return new MethodGroupExpr (best, queriedType, loc) {
4012                                 best_candidate = best,
4013                                 best_candidate_return = best.ReturnType
4014                         };
4015                 }
4016
4017                 public override string GetSignatureForError ()
4018                 {
4019                         if (best_candidate != null)
4020                                 return best_candidate.GetSignatureForError ();
4021
4022                         return Methods.First ().GetSignatureForError ();
4023                 }
4024
4025                 public override Expression CreateExpressionTree (ResolveContext ec)
4026                 {
4027                         if (best_candidate == null) {
4028                                 ec.Report.Error (1953, loc, "An expression tree cannot contain an expression with method group");
4029                                 return null;
4030                         }
4031
4032                         if (IsConditionallyExcluded)
4033                                 ec.Report.Error (765, loc,
4034                                         "Partial methods with only a defining declaration or removed conditional methods cannot be used in an expression tree");
4035
4036                         if (ConditionalAccess)
4037                                 Error_NullShortCircuitInsideExpressionTree (ec);
4038
4039                         return new TypeOfMethod (best_candidate, loc);
4040                 }
4041                 
4042                 protected override Expression DoResolve (ResolveContext ec)
4043                 {
4044                         this.eclass = ExprClass.MethodGroup;
4045
4046                         if (InstanceExpression != null) {
4047                                 InstanceExpression = InstanceExpression.Resolve (ec);
4048                                 if (InstanceExpression == null)
4049                                         return null;
4050                         }
4051
4052                         return this;
4053                 }
4054
4055                 public override void Emit (EmitContext ec)
4056                 {
4057                         throw new NotSupportedException ();
4058                 }
4059
4060                 public void EmitCall (EmitContext ec, Arguments arguments, bool statement)
4061                 {
4062                         var call = new CallEmitter ();
4063                         call.InstanceExpression = InstanceExpression;
4064                         call.ConditionalAccess = ConditionalAccess;
4065
4066                         if (statement)
4067                                 call.EmitStatement (ec, best_candidate, arguments, loc);
4068                         else
4069                                 call.Emit (ec, best_candidate, arguments, loc);
4070                 }
4071
4072                 public void EmitCall (EmitContext ec, Arguments arguments, TypeSpec conditionalAccessReceiver, bool statement)
4073                 {
4074                         var ca = ec.ConditionalAccess;
4075                         ec.ConditionalAccess = new ConditionalAccessContext (conditionalAccessReceiver, ec.DefineLabel ()) {
4076                                 Statement = statement
4077                         };
4078
4079                         EmitCall (ec, arguments, statement);
4080
4081                         ec.CloseConditionalAccess (!statement && best_candidate_return != conditionalAccessReceiver && conditionalAccessReceiver.IsNullableType ? conditionalAccessReceiver : null);
4082                         ec.ConditionalAccess = ca;
4083                 }
4084
4085                 public override void Error_ValueCannotBeConverted (ResolveContext ec, TypeSpec target, bool expl)
4086                 {
4087                         if (target != InternalType.ErrorType) {
4088                                 ec.Report.Error (428, loc, "Cannot convert method group `{0}' to non-delegate type `{1}'. Consider using parentheses to invoke the method",
4089                                         Name, target.GetSignatureForError ());
4090                         }
4091                 }
4092
4093                 public bool HasAccessibleCandidate (ResolveContext rc)
4094                 {
4095                         foreach (var candidate in Candidates) {
4096                                 if (candidate.IsAccessible (rc))
4097                                         return true;
4098                         }
4099
4100                         return false;
4101                 }
4102
4103                 public static bool IsExtensionMethodArgument (Expression expr)
4104                 {
4105                         //
4106                         // LAMESPEC: No details about which expressions are not allowed
4107                         //
4108                         return !(expr is TypeExpr) && !(expr is BaseThis);
4109                 }
4110
4111                 /// <summary>
4112                 ///   Find the Applicable Function Members (7.4.2.1)
4113                 ///
4114                 ///   me: Method Group expression with the members to select.
4115                 ///       it might contain constructors or methods (or anything
4116                 ///       that maps to a method).
4117                 ///
4118                 ///   Arguments: ArrayList containing resolved Argument objects.
4119                 ///
4120                 ///   loc: The location if we want an error to be reported, or a Null
4121                 ///        location for "probing" purposes.
4122                 ///
4123                 ///   Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
4124                 ///            that is the best match of me on Arguments.
4125                 ///
4126                 /// </summary>
4127                 public virtual MethodGroupExpr OverloadResolve (ResolveContext ec, ref Arguments args, OverloadResolver.IErrorHandler cerrors, OverloadResolver.Restrictions restr)
4128                 {
4129                         // TODO: causes issues with probing mode, remove explicit Kind check
4130                         if (best_candidate != null && best_candidate.Kind == MemberKind.Destructor)
4131                                 return this;
4132
4133                         var r = new OverloadResolver (Methods, type_arguments, restr, loc);
4134                         if ((restr & OverloadResolver.Restrictions.NoBaseMembers) == 0) {
4135                                 r.BaseMembersProvider = this;
4136                                 r.InstanceQualifier = this;
4137                         }
4138
4139                         if (cerrors != null)
4140                                 r.CustomErrors = cerrors;
4141
4142                         // TODO: When in probing mode do IsApplicable only and when called again do VerifyArguments for full error reporting
4143                         best_candidate = r.ResolveMember<MethodSpec> (ec, ref args);
4144                         if (best_candidate == null) {
4145                                 if (!r.BestCandidateIsDynamic)
4146                                         return null;
4147
4148                                 if (simple_name != null && ec.IsStatic)
4149                                         InstanceExpression = ProbeIdenticalTypeName (ec, InstanceExpression, simple_name);
4150
4151                                 return this;
4152                         }
4153
4154                         // Overload resolver had to create a new method group, all checks bellow have already been executed
4155                         if (r.BestCandidateNewMethodGroup != null)
4156                                 return r.BestCandidateNewMethodGroup;
4157
4158                         if (best_candidate.Kind == MemberKind.Method && (restr & OverloadResolver.Restrictions.ProbingOnly) == 0) {
4159                                 if (InstanceExpression != null) {
4160                                         if (best_candidate.IsExtensionMethod && args[0].Expr == InstanceExpression) {
4161                                                 InstanceExpression = null;
4162                                         } else {
4163                                                 if (simple_name != null && best_candidate.IsStatic) {
4164                                                         InstanceExpression = ProbeIdenticalTypeName (ec, InstanceExpression, simple_name);
4165                                                 }
4166
4167                                                 InstanceExpression.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup | ResolveFlags.Type);
4168                                         }
4169                                 }
4170
4171                                 ResolveInstanceExpression (ec, null);
4172                         }
4173
4174                         var base_override = CandidateToBaseOverride (ec, best_candidate);
4175                         if (base_override == best_candidate) {
4176                                 best_candidate_return = r.BestCandidateReturnType;
4177                         } else {
4178                                 best_candidate = base_override;
4179                                 best_candidate_return = best_candidate.ReturnType;
4180                         }
4181
4182                         if (best_candidate.IsGeneric && (restr & OverloadResolver.Restrictions.ProbingOnly) == 0 && TypeParameterSpec.HasAnyTypeParameterConstrained (best_candidate.GenericDefinition)) {
4183                                 ConstraintChecker cc = new ConstraintChecker (ec);
4184                                 cc.CheckAll (best_candidate.GetGenericMethodDefinition (), best_candidate.TypeArguments, best_candidate.Constraints, loc);
4185                         }
4186
4187                         //
4188                         // Additional check for possible imported base override method which
4189                         // could not be done during IsOverrideMethodBaseTypeAccessible
4190                         //
4191                         if (best_candidate.IsVirtual && (best_candidate.DeclaringType.Modifiers & Modifiers.PROTECTED) != 0 &&
4192                                 best_candidate.MemberDefinition.IsImported && !best_candidate.DeclaringType.IsAccessible (ec)) {
4193                                 ec.Report.SymbolRelatedToPreviousError (best_candidate);
4194                                 ErrorIsInaccesible (ec, best_candidate.GetSignatureForError (), loc);
4195                         }
4196
4197                         // Speed up the check by not doing it on disallowed targets
4198                         if (best_candidate_return.Kind == MemberKind.Void && best_candidate.IsConditionallyExcluded (ec))
4199                                 Methods = Excluded;
4200
4201                         return this;
4202                 }
4203
4204                 public override MemberExpr ResolveMemberAccess (ResolveContext ec, Expression left, SimpleName original)
4205                 {
4206                         var fe = left as FieldExpr;
4207                         if (fe != null) {
4208                                 //
4209                                 // Using method-group on struct fields makes the struct assigned. I am not sure
4210                                 // why but that's what .net does
4211                                 //
4212                                 fe.Spec.MemberDefinition.SetIsAssigned ();
4213                         }
4214
4215                         simple_name = original;
4216                         return base.ResolveMemberAccess (ec, left, original);
4217                 }
4218
4219                 public override void SetTypeArguments (ResolveContext ec, TypeArguments ta)
4220                 {
4221                         type_arguments = ta;
4222                 }
4223
4224                 #region IBaseMembersProvider Members
4225
4226                 public virtual IList<MemberSpec> GetBaseMembers (TypeSpec baseType)
4227                 {
4228                         return baseType == null ? null : MemberCache.FindMembers (baseType, Methods [0].Name, false);
4229                 }
4230
4231                 public IParametersMember GetOverrideMemberParameters (MemberSpec member)
4232                 {
4233                         if (queried_type == member.DeclaringType)
4234                                 return null;
4235
4236                         return MemberCache.FindMember (queried_type, new MemberFilter ((MethodSpec) member),
4237                                 BindingRestriction.InstanceOnly | BindingRestriction.OverrideOnly) as IParametersMember;
4238                 }
4239
4240                 //
4241                 // Extension methods lookup after ordinary methods candidates failed to apply
4242                 //
4243                 public virtual MethodGroupExpr LookupExtensionMethod (ResolveContext rc)
4244                 {
4245                         if (InstanceExpression == null || InstanceExpression.eclass == ExprClass.Type)
4246                                 return null;
4247
4248                         if (!IsExtensionMethodArgument (InstanceExpression))
4249                                 return null;
4250
4251                         int arity = type_arguments == null ? 0 : type_arguments.Count;
4252                         var methods = rc.LookupExtensionMethod (Methods[0].Name, arity);
4253                         if (methods == null)
4254                                 return null;
4255
4256                         var emg = new ExtensionMethodGroupExpr (methods, InstanceExpression, loc);
4257                         emg.SetTypeArguments (rc, type_arguments);
4258                         emg.ConditionalAccess = ConditionalAccess;
4259                         return emg;
4260                 }
4261
4262                 #endregion
4263         }
4264
4265         struct ConstructorInstanceQualifier : OverloadResolver.IInstanceQualifier
4266         {
4267                 public ConstructorInstanceQualifier (TypeSpec type)
4268                         : this ()
4269                 {
4270                         InstanceType = type;
4271                 }
4272
4273                 public TypeSpec InstanceType { get; private set; }
4274
4275                 public bool CheckProtectedMemberAccess (ResolveContext rc, MemberSpec member)
4276                 {
4277                         return MemberExpr.CheckProtectedMemberAccess (rc, member, InstanceType);
4278                 }
4279         }
4280
4281         public struct OverloadResolver
4282         {
4283                 [Flags]
4284                 public enum Restrictions
4285                 {
4286                         None = 0,
4287                         DelegateInvoke = 1,
4288                         ProbingOnly     = 1 << 1,
4289                         CovariantDelegate = 1 << 2,
4290                         NoBaseMembers = 1 << 3,
4291                         BaseMembersIncluded = 1 << 4,
4292                         GetEnumeratorLookup = 1 << 5
4293                 }
4294
4295                 public interface IBaseMembersProvider
4296                 {
4297                         IList<MemberSpec> GetBaseMembers (TypeSpec baseType);
4298                         IParametersMember GetOverrideMemberParameters (MemberSpec member);
4299                         MethodGroupExpr LookupExtensionMethod (ResolveContext rc);
4300                 }
4301
4302                 public interface IErrorHandler
4303                 {
4304                         bool AmbiguousCandidates (ResolveContext rc, MemberSpec best, MemberSpec ambiguous);
4305                         bool ArgumentMismatch (ResolveContext rc, MemberSpec best, Argument a, int index);
4306                         bool NoArgumentMatch (ResolveContext rc, MemberSpec best);
4307                         bool TypeInferenceFailed (ResolveContext rc, MemberSpec best);
4308                 }
4309
4310                 public interface IInstanceQualifier
4311                 {
4312                         TypeSpec InstanceType { get; }
4313                         bool CheckProtectedMemberAccess (ResolveContext rc, MemberSpec member);
4314                 }
4315
4316                 sealed class NoBaseMembers : IBaseMembersProvider
4317                 {
4318                         public static readonly IBaseMembersProvider Instance = new NoBaseMembers ();
4319
4320                         public IList<MemberSpec> GetBaseMembers (TypeSpec baseType)
4321                         {
4322                                 return null;
4323                         }
4324
4325                         public IParametersMember GetOverrideMemberParameters (MemberSpec member)
4326                         {
4327                                 return null;
4328                         }
4329
4330                         public MethodGroupExpr LookupExtensionMethod (ResolveContext rc)
4331                         {
4332                                 return null;
4333                         }
4334                 }
4335
4336                 struct AmbiguousCandidate
4337                 {
4338                         public readonly MemberSpec Member;
4339                         public readonly bool Expanded;
4340                         public readonly AParametersCollection Parameters;
4341
4342                         public AmbiguousCandidate (MemberSpec member, AParametersCollection parameters, bool expanded)
4343                         {
4344                                 Member = member;
4345                                 Parameters = parameters;
4346                                 Expanded = expanded;
4347                         }
4348                 }
4349
4350                 Location loc;
4351                 IList<MemberSpec> members;
4352                 TypeArguments type_arguments;
4353                 IBaseMembersProvider base_provider;
4354                 IErrorHandler custom_errors;
4355                 IInstanceQualifier instance_qualifier;
4356                 Restrictions restrictions;
4357                 MethodGroupExpr best_candidate_extension_group;
4358                 TypeSpec best_candidate_return_type;
4359
4360                 SessionReportPrinter lambda_conv_msgs;
4361
4362                 public OverloadResolver (IList<MemberSpec> members, Restrictions restrictions, Location loc)
4363                         : this (members, null, restrictions, loc)
4364                 {
4365                 }
4366
4367                 public OverloadResolver (IList<MemberSpec> members, TypeArguments targs, Restrictions restrictions, Location loc)
4368                         : this ()
4369                 {
4370                         if (members == null || members.Count == 0)
4371                                 throw new ArgumentException ("empty members set");
4372
4373                         this.members = members;
4374                         this.loc = loc;
4375                         type_arguments = targs;
4376                         this.restrictions = restrictions;
4377                         if (IsDelegateInvoke)
4378                                 this.restrictions |= Restrictions.NoBaseMembers;
4379
4380                         base_provider = NoBaseMembers.Instance;
4381                 }
4382
4383                 #region Properties
4384
4385                 public IBaseMembersProvider BaseMembersProvider {
4386                         get {
4387                                 return base_provider;
4388                         }
4389                         set {
4390                                 base_provider = value;
4391                         }
4392                 }
4393
4394                 public bool BestCandidateIsDynamic { get; set; }
4395
4396                 //
4397                 // Best candidate was found in newly created MethodGroupExpr, used by extension methods
4398                 //
4399                 public MethodGroupExpr BestCandidateNewMethodGroup {
4400                         get {
4401                                 return best_candidate_extension_group;
4402                         }
4403                 }
4404
4405                 //
4406                 // Return type can be different between best candidate and closest override
4407                 //
4408                 public TypeSpec BestCandidateReturnType {
4409                         get {
4410                                 return best_candidate_return_type;
4411                         }
4412                 }
4413
4414                 public IErrorHandler CustomErrors {
4415                         get {
4416                                 return custom_errors;
4417                         }
4418                         set {
4419                                 custom_errors = value;
4420                         }
4421                 }
4422
4423                 TypeSpec DelegateType {
4424                         get {
4425                                 if ((restrictions & Restrictions.DelegateInvoke) == 0)
4426                                         throw new InternalErrorException ("Not running in delegate mode", loc);
4427
4428                                 return members [0].DeclaringType;
4429                         }
4430                 }
4431
4432                 public IInstanceQualifier InstanceQualifier {
4433                         get {
4434                                 return instance_qualifier;
4435                         }
4436                         set {
4437                                 instance_qualifier = value;
4438                         }
4439                 }
4440
4441                 bool IsProbingOnly {
4442                         get {
4443                                 return (restrictions & Restrictions.ProbingOnly) != 0;
4444                         }
4445                 }
4446
4447                 bool IsDelegateInvoke {
4448                         get {
4449                                 return (restrictions & Restrictions.DelegateInvoke) != 0;
4450                         }
4451                 }
4452
4453                 #endregion
4454
4455                 //
4456                 //  7.4.3.3  Better conversion from expression
4457                 //  Returns :   1    if a->p is better,
4458                 //              2    if a->q is better,
4459                 //              0 if neither is better
4460                 //
4461                 static int BetterExpressionConversion (ResolveContext ec, Argument a, TypeSpec p, TypeSpec q)
4462                 {
4463                         TypeSpec argument_type = a.Type;
4464
4465                         //
4466                         // If argument is an anonymous function
4467                         //
4468                         if (argument_type == InternalType.AnonymousMethod && ec.Module.Compiler.Settings.Version > LanguageVersion.ISO_2) {
4469                                 //
4470                                 // p and q are delegate types or expression tree types
4471                                 //
4472                                 if (p.IsExpressionTreeType || q.IsExpressionTreeType) {
4473                                         if (q.MemberDefinition != p.MemberDefinition) {
4474                                                 return 0;
4475                                         }
4476
4477                                         //
4478                                         // Uwrap delegate from Expression<T>
4479                                         //
4480                                         q = TypeManager.GetTypeArguments (q)[0];
4481                                         p = TypeManager.GetTypeArguments (p)[0];
4482                                 }
4483
4484                                 var p_m = Delegate.GetInvokeMethod (p);
4485                                 var q_m = Delegate.GetInvokeMethod (q);
4486
4487                                 //
4488                                 // With identical parameter lists
4489                                 //
4490                                 if (!TypeSpecComparer.Equals (p_m.Parameters.Types, q_m.Parameters.Types))
4491                                         return 0;
4492
4493                                 p = p_m.ReturnType;
4494                                 var orig_q = q;
4495                                 q = q_m.ReturnType;
4496
4497                                 //
4498                                 // if p is void returning, and q has a return type Y, then C2 is the better conversion.
4499                                 //
4500                                 if (p.Kind == MemberKind.Void) {
4501                                         return q.Kind != MemberKind.Void ? 2 : 0;
4502                                 }
4503
4504                                 //
4505                                 // if p has a return type Y, and q is void returning, then C1 is the better conversion.
4506                                 //
4507                                 if (q.Kind == MemberKind.Void) {
4508                                         return p.Kind != MemberKind.Void ? 1: 0;
4509                                 }
4510
4511                                 var am = (AnonymousMethodExpression) a.Expr;
4512
4513                                 //
4514                                 // When anonymous method is an asynchronous, and P has a return type Task<Y1>, and Q has a return type Task<Y2>
4515                                 // better conversion is performed between underlying types Y1 and Y2
4516                                 //
4517                                 if (p.IsGenericTask || q.IsGenericTask) {
4518                                         if (am.Block.IsAsync && p.IsGenericTask && q.IsGenericTask) {
4519                                                 q = q.TypeArguments[0];
4520                                                 p = p.TypeArguments[0];
4521                                         }
4522                                 }
4523
4524                                 if (q != p) {
4525                                         //
4526                                         // An inferred return type X exists for E in the context of that parameter list, and 
4527                                         // the conversion from X to Y1 is better than the conversion from X to Y2
4528                                         //
4529                                         argument_type = am.InferReturnType (ec, null, orig_q);
4530                                         if (argument_type == null) {
4531                                                 // TODO: Can this be hit?
4532                                                 return 1;
4533                                         }
4534
4535                                         if (argument_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
4536                                                 argument_type = ec.BuiltinTypes.Object;
4537                                 }
4538                         }
4539
4540                         if (argument_type == p)
4541                                 return 1;
4542
4543                         if (argument_type == q)
4544                                 return 2;
4545
4546                         //
4547                         // The parameters are identicial and return type is not void, use better type conversion
4548                         // on return type to determine better one
4549                         //
4550                         return BetterTypeConversion (ec, p, q);
4551                 }
4552
4553                 //
4554                 // 7.4.3.4  Better conversion from type
4555                 //
4556                 public static int BetterTypeConversion (ResolveContext ec, TypeSpec p, TypeSpec q)
4557                 {
4558                         if (p == null || q == null)
4559                                 throw new InternalErrorException ("BetterTypeConversion got a null conversion");
4560
4561                         switch (p.BuiltinType) {
4562                         case BuiltinTypeSpec.Type.Int:
4563                                 if (q.BuiltinType == BuiltinTypeSpec.Type.UInt || q.BuiltinType == BuiltinTypeSpec.Type.ULong)
4564                                         return 1;
4565                                 break;
4566                         case BuiltinTypeSpec.Type.Long:
4567                                 if (q.BuiltinType == BuiltinTypeSpec.Type.ULong)
4568                                         return 1;
4569                                 break;
4570                         case BuiltinTypeSpec.Type.SByte:
4571                                 switch (q.BuiltinType) {
4572                                 case BuiltinTypeSpec.Type.Byte:
4573                                 case BuiltinTypeSpec.Type.UShort:
4574                                 case BuiltinTypeSpec.Type.UInt:
4575                                 case BuiltinTypeSpec.Type.ULong:
4576                                         return 1;
4577                                 }
4578                                 break;
4579                         case BuiltinTypeSpec.Type.Short:
4580                                 switch (q.BuiltinType) {
4581                                 case BuiltinTypeSpec.Type.UShort:
4582                                 case BuiltinTypeSpec.Type.UInt:
4583                                 case BuiltinTypeSpec.Type.ULong:
4584                                         return 1;
4585                                 }
4586                                 break;
4587                         case BuiltinTypeSpec.Type.Dynamic:
4588                                 // Dynamic is never better
4589                                 return 2;
4590                         }
4591
4592                         switch (q.BuiltinType) {
4593                         case BuiltinTypeSpec.Type.Int:
4594                                 if (p.BuiltinType == BuiltinTypeSpec.Type.UInt || p.BuiltinType == BuiltinTypeSpec.Type.ULong)
4595                                         return 2;
4596                                 break;
4597                         case BuiltinTypeSpec.Type.Long:
4598                                 if (p.BuiltinType == BuiltinTypeSpec.Type.ULong)
4599                                         return 2;
4600                                 break;
4601                         case BuiltinTypeSpec.Type.SByte:
4602                                 switch (p.BuiltinType) {
4603                                 case BuiltinTypeSpec.Type.Byte:
4604                                 case BuiltinTypeSpec.Type.UShort:
4605                                 case BuiltinTypeSpec.Type.UInt:
4606                                 case BuiltinTypeSpec.Type.ULong:
4607                                         return 2;
4608                                 }
4609                                 break;
4610                         case BuiltinTypeSpec.Type.Short:
4611                                 switch (p.BuiltinType) {
4612                                 case BuiltinTypeSpec.Type.UShort:
4613                                 case BuiltinTypeSpec.Type.UInt:
4614                                 case BuiltinTypeSpec.Type.ULong:
4615                                         return 2;
4616                                 }
4617                                 break;
4618                         case BuiltinTypeSpec.Type.Dynamic:
4619                                 // Dynamic is never better
4620                                 return 1;
4621                         }
4622
4623                         // FIXME: handle lifted operators
4624
4625                         // TODO: this is expensive
4626                         Expression p_tmp = new EmptyExpression (p);
4627                         Expression q_tmp = new EmptyExpression (q);
4628
4629                         bool p_to_q = Convert.ImplicitConversionExists (ec, p_tmp, q);
4630                         bool q_to_p = Convert.ImplicitConversionExists (ec, q_tmp, p);
4631
4632                         if (p_to_q && !q_to_p)
4633                                 return 1;
4634
4635                         if (q_to_p && !p_to_q)
4636                                 return 2;
4637
4638                         return 0;
4639                 }
4640
4641                 /// <summary>
4642                 ///   Determines "Better function" between candidate
4643                 ///   and the current best match
4644                 /// </summary>
4645                 /// <remarks>
4646                 ///    Returns a boolean indicating :
4647                 ///     false if candidate ain't better
4648                 ///     true  if candidate is better than the current best match
4649                 /// </remarks>
4650                 static bool BetterFunction (ResolveContext ec, Arguments args, MemberSpec candidate, AParametersCollection cparam, bool candidate_params,
4651                         MemberSpec best, AParametersCollection bparam, bool best_params)
4652                 {
4653                         AParametersCollection candidate_pd = ((IParametersMember) candidate).Parameters;
4654                         AParametersCollection best_pd = ((IParametersMember) best).Parameters;
4655
4656                         bool better_at_least_one = false;
4657                         bool are_equivalent = true;
4658                         int args_count = args == null ? 0 : args.Count;
4659                         int j = 0;
4660                         Argument a = null;
4661                         TypeSpec ct, bt;
4662                         for (int c_idx = 0, b_idx = 0; j < args_count; ++j, ++c_idx, ++b_idx) {
4663                                 a = args[j];
4664
4665                                 // Default arguments are ignored for better decision
4666                                 if (a.IsDefaultArgument)
4667                                         break;
4668
4669                                 //
4670                                 // When comparing named argument the parameter type index has to be looked up
4671                                 // in original parameter set (override version for virtual members)
4672                                 //
4673                                 NamedArgument na = a as NamedArgument;
4674                                 if (na != null) {
4675                                         int idx = cparam.GetParameterIndexByName (na.Name);
4676                                         ct = candidate_pd.Types[idx];
4677                                         if (candidate_params && candidate_pd.FixedParameters[idx].ModFlags == Parameter.Modifier.PARAMS)
4678                                                 ct = TypeManager.GetElementType (ct);
4679
4680                                         idx = bparam.GetParameterIndexByName (na.Name);
4681                                         bt = best_pd.Types[idx];
4682                                         if (best_params && best_pd.FixedParameters[idx].ModFlags == Parameter.Modifier.PARAMS)
4683                                                 bt = TypeManager.GetElementType (bt);
4684                                 } else {
4685                                         ct = candidate_pd.Types[c_idx];
4686                                         bt = best_pd.Types[b_idx];
4687
4688                                         if (candidate_params && candidate_pd.FixedParameters[c_idx].ModFlags == Parameter.Modifier.PARAMS) {
4689                                                 ct = TypeManager.GetElementType (ct);
4690                                                 --c_idx;
4691                                         }
4692
4693                                         if (best_params && best_pd.FixedParameters[b_idx].ModFlags == Parameter.Modifier.PARAMS) {
4694                                                 bt = TypeManager.GetElementType (bt);
4695                                                 --b_idx;
4696                                         }
4697                                 }
4698
4699                                 if (TypeSpecComparer.IsEqual (ct, bt))
4700                                         continue;
4701
4702                                 are_equivalent = false;
4703                                 int result = BetterExpressionConversion (ec, a, ct, bt);
4704
4705                                 // for each argument, the conversion to 'ct' should be no worse than 
4706                                 // the conversion to 'bt'.
4707                                 if (result == 2)
4708                                         return false;
4709
4710                                 // for at least one argument, the conversion to 'ct' should be better than 
4711                                 // the conversion to 'bt'.
4712                                 if (result != 0)
4713                                         better_at_least_one = true;
4714                         }
4715
4716                         if (better_at_least_one)
4717                                 return true;
4718
4719                         //
4720                         // Tie-breaking rules are applied only for equivalent parameter types
4721                         //
4722                         if (!are_equivalent) {
4723                                 //
4724                                 // LAMESPEC: A candidate with less default parameters is still better when there
4725                                 // is no better expression conversion
4726                                 //
4727                                 if (candidate_pd.Count < best_pd.Count && !candidate_params && best_pd.FixedParameters [j].HasDefaultValue) {
4728                                         return true;
4729                                 }
4730
4731                                 return false;
4732                         }
4733
4734                         //
4735                         // If candidate is applicable in its normal form and best has a params array and is applicable
4736                         // only in its expanded form, then candidate is better
4737                         //
4738                         if (candidate_params != best_params)
4739                                 return !candidate_params;
4740
4741                         //
4742                         // We have not reached end of parameters list due to params or used default parameters
4743                         //
4744                         bool defaults_ambiguity = false;
4745                         while (j < candidate_pd.Count && j < best_pd.Count) {
4746                                 var cand_param = candidate_pd.FixedParameters [j];
4747                                 var best_param = best_pd.FixedParameters [j];
4748
4749                                 if (cand_param.HasDefaultValue != best_param.HasDefaultValue)
4750                                         return cand_param.HasDefaultValue;
4751
4752                                 defaults_ambiguity = true;
4753                                 if (candidate_pd.Count == best_pd.Count) {
4754                                         //
4755                                         // LAMESPEC:
4756                                         //
4757                                         // void Foo (int i = 0) is better than void Foo (params int[]) for Foo ()
4758                                         // void Foo (string[] s, string value = null) is better than Foo (string s, params string[]) for Foo (null) or Foo ()
4759                                         //
4760                                         if (cand_param.HasDefaultValue) {
4761                                                 ++j;
4762                                                 continue;
4763                                         }
4764
4765                                         break;
4766                                 }
4767
4768                                 //
4769                                 // Neither is better when not all arguments are provided
4770                                 //
4771                                 // void Foo (string s, int i = 0) <-> Foo (string s, int i = 0, int i2 = 0)
4772                                 // void Foo (string s, int i = 0) <-> Foo (string s, byte i = 0)
4773                                 // void Foo (string s, params int[]) <-> Foo (string s, params byte[])
4774                                 //
4775                                 ++j;
4776                         }
4777
4778                         if (candidate_pd.Count != best_pd.Count) {
4779                                 if (defaults_ambiguity && best_pd.Count - 1 == j)
4780                                         return best_pd.HasParams;
4781
4782                                 return candidate_pd.Count < best_pd.Count;
4783                         }
4784
4785                         //
4786                         // One is a non-generic method and second is a generic method, then non-generic is better
4787                         //
4788                         if (best.IsGeneric != candidate.IsGeneric)
4789                                 return best.IsGeneric;
4790
4791                         //
4792                         // Both methods have the same number of parameters, and the parameters have equal types
4793                         // Pick the "more specific" signature using rules over original (non-inflated) types
4794                         //
4795                         var candidate_def_pd = ((IParametersMember) candidate.MemberDefinition).Parameters;
4796                         var best_def_pd = ((IParametersMember) best.MemberDefinition).Parameters;
4797
4798                         bool specific_at_least_once = false;
4799                         for (j = 0; j < args_count; ++j) {
4800                                 NamedArgument na = args_count == 0 ? null : args [j] as NamedArgument;
4801                                 if (na != null) {
4802                                         ct = candidate_def_pd.Types[cparam.GetParameterIndexByName (na.Name)];
4803                                         bt = best_def_pd.Types[bparam.GetParameterIndexByName (na.Name)];
4804                                 } else {
4805                                         ct = candidate_def_pd.Types[j];
4806                                         bt = best_def_pd.Types[j];
4807                                 }
4808
4809                                 if (ct == bt)
4810                                         continue;
4811                                 TypeSpec specific = MoreSpecific (ct, bt);
4812                                 if (specific == bt)
4813                                         return false;
4814                                 if (specific == ct)
4815                                         specific_at_least_once = true;
4816                         }
4817
4818                         if (specific_at_least_once)
4819                                 return true;
4820
4821                         return false;
4822                 }
4823
4824                 static bool CheckInflatedArguments (MethodSpec ms)
4825                 {
4826                         if (!TypeParameterSpec.HasAnyTypeParameterTypeConstrained (ms.GenericDefinition))
4827                                 return true;
4828
4829                         // Setup constraint checker for probing only
4830                         ConstraintChecker cc = new ConstraintChecker (null);
4831
4832                         var mp = ms.Parameters.Types;
4833                         for (int i = 0; i < mp.Length; ++i) {
4834                                 var type = mp[i] as InflatedTypeSpec;
4835                                 if (type == null)
4836                                         continue;
4837
4838                                 var targs = type.TypeArguments;
4839                                 if (targs.Length == 0)
4840                                         continue;
4841
4842                                 // TODO: Checking inflated MVAR arguments should be enough
4843                                 if (!cc.CheckAll (type.GetDefinition (), targs, type.Constraints, Location.Null))
4844                                         return false;
4845                         }
4846
4847                         return true;
4848                 }
4849
4850                 public static void Error_ConstructorMismatch (ResolveContext rc, TypeSpec type, int argCount, Location loc)
4851                 {
4852                         rc.Report.Error (1729, loc,
4853                                 "The type `{0}' does not contain a constructor that takes `{1}' arguments",
4854                                 type.GetSignatureForError (), argCount.ToString ());
4855                 }
4856
4857                 //
4858                 // Determines if the candidate method is applicable to the given set of arguments
4859                 // There could be two different set of parameters for same candidate where one
4860                 // is the closest override for default values and named arguments checks and second
4861                 // one being the virtual base for the parameter types and modifiers.
4862                 //
4863                 // A return value rates candidate method compatibility,
4864                 // -1 = fatal error
4865                 // 0 = the best, int.MaxValue = the worst
4866                 //
4867                 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, bool errorMode)
4868                 {
4869                         //
4870                         // Each step has allocated 10 values, it can overflow for
4871                         // more than 10 arguments but that's ok as it's used for
4872                         // better error reporting only
4873                         //
4874                         const int ArgumentCountMismatch         = 1000000000;
4875                         const int NamedArgumentsMismatch        = 100000000;
4876                         const int DefaultArgumentMismatch       = 10000000;
4877                         const int UnexpectedTypeArguments       = 1000000;
4878                         const int TypeArgumentsMismatch         = 100000;
4879                         const int InflatedTypesMismatch         = 10000;
4880
4881                         // Parameters of most-derived type used mainly for named and optional parameters
4882                         var pd = pm.Parameters;
4883
4884                         // Used for params modifier only, that's legacy of C# 1.0 which uses base type for
4885                         // params modifier instead of most-derived type
4886                         var cpd = ((IParametersMember) candidate).Parameters;
4887                         int param_count = pd.Count;
4888                         int optional_count = 0;
4889                         int score;
4890                         Arguments orig_args = arguments;
4891
4892                         if (arg_count != param_count) {
4893                                 //
4894                                 // No arguments expansion when doing exact match for delegates
4895                                 //
4896                                 if ((restrictions & Restrictions.CovariantDelegate) == 0) {
4897                                         for (int i = 0; i < pd.Count; ++i) {
4898                                                 if (pd.FixedParameters[i].HasDefaultValue) {
4899                                                         optional_count = pd.Count - i;
4900                                                         break;
4901                                                 }
4902                                         }
4903                                 }
4904
4905                                 if (optional_count != 0) {
4906                                         // Readjust expected number when params used
4907                                         if (cpd.HasParams) {
4908                                                 optional_count--;
4909                                                 if (arg_count < param_count)
4910                                                         param_count--;
4911                                         } else if (arg_count > param_count) {
4912                                                 int args_gap = System.Math.Abs (arg_count - param_count);
4913                                                 return ArgumentCountMismatch + args_gap;
4914                                         } else if (arg_count < param_count - optional_count) {
4915                                                 int args_gap = System.Math.Abs (param_count - optional_count - arg_count);
4916                                                 return ArgumentCountMismatch + args_gap;
4917                                         }
4918                                 } else if (arg_count != param_count) {
4919                                         int args_gap = System.Math.Abs (arg_count - param_count);
4920                                         if (!cpd.HasParams)
4921                                                 return ArgumentCountMismatch + args_gap;
4922                                         if (arg_count < param_count - 1)
4923                                                 return ArgumentCountMismatch + args_gap;
4924                                 }
4925
4926                                 // Resize to fit optional arguments
4927                                 if (optional_count != 0) {
4928                                         if (arguments == null) {
4929                                                 arguments = new Arguments (optional_count);
4930                                         } else {
4931                                                 // Have to create a new container, so the next run can do same
4932                                                 var resized = new Arguments (param_count);
4933                                                 resized.AddRange (arguments);
4934                                                 arguments = resized;
4935                                         }
4936
4937                                         for (int i = arg_count; i < param_count; ++i)
4938                                                 arguments.Add (null);
4939                                 }
4940                         }
4941
4942                         if (arg_count > 0) {
4943                                 //
4944                                 // Shuffle named arguments to the right positions if there are any
4945                                 //
4946                                 if (arguments[arg_count - 1] is NamedArgument) {
4947                                         arg_count = arguments.Count;
4948
4949                                         for (int i = 0; i < arg_count; ++i) {
4950                                                 bool arg_moved = false;
4951                                                 while (true) {
4952                                                         NamedArgument na = arguments[i] as NamedArgument;
4953                                                         if (na == null)
4954                                                                 break;
4955
4956                                                         int index = pd.GetParameterIndexByName (na.Name);
4957
4958                                                         // Named parameter not found
4959                                                         if (index < 0)
4960                                                                 return NamedArgumentsMismatch - i;
4961
4962                                                         // already reordered
4963                                                         if (index == i)
4964                                                                 break;
4965
4966                                                         Argument temp;
4967                                                         if (index >= param_count) {
4968                                                                 // When using parameters which should not be available to the user
4969                                                                 if ((cpd.FixedParameters[index].ModFlags & Parameter.Modifier.PARAMS) == 0)
4970                                                                         break;
4971
4972                                                                 arguments.Add (null);
4973                                                                 ++arg_count;
4974                                                                 temp = null;
4975                                                         } else {
4976                                                                 if (index == arg_count)
4977                                                                         return NamedArgumentsMismatch - i - 1;
4978
4979                                                                 temp = arguments [index];
4980
4981                                                                 // The slot has been taken by positional argument
4982                                                                 if (temp != null && !(temp is NamedArgument))
4983                                                                         break;
4984                                                         }
4985
4986                                                         if (!arg_moved) {
4987                                                                 arguments = arguments.MarkOrderedArgument (na);
4988                                                                 arg_moved = true;
4989                                                         }
4990
4991                                                         if (arguments == orig_args) {
4992                                                                 arguments = new Arguments (orig_args.Count);
4993                                                                 arguments.AddRange (orig_args);
4994                                                         }
4995
4996                                                         arguments[index] = arguments[i];
4997                                                         arguments[i] = temp;
4998
4999                                                         if (temp == null)
5000                                                                 break;
5001                                                 }
5002                                         }
5003                                 } else {
5004                                         arg_count = arguments.Count;
5005                                 }
5006                         } else if (arguments != null) {
5007                                 arg_count = arguments.Count;
5008                         }
5009
5010                         //
5011                         // Don't do any expensive checks when the candidate cannot succeed
5012                         //
5013                         if (arg_count != param_count && !cpd.HasParams)
5014                                 return DefaultArgumentMismatch - System.Math.Abs (param_count - arg_count);
5015
5016                         var dep = candidate.GetMissingDependencies ();
5017                         if (dep != null) {
5018                                 ImportedTypeDefinition.Error_MissingDependency (ec, dep, loc);
5019                                 return -1;
5020                         }
5021
5022                         //
5023                         // 1. Handle generic method using type arguments when specified or type inference
5024                         //
5025                         TypeSpec[] ptypes;
5026                         var ms = candidate as MethodSpec;
5027                         if (ms != null && ms.IsGeneric) {
5028                                 if (type_arguments != null) {
5029                                         var g_args_count = ms.Arity;
5030                                         if (g_args_count != type_arguments.Count)
5031                                                 return TypeArgumentsMismatch - System.Math.Abs (type_arguments.Count - g_args_count);
5032
5033                                         if (type_arguments.Arguments != null)
5034                                                 ms = ms.MakeGenericMethod (ec, type_arguments.Arguments);
5035                                 } else {
5036                                         //
5037                                         // Deploy custom error reporting for infered anonymous expression or lambda methods. When
5038                                         // probing lambda methods keep all errors reported in separate set and once we are done and no best
5039                                         // candidate was found use the set to report more details about what was wrong with lambda body.
5040                                         // The general idea is to distinguish between code errors and errors caused by
5041                                         // trial-and-error type inference
5042                                         //
5043                                         if (lambda_conv_msgs == null) {
5044                                                 for (int i = 0; i < arg_count; i++) {
5045                                                         Argument a = arguments[i];
5046                                                         if (a == null)
5047                                                                 continue;
5048
5049                                                         var am = a.Expr as AnonymousMethodExpression;
5050                                                         if (am != null) {
5051                                                                 if (lambda_conv_msgs == null)
5052                                                                         lambda_conv_msgs = new SessionReportPrinter ();
5053
5054                                                                 am.TypeInferenceReportPrinter = lambda_conv_msgs;
5055                                                         }
5056                                                 }
5057                                         }
5058
5059                                         var ti = new TypeInference (arguments);
5060                                         TypeSpec[] i_args = ti.InferMethodArguments (ec, ms);
5061
5062                                         if (i_args == null)
5063                                                 return TypeArgumentsMismatch - ti.InferenceScore;
5064
5065                                         //
5066                                         // Clear any error messages when the result was success
5067                                         //
5068                                         if (lambda_conv_msgs != null)
5069                                                 lambda_conv_msgs.ClearSession ();
5070
5071                                         if (i_args.Length != 0) {
5072                                                 if (!errorMode) {
5073                                                         for (int i = 0; i < i_args.Length; ++i) {
5074                                                                 var ta = i_args [i];
5075                                                                 if (!ta.IsAccessible (ec))
5076                                                                         return TypeArgumentsMismatch - i;
5077                                                         }
5078                                                 }
5079
5080                                                 ms = ms.MakeGenericMethod (ec, i_args);
5081                                         }
5082                                 }
5083
5084                                 //
5085                                 // Type arguments constraints have to match for the method to be applicable
5086                                 //
5087                                 if (!CheckInflatedArguments (ms)) {
5088                                         candidate = ms;
5089                                         return InflatedTypesMismatch;
5090                                 }
5091
5092                                 //
5093                                 // We have a generic return type and at same time the method is override which
5094                                 // means we have to also inflate override return type in case the candidate is
5095                                 // best candidate and override return type is different to base return type.
5096                                 // 
5097                                 // virtual Foo<T, object> with override Foo<T, dynamic>
5098                                 //
5099                                 if (candidate != pm) {
5100                                         MethodSpec override_ms = (MethodSpec) pm;
5101                                         var inflator = new TypeParameterInflator (ec, ms.DeclaringType, override_ms.GenericDefinition.TypeParameters, ms.TypeArguments);
5102                                         returnType = inflator.Inflate (returnType);
5103                                 } else {
5104                                         returnType = ms.ReturnType;
5105                                 }
5106
5107                                 candidate = ms;
5108                                 pd = ms.Parameters;
5109                                 ptypes = pd.Types;
5110                         } else {
5111                                 if (type_arguments != null)
5112                                         return UnexpectedTypeArguments;
5113
5114                                 ptypes = cpd.Types;
5115                         }
5116
5117                         //
5118                         // 2. Each argument has to be implicitly convertible to method parameter
5119                         //
5120                         Parameter.Modifier p_mod = 0;
5121                         TypeSpec pt = null;
5122
5123                         for (int i = 0; i < arg_count; i++) {
5124                                 Argument a = arguments[i];
5125                                 if (a == null) {
5126                                         var fp = pd.FixedParameters[i];
5127                                         if (!fp.HasDefaultValue) {
5128                                                 arguments = orig_args;
5129                                                 return arg_count * 2 + 2;
5130                                         }
5131
5132                                         //
5133                                         // Get the default value expression, we can use the same expression
5134                                         // if the type matches
5135                                         //
5136                                         Expression e = fp.DefaultValue;
5137                                         if (e != null) {
5138                                                 e = ResolveDefaultValueArgument (ec, ptypes[i], e, loc);
5139                                                 if (e == null) {
5140                                                         // Restore for possible error reporting
5141                                                         for (int ii = i; ii < arg_count; ++ii)
5142                                                                 arguments.RemoveAt (i);
5143
5144                                                         return (arg_count - i) * 2 + 1;
5145                                                 }
5146                                         }
5147
5148                                         if ((fp.ModFlags & Parameter.Modifier.CallerMask) != 0) {
5149                                                 //
5150                                                 // LAMESPEC: Attributes can be mixed together with build-in priority
5151                                                 //
5152                                                 if ((fp.ModFlags & Parameter.Modifier.CallerLineNumber) != 0) {
5153                                                         e = new IntLiteral (ec.BuiltinTypes, loc.Row, loc);
5154                                                 } else if ((fp.ModFlags & Parameter.Modifier.CallerFilePath) != 0) {
5155                                                         e = new StringLiteral (ec.BuiltinTypes, loc.NameFullPath, loc);
5156                                                 } else if (ec.MemberContext.CurrentMemberDefinition != null) {
5157                                                         e = new StringLiteral (ec.BuiltinTypes, ec.MemberContext.CurrentMemberDefinition.GetCallerMemberName (), loc);
5158                                                 }
5159                                         }
5160
5161                                         arguments[i] = new Argument (e, Argument.AType.Default);
5162                                         continue;
5163                                 }
5164
5165                                 if (p_mod != Parameter.Modifier.PARAMS) {
5166                                         p_mod = (pd.FixedParameters[i].ModFlags & ~Parameter.Modifier.PARAMS) | (cpd.FixedParameters[i].ModFlags & Parameter.Modifier.PARAMS);
5167                                         pt = ptypes [i];
5168                                 } else if (!params_expanded_form) {
5169                                         params_expanded_form = true;
5170                                         pt = ((ElementTypeSpec) pt).Element;
5171                                         i -= 2;
5172                                         continue;
5173                                 }
5174
5175                                 score = 1;
5176                                 if (!params_expanded_form) {
5177                                         if (a.IsExtensionType) {
5178                                                 if (ExtensionMethodGroupExpr.IsExtensionTypeCompatible (a.Type, pt)) {
5179                                                         score = 0;
5180                                                         continue;
5181                                                 }
5182                                         } else {
5183                                                 score = IsArgumentCompatible (ec, a, p_mod, pt);
5184
5185                                                 if (score < 0)
5186                                                         dynamicArgument = true;
5187                                         }
5188                                 }
5189
5190                                 //
5191                                 // It can be applicable in expanded form (when not doing exact match like for delegates)
5192                                 //
5193                                 if (score != 0 && (p_mod & Parameter.Modifier.PARAMS) != 0 && (restrictions & Restrictions.CovariantDelegate) == 0) {
5194                                         if (!params_expanded_form) {
5195                                                 pt = ((ElementTypeSpec) pt).Element;
5196                                         }
5197
5198                                         if (score > 0)
5199                                                 score = IsArgumentCompatible (ec, a, Parameter.Modifier.NONE, pt);
5200
5201                                         if (score < 0) {
5202                                                 params_expanded_form = true;
5203                                                 dynamicArgument = true;
5204                                         } else if (score == 0 || arg_count > pd.Count) {
5205                                                 params_expanded_form = true;
5206                                         }
5207                                 }
5208
5209                                 if (score > 0) {
5210                                         if (params_expanded_form)
5211                                                 ++score;
5212                                         return (arg_count - i) * 2 + score;
5213                                 }
5214                         }
5215
5216                         //
5217                         // Restore original arguments for dynamic binder to keep the intention of original source code
5218                         //
5219                         if (dynamicArgument)
5220                                 arguments = orig_args;
5221
5222                         return 0;
5223                 }
5224
5225                 public static Expression ResolveDefaultValueArgument (ResolveContext ec, TypeSpec ptype, Expression e, Location loc)
5226                 {
5227                         if (e is Constant && e.Type == ptype)
5228                                 return e;
5229
5230                         //
5231                         // LAMESPEC: No idea what the exact rules are for System.Reflection.Missing.Value instead of null
5232                         //
5233                         if (e == EmptyExpression.MissingValue && (ptype.BuiltinType == BuiltinTypeSpec.Type.Object || ptype.BuiltinType == BuiltinTypeSpec.Type.Dynamic)) {
5234                                 e = new MemberAccess (new MemberAccess (new MemberAccess (
5235                                         new QualifiedAliasMember (QualifiedAliasMember.GlobalAlias, "System", loc), "Reflection", loc), "Missing", loc), "Value", loc);
5236                         } else if (e is Constant) {
5237                                 //
5238                                 // Handles int to int? conversions, DefaultParameterValue check
5239                                 //
5240                                 e = Convert.ImplicitConversionStandard (ec, e, ptype, loc);
5241                                 if (e == null)
5242                                         return null;
5243                         } else {
5244                                 e = new DefaultValueExpression (new TypeExpression (ptype, loc), loc);
5245                         }
5246
5247                         return e.Resolve (ec);
5248                 }
5249
5250                 //
5251                 // Tests argument compatibility with the parameter
5252                 // The possible return values are
5253                 // 0 - success
5254                 // 1 - modifier mismatch
5255                 // 2 - type mismatch
5256                 // -1 - dynamic binding required
5257                 //
5258                 int IsArgumentCompatible (ResolveContext ec, Argument argument, Parameter.Modifier param_mod, TypeSpec parameter)
5259                 {
5260                         //
5261                         // Types have to be identical when ref or out modifer
5262                         // is used and argument is not of dynamic type
5263                         //
5264                         if (((argument.Modifier | param_mod) & Parameter.Modifier.RefOutMask) != 0) {
5265                                 var arg_type = argument.Type;
5266
5267                                 if ((argument.Modifier & Parameter.Modifier.RefOutMask) != (param_mod & Parameter.Modifier.RefOutMask)) {
5268                                         //
5269                                         // Using dynamic for ref/out parameter can still succeed at runtime
5270                                         //
5271                                         if (arg_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic && (argument.Modifier & Parameter.Modifier.RefOutMask) == 0 && (restrictions & Restrictions.CovariantDelegate) == 0)
5272                                                 return -1;
5273
5274                                         return 1;
5275                                 }
5276
5277                                 if (arg_type != parameter) {
5278                                         if (arg_type == InternalType.VarOutType)
5279                                                 return 0;
5280
5281                                         //
5282                                         // Do full equality check after quick path
5283                                         //
5284                                         if (!TypeSpecComparer.IsEqual (arg_type, parameter)) {
5285                                                 //
5286                                                 // Using dynamic for ref/out parameter can still succeed at runtime
5287                                                 //
5288                                                 if (arg_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic && (argument.Modifier & Parameter.Modifier.RefOutMask) == 0 && (restrictions & Restrictions.CovariantDelegate) == 0)
5289                                                         return -1;
5290
5291                                                 return 2;
5292                                         }
5293                                 }
5294
5295                         } else {
5296                                 if (argument.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic && (restrictions & Restrictions.CovariantDelegate) == 0)
5297                                         return -1;
5298
5299                                 //
5300                                 // Use implicit conversion in all modes to return same candidates when the expression
5301                                 // is used as argument or delegate conversion
5302                                 //
5303                                 if (!Convert.ImplicitConversionExists (ec, argument.Expr, parameter)) {
5304                                         return parameter.IsDelegate && argument.Expr is AnonymousMethodExpression ? 2 : 3;
5305                                 }
5306                         }
5307
5308                         return 0;
5309                 }
5310
5311                 static TypeSpec MoreSpecific (TypeSpec p, TypeSpec q)
5312                 {
5313                         if (TypeManager.IsGenericParameter (p) && !TypeManager.IsGenericParameter (q))
5314                                 return q;
5315                         if (!TypeManager.IsGenericParameter (p) && TypeManager.IsGenericParameter (q))
5316                                 return p;
5317
5318                         var ac_p = p as ArrayContainer;
5319                         if (ac_p != null) {
5320                                 var ac_q = q as ArrayContainer;
5321                                 if (ac_q == null)
5322                                         return null;
5323
5324                                 TypeSpec specific = MoreSpecific (ac_p.Element, ac_q.Element);
5325                                 if (specific == ac_p.Element)
5326                                         return p;
5327                                 if (specific == ac_q.Element)
5328                                         return q;
5329                         } else if (p.IsGeneric && q.IsGeneric) {
5330                                 var pargs = TypeManager.GetTypeArguments (p);
5331                                 var qargs = TypeManager.GetTypeArguments (q);
5332
5333                                 bool p_specific_at_least_once = false;
5334                                 bool q_specific_at_least_once = false;
5335
5336                                 for (int i = 0; i < pargs.Length; i++) {
5337                                         TypeSpec specific = MoreSpecific (pargs[i], qargs[i]);
5338                                         if (specific == pargs[i])
5339                                                 p_specific_at_least_once = true;
5340                                         if (specific == qargs[i])
5341                                                 q_specific_at_least_once = true;
5342                                 }
5343
5344                                 if (p_specific_at_least_once && !q_specific_at_least_once)
5345                                         return p;
5346                                 if (!p_specific_at_least_once && q_specific_at_least_once)
5347                                         return q;
5348                         }
5349
5350                         return null;
5351                 }
5352
5353                 //
5354                 // Find the best method from candidate list
5355                 //
5356                 public T ResolveMember<T> (ResolveContext rc, ref Arguments args) where T : MemberSpec, IParametersMember
5357                 {
5358                         List<AmbiguousCandidate> ambiguous_candidates = null;
5359
5360                         MemberSpec best_candidate;
5361                         Arguments best_candidate_args = null;
5362                         bool best_candidate_params = false;
5363                         bool best_candidate_dynamic = false;
5364                         int best_candidate_rate;
5365                         IParametersMember best_parameter_member = null;
5366
5367                         int args_count = args != null ? args.Count : 0;
5368
5369                         Arguments candidate_args = args;
5370                         bool error_mode = false;
5371                         MemberSpec invocable_member = null;
5372                         int applicable_candidates = 0;
5373
5374                         while (true) {
5375                                 best_candidate = null;
5376                                 best_candidate_rate = int.MaxValue;
5377
5378                                 var type_members = members;
5379                                 do {
5380                                         for (int i = 0; i < type_members.Count; ++i) {
5381                                                 var member = type_members[i];
5382
5383                                                 //
5384                                                 // Methods in a base class are not candidates if any method in a derived
5385                                                 // class is applicable
5386                                                 //
5387                                                 if ((member.Modifiers & Modifiers.OVERRIDE) != 0)
5388                                                         continue;
5389
5390                                                 if (!error_mode) {
5391                                                         if (!member.IsAccessible (rc))
5392                                                                 continue;
5393
5394                                                         if (rc.IsRuntimeBinder && !member.DeclaringType.IsAccessible (rc))
5395                                                                 continue;
5396
5397                                                         if ((member.Modifiers & (Modifiers.PROTECTED | Modifiers.STATIC)) == Modifiers.PROTECTED &&
5398                                                                 instance_qualifier != null && !instance_qualifier.CheckProtectedMemberAccess (rc, member)) {
5399                                                                 continue;
5400                                                         }
5401                                                 }
5402
5403                                                 IParametersMember pm = member as IParametersMember;
5404                                                 if (pm == null) {
5405                                                         //
5406                                                         // Will use it later to report ambiguity between best method and invocable member
5407                                                         //
5408                                                         if (Invocation.IsMemberInvocable (member))
5409                                                                 invocable_member = member;
5410
5411                                                         continue;
5412                                                 }
5413
5414                                                 //
5415                                                 // Overload resolution is looking for base member but using parameter names
5416                                                 // and default values from the closest member. That means to do expensive lookup
5417                                                 // for the closest override for virtual or abstract members
5418                                                 //
5419                                                 if ((member.Modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT)) != 0) {
5420                                                         var override_params = base_provider.GetOverrideMemberParameters (member);
5421                                                         if (override_params != null)
5422                                                                 pm = override_params;
5423                                                 }
5424
5425                                                 //
5426                                                 // Check if the member candidate is applicable
5427                                                 //
5428                                                 bool params_expanded_form = false;
5429                                                 bool dynamic_argument = false;
5430                                                 TypeSpec rt = pm.MemberType;
5431                                                 int candidate_rate = IsApplicable (rc, ref candidate_args, args_count, ref member, pm, ref params_expanded_form, ref dynamic_argument, ref rt, error_mode);
5432
5433                                                 if (lambda_conv_msgs != null)
5434                                                         lambda_conv_msgs.EndSession ();
5435
5436                                                 //
5437                                                 // How does it score compare to others
5438                                                 //
5439                                                 if (candidate_rate < best_candidate_rate) {
5440
5441                                                         // Fatal error (missing dependency), cannot continue
5442                                                         if (candidate_rate < 0)
5443                                                                 return null;
5444
5445                                                         applicable_candidates = 1;
5446                                                         if ((restrictions & Restrictions.GetEnumeratorLookup) != 0 && candidate_args.Count != 0) {
5447                                                                 // Only parameterless methods are considered
5448                                                         } else {
5449                                                                 best_candidate_rate = candidate_rate;
5450                                                                 best_candidate = member;
5451                                                                 best_candidate_args = candidate_args;
5452                                                                 best_candidate_params = params_expanded_form;
5453                                                                 best_candidate_dynamic = dynamic_argument;
5454                                                                 best_parameter_member = pm;
5455                                                                 best_candidate_return_type = rt;
5456                                                         }
5457                                                 } else if (candidate_rate == 0) {
5458                                                         //
5459                                                         // The member look is done per type for most operations but sometimes
5460                                                         // it's not possible like for binary operators overload because they
5461                                                         // are unioned between 2 sides
5462                                                         //
5463                                                         if ((restrictions & Restrictions.BaseMembersIncluded) != 0) {
5464                                                                 if (TypeSpec.IsBaseClass (best_candidate.DeclaringType, member.DeclaringType, true))
5465                                                                         continue;
5466                                                         }
5467
5468                                                         ++applicable_candidates;
5469                                                         bool is_better;
5470                                                         if (best_candidate.DeclaringType.IsInterface && member.DeclaringType.ImplementsInterface (best_candidate.DeclaringType, false)) {
5471                                                                 //
5472                                                                 // We pack all interface members into top level type which makes the overload resolution
5473                                                                 // more complicated for interfaces. We compensate it by removing methods with same
5474                                                                 // signature when building the cache hence this path should not really be hit often
5475                                                                 //
5476                                                                 // Example:
5477                                                                 // interface IA { void Foo (int arg); }
5478                                                                 // interface IB : IA { void Foo (params int[] args); }
5479                                                                 //
5480                                                                 // IB::Foo is the best overload when calling IB.Foo (1)
5481                                                                 //
5482                                                                 is_better = true;
5483                                                                 if (ambiguous_candidates != null) {
5484                                                                         foreach (var amb_cand in ambiguous_candidates) {
5485                                                                                 if (member.DeclaringType.ImplementsInterface (best_candidate.DeclaringType, false)) {
5486                                                                                         continue;
5487                                                                                 }
5488
5489                                                                                 is_better = false;
5490                                                                                 break;
5491                                                                         }
5492
5493                                                                         if (is_better)
5494                                                                                 ambiguous_candidates = null;
5495                                                                 }
5496                                                         } else {
5497                                                                 // Is the new candidate better
5498                                                                 is_better = BetterFunction (rc, candidate_args, member, pm.Parameters, params_expanded_form, best_candidate, best_parameter_member.Parameters, best_candidate_params);
5499                                                         }
5500
5501                                                         if (is_better) {
5502                                                                 best_candidate = member;
5503                                                                 best_candidate_args = candidate_args;
5504                                                                 best_candidate_params = params_expanded_form;
5505                                                                 best_candidate_dynamic = dynamic_argument;
5506                                                                 best_parameter_member = pm;
5507                                                                 best_candidate_return_type = rt;
5508                                                         } else {
5509                                                                 // It's not better but any other found later could be but we are not sure yet
5510                                                                 if (ambiguous_candidates == null)
5511                                                                         ambiguous_candidates = new List<AmbiguousCandidate> ();
5512
5513                                                                 ambiguous_candidates.Add (new AmbiguousCandidate (member, pm.Parameters, params_expanded_form));
5514                                                         }
5515                                                 }
5516
5517                                                 // Restore expanded arguments
5518                                                 candidate_args = args;
5519                                         }
5520                                 } while (best_candidate_rate != 0 && (type_members = base_provider.GetBaseMembers (type_members[0].DeclaringType.BaseType)) != null);
5521
5522                                 //
5523                                 // We've found exact match
5524                                 //
5525                                 if (best_candidate_rate == 0)
5526                                         break;
5527
5528                                 //
5529                                 // Try extension methods lookup when no ordinary method match was found and provider enables it
5530                                 //
5531                                 if (!error_mode) {
5532                                         var emg = base_provider.LookupExtensionMethod (rc);
5533                                         if (emg != null) {
5534                                                 emg = emg.OverloadResolve (rc, ref args, null, restrictions);
5535                                                 if (emg != null) {
5536                                                         best_candidate_extension_group = emg;
5537                                                         return (T) (MemberSpec) emg.BestCandidate;
5538                                                 }
5539                                         }
5540                                 }
5541
5542                                 // Don't run expensive error reporting mode for probing
5543                                 if (IsProbingOnly)
5544                                         return null;
5545
5546                                 if (error_mode)
5547                                         break;
5548
5549                                 if (lambda_conv_msgs != null && !lambda_conv_msgs.IsEmpty)
5550                                         break;
5551
5552                                 lambda_conv_msgs = null;
5553                                 error_mode = true;
5554                         }
5555
5556                         //
5557                         // No best member match found, report an error
5558                         //
5559                         if (best_candidate_rate != 0 || error_mode) {
5560                                 ReportOverloadError (rc, best_candidate, best_parameter_member, best_candidate_args, best_candidate_params);
5561                                 return null;
5562                         }
5563
5564                         if (best_candidate_dynamic) {
5565                                 if (args[0].IsExtensionType) {
5566                                         rc.Report.Error (1973, loc,
5567                                                 "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",
5568                                                 args [0].Type.GetSignatureForError (), best_candidate.Name, best_candidate.GetSignatureForError ());
5569                                 }
5570
5571                                 //
5572                                 // Check type constraints only when explicit type arguments are used
5573                                 //
5574                                 if (applicable_candidates == 1 && best_candidate.IsGeneric && type_arguments != null) {
5575                                         MethodSpec bc = best_candidate as MethodSpec;
5576                                         if (bc != null && TypeParameterSpec.HasAnyTypeParameterConstrained (bc.GenericDefinition)) {
5577                                                 ConstraintChecker cc = new ConstraintChecker (rc);
5578                                                 cc.CheckAll (bc.GetGenericMethodDefinition (), bc.TypeArguments, bc.Constraints, loc);
5579                                         }
5580                                 }
5581
5582                                 BestCandidateIsDynamic = true;
5583                                 return null;
5584                         }
5585
5586                         //
5587                         // These flags indicates we are running delegate probing conversion. No need to
5588                         // do more expensive checks
5589                         // 
5590                         if ((restrictions & (Restrictions.ProbingOnly | Restrictions.CovariantDelegate)) == (Restrictions.CovariantDelegate | Restrictions.ProbingOnly))
5591                                 return (T) best_candidate;
5592
5593                         if (ambiguous_candidates != null) {
5594                                 //
5595                                 // Now check that there are no ambiguities i.e the selected method
5596                                 // should be better than all the others
5597                                 //
5598                                 for (int ix = 0; ix < ambiguous_candidates.Count; ix++) {
5599                                         var candidate = ambiguous_candidates [ix];
5600
5601                                         if (!BetterFunction (rc, best_candidate_args, best_candidate, best_parameter_member.Parameters, best_candidate_params, candidate.Member, candidate.Parameters, candidate.Expanded)) {
5602                                                 var ambiguous = candidate.Member;
5603                                                 if (custom_errors == null || !custom_errors.AmbiguousCandidates (rc, best_candidate, ambiguous)) {
5604                                                         rc.Report.SymbolRelatedToPreviousError (best_candidate);
5605                                                         rc.Report.SymbolRelatedToPreviousError (ambiguous);
5606                                                         rc.Report.Error (121, loc, "The call is ambiguous between the following methods or properties: `{0}' and `{1}'",
5607                                                                 best_candidate.GetSignatureForError (), ambiguous.GetSignatureForError ());
5608                                                 }
5609
5610                                                 return (T) best_candidate;
5611                                         }
5612                                 }
5613                         }
5614
5615                         if (invocable_member != null && !IsProbingOnly) {
5616                                 rc.Report.SymbolRelatedToPreviousError (best_candidate);
5617                                 rc.Report.SymbolRelatedToPreviousError (invocable_member);
5618                                 rc.Report.Warning (467, 2, loc, "Ambiguity between method `{0}' and invocable non-method `{1}'. Using method group",
5619                                         best_candidate.GetSignatureForError (), invocable_member.GetSignatureForError ());
5620                         }
5621
5622                         //
5623                         // And now check if the arguments are all
5624                         // compatible, perform conversions if
5625                         // necessary etc. and return if everything is
5626                         // all right
5627                         //
5628                         if (!VerifyArguments (rc, ref best_candidate_args, best_candidate, best_parameter_member, best_candidate_params))
5629                                 return null;
5630
5631                         if (best_candidate == null)
5632                                 return null;
5633
5634                         //
5635                         // Don't run possibly expensive checks in probing mode
5636                         //
5637                         if (!IsProbingOnly && !rc.IsInProbingMode) {
5638                                 //
5639                                 // Check ObsoleteAttribute on the best method
5640                                 //
5641                                 best_candidate.CheckObsoleteness (rc, loc);
5642
5643                                 best_candidate.MemberDefinition.SetIsUsed ();
5644                         }
5645
5646                         args = best_candidate_args;
5647                         return (T) best_candidate;
5648                 }
5649
5650                 public MethodSpec ResolveOperator (ResolveContext rc, ref Arguments args)
5651                 {
5652                         return ResolveMember<MethodSpec> (rc, ref args);
5653                 }
5654
5655                 void ReportArgumentMismatch (ResolveContext ec, int idx, MemberSpec method,
5656                                                                                                         Argument a, AParametersCollection expected_par, TypeSpec paramType)
5657                 {
5658                         if (custom_errors != null && custom_errors.ArgumentMismatch (ec, method, a, idx))
5659                                 return;
5660
5661                         if (a.Type == InternalType.ErrorType)
5662                                 return;
5663
5664                         if (a is CollectionElementInitializer.ElementInitializerArgument) {
5665                                 ec.Report.SymbolRelatedToPreviousError (method);
5666                                 if ((expected_par.FixedParameters[idx].ModFlags & Parameter.Modifier.RefOutMask) != 0) {
5667                                         ec.Report.Error (1954, loc, "The best overloaded collection initalizer method `{0}' cannot have `ref' or `out' modifier",
5668                                                 TypeManager.CSharpSignature (method));
5669                                         return;
5670                                 }
5671                                 ec.Report.Error (1950, loc, "The best overloaded collection initalizer method `{0}' has some invalid arguments",
5672                                           TypeManager.CSharpSignature (method));
5673                         } else if (IsDelegateInvoke) {
5674                                 ec.Report.Error (1594, loc, "Delegate `{0}' has some invalid arguments",
5675                                         DelegateType.GetSignatureForError ());
5676                         } else {
5677                                 ec.Report.SymbolRelatedToPreviousError (method);
5678                                 ec.Report.Error (1502, loc, "The best overloaded method match for `{0}' has some invalid arguments",
5679                                         method.GetSignatureForError ());
5680                         }
5681
5682                         Parameter.Modifier mod = idx >= expected_par.Count ? 0 : expected_par.FixedParameters[idx].ModFlags;
5683
5684                         string index = (idx + 1).ToString ();
5685                         if (((mod & Parameter.Modifier.RefOutMask) ^ (a.Modifier & Parameter.Modifier.RefOutMask)) != 0) {
5686                                 if ((mod & Parameter.Modifier.RefOutMask) == 0)
5687                                         ec.Report.Error (1615, a.Expr.Location, "Argument `#{0}' does not require `{1}' modifier. Consider removing `{1}' modifier",
5688                                                 index, Parameter.GetModifierSignature (a.Modifier));
5689                                 else
5690                                         ec.Report.Error (1620, a.Expr.Location, "Argument `#{0}' is missing `{1}' modifier",
5691                                                 index, Parameter.GetModifierSignature (mod));
5692                         } else {
5693                                 string p1 = a.GetSignatureForError ();
5694                                 string p2 = paramType.GetSignatureForError ();
5695
5696                                 if (p1 == p2) {
5697                                         p1 = a.Type.GetSignatureForErrorIncludingAssemblyName ();
5698                                         p2 = paramType.GetSignatureForErrorIncludingAssemblyName ();
5699                                 }
5700
5701                                 if ((mod & Parameter.Modifier.RefOutMask) != 0) {
5702                                         p1 = Parameter.GetModifierSignature (a.Modifier) + " " + p1;
5703                                         p2 = Parameter.GetModifierSignature (a.Modifier) + " " + p2;
5704                                 }
5705
5706                                 ec.Report.Error (1503, a.Expr.Location,
5707                                         "Argument `#{0}' cannot convert `{1}' expression to type `{2}'", index, p1, p2);
5708                         }
5709                 }
5710
5711                 //
5712                 // We have failed to find exact match so we return error info about the closest match
5713                 //
5714                 void ReportOverloadError (ResolveContext rc, MemberSpec best_candidate, IParametersMember pm, Arguments args, bool params_expanded)
5715                 {
5716                         int ta_count = type_arguments == null ? 0 : type_arguments.Count;
5717                         int arg_count = args == null ? 0 : args.Count;
5718
5719                         if (ta_count != best_candidate.Arity && (ta_count > 0 || ((IParametersMember) best_candidate).Parameters.IsEmpty)) {
5720                                 var mg = new MethodGroupExpr (new [] { best_candidate }, best_candidate.DeclaringType, loc);
5721                                 mg.Error_TypeArgumentsCannotBeUsed (rc, best_candidate, loc);
5722                                 return;
5723                         }
5724
5725                         if (lambda_conv_msgs != null && lambda_conv_msgs.Merge (rc.Report.Printer)) {
5726                                 return;
5727                         }
5728
5729
5730                         if ((best_candidate.Modifiers & (Modifiers.PROTECTED | Modifiers.STATIC)) == Modifiers.PROTECTED &&
5731                                 InstanceQualifier != null && !InstanceQualifier.CheckProtectedMemberAccess (rc, best_candidate)) {
5732                                 MemberExpr.Error_ProtectedMemberAccess (rc, best_candidate, InstanceQualifier.InstanceType, loc);
5733                         }
5734
5735                         //
5736                         // For candidates which match on parameters count report more details about incorrect arguments
5737                         //
5738                         if (pm != null) {
5739                                 if (pm.Parameters.Count == arg_count || params_expanded || HasUnfilledParams (best_candidate, pm, args)) {
5740                                         // Reject any inaccessible member
5741                                         if (!best_candidate.IsAccessible (rc) || !best_candidate.DeclaringType.IsAccessible (rc)) {
5742                                                 rc.Report.SymbolRelatedToPreviousError (best_candidate);
5743                                                 Expression.ErrorIsInaccesible (rc, best_candidate.GetSignatureForError (), loc);
5744                                                 return;
5745                                         }
5746
5747                                         var ms = best_candidate as MethodSpec;
5748                                         if (ms != null && ms.IsGeneric) {
5749                                                 bool constr_ok = true;
5750                                                 if (ms.TypeArguments != null)
5751                                                         constr_ok = new ConstraintChecker (rc.MemberContext).CheckAll (ms.GetGenericMethodDefinition (), ms.TypeArguments, ms.Constraints, loc);
5752
5753                                                 if (ta_count == 0 && ms.TypeArguments == null) {
5754                                                         if (custom_errors != null && custom_errors.TypeInferenceFailed (rc, best_candidate))
5755                                                                 return;
5756
5757                                                         if (constr_ok) {
5758                                                                 rc.Report.Error (411, loc,
5759                                                                         "The type arguments for method `{0}' cannot be inferred from the usage. Try specifying the type arguments explicitly",
5760                                                                         ms.GetGenericMethodDefinition ().GetSignatureForError ());
5761                                                         }
5762
5763                                                         return;
5764                                                 }
5765                                         }
5766
5767                                         VerifyArguments (rc, ref args, best_candidate, pm, params_expanded);
5768                                         return;
5769                                 }
5770                         }
5771
5772                         //
5773                         // We failed to find any method with correct argument count, report best candidate
5774                         //
5775                         if (custom_errors != null && custom_errors.NoArgumentMatch (rc, best_candidate))
5776                                 return;
5777
5778                         if (best_candidate.Kind == MemberKind.Constructor) {
5779                                 rc.Report.SymbolRelatedToPreviousError (best_candidate);
5780                                 Error_ConstructorMismatch (rc, best_candidate.DeclaringType, arg_count, loc);
5781                         } else if (IsDelegateInvoke) {
5782                                 rc.Report.SymbolRelatedToPreviousError (DelegateType);
5783                                 rc.Report.Error (1593, loc, "Delegate `{0}' does not take `{1}' arguments",
5784                                         DelegateType.GetSignatureForError (), arg_count.ToString ());
5785                         } else {
5786                                 string name = best_candidate.Kind == MemberKind.Indexer ? "this" : best_candidate.Name;
5787                                 rc.Report.SymbolRelatedToPreviousError (best_candidate);
5788                                 rc.Report.Error (1501, loc, "No overload for method `{0}' takes `{1}' arguments",
5789                                         name, arg_count.ToString ());
5790                         }
5791                 }
5792
5793                 static bool HasUnfilledParams (MemberSpec best_candidate, IParametersMember pm, Arguments args)
5794                 {
5795                         var p = ((IParametersMember)best_candidate).Parameters;
5796                         if (!p.HasParams)
5797                                 return false;
5798
5799                         string name = null;
5800                         for (int i = p.Count - 1; i != 0; --i) {
5801                                 var fp = p.FixedParameters [i];
5802                                 if ((fp.ModFlags & Parameter.Modifier.PARAMS) == 0)
5803                                         continue;
5804
5805                                 name = fp.Name;
5806                                 break;
5807                         }
5808
5809                         if (args == null)
5810                                 return false;
5811
5812                         foreach (var arg in args) {
5813                                 var na = arg as NamedArgument;
5814                                 if (na == null)
5815                                         continue;
5816
5817                                 if (na.Name == name) {
5818                                         name = null;
5819                                         break;
5820                                 }
5821                         }
5822
5823                         if (name == null)
5824                                 return false;
5825
5826                         return args.Count + 1 == pm.Parameters.Count;
5827                 }
5828
5829                 bool VerifyArguments (ResolveContext ec, ref Arguments args, MemberSpec member, IParametersMember pm, bool chose_params_expanded)
5830                 {
5831                         var pd = pm.Parameters;
5832                         var cpd = ((IParametersMember) member).Parameters;
5833                         var ptypes = cpd.Types;
5834
5835                         Parameter.Modifier p_mod = 0;
5836                         TypeSpec pt = null;
5837                         int a_idx = 0, a_pos = 0;
5838                         Argument a = null;
5839                         ArrayInitializer params_initializers = null;
5840                         bool has_unsafe_arg = pm.MemberType.IsPointer;
5841                         int arg_count = args == null ? 0 : args.Count;
5842
5843                         for (; a_idx < arg_count; a_idx++, ++a_pos) {
5844                                 a = args[a_idx];
5845                                 if (a == null)
5846                                         continue;
5847
5848                                 if (p_mod != Parameter.Modifier.PARAMS) {
5849                                         p_mod = cpd.FixedParameters [a_idx].ModFlags;
5850                                         pt = ptypes[a_idx];
5851                                         has_unsafe_arg |= pt.IsPointer;
5852
5853                                         if (p_mod == Parameter.Modifier.PARAMS) {
5854                                                 if (chose_params_expanded) {
5855                                                         params_initializers = new ArrayInitializer (arg_count - a_idx, a.Expr.Location);
5856                                                         pt = TypeManager.GetElementType (pt);
5857                                                 }
5858                                         }
5859                                 }
5860
5861                                 //
5862                                 // Types have to be identical when ref or out modifer is used 
5863                                 //
5864                                 if (((a.Modifier | p_mod) & Parameter.Modifier.RefOutMask) != 0) {
5865                                         if ((a.Modifier & Parameter.Modifier.RefOutMask) != (p_mod & Parameter.Modifier.RefOutMask))
5866                                                 break;
5867
5868                                         var arg_type = a.Type;
5869                                         if (arg_type == pt)
5870                                                 continue;
5871
5872                                         if (arg_type == InternalType.VarOutType) {
5873                                                 //
5874                                                 // Set underlying variable type based on parameter type
5875                                                 //
5876                                                 ((DeclarationExpression)a.Expr).Variable.Type = pt;
5877                                                 continue;
5878                                         }
5879
5880                                         if (!TypeSpecComparer.IsEqual (arg_type, pt))
5881                                                 break;
5882                                 }
5883
5884                                 NamedArgument na = a as NamedArgument;
5885                                 if (na != null) {
5886                                         int name_index = pd.GetParameterIndexByName (na.Name);
5887                                         if (name_index < 0 || name_index >= pd.Count) {
5888                                                 if (IsDelegateInvoke) {
5889                                                         ec.Report.SymbolRelatedToPreviousError (DelegateType);
5890                                                         ec.Report.Error (1746, na.Location,
5891                                                                 "The delegate `{0}' does not contain a parameter named `{1}'",
5892                                                                 DelegateType.GetSignatureForError (), na.Name);
5893                                                 } else {
5894                                                         ec.Report.SymbolRelatedToPreviousError (member);
5895                                                         ec.Report.Error (1739, na.Location,
5896                                                                 "The best overloaded method match for `{0}' does not contain a parameter named `{1}'",
5897                                                                 TypeManager.CSharpSignature (member), na.Name);
5898                                                 }
5899                                         } else if (args[name_index] != a && args[name_index] != null) {
5900                                                 if (IsDelegateInvoke)
5901                                                         ec.Report.SymbolRelatedToPreviousError (DelegateType);
5902                                                 else
5903                                                         ec.Report.SymbolRelatedToPreviousError (member);
5904
5905                                                 ec.Report.Error (1744, na.Location,
5906                                                         "Named argument `{0}' cannot be used for a parameter which has positional argument specified",
5907                                                         na.Name);
5908                                         }
5909                                 }
5910                                 
5911                                 if (a.Expr.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
5912                                         continue;
5913
5914                                 if ((restrictions & Restrictions.CovariantDelegate) != 0 && !Delegate.IsTypeCovariant (ec, a.Expr.Type, pt)) {
5915                                         if (a.IsExtensionType) {
5916                                                 // TODO: Should report better message type, something similar to CS1928/1929 instead of
5917                                                 // CS1061 but that still better than confusing CS0123
5918                                                 var ma = new MemberAccess (a.Expr, member.Name, loc);
5919                                                 ma.Error_TypeDoesNotContainDefinition (ec, a.Expr.Type, ma.Name);
5920                                         } else {
5921                                                 custom_errors.NoArgumentMatch (ec, member);
5922                                         }
5923                                         return false;
5924                                 }
5925
5926                                 Expression conv;
5927                                 if (a.IsExtensionType) {
5928                                         if (a.Expr.Type == pt || TypeSpecComparer.IsEqual (a.Expr.Type, pt)) {
5929                                                 conv = a.Expr;
5930                                         } else {
5931                                                 conv = Convert.ImplicitReferenceConversion (a.Expr, pt, false);
5932                                                 if (conv == null)
5933                                                         conv = Convert.ImplicitBoxingConversion (a.Expr, a.Expr.Type, pt);
5934                                         }
5935                                 } else {
5936                                         conv = Convert.ImplicitConversion (ec, a.Expr, pt, loc);
5937                                 }
5938
5939                                 if (conv == null)
5940                                         break;
5941
5942                                 //
5943                                 // Convert params arguments to an array initializer
5944                                 //
5945                                 if (params_initializers != null) {
5946                                         // we choose to use 'a.Expr' rather than 'conv' so that
5947                                         // we don't hide the kind of expression we have (esp. CompoundAssign.Helper)
5948                                         params_initializers.Add (a.Expr);
5949                                         args.RemoveAt (a_idx--);
5950                                         --arg_count;
5951                                         a.Expr = conv;
5952                                         continue;
5953                                 }
5954
5955                                 // Update the argument with the implicit conversion
5956                                 a.Expr = conv;
5957                         }
5958
5959                         if (a_idx != arg_count) {
5960                                 //
5961                                 // Convert all var out argument to error type for less confusing error reporting
5962                                 // when no matching overload is found
5963                                 //
5964                                 for (; a_idx < arg_count; a_idx++) {
5965                                         var arg = args [a_idx];
5966                                         if (arg == null)
5967                                                 continue;
5968
5969                                         if (arg.Type == InternalType.VarOutType) {
5970                                                 ((DeclarationExpression)arg.Expr).Variable.Type = InternalType.ErrorType;
5971                                         }
5972                                 }
5973
5974                                 ReportArgumentMismatch (ec, a_pos, member, a, pd, pt);
5975                                 return false;
5976                         }
5977
5978                         //
5979                         // Fill not provided arguments required by params modifier
5980                         //
5981                         if (params_initializers == null && arg_count + 1 == pd.Count) {
5982                                 if (args == null)
5983                                         args = new Arguments (1);
5984
5985                                 pt = ptypes[pd.Count - 1];
5986                                 pt = TypeManager.GetElementType (pt);
5987                                 has_unsafe_arg |= pt.IsPointer;
5988                                 params_initializers = new ArrayInitializer (0, loc);
5989                         }
5990
5991                         //
5992                         // Append an array argument with all params arguments
5993                         //
5994                         if (params_initializers != null) {
5995                                 args.Add (new Argument (
5996                                         new ArrayCreation (new TypeExpression (pt, loc), params_initializers, loc).Resolve (ec)));
5997                                 arg_count++;
5998                         }
5999
6000                         if (has_unsafe_arg && !ec.IsUnsafe) {
6001                                 Expression.UnsafeError (ec, loc);
6002                         }
6003
6004                         //
6005                         // We could infer inaccesible type arguments
6006                         //
6007                         if (type_arguments == null && member.IsGeneric) {
6008                                 var ms = (MethodSpec) member;
6009                                 foreach (var ta in ms.TypeArguments) {
6010                                         if (!ta.IsAccessible (ec)) {
6011                                                 ec.Report.SymbolRelatedToPreviousError (ta);
6012                                                 Expression.ErrorIsInaccesible (ec, member.GetSignatureForError (), loc);
6013                                                 break;
6014                                         }
6015                                 }
6016                         }
6017
6018                         return true;
6019                 }
6020         }
6021
6022         public class ConstantExpr : MemberExpr
6023         {
6024                 readonly ConstSpec constant;
6025
6026                 public ConstantExpr (ConstSpec constant, Location loc)
6027                 {
6028                         this.constant = constant;
6029                         this.loc = loc;
6030                 }
6031
6032                 public override string Name {
6033                         get { throw new NotImplementedException (); }
6034                 }
6035
6036                 public override string KindName {
6037                         get { return "constant"; }
6038                 }
6039
6040                 public override bool IsInstance {
6041                         get { return !IsStatic; }
6042                 }
6043
6044                 public override bool IsStatic {
6045                         get { return true; }
6046                 }
6047
6048                 protected override TypeSpec DeclaringType {
6049                         get { return constant.DeclaringType; }
6050                 }
6051
6052                 public override Expression CreateExpressionTree (ResolveContext ec)
6053                 {
6054                         throw new NotSupportedException ("ET");
6055                 }
6056
6057                 protected override Expression DoResolve (ResolveContext rc)
6058                 {
6059                         ResolveInstanceExpression (rc, null);
6060                         DoBestMemberChecks (rc, constant);
6061
6062                         if (rc.HasSet (ResolveContext.Options.NameOfScope)) {
6063                                 eclass = ExprClass.Value;
6064                                 type = constant.MemberType;
6065                                 return this;
6066                         }
6067
6068                         var c = constant.GetConstant (rc);
6069
6070                         // Creates reference expression to the constant value
6071                         return Constant.CreateConstantFromValue (constant.MemberType, c.GetValue (), loc);
6072                 }
6073
6074                 public override void Emit (EmitContext ec)
6075                 {
6076                         throw new NotSupportedException ();
6077                 }
6078
6079                 public override string GetSignatureForError ()
6080                 {
6081                         return constant.GetSignatureForError ();
6082                 }
6083
6084                 public override void SetTypeArguments (ResolveContext ec, TypeArguments ta)
6085                 {
6086                         Error_TypeArgumentsCannotBeUsed (ec, "constant", GetSignatureForError (), loc);
6087                 }
6088         }
6089
6090         //
6091         // Fully resolved expression that references a Field
6092         //
6093         public class FieldExpr : MemberExpr, IDynamicAssign, IMemoryLocation, IVariableReference
6094         {
6095                 protected FieldSpec spec;
6096                 VariableInfo variable_info;
6097                 
6098                 LocalTemporary temp;
6099                 bool prepared;
6100                 
6101                 protected FieldExpr (Location l)
6102                 {
6103                         loc = l;
6104                 }
6105
6106                 public FieldExpr (FieldSpec spec, Location loc)
6107                 {
6108                         this.spec = spec;
6109                         this.loc = loc;
6110
6111                         type = spec.MemberType;
6112                 }
6113                 
6114                 public FieldExpr (FieldBase fi, Location l)
6115                         : this (fi.Spec, l)
6116                 {
6117                 }
6118
6119                 #region Properties
6120
6121                 public override string Name {
6122                         get {
6123                                 return spec.Name;
6124                         }
6125                 }
6126
6127                 public bool IsHoisted {
6128                         get {
6129                                 IVariableReference hv = InstanceExpression as IVariableReference;
6130                                 return hv != null && hv.IsHoisted;
6131                         }
6132                 }
6133
6134                 public override bool IsInstance {
6135                         get {
6136                                 return !spec.IsStatic;
6137                         }
6138                 }
6139
6140                 public override bool IsStatic {
6141                         get {
6142                                 return spec.IsStatic;
6143                         }
6144                 }
6145
6146                 public override string KindName {
6147                         get { return "field"; }
6148                 }
6149
6150                 public FieldSpec Spec {
6151                         get {
6152                                 return spec;
6153                         }
6154                 }
6155
6156                 protected override TypeSpec DeclaringType {
6157                         get {
6158                                 return spec.DeclaringType;
6159                         }
6160                 }
6161
6162                 public VariableInfo VariableInfo {
6163                         get {
6164                                 return variable_info;
6165                         }
6166                 }
6167
6168 #endregion
6169
6170                 public override string GetSignatureForError ()
6171                 {
6172                         return spec.GetSignatureForError ();
6173                 }
6174
6175                 public bool IsMarshalByRefAccess (ResolveContext rc)
6176                 {
6177                         // Checks possible ldflda of field access expression
6178                         return !spec.IsStatic && TypeSpec.IsValueType (spec.MemberType) && !(InstanceExpression is This) &&
6179                                 rc.Module.PredefinedTypes.MarshalByRefObject.Define () &&
6180                                 TypeSpec.IsBaseClass (spec.DeclaringType, rc.Module.PredefinedTypes.MarshalByRefObject.TypeSpec, false);
6181                 }
6182
6183                 public void SetHasAddressTaken ()
6184                 {
6185                         IVariableReference vr = InstanceExpression as IVariableReference;
6186                         if (vr != null) {
6187                                 vr.SetHasAddressTaken ();
6188                         }
6189                 }
6190
6191                 protected override void CloneTo (CloneContext clonectx, Expression target)
6192                 {
6193                         var t = (FieldExpr) target;
6194
6195                         if (InstanceExpression != null)
6196                                 t.InstanceExpression = InstanceExpression.Clone (clonectx);
6197                 }
6198
6199                 public override Expression CreateExpressionTree (ResolveContext ec)
6200                 {
6201                         if (ConditionalAccess) {
6202                                 Error_NullShortCircuitInsideExpressionTree (ec);
6203                         }
6204
6205                         return CreateExpressionTree (ec, true);
6206                 }
6207
6208                 public Expression CreateExpressionTree (ResolveContext ec, bool convertInstance)
6209                 {
6210                         Arguments args;
6211                         Expression instance;
6212
6213                         if (InstanceExpression == null) {
6214                                 instance = new NullLiteral (loc);
6215                         } else if (convertInstance) {
6216                                 instance = InstanceExpression.CreateExpressionTree (ec);
6217                         } else {
6218                                 args = new Arguments (1);
6219                                 args.Add (new Argument (InstanceExpression));
6220                                 instance = CreateExpressionFactoryCall (ec, "Constant", args);
6221                         }
6222
6223                         args = Arguments.CreateForExpressionTree (ec, null,
6224                                 instance,
6225                                 CreateTypeOfExpression ());
6226
6227                         return CreateExpressionFactoryCall (ec, "Field", args);
6228                 }
6229
6230                 public Expression CreateTypeOfExpression ()
6231                 {
6232                         return new TypeOfField (spec, loc);
6233                 }
6234
6235                 protected override Expression DoResolve (ResolveContext ec)
6236                 {
6237                         spec.MemberDefinition.SetIsUsed ();
6238
6239                         return DoResolve (ec, null);
6240                 }
6241
6242                 Expression DoResolve (ResolveContext ec, Expression rhs)
6243                 {
6244                         bool lvalue_instance = rhs != null && IsInstance && spec.DeclaringType.IsStruct;
6245
6246                         if (rhs != this) {
6247                                 ResolveConditionalAccessReceiver (ec);
6248
6249                                 if (ResolveInstanceExpression (ec, rhs)) {
6250                                         // Resolve the field's instance expression while flow analysis is turned
6251                                         // off: when accessing a field "a.b", we must check whether the field
6252                                         // "a.b" is initialized, not whether the whole struct "a" is initialized.
6253
6254                                         if (lvalue_instance) {
6255                                                 bool out_access = rhs == EmptyExpression.OutAccess || rhs == EmptyExpression.LValueMemberOutAccess;
6256
6257                                                 Expression right_side =
6258                                                         out_access ? EmptyExpression.LValueMemberOutAccess : EmptyExpression.LValueMemberAccess;
6259
6260                                                 InstanceExpression = InstanceExpression.ResolveLValue (ec, right_side);
6261                                         } else {
6262                                                 InstanceExpression = InstanceExpression.Resolve (ec, ResolveFlags.VariableOrValue);
6263                                         }
6264
6265                                         if (InstanceExpression == null)
6266                                                 return null;
6267                                 }
6268
6269                                 DoBestMemberChecks (ec, spec);
6270
6271                                 if (conditional_access_receiver)
6272                                         ec.With (ResolveContext.Options.DontSetConditionalAccessReceiver, false);
6273                         }
6274
6275                         var fb = spec as FixedFieldSpec;
6276                         IVariableReference var = InstanceExpression as IVariableReference;
6277
6278                         if (fb != null) {
6279                                 IFixedExpression fe = InstanceExpression as IFixedExpression;
6280                                 if (!ec.HasSet (ResolveContext.Options.FixedInitializerScope) && (fe == null || !fe.IsFixed)) {
6281                                         ec.Report.Error (1666, loc, "You cannot use fixed size buffers contained in unfixed expressions. Try using the fixed statement");
6282                                 }
6283
6284                                 if (InstanceExpression.eclass != ExprClass.Variable) {
6285                                         ec.Report.SymbolRelatedToPreviousError (spec);
6286                                         ec.Report.Error (1708, loc, "`{0}': Fixed size buffers can only be accessed through locals or fields",
6287                                                 TypeManager.GetFullNameSignature (spec));
6288                                 } else if (var != null && var.IsHoisted) {
6289                                         AnonymousMethodExpression.Error_AddressOfCapturedVar (ec, var, loc);
6290                                 }
6291
6292                                 return new FixedBufferPtr (this, fb.ElementType, loc).Resolve (ec);
6293                         }
6294
6295                         //
6296                         // Set flow-analysis variable info for struct member access. It will be check later
6297                         // for precise error reporting
6298                         //
6299                         if (var != null && var.VariableInfo != null && InstanceExpression.Type.IsStruct) {
6300                                 variable_info = var.VariableInfo.GetStructFieldInfo (Name);
6301                         }
6302
6303                         if (conditional_access_receiver)
6304                                 type = LiftMemberType (ec, type);
6305
6306                         if (ConditionalAccess && InstanceExpression != null && InstanceExpression.IsNull)
6307                                 return Constant.CreateConstantFromValue (type, null, loc);
6308
6309                         eclass = ExprClass.Variable;
6310                         return this;
6311                 }
6312
6313                 public void SetFieldAssigned (FlowAnalysisContext fc)
6314                 {
6315                         if (!IsInstance)
6316                                 return;
6317
6318                         bool lvalue_instance = spec.DeclaringType.IsStruct;
6319                         if (lvalue_instance) {
6320                                 var var = InstanceExpression as IVariableReference;
6321                                 if (var != null && var.VariableInfo != null) {
6322                                         fc.SetStructFieldAssigned (var.VariableInfo, Name);
6323                                 }
6324                         }
6325
6326                         var fe = InstanceExpression as FieldExpr;
6327                         if (fe != null) {
6328                                 Expression instance;
6329
6330                                 do {
6331                                         instance = fe.InstanceExpression;
6332                                         var fe_instance = instance as FieldExpr;
6333                                         if ((fe_instance != null && !fe_instance.IsStatic) || instance is LocalVariableReference) {
6334                                                 if (TypeSpec.IsReferenceType (fe.Type) && instance.Type.IsStruct) {
6335                                                         var var = InstanceExpression as IVariableReference;
6336                                                         if (var != null && var.VariableInfo == null) {
6337                                                                 var var_inst = instance as IVariableReference;
6338                                                                 if (var_inst == null || (var_inst.VariableInfo != null && !fc.IsDefinitelyAssigned (var_inst.VariableInfo)))
6339                                                                         fc.Report.Warning (1060, 1, fe.loc, "Use of possibly unassigned field `{0}'", fe.Name);
6340                                                         }
6341                                                 }
6342
6343                                                 if (fe_instance != null) {
6344                                                         fe = fe_instance;
6345                                                         continue;
6346                                                 }
6347                                         }
6348
6349                                         break;
6350                                 } while (true);
6351
6352                                 if (instance != null && TypeSpec.IsReferenceType (instance.Type))
6353                                         instance.FlowAnalysis (fc);
6354                         } else {
6355                                 if (TypeSpec.IsReferenceType (InstanceExpression.Type))
6356                                         InstanceExpression.FlowAnalysis (fc);
6357                         }
6358                 }
6359
6360                 Expression Error_AssignToReadonly (ResolveContext rc, Expression right_side)
6361                 {
6362                         // The return value is always null.  Returning a value simplifies calling code.
6363         
6364                         if (right_side == EmptyExpression.OutAccess) {
6365                                 if (IsStatic) {
6366                                         rc.Report.Error (199, loc, "A static readonly field `{0}' cannot be passed ref or out (except in a static constructor)",
6367                                                 GetSignatureForError ());
6368                                 } else {
6369                                         rc.Report.Error (192, loc, "A readonly field `{0}' cannot be passed ref or out (except in a constructor)",
6370                                                 GetSignatureForError ());
6371                                 }
6372
6373                                 return null;
6374                         }
6375
6376                         if (right_side == EmptyExpression.LValueMemberAccess) {
6377                                 // Already reported as CS1648/CS1650
6378                                 return null;
6379                         }
6380
6381                         if (right_side == EmptyExpression.LValueMemberOutAccess) {
6382                                 if (IsStatic) {
6383                                         rc.Report.Error (1651, loc, "Fields of static readonly field `{0}' cannot be passed ref or out (except in a static constructor)",
6384                                                 GetSignatureForError ());
6385                                 } else {
6386                                         rc.Report.Error (1649, loc, "Members of readonly field `{0}' cannot be passed ref or out (except in a constructor)",
6387                                                 GetSignatureForError ());
6388                                 }
6389                                 return null;
6390                         }
6391
6392                         if (IsStatic) {
6393                                 rc.Report.Error (198, loc, "A static readonly field `{0}' cannot be assigned to (except in a static constructor or a variable initializer)",
6394                                         GetSignatureForError ());
6395                         } else {
6396                                 rc.Report.Error (191, loc, "A readonly field `{0}' cannot be assigned to (except in a constructor or a variable initializer)",
6397                                         GetSignatureForError ());
6398                         }
6399
6400                         return null;
6401                 }
6402
6403                 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
6404                 {
6405                         if (HasConditionalAccess ())
6406                                 Error_NullPropagatingLValue (ec);
6407
6408                         if (spec is FixedFieldSpec) {
6409                                 // It could be much better error message but we want to be error compatible
6410                                 Error_ValueAssignment (ec, right_side);
6411                         }
6412
6413                         Expression e = DoResolve (ec, right_side);
6414
6415                         if (e == null)
6416                                 return null;
6417
6418                         spec.MemberDefinition.SetIsAssigned ();
6419
6420                         if ((right_side == EmptyExpression.UnaryAddress || right_side == EmptyExpression.OutAccess) &&
6421                                         (spec.Modifiers & Modifiers.VOLATILE) != 0) {
6422                                 ec.Report.Warning (420, 1, loc,
6423                                         "`{0}': A volatile field references will not be treated as volatile",
6424                                         spec.GetSignatureForError ());
6425                         }
6426
6427                         if (spec.IsReadOnly) {
6428                                 // InitOnly fields can only be assigned in constructors or initializers
6429                                 if (!ec.HasAny (ResolveContext.Options.FieldInitializerScope | ResolveContext.Options.ConstructorScope))
6430                                         return Error_AssignToReadonly (ec, right_side);
6431
6432                                 if (ec.HasSet (ResolveContext.Options.ConstructorScope)) {
6433
6434                                         // InitOnly fields cannot be assigned-to in a different constructor from their declaring type
6435                                         if (ec.CurrentMemberDefinition.Parent.PartialContainer.Definition != spec.DeclaringType.GetDefinition ())
6436                                                 return Error_AssignToReadonly (ec, right_side);
6437                                         // static InitOnly fields cannot be assigned-to in an instance constructor
6438                                         if (IsStatic && !ec.IsStatic)
6439                                                 return Error_AssignToReadonly (ec, right_side);
6440                                         // instance constructors can't modify InitOnly fields of other instances of the same type
6441                                         if (!IsStatic && !(InstanceExpression is This))
6442                                                 return Error_AssignToReadonly (ec, right_side);
6443                                 }
6444                         }
6445
6446                         if (right_side == EmptyExpression.OutAccess && IsMarshalByRefAccess (ec)) {
6447                                 ec.Report.SymbolRelatedToPreviousError (spec.DeclaringType);
6448                                 ec.Report.Warning (197, 1, loc,
6449                                                 "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",
6450                                                 GetSignatureForError ());
6451                         }
6452
6453                         eclass = ExprClass.Variable;
6454                         return this;
6455                 }
6456
6457                 public override void FlowAnalysis (FlowAnalysisContext fc)
6458                 {
6459                         var var = InstanceExpression as IVariableReference;
6460                         if (var != null) {
6461                                 var vi = var.VariableInfo;
6462                                 if (vi != null && !fc.IsStructFieldDefinitelyAssigned (vi, Name)) {
6463                                         fc.Report.Error (170, loc, "Use of possibly unassigned field `{0}'", Name);
6464                                         return;
6465                                 }
6466
6467                                 if (TypeSpec.IsValueType (InstanceExpression.Type)) {
6468                                         var le = SkipLeftValueTypeAccess (InstanceExpression);
6469                                         if (le != null)
6470                                                 le.FlowAnalysis (fc);
6471
6472                                         return;
6473                                 }
6474                         }
6475
6476                         var da = conditional_access_receiver ? fc.BranchDefiniteAssignment () : null;
6477
6478                         base.FlowAnalysis (fc);
6479
6480                         if (conditional_access_receiver)
6481                                 fc.DefiniteAssignment = da;
6482                 }
6483
6484                 static Expression SkipLeftValueTypeAccess (Expression expr)
6485                 {
6486                         if (!TypeSpec.IsValueType (expr.Type))
6487                                 return expr;
6488
6489                         if (expr is VariableReference)
6490                                 return null;
6491
6492                         var fe = expr as FieldExpr;
6493                         if (fe == null)
6494                                 return expr;
6495
6496                         if (fe.InstanceExpression == null)
6497                                 return expr;
6498
6499                         return SkipLeftValueTypeAccess (fe.InstanceExpression);
6500                 }
6501
6502                 public override int GetHashCode ()
6503                 {
6504                         return spec.GetHashCode ();
6505                 }
6506                 
6507                 public bool IsFixed {
6508                         get {
6509                                 //
6510                                 // A variable of the form V.I is fixed when V is a fixed variable of a struct type
6511                                 //
6512                                 IVariableReference variable = InstanceExpression as IVariableReference;
6513                                 if (variable != null)
6514                                         return InstanceExpression.Type.IsStruct && variable.IsFixed;
6515
6516                                 IFixedExpression fe = InstanceExpression as IFixedExpression;
6517                                 return fe != null && fe.IsFixed;
6518                         }
6519                 }
6520
6521                 public override bool Equals (object obj)
6522                 {
6523                         FieldExpr fe = obj as FieldExpr;
6524                         if (fe == null)
6525                                 return false;
6526
6527                         if (spec != fe.spec)
6528                                 return false;
6529
6530                         if (InstanceExpression == null || fe.InstanceExpression == null)
6531                                 return true;
6532
6533                         return InstanceExpression.Equals (fe.InstanceExpression);
6534                 }
6535                 
6536                 public void Emit (EmitContext ec, bool leave_copy)
6537                 {
6538                         bool is_volatile = (spec.Modifiers & Modifiers.VOLATILE) != 0;
6539
6540                         if (IsStatic){
6541                                 if (is_volatile)
6542                                         ec.Emit (OpCodes.Volatile);
6543
6544                                 ec.Emit (OpCodes.Ldsfld, spec);
6545                         } else {
6546                                 var ca = ec.ConditionalAccess;
6547
6548                                 if (!prepared) {
6549                                         if (conditional_access_receiver)
6550                                                 ec.ConditionalAccess = new ConditionalAccessContext (type, ec.DefineLabel ());
6551
6552                                         EmitInstance (ec, false);
6553                                 }
6554
6555                                 // Optimization for build-in types
6556                                 if (type.IsStruct && type == ec.CurrentType && InstanceExpression.Type == type) {
6557                                         ec.EmitLoadFromPtr (type);
6558                                 } else {
6559                                         var ff = spec as FixedFieldSpec;
6560                                         if (ff != null) {
6561                                                 ec.Emit (OpCodes.Ldflda, spec);
6562                                                 ec.Emit (OpCodes.Ldflda, ff.Element);
6563                                         } else {
6564                                                 if (is_volatile)
6565                                                         ec.Emit (OpCodes.Volatile);
6566
6567                                                 ec.Emit (OpCodes.Ldfld, spec);
6568                                         }
6569                                 }
6570
6571                                 if (conditional_access_receiver) {
6572                                         ec.CloseConditionalAccess (type.IsNullableType && type != spec.MemberType ? type : null);
6573                                         ec.ConditionalAccess = ca;
6574                                 }
6575                         }
6576
6577                         if (leave_copy) {
6578                                 ec.Emit (OpCodes.Dup);
6579                                 if (!IsStatic) {
6580                                         temp = new LocalTemporary (this.Type);
6581                                         temp.Store (ec);
6582                                 }
6583                         }
6584                 }
6585
6586                 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
6587                 {
6588                         bool has_await_source = ec.HasSet (BuilderContext.Options.AsyncBody) && source.ContainsEmitWithAwait ();
6589                         if (isCompound && !(source is DynamicExpressionStatement) && !has_await_source) {
6590                                 prepared = true;
6591                         }
6592
6593                         if (IsInstance) {
6594                                 if (ConditionalAccess)
6595                                         throw new NotImplementedException ("null operator assignment");
6596
6597                                 if (has_await_source)
6598                                         source = source.EmitToField (ec);
6599
6600                                 EmitInstance (ec, prepared);
6601                         }
6602
6603                         source.Emit (ec);
6604
6605                         if (leave_copy || ec.NotifyEvaluatorOnStore) {
6606                                 ec.Emit (OpCodes.Dup);
6607                                 if (!IsStatic) {
6608                                         temp = new LocalTemporary (this.Type);
6609                                         temp.Store (ec);
6610                                 }
6611                         }
6612
6613                         if ((spec.Modifiers & Modifiers.VOLATILE) != 0)
6614                                 ec.Emit (OpCodes.Volatile);
6615                                         
6616                         spec.MemberDefinition.SetIsAssigned ();
6617
6618                         if (IsStatic)
6619                                 ec.Emit (OpCodes.Stsfld, spec);
6620                         else
6621                                 ec.Emit (OpCodes.Stfld, spec);
6622
6623                         if (ec.NotifyEvaluatorOnStore) {
6624                                 if (!IsStatic)
6625                                         throw new NotImplementedException ("instance field write");
6626
6627                                 if (leave_copy)
6628                                         ec.Emit (OpCodes.Dup);
6629
6630                                 ec.Module.Evaluator.EmitValueChangedCallback (ec, Name, type, loc);
6631                         }
6632                         
6633                         if (temp != null) {
6634                                 temp.Emit (ec);
6635                                 temp.Release (ec);
6636                                 temp = null;
6637                         }
6638                 }
6639
6640                 //
6641                 // Emits store to field with prepared values on stack
6642                 //
6643                 public void EmitAssignFromStack (EmitContext ec)
6644                 {
6645                         if (IsStatic) {
6646                                 ec.Emit (OpCodes.Stsfld, spec);
6647                         } else {
6648                                 ec.Emit (OpCodes.Stfld, spec);
6649                         }
6650                 }
6651
6652                 public override void Emit (EmitContext ec)
6653                 {
6654                         Emit (ec, false);
6655                 }
6656
6657                 public override void EmitSideEffect (EmitContext ec)
6658                 {
6659                         bool is_volatile = (spec.Modifiers & Modifiers.VOLATILE) != 0;
6660
6661                         if (is_volatile) // || is_marshal_by_ref ())
6662                                 base.EmitSideEffect (ec);
6663                 }
6664
6665                 public virtual void AddressOf (EmitContext ec, AddressOp mode)
6666                 {
6667                         if ((mode & AddressOp.Store) != 0)
6668                                 spec.MemberDefinition.SetIsAssigned ();
6669                         if ((mode & AddressOp.Load) != 0)
6670                                 spec.MemberDefinition.SetIsUsed ();
6671
6672                         //
6673                         // Handle initonly fields specially: make a copy and then
6674                         // get the address of the copy.
6675                         //
6676                         bool need_copy;
6677                         if (spec.IsReadOnly){
6678                                 need_copy = true;
6679                                 if (ec.HasSet (EmitContext.Options.ConstructorScope) && spec.DeclaringType == ec.CurrentType) {
6680                                         if (IsStatic){
6681                                                 if (ec.IsStatic)
6682                                                         need_copy = false;
6683                                         } else
6684                                                 need_copy = false;
6685                                 }
6686                         } else
6687                                 need_copy = false;
6688                         
6689                         if (need_copy) {
6690                                 Emit (ec);
6691                                 var temp = ec.GetTemporaryLocal (type);
6692                                 ec.Emit (OpCodes.Stloc, temp);
6693                                 ec.Emit (OpCodes.Ldloca, temp);
6694                                 return;
6695                         }
6696
6697
6698                         if (IsStatic){
6699                                 ec.Emit (OpCodes.Ldsflda, spec);
6700                         } else {
6701                                 if (!prepared)
6702                                         EmitInstance (ec, false);
6703                                 ec.Emit (OpCodes.Ldflda, spec);
6704                         }
6705                 }
6706
6707                 public SLE.Expression MakeAssignExpression (BuilderContext ctx, Expression source)
6708                 {
6709                         return MakeExpression (ctx);
6710                 }
6711
6712                 public override SLE.Expression MakeExpression (BuilderContext ctx)
6713                 {
6714 #if STATIC
6715                         return base.MakeExpression (ctx);
6716 #else
6717                         return SLE.Expression.Field (
6718                                 IsStatic ? null : InstanceExpression.MakeExpression (ctx),
6719                                 spec.GetMetaInfo ());
6720 #endif
6721                 }
6722
6723                 public override void SetTypeArguments (ResolveContext ec, TypeArguments ta)
6724                 {
6725                         Error_TypeArgumentsCannotBeUsed (ec, "field", GetSignatureForError (), loc);
6726                 }
6727         }
6728
6729         
6730         //
6731         // Expression that evaluates to a Property.
6732         //
6733         // This is not an LValue because we need to re-write the expression. We
6734         // can not take data from the stack and store it.
6735         //
6736         sealed class PropertyExpr : PropertyOrIndexerExpr<PropertySpec>
6737         {
6738                 Arguments arguments;
6739                 FieldExpr backing_field;
6740
6741                 public PropertyExpr (PropertySpec spec, Location l)
6742                         : base (l)
6743                 {
6744                         best_candidate = spec;
6745                         type = spec.MemberType;
6746                 }
6747
6748                 #region Properties
6749
6750                 protected override Arguments Arguments {
6751                         get {
6752                                 return arguments;
6753                         }
6754                         set {
6755                                 arguments = value;
6756                         }
6757                 }
6758
6759                 protected override TypeSpec DeclaringType {
6760                         get {
6761                                 return best_candidate.DeclaringType;
6762                         }
6763                 }
6764
6765                 public override string Name {
6766                         get {
6767                                 return best_candidate.Name;
6768                         }
6769                 }
6770
6771                 public bool IsAutoPropertyAccess {
6772                         get {
6773                                 var prop = best_candidate.MemberDefinition as Property;
6774                                 return prop != null && prop.BackingField != null;
6775                         }
6776                 }
6777
6778                 public override bool IsInstance {
6779                         get {
6780                                 return !IsStatic;
6781                         }
6782                 }
6783
6784                 public override bool IsStatic {
6785                         get {
6786                                 return best_candidate.IsStatic;
6787                         }
6788                 }
6789
6790                 public override string KindName {
6791                         get { return "property"; }
6792                 }
6793
6794                 public PropertySpec PropertyInfo {
6795                         get {
6796                                 return best_candidate;
6797                         }
6798                 }
6799
6800                 #endregion
6801
6802                 public override MethodGroupExpr CanReduceLambda (AnonymousMethodBody body)
6803                 {
6804                         if (best_candidate == null || !(best_candidate.IsStatic || InstanceExpression is This))
6805                                 return null;
6806
6807                         var args_count = arguments == null ? 0 : arguments.Count;
6808                         if (args_count != body.Parameters.Count && args_count == 0)
6809                                 return null;
6810
6811                         var mg = MethodGroupExpr.CreatePredefined (best_candidate.Get, DeclaringType, loc);
6812                         mg.InstanceExpression = InstanceExpression;
6813
6814                         return mg;
6815                 }
6816
6817                 public static PropertyExpr CreatePredefined (PropertySpec spec, Location loc)
6818                 {
6819                         return new PropertyExpr (spec, loc) {
6820                                 Getter = spec.Get,
6821                                 Setter = spec.Set
6822                         };
6823                 }
6824
6825                 public override Expression CreateExpressionTree (ResolveContext ec)
6826                 {
6827                         if (ConditionalAccess) {
6828                                 Error_NullShortCircuitInsideExpressionTree (ec);
6829                         }
6830
6831                         Arguments args;
6832                         if (IsSingleDimensionalArrayLength ()) {
6833                                 args = new Arguments (1);
6834                                 args.Add (new Argument (InstanceExpression.CreateExpressionTree (ec)));
6835                                 return CreateExpressionFactoryCall (ec, "ArrayLength", args);
6836                         }
6837
6838                         args = new Arguments (2);
6839                         if (InstanceExpression == null)
6840                                 args.Add (new Argument (new NullLiteral (loc)));
6841                         else
6842                                 args.Add (new Argument (InstanceExpression.CreateExpressionTree (ec)));
6843                         args.Add (new Argument (new TypeOfMethod (Getter, loc)));
6844                         return CreateExpressionFactoryCall (ec, "Property", args);
6845                 }
6846
6847                 public Expression CreateSetterTypeOfExpression (ResolveContext rc)
6848                 {
6849                         DoResolveLValue (rc, null);
6850                         return new TypeOfMethod (Setter, loc);
6851                 }
6852
6853                 public override string GetSignatureForError ()
6854                 {
6855                         return best_candidate.GetSignatureForError ();
6856                 }
6857
6858                 public override SLE.Expression MakeAssignExpression (BuilderContext ctx, Expression source)
6859                 {
6860 #if STATIC
6861                         return base.MakeExpression (ctx);
6862 #else
6863                         return SLE.Expression.Property (InstanceExpression.MakeExpression (ctx), (MethodInfo) Setter.GetMetaInfo ());
6864 #endif
6865                 }
6866
6867                 public override SLE.Expression MakeExpression (BuilderContext ctx)
6868                 {
6869 #if STATIC
6870                         return base.MakeExpression (ctx);
6871 #else
6872                         return SLE.Expression.Property (InstanceExpression.MakeExpression (ctx), (MethodInfo) Getter.GetMetaInfo ());
6873 #endif
6874                 }
6875
6876                 void Error_PropertyNotValid (ResolveContext ec)
6877                 {
6878                         ec.Report.SymbolRelatedToPreviousError (best_candidate);
6879                         ec.Report.Error (1546, loc, "Property or event `{0}' is not supported by the C# language",
6880                                 GetSignatureForError ());
6881                 }
6882
6883                 bool IsSingleDimensionalArrayLength ()
6884                 {
6885                         if (best_candidate.DeclaringType.BuiltinType != BuiltinTypeSpec.Type.Array || !best_candidate.HasGet || Name != "Length")
6886                                 return false;
6887
6888                         ArrayContainer ac = InstanceExpression.Type as ArrayContainer;
6889                         return ac != null && ac.Rank == 1;
6890                 }
6891
6892                 public override void Emit (EmitContext ec, bool leave_copy)
6893                 {
6894                         //
6895                         // Special case: length of single dimension array property is turned into ldlen
6896                         //
6897                         if (IsSingleDimensionalArrayLength ()) {
6898                                 if (conditional_access_receiver) {
6899                                         ec.ConditionalAccess = new ConditionalAccessContext (type, ec.DefineLabel ());
6900                                 }
6901
6902                                 EmitInstance (ec, false);
6903
6904                                 ec.Emit (OpCodes.Ldlen);
6905                                 ec.Emit (OpCodes.Conv_I4);
6906
6907                                 if (conditional_access_receiver) {
6908                                         ec.CloseConditionalAccess (type);
6909                                 }
6910
6911                                 return;
6912                         }
6913
6914                         base.Emit (ec, leave_copy);
6915                 }
6916
6917                 public override void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
6918                 {
6919                         if (backing_field != null) {
6920                                 backing_field.EmitAssign (ec, source, false, false);
6921                                 return;
6922                         }
6923
6924                         Arguments args;
6925                         LocalTemporary await_source_arg = null;
6926
6927                         if (isCompound && !(source is DynamicExpressionStatement)) {
6928                                 emitting_compound_assignment = true;
6929                                 source.Emit (ec);
6930
6931                                 if (has_await_arguments) {
6932                                         await_source_arg = new LocalTemporary (Type);
6933                                         await_source_arg.Store (ec);
6934
6935                                         args = new Arguments (1);
6936                                         args.Add (new Argument (await_source_arg));
6937
6938                                         if (leave_copy) {
6939                                                 temp = await_source_arg;
6940                                         }
6941
6942                                         has_await_arguments = false;
6943                                 } else {
6944                                         args = null;
6945
6946                                         if (leave_copy) {
6947                                                 ec.Emit (OpCodes.Dup);
6948                                                 temp = new LocalTemporary (this.Type);
6949                                                 temp.Store (ec);
6950                                         }
6951                                 }
6952                         } else {
6953                                 args = arguments ?? new Arguments (1);
6954
6955                                 if (leave_copy) {
6956                                         source.Emit (ec);
6957                                         temp = new LocalTemporary (this.Type);
6958                                         temp.Store (ec);
6959                                         args.Add (new Argument (temp));
6960                                 } else {
6961                                         args.Add (new Argument (source));
6962                                 }
6963                         }
6964
6965                         emitting_compound_assignment = false;
6966
6967                         var call = new CallEmitter ();
6968                         call.InstanceExpression = InstanceExpression;
6969                         if (args == null)
6970                                 call.InstanceExpressionOnStack = true;
6971
6972                         if (ConditionalAccess) {
6973                                 call.ConditionalAccess = true;
6974                         }
6975
6976                         if (leave_copy)
6977                                 call.Emit (ec, Setter, args, loc);
6978                         else
6979                                 call.EmitStatement (ec, Setter, args, loc);
6980
6981                         if (temp != null) {
6982                                 temp.Emit (ec);
6983                                 temp.Release (ec);
6984                         }
6985
6986                         if (await_source_arg != null) {
6987                                 await_source_arg.Release (ec);
6988                         }
6989                 }
6990
6991                 public override void FlowAnalysis (FlowAnalysisContext fc)
6992                 {
6993                         var prop = best_candidate.MemberDefinition as Property;
6994                         if (prop != null && prop.BackingField != null) {
6995                                 var var = InstanceExpression as IVariableReference;
6996                                 if (var != null) {
6997                                         var vi = var.VariableInfo;
6998                                         if (vi != null && !fc.IsStructFieldDefinitelyAssigned (vi, prop.BackingField.Name)) {
6999                                                 fc.Report.Error (8079, loc, "Use of possibly unassigned auto-implemented property `{0}'", Name);
7000                                                 return;
7001                                         }
7002
7003                                         if (TypeSpec.IsValueType (InstanceExpression.Type) && InstanceExpression is VariableReference)
7004                                                 return;
7005                                 }
7006                         }
7007
7008                         var da = conditional_access_receiver ? fc.BranchDefiniteAssignment () : null;
7009
7010                         base.FlowAnalysis (fc);
7011
7012                         if (conditional_access_receiver)
7013                                 fc.DefiniteAssignment = da;
7014                 }
7015
7016                 protected override Expression OverloadResolve (ResolveContext rc, Expression right_side)
7017                 {
7018                         eclass = ExprClass.PropertyAccess;
7019
7020                         if (best_candidate.IsNotCSharpCompatible) {
7021                                 Error_PropertyNotValid (rc);
7022                         }
7023
7024                         ResolveInstanceExpression (rc, right_side);
7025
7026                         if ((best_candidate.Modifiers & (Modifiers.ABSTRACT | Modifiers.VIRTUAL)) != 0 && best_candidate.DeclaringType != InstanceExpression.Type) {
7027                                 var filter = new MemberFilter (best_candidate.Name, 0, MemberKind.Property, null, null);
7028                                 var p = MemberCache.FindMember (InstanceExpression.Type, filter, BindingRestriction.InstanceOnly | BindingRestriction.OverrideOnly) as PropertySpec;
7029                                 if (p != null) {
7030                                         type = p.MemberType;
7031                                 }
7032                         }
7033
7034                         DoBestMemberChecks (rc, best_candidate);
7035
7036                         // Handling of com-imported properties with any number of default property parameters
7037                         if (best_candidate.HasGet && !best_candidate.Get.Parameters.IsEmpty) {
7038                                 var p = best_candidate.Get.Parameters;
7039                                 arguments = new Arguments (p.Count);
7040                                 for (int i = 0; i < p.Count; ++i) {
7041                                         arguments.Add (new Argument (OverloadResolver.ResolveDefaultValueArgument (rc, p.Types [i], p.FixedParameters [i].DefaultValue, loc)));
7042                                 }
7043                         } else if (best_candidate.HasSet && best_candidate.Set.Parameters.Count > 1) {
7044                                 var p = best_candidate.Set.Parameters;
7045                                 arguments = new Arguments (p.Count - 1);
7046                                 for (int i = 0; i < p.Count - 1; ++i) {
7047                                         arguments.Add (new Argument (OverloadResolver.ResolveDefaultValueArgument (rc, p.Types [i], p.FixedParameters [i].DefaultValue, loc)));
7048                                 }
7049                         }
7050
7051                         return this;
7052                 }
7053
7054                 protected override bool ResolveAutopropertyAssignment (ResolveContext rc, Expression rhs)
7055                 {
7056                         if (!rc.HasSet (ResolveContext.Options.ConstructorScope))
7057                                 return false;
7058
7059                         var prop = best_candidate.MemberDefinition as Property;
7060                         if (prop == null || prop.Parent.PartialContainer != rc.CurrentMemberDefinition.Parent.PartialContainer) {
7061                                 var ps = MemberCache.FindMember (rc.CurrentType, MemberFilter.Property (best_candidate.Name, best_candidate.MemberType), BindingRestriction.DeclaredOnly) as PropertySpec;
7062                                 if (ps == null)
7063                                         return false;
7064
7065                                 prop = (Property)ps.MemberDefinition;
7066                         }
7067
7068                         var spec = prop.BackingField;
7069                         if (spec == null)
7070                                 return false;
7071
7072                         if (rc.IsStatic != spec.IsStatic)
7073                                 return false;
7074
7075                         if (!spec.IsStatic && (!(InstanceExpression is This) || InstanceExpression is BaseThis))
7076                                 return false;
7077
7078                         backing_field = new FieldExpr (prop.BackingField, loc);
7079                         backing_field.ResolveLValue (rc, rhs);
7080                         return true;
7081                 }
7082
7083                 public void SetBackingFieldAssigned (FlowAnalysisContext fc)
7084                 {
7085                         if (backing_field != null) {
7086                                 backing_field.SetFieldAssigned (fc);
7087                                 return;
7088                         }
7089
7090                         if (!IsAutoPropertyAccess)
7091                                 return;
7092
7093                         var prop = best_candidate.MemberDefinition as Property;
7094                         if (prop != null && prop.BackingField != null) {
7095                                 bool lvalue_instance = best_candidate.DeclaringType.IsStruct;
7096                                 if (lvalue_instance) {
7097                                         var var = InstanceExpression as IVariableReference;
7098                                         if (var != null && var.VariableInfo != null) {
7099                                                 fc.SetStructFieldAssigned (var.VariableInfo, prop.BackingField.Name);
7100                                         }
7101                                 }
7102                         }
7103                 }
7104
7105                 public override void SetTypeArguments (ResolveContext ec, TypeArguments ta)
7106                 {
7107                         Error_TypeArgumentsCannotBeUsed (ec, "property", GetSignatureForError (), loc);
7108                 }
7109         }
7110
7111         abstract class PropertyOrIndexerExpr<T> : MemberExpr, IDynamicAssign where T : PropertySpec
7112         {
7113                 // getter and setter can be different for base calls
7114                 MethodSpec getter, setter;
7115                 protected T best_candidate;
7116
7117                 protected LocalTemporary temp;
7118                 protected bool emitting_compound_assignment;
7119                 protected bool has_await_arguments;
7120
7121                 protected PropertyOrIndexerExpr (Location l)
7122                 {
7123                         loc = l;
7124                 }
7125
7126                 #region Properties
7127
7128                 protected abstract Arguments Arguments { get; set; }
7129
7130                 public MethodSpec Getter {
7131                         get {
7132                                 return getter;
7133                         }
7134                         set {
7135                                 getter = value;
7136                         }
7137                 }
7138
7139                 public MethodSpec Setter {
7140                         get {
7141                                 return setter;
7142                         }
7143                         set {
7144                                 setter = value;
7145                         }
7146                 }
7147
7148                 #endregion
7149
7150                 protected override Expression DoResolve (ResolveContext ec)
7151                 {
7152                         if (eclass == ExprClass.Unresolved) {
7153                                 ResolveConditionalAccessReceiver (ec);
7154
7155                                 var expr = OverloadResolve (ec, null);
7156                                 if (expr == null)
7157                                         return null;
7158
7159                                 if (expr != this) {
7160                                         using (ec.With (ResolveContext.Options.DontSetConditionalAccessReceiver, conditional_access_receiver))
7161                                                 return expr.Resolve (ec);
7162                                 }
7163
7164                                 if (conditional_access_receiver) {
7165                                         type = LiftMemberType (ec, type);
7166                                 }
7167                         }
7168
7169                         if (!ResolveGetter (ec))
7170                                 return null;
7171
7172                         return this;
7173                 }
7174
7175                 public override Expression DoResolveLValue (ResolveContext rc, Expression right_side)
7176                 {
7177                         if (HasConditionalAccess ())
7178                                 Error_NullPropagatingLValue (rc);
7179
7180                         if (right_side == EmptyExpression.OutAccess) {
7181                                 // TODO: best_candidate can be null at this point
7182                                 INamedBlockVariable variable = null;
7183                                 if (best_candidate != null && rc.CurrentBlock.ParametersBlock.TopBlock.GetLocalName (best_candidate.Name, rc.CurrentBlock, ref variable) && variable is Linq.RangeVariable) {
7184                                         rc.Report.Error (1939, loc, "A range variable `{0}' may not be passes as `ref' or `out' parameter",
7185                                                 best_candidate.Name);
7186                                 } else {
7187                                         right_side.DoResolveLValue (rc, this);
7188                                 }
7189                                 return null;
7190                         }
7191
7192                         if (eclass == ExprClass.Unresolved) {
7193                                 var expr = OverloadResolve (rc, right_side);
7194                                 if (expr == null)
7195                                         return null;
7196
7197                                 if (expr != this)
7198                                         return expr.ResolveLValue (rc, right_side);
7199                         } else {
7200                                 ResolveInstanceExpression (rc, right_side);
7201                         }
7202
7203                         if (!best_candidate.HasSet) {
7204                                 if (ResolveAutopropertyAssignment (rc, right_side))
7205                                         return this;
7206
7207                                 rc.Report.Error (200, loc, "Property or indexer `{0}' cannot be assigned to (it is read-only)",
7208                                         GetSignatureForError ());
7209                                 return null;
7210                         }
7211
7212                         if (!best_candidate.Set.IsAccessible (rc) || !best_candidate.Set.DeclaringType.IsAccessible (rc)) {
7213                                 if (best_candidate.HasDifferentAccessibility) {
7214                                         rc.Report.SymbolRelatedToPreviousError (best_candidate.Set);
7215                                         rc.Report.Error (272, loc, "The property or indexer `{0}' cannot be used in this context because the set accessor is inaccessible",
7216                                                 GetSignatureForError ());
7217                                 } else {
7218                                         rc.Report.SymbolRelatedToPreviousError (best_candidate.Set);
7219                                         ErrorIsInaccesible (rc, best_candidate.GetSignatureForError (), loc);
7220                                 }
7221                         }
7222
7223                         if (best_candidate.HasDifferentAccessibility)
7224                                 CheckProtectedMemberAccess (rc, best_candidate.Set);
7225
7226                         setter = CandidateToBaseOverride (rc, best_candidate.Set);
7227                         return this;
7228                 }
7229
7230                 void EmitConditionalAccess (EmitContext ec, ref CallEmitter call, MethodSpec method, Arguments arguments)
7231                 {
7232                         var ca = ec.ConditionalAccess;
7233                         ec.ConditionalAccess = new ConditionalAccessContext (type, ec.DefineLabel ());
7234
7235                         call.Emit (ec, method, arguments, loc);
7236
7237                         ec.CloseConditionalAccess (method.ReturnType != type && type.IsNullableType ? type : null);
7238                         ec.ConditionalAccess = ca;
7239                 }
7240
7241                 //
7242                 // Implements the IAssignMethod interface for assignments
7243                 //
7244                 public virtual void Emit (EmitContext ec, bool leave_copy)
7245                 {
7246                         var call = new CallEmitter ();
7247                         call.ConditionalAccess = ConditionalAccess;
7248                         call.InstanceExpression = InstanceExpression;
7249                         if (has_await_arguments)
7250                                 call.HasAwaitArguments = true;
7251                         else
7252                                 call.DuplicateArguments = emitting_compound_assignment;
7253
7254                         if (conditional_access_receiver)
7255                                 EmitConditionalAccess (ec, ref call, Getter, Arguments);
7256                         else
7257                                 call.Emit (ec, Getter, Arguments, loc);
7258
7259                         if (call.HasAwaitArguments) {
7260                                 InstanceExpression = call.InstanceExpression;
7261                                 Arguments = call.EmittedArguments;
7262                                 has_await_arguments = true;
7263                         }
7264
7265                         if (leave_copy) {
7266                                 ec.Emit (OpCodes.Dup);
7267                                 temp = new LocalTemporary (Type);
7268                                 temp.Store (ec);
7269                         }
7270                 }
7271
7272                 public abstract void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound);
7273
7274                 public override void Emit (EmitContext ec)
7275                 {
7276                         Emit (ec, false);
7277                 }
7278
7279                 protected override FieldExpr EmitToFieldSource (EmitContext ec)
7280                 {
7281                         has_await_arguments = true;
7282                         Emit (ec, false);
7283                         return null;
7284                 }
7285
7286                 public abstract SLE.Expression MakeAssignExpression (BuilderContext ctx, Expression source);
7287
7288                 protected abstract Expression OverloadResolve (ResolveContext rc, Expression right_side);
7289
7290                 bool ResolveGetter (ResolveContext rc)
7291                 {
7292                         if (!best_candidate.HasGet) {
7293                                 if (InstanceExpression != EmptyExpression.Null) {
7294                                         rc.Report.SymbolRelatedToPreviousError (best_candidate);
7295                                         rc.Report.Error (154, loc, "The property or indexer `{0}' cannot be used in this context because it lacks the `get' accessor",
7296                                                 best_candidate.GetSignatureForError ());
7297                                         return false;
7298                                 }
7299                         } else if (!best_candidate.Get.IsAccessible (rc) || !best_candidate.Get.DeclaringType.IsAccessible (rc)) {
7300                                 if (best_candidate.HasDifferentAccessibility) {
7301                                         rc.Report.SymbolRelatedToPreviousError (best_candidate.Get);
7302                                         rc.Report.Error (271, loc, "The property or indexer `{0}' cannot be used in this context because the get accessor is inaccessible",
7303                                                 TypeManager.CSharpSignature (best_candidate));
7304                                 } else {
7305                                         rc.Report.SymbolRelatedToPreviousError (best_candidate.Get);
7306                                         ErrorIsInaccesible (rc, best_candidate.Get.GetSignatureForError (), loc);
7307                                 }
7308                         }
7309
7310                         if (best_candidate.HasDifferentAccessibility) {
7311                                 CheckProtectedMemberAccess (rc, best_candidate.Get);
7312                         }
7313
7314                         getter = CandidateToBaseOverride (rc, best_candidate.Get);
7315                         return true;
7316                 }
7317
7318                 protected virtual bool ResolveAutopropertyAssignment (ResolveContext rc, Expression rhs)
7319                 {
7320                         return false;
7321                 }
7322         }
7323
7324         /// <summary>
7325         ///   Fully resolved expression that evaluates to an Event
7326         /// </summary>
7327         public class EventExpr : MemberExpr, IAssignMethod
7328         {
7329                 readonly EventSpec spec;
7330                 MethodSpec op;
7331
7332                 public EventExpr (EventSpec spec, Location loc)
7333                 {
7334                         this.spec = spec;
7335                         this.loc = loc;
7336                 }
7337
7338                 #region Properties
7339
7340                 protected override TypeSpec DeclaringType {
7341                         get {
7342                                 return spec.DeclaringType;
7343                         }
7344                 }
7345
7346                 public override string Name {
7347                         get {
7348                                 return spec.Name;
7349                         }
7350                 }
7351
7352                 public override bool IsInstance {
7353                         get {
7354                                 return !spec.IsStatic;
7355                         }
7356                 }
7357
7358                 public override bool IsStatic {
7359                         get {
7360                                 return spec.IsStatic;
7361                         }
7362                 }
7363
7364                 public override string KindName {
7365                         get { return "event"; }
7366                 }
7367
7368                 public MethodSpec Operator {
7369                         get {
7370                                 return op;
7371                         }
7372                 }
7373
7374                 #endregion
7375
7376                 public override MemberExpr ResolveMemberAccess (ResolveContext ec, Expression left, SimpleName original)
7377                 {
7378                         //
7379                         // If the event is local to this class and we are not lhs of +=/-= we transform ourselves into a FieldExpr
7380                         //
7381                         if (!ec.HasSet (ResolveContext.Options.CompoundAssignmentScope)) {
7382                                 if (spec.BackingField != null &&
7383                                         (spec.DeclaringType == ec.CurrentType || TypeManager.IsNestedChildOf (ec.CurrentType, spec.DeclaringType.MemberDefinition))) {
7384
7385                                         spec.MemberDefinition.SetIsUsed ();
7386
7387                                         spec.CheckObsoleteness (ec, loc);
7388
7389                                         if ((spec.Modifiers & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
7390                                                 Error_AssignmentEventOnly (ec);
7391
7392                                         FieldExpr ml = new FieldExpr (spec.BackingField, loc);
7393
7394                                         InstanceExpression = null;
7395
7396                                         return ml.ResolveMemberAccess (ec, left, original);
7397                                 }
7398                         }
7399
7400                         return base.ResolveMemberAccess (ec, left, original);
7401                 }
7402
7403                 public override Expression CreateExpressionTree (ResolveContext ec)
7404                 {
7405                         throw new NotSupportedException ("ET");
7406                 }
7407
7408                 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
7409                 {
7410                         if (right_side == EmptyExpression.EventAddition) {
7411                                 op = spec.AccessorAdd;
7412                         } else if (right_side == EmptyExpression.EventSubtraction) {
7413                                 op = spec.AccessorRemove;
7414                         }
7415
7416                         if (op == null) {
7417                                 Error_AssignmentEventOnly (ec);
7418                                 return null;
7419                         }
7420
7421                         if (HasConditionalAccess ())
7422                                 Error_NullPropagatingLValue (ec);
7423
7424                         op = CandidateToBaseOverride (ec, op);
7425                         return this;
7426                 }
7427
7428                 protected override Expression DoResolve (ResolveContext ec)
7429                 {
7430                         eclass = ExprClass.EventAccess;
7431                         type = spec.MemberType;
7432
7433                         ResolveInstanceExpression (ec, null);
7434
7435                         if (!ec.HasSet (ResolveContext.Options.CompoundAssignmentScope)) {
7436                                 Error_AssignmentEventOnly (ec);
7437                         }
7438
7439                         DoBestMemberChecks (ec, spec);
7440                         return this;
7441                 }               
7442
7443                 public override void Emit (EmitContext ec)
7444                 {
7445                         throw new NotSupportedException ();
7446                         //Error_CannotAssign ();
7447                 }
7448
7449                 #region IAssignMethod Members
7450
7451                 public void Emit (EmitContext ec, bool leave_copy)
7452                 {
7453                         throw new NotImplementedException ();
7454                 }
7455
7456                 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
7457                 {
7458                         if (leave_copy || !isCompound)
7459                                 throw new NotImplementedException ("EventExpr::EmitAssign");
7460
7461                         Arguments args = new Arguments (1);
7462                         args.Add (new Argument (source));
7463
7464                         // TODO: Wrong, needs receiver
7465 //                      if (NullShortCircuit) {
7466 //                              ec.ConditionalAccess = new ConditionalAccessContext (type, ec.DefineLabel ());
7467 //                      }
7468
7469                         var call = new CallEmitter ();
7470                         call.InstanceExpression = InstanceExpression;
7471                         call.ConditionalAccess = ConditionalAccess;
7472                         call.EmitStatement (ec, op, args, loc);
7473
7474 //                      if (NullShortCircuit)
7475 //                              ec.CloseConditionalAccess (null);
7476                 }
7477
7478                 #endregion
7479
7480                 void Error_AssignmentEventOnly (ResolveContext ec)
7481                 {
7482                         if (spec.DeclaringType == ec.CurrentType || TypeManager.IsNestedChildOf (ec.CurrentType, spec.DeclaringType.MemberDefinition)) {
7483                                 ec.Report.Error (79, loc,
7484                                         "The event `{0}' can only appear on the left hand side of `+=' or `-=' operator",
7485                                         GetSignatureForError ());
7486                         } else {
7487                                 ec.Report.Error (70, loc,
7488                                         "The event `{0}' can only appear on the left hand side of += or -= when used outside of the type `{1}'",
7489                                         GetSignatureForError (), spec.DeclaringType.GetSignatureForError ());
7490                         }
7491                 }
7492
7493                 protected override void Error_CannotCallAbstractBase (ResolveContext rc, string name)
7494                 {
7495                         name = name.Substring (0, name.LastIndexOf ('.'));
7496                         base.Error_CannotCallAbstractBase (rc, name);
7497                 }
7498
7499                 public override string GetSignatureForError ()
7500                 {
7501                         return TypeManager.CSharpSignature (spec);
7502                 }
7503
7504                 public override void SetTypeArguments (ResolveContext ec, TypeArguments ta)
7505                 {
7506                         Error_TypeArgumentsCannotBeUsed (ec, "event", GetSignatureForError (), loc);
7507                 }
7508         }
7509
7510         public class TemporaryVariableReference : VariableReference
7511         {
7512                 public class Declarator : Statement
7513                 {
7514                         TemporaryVariableReference variable;
7515
7516                         public Declarator (TemporaryVariableReference variable)
7517                         {
7518                                 this.variable = variable;
7519                                 loc = variable.loc;
7520                         }
7521
7522                         protected override void DoEmit (EmitContext ec)
7523                         {
7524                                 variable.li.CreateBuilder (ec);
7525                         }
7526
7527                         public override void Emit (EmitContext ec)
7528                         {
7529                                 // Don't create sequence point
7530                                 DoEmit (ec);
7531                         }
7532
7533                         protected override bool DoFlowAnalysis (FlowAnalysisContext fc)
7534                         {
7535                                 return false;
7536                         }
7537
7538                         protected override void CloneTo (CloneContext clonectx, Statement target)
7539                         {
7540                                 // Nothing
7541                         }
7542                 }
7543
7544                 LocalVariable li;
7545
7546                 public TemporaryVariableReference (LocalVariable li, Location loc)
7547                 {
7548                         this.li = li;
7549                         this.type = li.Type;
7550                         this.loc = loc;
7551                 }
7552
7553                 public override bool IsLockedByStatement {
7554                         get {
7555                                 return false;
7556                         }
7557                         set {
7558                         }
7559                 }
7560
7561                 public LocalVariable LocalInfo {
7562                     get {
7563                         return li;
7564                     }
7565                 }
7566
7567                 public static TemporaryVariableReference Create (TypeSpec type, Block block, Location loc, bool writeToSymbolFile = false)
7568                 {
7569                         var li = LocalVariable.CreateCompilerGenerated (type, block, loc, writeToSymbolFile);
7570                         return new TemporaryVariableReference (li, loc);
7571                 }
7572
7573                 protected override Expression DoResolve (ResolveContext ec)
7574                 {
7575                         eclass = ExprClass.Variable;
7576
7577                         //
7578                         // Don't capture temporary variables except when using
7579                         // state machine redirection and block yields
7580                         //
7581                         if (ec.CurrentAnonymousMethod is StateMachineInitializer &&
7582                                 (ec.CurrentBlock.Explicit.HasYield || ec.CurrentBlock.Explicit.HasAwait) &&
7583                                 ec.IsVariableCapturingRequired) {
7584                                 AnonymousMethodStorey storey = li.Block.Explicit.CreateAnonymousMethodStorey (ec);
7585                                 storey.CaptureLocalVariable (ec, li);
7586                         }
7587
7588                         return this;
7589                 }
7590
7591                 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
7592                 {
7593                         return Resolve (ec);
7594                 }
7595                 
7596                 public override void Emit (EmitContext ec)
7597                 {
7598                         li.CreateBuilder (ec);
7599
7600                         Emit (ec, false);
7601                 }
7602
7603                 public void EmitAssign (EmitContext ec, Expression source)
7604                 {
7605                         li.CreateBuilder (ec);
7606
7607                         EmitAssign (ec, source, false, false);
7608                 }
7609
7610                 public override HoistedVariable GetHoistedVariable (AnonymousExpression ae)
7611                 {
7612                         return li.HoistedVariant;
7613                 }
7614
7615                 public override bool IsFixed {
7616                         get { return true; }
7617                 }
7618
7619                 public override bool IsRef {
7620                         get { return false; }
7621                 }
7622
7623                 public override string Name {
7624                         get { throw new NotImplementedException (); }
7625                 }
7626
7627                 public override void SetHasAddressTaken ()
7628                 {
7629                         throw new NotImplementedException ();
7630                 }
7631
7632                 protected override ILocalVariable Variable {
7633                         get { return li; }
7634                 }
7635
7636                 public override VariableInfo VariableInfo {
7637                         get { return null; }
7638                 }
7639         }
7640
7641         /// 
7642         /// Handles `var' contextual keyword; var becomes a keyword only
7643         /// if no type called var exists in a variable scope
7644         /// 
7645         class VarExpr : SimpleName
7646         {
7647                 public VarExpr (Location loc)
7648                         : base ("var", loc)
7649                 {
7650                 }
7651
7652                 public bool InferType (ResolveContext ec, Expression right_side)
7653                 {
7654                         if (type != null)
7655                                 throw new InternalErrorException ("An implicitly typed local variable could not be redefined");
7656                         
7657                         type = right_side.Type;
7658                         if (type == InternalType.NullLiteral || type.Kind == MemberKind.Void || type == InternalType.AnonymousMethod || type == InternalType.MethodGroup) {
7659                                 ec.Report.Error (815, loc,
7660                                         "An implicitly typed local variable declaration cannot be initialized with `{0}'",
7661                                         type.GetSignatureForError ());
7662                                 type = InternalType.ErrorType;
7663                                 return false;
7664                         }
7665
7666                         eclass = ExprClass.Variable;
7667                         return true;
7668                 }
7669
7670                 protected override void Error_TypeOrNamespaceNotFound (IMemberContext ec)
7671                 {
7672                         if (ec.Module.Compiler.Settings.Version < LanguageVersion.V_3)
7673                                 base.Error_TypeOrNamespaceNotFound (ec);
7674                         else
7675                                 ec.Module.Compiler.Report.Error (825, loc, "The contextual keyword `var' may only appear within a local variable declaration");
7676                 }
7677         }
7678 }