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