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