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