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