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