Fix sequence points for more statements
[mono.git] / mcs / mcs / method.cs
index 2af0b7850937880452cd5226450829814e2650a9..c33050915f0094c3c1b86c07413379af78dd8dbb 100644 (file)
@@ -9,6 +9,7 @@
 //
 // Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
 // Copyright 2004-2008 Novell, Inc
+// Copyright 2011 Xamarin Inc.
 //
 
 using System;
@@ -46,14 +47,19 @@ namespace Mono.CSharp {
                protected ToplevelBlock block;
                protected MethodSpec spec;
 
-               public MethodCore (DeclSpace parent, GenericMethod generic,
-                       FullNamedExpression type, Modifiers mod, Modifiers allowed_mod,
+               public MethodCore (TypeDefinition parent, FullNamedExpression type, Modifiers mod, Modifiers allowed_mod,
                        MemberName name, Attributes attrs, ParametersCompiled parameters)
-                       : base (parent, generic, type, mod, allowed_mod, name, attrs)
+                       : base (parent, type, mod, allowed_mod, name, attrs)
                {
                        this.parameters = parameters;
                }
 
+               public override Variance ExpectedMemberTypeVariance {
+                       get {
+                               return Variance.Covariant;
+                       }
+               }
+
                //
                //  Returns the System.Type array for the parameters of this method
                //
@@ -121,32 +127,20 @@ namespace Mono.CSharp {
                }
 
                //
-               // Returns a string that represents the signature for this 
-               // member which should be used in XML documentation.
+               //   Represents header string for documentation comment.
                //
-               public override string GetDocCommentName (DeclSpace ds)
+               public override string DocCommentHeader 
                {
-                       return DocUtil.GetMethodDocCommentName (this, parameters, ds);
+                       get { return "M:"; }
                }
 
-               //
-               // Raised (and passed an XmlElement that contains the comment)
-               // when GenerateDocComment is writing documentation expectedly.
-               //
-               // FIXME: with a few effort, it could be done with XmlReader,
-               // that means removal of DOM use.
-               //
-               internal override void OnGenerateDocComment (XmlElement el)
+               public override void Emit ()
                {
-                       DocUtil.OnMethodGenerateDocComment (this, el, Report);
-               }
+                       if ((ModFlags & Modifiers.COMPILER_GENERATED) == 0) {
+                               parameters.CheckConstraints (this);
+                       }
 
-               //
-               //   Represents header string for documentation comment.
-               //
-               public override string DocCommentHeader 
-               {
-                       get { return "M:"; }
+                       base.Emit ();
                }
 
                public override bool EnableOverloadChecks (MemberCore overload)
@@ -162,6 +156,15 @@ namespace Mono.CSharp {
                        return base.EnableOverloadChecks (overload);
                }
 
+               public override string GetSignatureForDocumentation ()
+               {
+                       string s = base.GetSignatureForDocumentation ();
+                       if (MemberName.Arity > 0)
+                               s += "``" + MemberName.Arity.ToString ();
+
+                       return s + parameters.GetSignatureForDocumentation ();
+               }
+
                public MethodSpec Spec {
                        get { return spec; }
                }
@@ -343,6 +346,36 @@ namespace Mono.CSharp {
                        return inflatedMetaInfo;
                }
 
+               public override string GetSignatureForDocumentation ()
+               {
+                       string name;
+                       switch (Kind) {
+                       case MemberKind.Constructor:
+                               name = "#ctor";
+                               break;
+                       case MemberKind.Method:
+                               if (Arity > 0)
+                                       name = Name + "``" + Arity.ToString ();
+                               else
+                                       name = Name;
+
+                               break;
+                       default:
+                               name = Name;
+                               break;
+                       }
+
+                       name = DeclaringType.GetSignatureForDocumentation () + "." + name + parameters.GetSignatureForDocumentation ();
+                       if (Kind == MemberKind.Operator) {
+                               var op = Operator.GetType (Name).Value;
+                               if (op == Operator.OpType.Explicit || op == Operator.OpType.Implicit) {
+                                       name += "~" + ReturnType.GetSignatureForDocumentation ();
+                               }
+                       }
+
+                       return name;
+               }
+
                public override string GetSignatureForError ()
                {
                        string name;
@@ -392,7 +425,7 @@ namespace Mono.CSharp {
                        return ms;
                }
 
-               public MethodSpec MakeGenericMethod (params TypeSpec[] targs)
+               public MethodSpec MakeGenericMethod (IMemberContext context, params TypeSpec[] targs)
                {
                        if (targs == null)
                                throw new ArgumentNullException ();
@@ -403,7 +436,7 @@ namespace Mono.CSharp {
                        //if (generic_intances == null)
                        //    generic_intances = new Dictionary<TypeSpec[], Method> (TypeSpecArrayComparer.Default);
 
-                       var inflator = new TypeParameterInflator (DeclaringType, GenericDefinition.TypeParameters, targs);
+                       var inflator = new TypeParameterInflator (context, DeclaringType, GenericDefinition.TypeParameters, targs);
 
                        var inflated = (MethodSpec) MemberwiseClone ();
                        inflated.declaringType = inflator.TypeInstance;
@@ -449,6 +482,23 @@ namespace Mono.CSharp {
                        return ms;
                }
 
+               public override List<TypeSpec> ResolveMissingDependencies ()
+               {
+                       var missing = returnType.ResolveMissingDependencies ();
+                       foreach (var pt in parameters.Types) {
+                               var m = pt.GetMissingDependencies ();
+                               if (m == null)
+                                       continue;
+
+                               if (missing == null)
+                                       missing = new List<TypeSpec> ();
+
+                               missing.AddRange (m);
+                       }
+
+                       return missing;                 
+               }
+
                public void SetMetaInfo (MethodInfo info)
                {
                        if (this.metaInfo != null)
@@ -465,13 +515,11 @@ namespace Mono.CSharp {
                SecurityType declarative_security;
                protected MethodData MethodData;
 
-               static string[] attribute_targets = new string [] { "method", "return" };
+               static readonly string[] attribute_targets = new string [] { "method", "return" };
 
-               protected MethodOrOperator (DeclSpace parent, GenericMethod generic, FullNamedExpression type, Modifiers mod,
-                               Modifiers allowed_mod, MemberName name,
+               protected MethodOrOperator (TypeDefinition parent, FullNamedExpression type, Modifiers mod, Modifiers allowed_mod, MemberName name,
                                Attributes attrs, ParametersCompiled parameters)
-                       : base (parent, generic, type, mod, allowed_mod, name,
-                                       attrs, parameters)
+                       : base (parent, type, mod, allowed_mod, name, attrs, parameters)
                {
                }
 
@@ -547,6 +595,13 @@ namespace Mono.CSharp {
 //                                     MethodBase mb = new PartialMethodDefinitionInfo (this);
 
                                        spec = new MethodSpec (kind, Parent.Definition, this, ReturnType, null, parameters, ModFlags);
+                                       if (MemberName.Arity > 0) {
+                                               spec.IsGeneric = true;
+
+                                               // TODO: Have to move DefineMethod after Define (ideally to Emit)
+                                               throw new NotImplementedException ("Generic partial methods");
+                                       }
+
                                        Parent.MemberCache.AddMember (spec);
                                }
 
@@ -554,9 +609,9 @@ namespace Mono.CSharp {
                        }
 
                        MethodData = new MethodData (
-                               this, ModFlags, flags, this, MethodBuilder, GenericMethod, base_method);
+                               this, ModFlags, flags, this, MethodBuilder, base_method);
 
-                       if (!MethodData.Define (Parent.PartialContainer, GetFullName (MemberName), Report))
+                       if (!MethodData.Define (Parent.PartialContainer, GetFullName (MemberName)))
                                return false;
                                        
                        MethodBuilder = MethodData.MethodBuilder;
@@ -606,7 +661,7 @@ namespace Mono.CSharp {
                        if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
                                Module.PredefinedAttributes.DebuggerHidden.EmitAttribute (MethodBuilder);
 
-                       if (ReturnType == InternalType.Dynamic) {
+                       if (ReturnType.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
                                return_attributes = new ReturnParameter (this, MethodBuilder, Location);
                                Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder);
                        } else if (ReturnType.HasDynamicElement) {
@@ -627,11 +682,14 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       if (MethodData != null)
-                               MethodData.Emit (Parent);
+                       if (type_expr != null)
+                               ConstraintChecker.Check (this, member_type, type_expr.Location);
 
                        base.Emit ();
 
+                       if (MethodData != null)
+                               MethodData.Emit (Parent);
+
                        Block = null;
                        MethodData = null;
                }
@@ -663,6 +721,12 @@ namespace Mono.CSharp {
 
                #region IMethodData Members
 
+               bool IMethodData.IsAccessor {
+                       get {
+                               return false;
+                       }
+               }
+
                public TypeSpec ReturnType {
                        get {
                                return MemberType;
@@ -710,15 +774,6 @@ namespace Mono.CSharp {
                        return conditions;
                }
 
-               GenericMethod IMethodData.GenericMethod {
-                       get {
-                               return GenericMethod;
-                       }
-               }
-
-               public virtual void EmitExtraSymbolInfo (SourceMethod source)
-               { }
-
                #endregion
 
        }
@@ -726,13 +781,11 @@ namespace Mono.CSharp {
        public class SourceMethod : IMethodDef
        {
                MethodBase method;
-               SourceMethodBuilder builder;
 
-               protected SourceMethod (DeclSpace parent, MethodBase method, ICompileUnit file)
+               SourceMethod (MethodBase method, ICompileUnit file)
                {
                        this.method = method;
-                       
-                       builder = SymbolWriter.OpenMethod (file, parent.NamespaceEntry.SymbolFileID, this);
+                       SymbolWriter.OpenMethod (file, this);
                }
 
                public string Name {
@@ -760,28 +813,16 @@ namespace Mono.CSharp {
                        SymbolWriter.CloseMethod ();
                }
 
-               public void SetRealMethodName (string name)
-               {
-                       if (builder != null)
-                               builder.SetRealMethodName (name);
-               }
-
-               public static SourceMethod Create (DeclSpace parent, MethodBase method, Block block)
+               public static SourceMethod Create (TypeDefinition parent, MethodBase method)
                {
                        if (!SymbolWriter.HasSymbolWriter)
                                return null;
-                       if (block == null)
-                               return null;
 
-                       Location start_loc = block.StartLocation;
-                       if (start_loc.IsNull)
+                       var source_file = parent.GetCompilationSourceFile ();
+                       if (source_file == null)
                                return null;
 
-                       ICompileUnit compile_unit = start_loc.CompilationUnit;
-                       if (compile_unit == null)
-                               return null;
-
-                       return new SourceMethod (parent, method, compile_unit);
+                       return new SourceMethod (method, source_file.SymbolUnitEntry);
                }
        }
 
@@ -789,66 +830,84 @@ namespace Mono.CSharp {
        {
                Method partialMethodImplementation;
 
-               public Method (DeclSpace parent, GenericMethod generic,
-                              FullNamedExpression return_type, Modifiers mod,
-                              MemberName name, ParametersCompiled parameters, Attributes attrs)
-                       : base (parent, generic, return_type, mod,
+               public Method (TypeDefinition parent, FullNamedExpression return_type, Modifiers mod, MemberName name, ParametersCompiled parameters, Attributes attrs)
+                       : base (parent, return_type, mod,
                                parent.PartialContainer.Kind == MemberKind.Interface ? AllowedModifiersInterface :
-                               parent.PartialContainer.Kind == MemberKind.Struct ? AllowedModifiersStruct :
-                               AllowedModifiersClass,
+                               parent.PartialContainer.Kind == MemberKind.Struct ? AllowedModifiersStruct | Modifiers.ASYNC :
+                               AllowedModifiersClass | Modifiers.ASYNC,
                                name, attrs, parameters)
                {
                }
 
-               protected Method (DeclSpace parent, FullNamedExpression return_type, Modifiers mod, Modifiers amod,
+               protected Method (TypeDefinition parent, FullNamedExpression return_type, Modifiers mod, Modifiers amod,
                                        MemberName name, ParametersCompiled parameters, Attributes attrs)
-                       : base (parent, null, return_type, mod, amod, name, attrs, parameters)
+                       : base (parent, return_type, mod, amod, name, attrs, parameters)
                {
                }
 
                #region Properties
 
-               public override TypeParameter[] CurrentTypeParameters {
+               public override TypeParameters CurrentTypeParameters {
                        get {
-                               if (GenericMethod != null)
-                                       return GenericMethod.CurrentTypeParameters;
-
-                               return null;
-                       }
-               }
-
-               public override bool HasUnresolvedConstraints {
-                       get {
-                               if (CurrentTypeParameters == null)
-                                       return false;
-
-                               // When overriding base method constraints are fetched from
-                               // base method but to find it we have to resolve parameters
-                               // to find exact base method match
-                               if (IsExplicitImpl || (ModFlags & Modifiers.OVERRIDE) != 0)
-                                       return base_method == null;
-
-                               // Even for non-override generic method constraints check has to be
-                               // delayed after all constraints are resolved
-                               return true;
+                               return MemberName.TypeParameters;
                        }
                }
 
                public TypeParameterSpec[] TypeParameters {
                        get {
-                               // TODO: Cache this
-                               return CurrentTypeParameters.Select (l => l.Type).ToArray ();
+                               return CurrentTypeParameters.Types;
                        }
                }
 
                public int TypeParametersCount {
                        get {
-                               return CurrentTypeParameters == null ? 0 : CurrentTypeParameters.Length;
+                               return CurrentTypeParameters == null ? 0 : CurrentTypeParameters.Count;
                        }
                }
 
 #endregion
 
+               public override void Accept (StructuralVisitor visitor)
+               {
+                       visitor.Visit (this);
+               }
+
+               public static Method Create (TypeDefinition parent, FullNamedExpression returnType, Modifiers mod,
+                                  MemberName name, ParametersCompiled parameters, Attributes attrs, bool hasConstraints)
+               {
+                       var m = new Method (parent, returnType, mod, name, parameters, attrs);
+
+                       if (hasConstraints && ((mod & Modifiers.OVERRIDE) != 0 || m.IsExplicitImpl)) {
+                               m.Report.Error (460, m.Location,
+                                       "`{0}': Cannot specify constraints for overrides and explicit interface implementation methods",
+                                       m.GetSignatureForError ());
+                       }
+
+                       if ((mod & Modifiers.PARTIAL) != 0) {
+                               const Modifiers invalid_partial_mod = Modifiers.AccessibilityMask | Modifiers.ABSTRACT | Modifiers.EXTERN |
+                                       Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.SEALED | Modifiers.VIRTUAL;
+
+                               if ((mod & invalid_partial_mod) != 0) {
+                                       m.Report.Error (750, m.Location,
+                                               "A partial method cannot define access modifier or any of abstract, extern, new, override, sealed, or virtual modifiers");
+                                       mod &= ~invalid_partial_mod;
+                               }
+
+                               if ((parent.ModFlags & Modifiers.PARTIAL) == 0) {
+                                       m.Report.Error (751, m.Location, 
+                                               "A partial method must be declared within a partial class or partial struct");
+                               }
+                       }
+
+                       if ((mod & Modifiers.STATIC) == 0 && parameters.HasExtensionMethodType) {
+                               m.Report.Error (1105, m.Location, "`{0}': Extension methods must be declared static",
+                                       m.GetSignatureForError ());
+                       }
+
+
+                       return m;
+               }
+
                public override string GetSignatureForError()
                {
                        return base.GetSignatureForError () + parameters.GetSignatureForError ();
@@ -863,8 +922,7 @@ namespace Mono.CSharp {
 
                bool IsEntryPoint ()
                {
-                       if (ReturnType != TypeManager.void_type &&
-                               ReturnType != TypeManager.int32_type)
+                       if (ReturnType.Kind != MemberKind.Void && ReturnType.BuiltinType != BuiltinTypeSpec.Type.Int)
                                return false;
 
                        if (parameters.IsEmpty)
@@ -874,22 +932,22 @@ namespace Mono.CSharp {
                                return false;
 
                        var ac = parameters.Types [0] as ArrayContainer;
-                       return ac != null && ac.Rank == 1 && ac.Element == TypeManager.string_type &&
+                       return ac != null && ac.Rank == 1 && ac.Element.BuiltinType == BuiltinTypeSpec.Type.String &&
                                        (parameters[0].ModFlags & ~Parameter.Modifier.PARAMS) == Parameter.Modifier.NONE;
                }
 
-               public override FullNamedExpression LookupNamespaceOrType (string name, int arity, Location loc, bool ignore_cs0104)
+               public override FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
                {
                        if (arity == 0) {
-                               TypeParameter[] tp = CurrentTypeParameters;
+                               var tp = CurrentTypeParameters;
                                if (tp != null) {
-                                       TypeParameter t = TypeParameter.FindTypeParameter (tp, name);
+                                       TypeParameter t = tp.Find (name);
                                        if (t != null)
                                                return new TypeParameterExpr (t, loc);
                                }
                        }
 
-                       return base.LookupNamespaceOrType (name, arity, loc, ignore_cs0104);
+                       return base.LookupNamespaceOrType (name, arity, mode, loc);
                }
 
                public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
@@ -905,7 +963,7 @@ namespace Mono.CSharp {
                                        return;
                                }
 
-                               if (ReturnType != TypeManager.void_type) {
+                               if (ReturnType.Kind != MemberKind.Void) {
                                        Report.Error (578, Location, "Conditional not valid on `{0}' because its return type is not void", GetSignatureForError ());
                                        return;
                                }
@@ -938,6 +996,45 @@ namespace Mono.CSharp {
                        base.ApplyAttributeBuilder (a, ctor, cdata, pa);
                }
 
+               void CreateTypeParameters ()
+               {
+                       var tparams = MemberName.TypeParameters;
+                       string[] snames = new string[MemberName.Arity];
+                       var parent_tparams = Parent.TypeParametersAll;
+
+                       for (int i = 0; i < snames.Length; i++) {
+                               string type_argument_name = tparams[i].MemberName.Name;
+
+                               if (block == null) {
+                                       int idx = parameters.GetParameterIndexByName (type_argument_name);
+                                       if (idx >= 0) {
+                                               var b = block;
+                                               if (b == null)
+                                                       b = new ToplevelBlock (Compiler, Location);
+
+                                               b.Error_AlreadyDeclaredTypeParameter (type_argument_name, parameters[i].Location);
+                                       }
+                               } else {
+                                       INamedBlockVariable variable = null;
+                                       block.GetLocalName (type_argument_name, block, ref variable);
+                                       if (variable != null)
+                                               variable.Block.Error_AlreadyDeclaredTypeParameter (type_argument_name, variable.Location);
+                               }
+
+                               if (parent_tparams != null) {
+                                       var tp = parent_tparams.Find (type_argument_name);
+                                       if (tp != null) {
+                                               tparams[i].WarningParentNameConflict (tp);
+                                       }
+                               }
+
+                               snames[i] = type_argument_name;
+                       }
+
+                       GenericTypeParameterBuilder[] gen_params = MethodBuilder.DefineGenericParameters (snames);
+                       tparams.Define (gen_params, null, 0, Parent);
+               }
+
                protected virtual void DefineTypeParameters ()
                {
                        var tparams = CurrentTypeParameters;
@@ -980,10 +1077,10 @@ namespace Mono.CSharp {
 
                                                if (base_decl_tparams.Length != 0) {
                                                        base_decl_tparams = base_decl_tparams.Concat (base_tparams).ToArray ();
-                                                       base_targs = base_targs.Concat (tparams.Select<TypeParameter, TypeSpec> (l => l.Type)).ToArray ();
+                                                       base_targs = base_targs.Concat (tparams.Types).ToArray ();
                                                } else {
                                                        base_decl_tparams = base_tparams;
-                                                       base_targs = tparams.Select (l => l.Type).ToArray ();
+                                                       base_targs = tparams.Types;
                                                }
                                        }
                                } else if (MethodData.implementing != null) {
@@ -1000,7 +1097,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       for (int i = 0; i < tparams.Length; ++i) {
+                       for (int i = 0; i < tparams.Count; ++i) {
                                var tp = tparams[i];
 
                                if (!tp.ResolveConstraints (this))
@@ -1014,11 +1111,11 @@ namespace Mono.CSharp {
                                        var local_tparam = tp.Type;
                                        local_tparam.SpecialConstraint = base_tparam.SpecialConstraint;
 
-                                       var inflator = new TypeParameterInflator (CurrentType, base_decl_tparams, base_targs);
+                                       var inflator = new TypeParameterInflator (this, CurrentType, base_decl_tparams, base_targs);
                                        base_tparam.InflateConstraints (inflator, local_tparam);
 
                                        //
-                                       // Check all type argument constraints for possible collision
+                                       // Check all type argument constraints for possible collision or unification
                                        // introduced by inflating inherited constraints in this context
                                        //
                                        // Conflict example:
@@ -1027,31 +1124,67 @@ namespace Mono.CSharp {
                                        // class B : A<int> { override void Foo<U> {} }
                                        //
                                        var local_tparam_targs = local_tparam.TypeArguments;
-                                       if (local_tparam_targs != null) {                                       
+                                       if (local_tparam_targs != null) {
                                                for (int ii = 0; ii < local_tparam_targs.Length; ++ii) {
                                                        var ta = local_tparam_targs [ii];
                                                        if (!ta.IsClass && !ta.IsStruct)
                                                                continue;
 
-                                                       if (Constraints.CheckConflictingInheritedConstraint (local_tparam, ta, this, Location)) {
-                                                               local_tparam.ChangeTypeArgumentToBaseType (ii);
+                                                       TypeSpec[] unique_tparams = null;
+                                                       for (int iii = ii + 1; iii < local_tparam_targs.Length; ++iii) {
+                                                               //
+                                                               // Remove any identical or unified constraint types
+                                                               //
+                                                               var tparam_checked = local_tparam_targs[iii];
+                                                               if (TypeSpecComparer.IsEqual (ta, tparam_checked) || TypeSpec.IsBaseClass (ta, tparam_checked, false)) {
+                                                                       unique_tparams = new TypeSpec[local_tparam_targs.Length - 1];
+                                                                       Array.Copy (local_tparam_targs, 0, unique_tparams, 0, iii);
+                                                                       Array.Copy (local_tparam_targs, iii + 1, unique_tparams, iii, local_tparam_targs.Length - iii - 1);
+                                                               } else if (!TypeSpec.IsBaseClass (tparam_checked, ta, false)) {
+                                                                       Constraints.Error_ConflictingConstraints (this, local_tparam, ta, tparam_checked, Location);
+                                                               }
                                                        }
+
+                                                       if (unique_tparams != null) {
+                                                               local_tparam_targs = unique_tparams;
+                                                               local_tparam.TypeArguments = local_tparam_targs;
+                                                               continue;
+                                                       }
+
+                                                       Constraints.CheckConflictingInheritedConstraint (local_tparam, ta, this, Location);
                                                }
                                        }
 
                                        continue;
                                }
-                               
-                               if (MethodData.implementing != null) {
-                                       var base_tp = MethodData.implementing.Constraints[i];
-                                       if (!tp.Type.HasSameConstraintsImplementation (base_tp)) {
-                                               Report.SymbolRelatedToPreviousError (MethodData.implementing);
-                                               Report.Error (425, Location,
-                                                       "The constraints for type parameter `{0}' of method `{1}' must match the constraints for type parameter `{2}' of interface method `{3}'. Consider using an explicit interface implementation instead",
-                                                       tp.GetSignatureForError (), GetSignatureForError (), base_tp.GetSignatureForError (), MethodData.implementing.GetSignatureForError ());
-                                       }
+                       }
+
+                       if (base_tparams == null && MethodData != null && MethodData.implementing != null) {
+                               CheckImplementingMethodConstraints (Parent, spec, MethodData.implementing);
+                       }
+               }
+
+               public static bool CheckImplementingMethodConstraints (TypeContainer container, MethodSpec method, MethodSpec baseMethod)
+               {
+                       var tparams = method.Constraints;
+                       var base_tparams = baseMethod.Constraints;
+                       for (int i = 0; i < tparams.Length; ++i) {
+                               if (!tparams[i].HasSameConstraintsImplementation (base_tparams[i])) {
+                                       container.Compiler.Report.SymbolRelatedToPreviousError (method);
+                                       container.Compiler.Report.SymbolRelatedToPreviousError (baseMethod);
+
+                                       // Using container location because the interface can be implemented
+                                       // by base class
+                                       container.Compiler.Report.Error (425, container.Location,
+                                               "The constraints for type parameter `{0}' of method `{1}' must match the constraints for type parameter `{2}' of interface method `{3}'. Consider using an explicit interface implementation instead",
+                                               tparams[i].GetSignatureForError (), method.GetSignatureForError (),
+                                               base_tparams[i].GetSignatureForError (), baseMethod.GetSignatureForError ());
+                                       return false;
                                }
                        }
+
+                       return true;
                }
 
                //
@@ -1059,26 +1192,27 @@ namespace Mono.CSharp {
                //
                public override bool Define ()
                {
-                       if (type_expr.Type == TypeManager.void_type && parameters.IsEmpty && MemberName.Arity == 0 && MemberName.Name == Destructor.MetadataName) {
-                               Report.Warning (465, 1, Location, "Introducing `Finalize' method can interfere with destructor invocation. Did you intend to declare a destructor?");
-                       }
-
                        if (!base.Define ())
                                return false;
 
+                       if (member_type.Kind == MemberKind.Void && parameters.IsEmpty && MemberName.Arity == 0 && MemberName.Name == Destructor.MetadataName) {
+                               Report.Warning (465, 1, Location,
+                                       "Introducing `Finalize' method can interfere with destructor invocation. Did you intend to declare a destructor?");
+                       }
+
                        if (partialMethodImplementation != null && IsPartialDefinition)
                                MethodBuilder = partialMethodImplementation.MethodBuilder;
 
-                       if (RootContext.StdLib && TypeManager.IsSpecialType (ReturnType)) {
+                       if (Compiler.Settings.StdLib && ReturnType.IsSpecialRuntimeType) {
                                Error1599 (Location, ReturnType, Report);
                                return false;
                        }
 
                        if (CurrentTypeParameters == null) {
-                               if (base_method != null) {
-                                       if (parameters.Count == 1 && ParameterTypes[0] == TypeManager.object_type && Name == "Equals")
+                               if (base_method != null && !IsExplicitImpl) {
+                                       if (parameters.Count == 1 && ParameterTypes[0].BuiltinType == BuiltinTypeSpec.Type.Object && MemberName.Name == "Equals")
                                                Parent.PartialContainer.Mark_HasEquals ();
-                                       else if (parameters.IsEmpty && Name == "GetHashCode")
+                                       else if (parameters.IsEmpty && MemberName.Name == "GetHashCode")
                                                Parent.PartialContainer.Mark_HasGetHashCode ();
                                }
                                        
@@ -1086,20 +1220,32 @@ namespace Mono.CSharp {
                                DefineTypeParameters ();
                        }
 
-                       if (block != null && block.IsIterator) {
-                               //
-                               // Current method is turned into automatically generated
-                               // wrapper which creates an instance of iterator
-                               //
-                               Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags, Compiler);
-                               ModFlags |= Modifiers.DEBUGGER_HIDDEN;
+                       if (block != null) {
+                               if (block.IsIterator) {
+                                       //
+                                       // Current method is turned into automatically generated
+                                       // wrapper which creates an instance of iterator
+                                       //
+                                       Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
+                                       ModFlags |= Modifiers.DEBUGGER_HIDDEN;
+                               }
+
+                               if ((ModFlags & Modifiers.ASYNC) != 0) {
+                                       if (ReturnType.Kind != MemberKind.Void &&
+                                               ReturnType != Module.PredefinedTypes.Task.TypeSpec &&
+                                               !ReturnType.IsGenericTask) {
+                                               Report.Error (1983, Location, "The return type of an async method must be void, Task, or Task<T>");
+                                       }
+
+                                       AsyncInitializer.Create (this, block, parameters, Parent.PartialContainer, ReturnType, Location);
+                               }
                        }
 
                        if ((ModFlags & Modifiers.STATIC) == 0)
                                return true;
 
                        if (parameters.HasExtensionMethodType) {
-                               if (Parent.PartialContainer.IsStatic && !Parent.IsGeneric) {
+                               if (Parent.PartialContainer.IsStatic && !Parent.IsGenericOrParentIsGeneric) {
                                        if (!Parent.IsTopLevel)
                                                Report.Error (1109, Location, "`{0}': Extension methods cannot be defined in a nested class",
                                                        GetSignatureForError ());
@@ -1107,7 +1253,7 @@ namespace Mono.CSharp {
                                        PredefinedAttribute pa = Module.PredefinedAttributes.Extension;
                                        if (!pa.IsDefined) {
                                                Report.Error (1110, Location,
-                                                       "`{0}': Extension methods cannot be declared without a reference to System.Core.dll assembly. Add the assembly reference or remove `this' modifer from the first parameter",
+                                                       "`{0}': Extension methods require `System.Runtime.CompilerServices.ExtensionAttribute' type to be available. Are you missing an assembly reference?",
                                                        GetSignatureForError ());
                                        }
 
@@ -1124,16 +1270,16 @@ namespace Mono.CSharp {
                        //
                        // This is used to track the Entry Point,
                        //
-                       if (RootContext.NeedsEntryPoint &&
-                               Name == "Main" &&
-                               (RootContext.MainClass == null ||
-                               RootContext.MainClass == Parent.TypeBuilder.FullName)){
+                       var settings = Compiler.Settings;
+                       if (settings.NeedsEntryPoint && MemberName.Name == "Main" && (settings.MainClass == null || settings.MainClass == Parent.TypeBuilder.FullName)) {
                                if (IsEntryPoint ()) {
-
                                        if (Parent.DeclaringAssembly.EntryPoint == null) {
-                                               if (Parent.IsGeneric || MemberName.IsGeneric) {
+                                               if (Parent.IsGenericOrParentIsGeneric || MemberName.IsGeneric) {
                                                        Report.Warning (402, 4, Location, "`{0}': an entry point cannot be generic or in a generic type",
                                                                GetSignatureForError ());
+                                               } else if ((ModFlags & Modifiers.ASYNC) != 0) {
+                                                       Report.Error (4009, Location, "`{0}': an entry point cannot be async method",
+                                                               GetSignatureForError ());
                                                } else {
                                                        SetIsUsed ();
                                                        Parent.DeclaringAssembly.EntryPoint = this;
@@ -1174,19 +1320,9 @@ namespace Mono.CSharp {
                                }
 
                                if (CurrentTypeParameters != null) {
-                                       var ge = type_expr as GenericTypeExpr;
-                                       if (ge != null)
-                                               ge.CheckConstraints (this);
-
-                                       foreach (Parameter p in parameters.FixedParameters) {
-                                               ge = p.TypeExpression as GenericTypeExpr;
-                                               if (ge != null)
-                                                       ge.CheckConstraints (this);
-                                       }
-
-                                       for (int i = 0; i < CurrentTypeParameters.Length; ++i) {
+                                       for (int i = 0; i < CurrentTypeParameters.Count; ++i) {
                                                var tp = CurrentTypeParameters [i];
-                                               tp.CheckGenericConstraints ();
+                                               tp.CheckGenericConstraints (false);
                                                tp.Emit ();
                                        }
                                }
@@ -1204,10 +1340,6 @@ namespace Mono.CSharp {
 
                public override bool EnableOverloadChecks (MemberCore overload)
                {
-                       // TODO: It can be deleted when members will be defined in correct order
-                       if (overload is Operator)
-                               return overload.EnableOverloadChecks (this);
-
                        if (overload is Indexer)
                                return false;
 
@@ -1221,10 +1353,9 @@ namespace Mono.CSharp {
 
                protected override bool ResolveMemberType ()
                {
-                       if (GenericMethod != null) {
+                       if (CurrentTypeParameters != null) {
                                MethodBuilder = Parent.TypeBuilder.DefineMethod (GetFullName (MemberName), flags);
-                               if (!GenericMethod.Define (this))
-                                       return false;
+                               CreateTypeParameters ();
                        }
 
                        return base.ResolveMemberType ();
@@ -1269,6 +1400,11 @@ namespace Mono.CSharp {
                        }
                }
 
+               public override bool ContainsEmitWithAwait ()
+               {
+                       throw new NotSupportedException ();
+               }
+
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        throw new NotSupportedException ("ET");
@@ -1315,7 +1451,7 @@ namespace Mono.CSharp {
                                        //
                                        // struct D { public D (int a) : this () {}
                                        //
-                                       if (TypeManager.IsStruct (ec.CurrentType) && argument_list == null)
+                                       if (ec.CurrentType.IsStruct && argument_list == null)
                                                return this;
                                }
 
@@ -1327,7 +1463,7 @@ namespace Mono.CSharp {
                                ec.Report.Error (516, loc, "Constructor `{0}' cannot call itself",
                                        caller_builder.GetSignatureForError ());
                        }
-                                               
+
                        return this;
                }
 
@@ -1337,9 +1473,9 @@ namespace Mono.CSharp {
                        if (base_ctor == null)
                                return;
                        
-                       ec.Mark (loc);
-
-                       Invocation.EmitCall (ec, new CompilerGeneratedThis (type, loc), base_ctor, argument_list, loc);
+                       var call = new CallEmitter ();
+                       call.InstanceExpression = new CompilerGeneratedThis (type, loc); 
+                       call.EmitPredefined (ec, base_ctor, argument_list);
                }
 
                public override void EmitStatement (EmitContext ec)
@@ -1392,24 +1528,27 @@ namespace Mono.CSharp {
                public static readonly string ConstructorName = ".ctor";
                public static readonly string TypeConstructorName = ".cctor";
 
-               //
-               // The spec claims that static is not permitted, but
-               // my very own code has static constructors.
-               //
-               public Constructor (DeclSpace parent, string name, Modifiers mod, Attributes attrs, ParametersCompiled args,
-                                   ConstructorInitializer init, Location loc)
-                       : base (parent, null, null, mod, AllowedModifiers,
-                               new MemberName (name, loc), attrs, args)
+               public Constructor (TypeDefinition parent, string name, Modifiers mod, Attributes attrs, ParametersCompiled args, Location loc)
+                       : base (parent, null, mod, AllowedModifiers, new MemberName (name, loc), attrs, args)
                {
-                       Initializer = init;
                }
 
                public bool HasCompliantArgs {
-                       get { return has_compliant_args; }
+                       get {
+                               return has_compliant_args;
+                       }
                }
 
                public override AttributeTargets AttributeTargets {
-                       get { return AttributeTargets.Constructor; }
+                       get {
+                               return AttributeTargets.Constructor;
+                       }
+               }
+
+               bool IMethodData.IsAccessor {
+                       get {
+                               return false;
+                       }
                }
 
                //
@@ -1425,6 +1564,11 @@ namespace Mono.CSharp {
                                        (Initializer.Arguments == null);
                }
 
+               public override void Accept (StructuralVisitor visitor)
+               {
+                       visitor.Visit (this);
+               }
+
                public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
                {
                        if (a.IsValidSecurityAttribute ()) {
@@ -1448,6 +1592,9 @@ namespace Mono.CSharp {
                                        return false;
                                }
 
+                               if ((caching_flags & Flags.MethodOverloadsExist) != 0)
+                                       Parent.MemberCache.CheckExistingMembersOverloads (this, parameters);
+
                                // the rest can be ignored
                                return true;
                        }
@@ -1478,14 +1625,6 @@ namespace Mono.CSharp {
                        if (ConstructorBuilder != null)
                                return true;
 
-                       var ca = MethodAttributes.RTSpecialName | MethodAttributes.SpecialName;
-                       
-                       if ((ModFlags & Modifiers.STATIC) != 0) {
-                               ca |= MethodAttributes.Static | MethodAttributes.Private;
-                       } else {
-                               ca |= ModifiersExtensions.MethodAttr (ModFlags);
-                       }
-
                        if (!CheckAbstractAndExtern (block != null))
                                return false;
                        
@@ -1493,18 +1632,20 @@ namespace Mono.CSharp {
                        if (!CheckBase ())
                                return false;
 
+                       var ca = ModifiersExtensions.MethodAttr (ModFlags) | MethodAttributes.RTSpecialName | MethodAttributes.SpecialName;
+
                        ConstructorBuilder = Parent.TypeBuilder.DefineConstructor (
                                ca, CallingConventions,
                                parameters.GetMetaInfo ());
 
-                       spec = new MethodSpec (MemberKind.Constructor, Parent.Definition, this, TypeManager.void_type, ConstructorBuilder, parameters, ModFlags);
+                       spec = new MethodSpec (MemberKind.Constructor, Parent.Definition, this, Compiler.BuiltinTypes.Void, ConstructorBuilder, parameters, ModFlags);
                        
                        Parent.MemberCache.AddMember (spec);
                        
                        // It's here only to report an error
                        if (block != null && block.IsIterator) {
-                               member_type = TypeManager.void_type;
-                               Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags, Compiler);
+                               member_type = Compiler.BuiltinTypes.Void;
+                               Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
                        }
 
                        return true;
@@ -1534,58 +1675,58 @@ namespace Mono.CSharp {
                                OptAttributes.Emit ();
 
                        base.Emit ();
+                       parameters.ApplyAttributes (this, ConstructorBuilder);
+
+
+                       BlockContext bc = new BlockContext (this, block, Compiler.BuiltinTypes.Void);
+                       bc.Set (ResolveContext.Options.ConstructorScope);
 
                        //
                        // If we use a "this (...)" constructor initializer, then
                        // do not emit field initializers, they are initialized in the other constructor
                        //
-                       bool emit_field_initializers = ((ModFlags & Modifiers.STATIC) != 0) ||
-                               !(Initializer is ConstructorThisInitializer);
-
-                       BlockContext bc = new BlockContext (this, block, TypeManager.void_type);
-                       bc.Set (ResolveContext.Options.ConstructorScope);
-
-                       if (emit_field_initializers)
+                       if (!(Initializer is ConstructorThisInitializer))
                                Parent.PartialContainer.ResolveFieldInitializers (bc);
 
                        if (block != null) {
-                               // If this is a non-static `struct' constructor and doesn't have any
-                               // initializer, it must initialize all of the struct's fields.
-                               if ((Parent.PartialContainer.Kind == MemberKind.Struct) &&
-                                       ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
-                                       block.AddThisVariable (bc, Parent, Location);
-
-                               if (block != null && (ModFlags & Modifiers.STATIC) == 0){
-                                       if (Parent.PartialContainer.Kind == MemberKind.Class && Initializer == null)
-                                               Initializer = new GeneratedBaseInitializer (Location);
+                               if (!IsStatic) {
+                                       if (Initializer == null) {
+                                               if (Parent.PartialContainer.Kind == MemberKind.Struct) {
+                                                       //
+                                                       // If this is a non-static `struct' constructor and doesn't have any
+                                                       // initializer, it must initialize all of the struct's fields.
+                                                       //
+                                                       block.AddThisVariable (bc);
+                                               } else if (Parent.PartialContainer.Kind == MemberKind.Class) {
+                                                       Initializer = new GeneratedBaseInitializer (Location);
+                                               }
+                                       }
 
                                        if (Initializer != null) {
-                                               block.AddScopeStatement (new StatementExpression (Initializer));
+                                               //
+                                               // Use location of the constructor to emit sequence point of initializers
+                                               // at beginning of constructor name
+                                               //
+                                               // TODO: Need to extend mdb to support line regions to allow set a breakpoint at
+                                               // initializer
+                                               //
+                                               block.AddScopeStatement (new StatementExpression (Initializer, Location));
                                        }
                                }
-                       }
-
-                       parameters.ApplyAttributes (this, ConstructorBuilder);
 
-                       SourceMethod source = SourceMethod.Create (Parent, ConstructorBuilder, block);
-
-                       if (block != null) {
                                if (block.Resolve (null, bc, this)) {
                                        EmitContext ec = new EmitContext (this, ConstructorBuilder.GetILGenerator (), bc.ReturnType);
                                        ec.With (EmitContext.Options.ConstructorScope, true);
 
-                                       if (!ec.HasReturnLabel && bc.HasReturnLabel) {
-                                               ec.ReturnLabel = bc.ReturnLabel;
-                                               ec.HasReturnLabel = true;
-                                       }
+                                       SourceMethod source = SourceMethod.Create (Parent, ConstructorBuilder);
 
                                        block.Emit (ec);
+
+                                       if (source != null)
+                                               source.CloseMethod ();
                                }
                        }
 
-                       if (source != null)
-                               source.CloseMethod ();
-
                        if (declarative_security != null) {
                                foreach (var de in declarative_security) {
 #if STATIC
@@ -1599,13 +1740,18 @@ namespace Mono.CSharp {
                        block = null;
                }
 
-               protected override MemberSpec FindBaseMember (out MemberSpec bestCandidate)
+               protected override MemberSpec FindBaseMember (out MemberSpec bestCandidate, ref bool overrides)
                {
                        // Is never override
                        bestCandidate = null;
                        return null;
                }
 
+               public override string GetSignatureForDocumentation ()
+               {
+                       return Parent.GetSignatureForDocumentation () + ".#ctor" + parameters.GetSignatureForDocumentation ();
+               }
+
                public override string GetSignatureForError()
                {
                        return base.GetSignatureForError () + parameters.GetSignatureForError ();
@@ -1659,47 +1805,38 @@ namespace Mono.CSharp {
                        return false;
                }
 
-               GenericMethod IMethodData.GenericMethod {
-                       get {
-                               return null;
-                       }
-               }
-
-               void IMethodData.EmitExtraSymbolInfo (SourceMethod source)
-               { }
-
                #endregion
        }
 
        /// <summary>
        /// Interface for MethodData class. Holds links to parent members to avoid member duplication.
        /// </summary>
-       public interface IMethodData
+       public interface IMethodData : IMemberContext
        {
                CallingConventions CallingConventions { get; }
                Location Location { get; }
                MemberName MethodName { get; }
                TypeSpec ReturnType { get; }
-               GenericMethod GenericMethod { get; }
                ParametersCompiled ParameterInfo { get; }
                MethodSpec Spec { get; }
+               bool IsAccessor { get; }
 
                Attributes OptAttributes { get; }
                ToplevelBlock Block { get; set; }
 
                EmitContext CreateEmitContext (ILGenerator ig);
-               string GetSignatureForError ();
-               void EmitExtraSymbolInfo (SourceMethod source);
        }
 
        //
        // Encapsulates most of the Method's state
        //
-       public class MethodData {
+       public class MethodData
+       {
+#if !STATIC
                static FieldInfo methodbuilder_attrs_field;
-               public readonly IMethodData method;
+#endif
 
-               public readonly GenericMethod GenericMethod;
+               public readonly IMethodData method;
 
                //
                // Are we implementing an interface ?
@@ -1741,67 +1878,73 @@ namespace Mono.CSharp {
                public MethodData (InterfaceMemberBase member,
                                   Modifiers modifiers, MethodAttributes flags, 
                                   IMethodData method, MethodBuilder builder,
-                                  GenericMethod generic, MethodSpec parent_method)
+                                  MethodSpec parent_method)
                        : this (member, modifiers, flags, method)
                {
                        this.builder = builder;
-                       this.GenericMethod = generic;
                        this.parent_method = parent_method;
                }
 
-               public bool Define (DeclSpace parent, string method_full_name, Report Report)
+               public bool Define (TypeDefinition container, string method_full_name)
                {
-                       TypeContainer container = parent.PartialContainer;
-
                        PendingImplementation pending = container.PendingImplementations;
-                       if (pending != null){
-                               implementing = pending.IsInterfaceMethod (method.MethodName, member.InterfaceType, this);
+                       MethodSpec ambig_iface_method;
+                       bool optional = false;
 
-                               if (member.InterfaceType != null){
-                                       if (implementing == null){
+                       if (pending != null) {
+                               implementing = pending.IsInterfaceMethod (method.MethodName, member.InterfaceType, this, out ambig_iface_method, ref optional);
+
+                               if (member.InterfaceType != null) {
+                                       if (implementing == null) {
                                                if (member is PropertyBase) {
-                                                       Report.Error (550, method.Location, "`{0}' is an accessor not found in interface member `{1}{2}'",
-                                                                     method.GetSignatureForError (), TypeManager.CSharpName (member.InterfaceType),
-                                                                     member.GetSignatureForError ().Substring (member.GetSignatureForError ().LastIndexOf ('.')));
+                                                       container.Compiler.Report.Error (550, method.Location,
+                                                               "`{0}' is an accessor not found in interface member `{1}{2}'",
+                                                                         method.GetSignatureForError (), TypeManager.CSharpName (member.InterfaceType),
+                                                                         member.GetSignatureForError ().Substring (member.GetSignatureForError ().LastIndexOf ('.')));
 
                                                } else {
-                                                       Report.Error (539, method.Location,
-                                                                     "`{0}.{1}' in explicit interface declaration is not a member of interface",
-                                                                     TypeManager.CSharpName (member.InterfaceType), member.ShortName);
+                                                       container.Compiler.Report.Error (539, method.Location,
+                                                                         "`{0}.{1}' in explicit interface declaration is not a member of interface",
+                                                                         TypeManager.CSharpName (member.InterfaceType), member.ShortName);
                                                }
                                                return false;
                                        }
-                                       if (implementing.IsAccessor && !(method is AbstractPropertyEventMethod)) {
-                                               Report.SymbolRelatedToPreviousError (implementing);
-                                               Report.Error (683, method.Location, "`{0}' explicit method implementation cannot implement `{1}' because it is an accessor",
+                                       if (implementing.IsAccessor && !method.IsAccessor) {
+                                               container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
+                                               container.Compiler.Report.Error (683, method.Location,
+                                                       "`{0}' explicit method implementation cannot implement `{1}' because it is an accessor",
                                                        member.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
                                                return false;
                                        }
                                } else {
                                        if (implementing != null) {
-                                               AbstractPropertyEventMethod prop_method = method as AbstractPropertyEventMethod;
-                                               if (prop_method == null) {
+                                               if (!method.IsAccessor) {
                                                        if (implementing.IsAccessor) {
-                                                               Report.SymbolRelatedToPreviousError (implementing);
-                                                               Report.Error (470, method.Location, "Method `{0}' cannot implement interface accessor `{1}'",
+                                                               container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
+                                                               container.Compiler.Report.Error (470, method.Location,
+                                                                       "Method `{0}' cannot implement interface accessor `{1}'",
                                                                        method.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
                                                        }
                                                } else if (implementing.DeclaringType.IsInterface) {
                                                        if (!implementing.IsAccessor) {
-                                                               Report.SymbolRelatedToPreviousError (implementing);
-                                                               Report.Error (686, method.Location, "Accessor `{0}' cannot implement interface member `{1}' for type `{2}'. Use an explicit interface implementation",
+                                                               container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
+                                                               container.Compiler.Report.Error (686, method.Location,
+                                                                       "Accessor `{0}' cannot implement interface member `{1}' for type `{2}'. Use an explicit interface implementation",
                                                                        method.GetSignatureForError (), TypeManager.CSharpSignature (implementing), container.GetSignatureForError ());
                                                        } else {
-                                                               PropertyBase.PropertyMethod pm = prop_method as PropertyBase.PropertyMethod;
+                                                               PropertyBase.PropertyMethod pm = method as PropertyBase.PropertyMethod;
                                                                if (pm != null && pm.HasCustomAccessModifier && (pm.ModFlags & Modifiers.PUBLIC) == 0) {
-                                                                       Report.SymbolRelatedToPreviousError (implementing);
-                                                                       Report.Error (277, method.Location, "Accessor `{0}' must be declared public to implement interface member `{1}'",
+                                                                       container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
+                                                                       container.Compiler.Report.Error (277, method.Location,
+                                                                               "Accessor `{0}' must be declared public to implement interface member `{1}'",
                                                                                method.GetSignatureForError (), implementing.GetSignatureForError ());
                                                                }
                                                        }
                                                }
                                        }
                                }
+                       } else {
+                               ambig_iface_method = null;
                        }
 
                        //
@@ -1809,34 +1952,44 @@ namespace Mono.CSharp {
                        // explicit implementations, make sure we are private.
                        //
                        if (implementing != null){
-                               //
-                               // Setting null inside this block will trigger a more
-                               // verbose error reporting for missing interface implementations
-                               //
-                               // The "candidate" function has been flagged already
-                               // but it wont get cleared
-                               //
-                               if (member.IsExplicitImpl){
+                               if (member.IsExplicitImpl) {
                                        if (method.ParameterInfo.HasParams && !implementing.Parameters.HasParams) {
-                                               Report.SymbolRelatedToPreviousError (implementing);
-                                               Report.Error (466, method.Location, "`{0}': the explicit interface implementation cannot introduce the params modifier",
+                                               container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
+                                               container.Compiler.Report.Error (466, method.Location,
+                                                       "`{0}': the explicit interface implementation cannot introduce the params modifier",
+                                                       method.GetSignatureForError ());
+                                       }
+
+                                       if (ambig_iface_method != null) {
+                                               container.Compiler.Report.SymbolRelatedToPreviousError (ambig_iface_method);
+                                               container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
+                                               container.Compiler.Report.Warning (473, 2, method.Location,
+                                                       "Explicit interface implementation `{0}' matches more than one interface member. Consider using a non-explicit implementation instead",
                                                        method.GetSignatureForError ());
                                        }
                                } else {
+                                       //
+                                       // Setting implementin to null inside this block will trigger a more
+                                       // verbose error reporting for missing interface implementations
+                                       //
                                        if (implementing.DeclaringType.IsInterface) {
                                                //
                                                // If this is an interface method implementation,
                                                // check for public accessibility
                                                //
-                                               if ((flags & MethodAttributes.MemberAccessMask) != MethodAttributes.Public)
-                                               {
+                                               if ((flags & MethodAttributes.MemberAccessMask) != MethodAttributes.Public) {
+                                                       implementing = null;
+                                               } else if (optional && (container.Interfaces == null || Array.IndexOf (container.Interfaces, implementing.DeclaringType) < 0)) {
+                                                       //
+                                                       // We are not implementing interface when base class already implemented it
+                                                       //
                                                        implementing = null;
                                                }
-                                       } else if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private){
+                                       } else if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private) {
                                                // We may never be private.
                                                implementing = null;
 
-                                       } else if ((modifiers & Modifiers.OVERRIDE) == 0){
+                                       } else if ((modifiers & Modifiers.OVERRIDE) == 0) {
                                                //
                                                // We may be protected if we're overriding something.
                                                //
@@ -1860,9 +2013,8 @@ namespace Mono.CSharp {
                                // When implementing interface methods, set NewSlot
                                // unless, we are overwriting a method.
                                //
-                               if (implementing.DeclaringType.IsInterface){
-                                       if ((modifiers & Modifiers.OVERRIDE) == 0)
-                                               flags |= MethodAttributes.NewSlot;
+                               if ((modifiers & Modifiers.OVERRIDE) == 0 && implementing.DeclaringType.IsInterface) {
+                                       flags |= MethodAttributes.NewSlot;
                                }
 
                                flags |= MethodAttributes.Virtual | MethodAttributes.HideBySig;
@@ -1874,8 +2026,8 @@ namespace Mono.CSharp {
                                //
                                // clear the pending implementation flag (requires explicit methods to be defined first)
                                //
-                               parent.PartialContainer.PendingImplementations.ImplementMethod (method.MethodName,
-                                       member.InterfaceType, this, member.IsExplicitImpl);
+                               pending.ImplementMethod (method.MethodName,
+                                       member.InterfaceType, this, member.IsExplicitImpl, out ambig_iface_method, ref optional);
 
                                //
                                // Update indexer accessor name to match implementing abstract accessor
@@ -1905,7 +2057,7 @@ namespace Mono.CSharp {
                /// <summary>
                /// Create the MethodBuilder for the method 
                /// </summary>
-               void DefineMethodBuilder (TypeContainer container, string method_name, ParametersCompiled param)
+               void DefineMethodBuilder (TypeDefinition container, string method_name, ParametersCompiled param)
                {
                        var return_type = method.ReturnType.GetMetaInfo ();
                        var p_types = param.GetMetaInfo ();
@@ -1941,34 +2093,25 @@ namespace Mono.CSharp {
                //
                // Emits the code
                // 
-               public void Emit (DeclSpace parent)
+               public void Emit (TypeDefinition parent)
                {
-                       if (GenericMethod != null)
-                               GenericMethod.EmitAttributes ();
-
                        var mc = (IMemberContext) method;
 
                        method.ParameterInfo.ApplyAttributes (mc, MethodBuilder);
 
-                       SourceMethod source = SourceMethod.Create (parent, MethodBuilder, method.Block);
-
                        ToplevelBlock block = method.Block;
                        if (block != null) {
                                BlockContext bc = new BlockContext (mc, block, method.ReturnType);
                                if (block.Resolve (null, bc, method)) {
                                        EmitContext ec = method.CreateEmitContext (MethodBuilder.GetILGenerator ());
-                                       if (!ec.HasReturnLabel && bc.HasReturnLabel) {
-                                               ec.ReturnLabel = bc.ReturnLabel;
-                                               ec.HasReturnLabel = true;
-                                       }
+
+                                       SourceMethod source = SourceMethod.Create (parent, MethodBuilder);
 
                                        block.Emit (ec);
-                               }
-                       }
 
-                       if (source != null) {
-                               method.EmitExtraSymbolInfo (source);
-                               source.CloseMethod ();
+                                       if (source != null)
+                                               source.CloseMethod ();
+                               }
                        }
                }
        }
@@ -1983,14 +2126,18 @@ namespace Mono.CSharp {
 
                public static readonly string MetadataName = "Finalize";
 
-               public Destructor (DeclSpace parent, Modifiers mod, ParametersCompiled parameters, Attributes attrs, Location l)
-                       : base (parent, null, null, mod, AllowedModifiers,
-                               new MemberName (MetadataName, l), attrs, parameters)
+               public Destructor (TypeDefinition parent, Modifiers mod, ParametersCompiled parameters, Attributes attrs, Location l)
+                       : base (parent, null, mod, AllowedModifiers, new MemberName (MetadataName, l), attrs, parameters)
                {
                        ModFlags &= ~Modifiers.PRIVATE;
                        ModFlags |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
                }
 
+               public override void Accept (StructuralVisitor visitor)
+               {
+                       visitor.Visit (this);
+               }
+
                public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
                {
                        if (a.Type == pa.Conditional) {
@@ -2020,15 +2167,19 @@ namespace Mono.CSharp {
                                MethodGroupExpr method_expr = MethodGroupExpr.CreatePredefined (base_dtor, base_type, Location);
                                method_expr.InstanceExpression = new BaseThis (base_type, Location);
 
-                               var try_block = new ExplicitBlock (block, block.StartLocation, block.EndLocation);
-                               var finaly_block = new ExplicitBlock (block, Location, Location);
+                               var try_block = new ExplicitBlock (block, block.StartLocation, block.EndLocation) {
+                                       IsCompilerGenerated = true
+                               };
+                               var finaly_block = new ExplicitBlock (block, Location, Location) {
+                                       IsCompilerGenerated = true
+                               };
 
                                //
                                // 0-size arguments to avoid CS0250 error
                                // TODO: Should use AddScopeStatement or something else which emits correct
                                // debugger scope
                                //
-                               finaly_block.AddStatement (new StatementExpression (new Invocation (method_expr, new Arguments (0))));
+                               finaly_block.AddStatement (new StatementExpression (new Invocation (method_expr, new Arguments (0)), Location.Null));
 
                                var tf = new TryFinally (try_block, finaly_block, Location);
                                block.WrapIntoDestructor (tf, try_block);
@@ -2044,7 +2195,7 @@ namespace Mono.CSharp {
 
                protected override bool ResolveMemberType ()
                {
-                       member_type = TypeManager.void_type;
+                       member_type = Compiler.BuiltinTypes.Void;
                        return true;
                }
 
@@ -2074,7 +2225,7 @@ namespace Mono.CSharp {
 
                static MemberName SetupName (string prefix, InterfaceMemberBase member, Location loc)
                {
-                       return new MemberName (member.MemberName.Left, prefix + member.ShortName, loc);
+                       return new MemberName (member.MemberName.Left, prefix + member.ShortName, member.MemberName.ExplicitInterface, loc);
                }
 
                public void UpdateName (InterfaceMemberBase member)
@@ -2105,17 +2256,17 @@ namespace Mono.CSharp {
                        return new EmitContext (this, ig, ReturnType);
                }
 
+               public bool IsAccessor {
+                       get {
+                               return true;
+                       }
+               }
+
                public bool IsExcluded ()
                {
                        return false;
                }
 
-               GenericMethod IMethodData.GenericMethod {
-                       get {
-                               return null;
-                       }
-               }
-
                public MemberName MethodName {
                        get {
                                return MemberName;
@@ -2174,7 +2325,7 @@ namespace Mono.CSharp {
                        throw new NotSupportedException ();
                }
 
-               public virtual void Emit (DeclSpace parent)
+               public virtual void Emit (TypeDefinition parent)
                {
                        method_data.Emit (parent);
 
@@ -2183,7 +2334,7 @@ namespace Mono.CSharp {
                        if (((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0))
                                Module.PredefinedAttributes.DebuggerHidden.EmitAttribute (method_data.MethodBuilder);
 
-                       if (ReturnType == InternalType.Dynamic) {
+                       if (ReturnType.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
                                return_attributes = new ReturnParameter (this, method_data.MethodBuilder, Location);
                                Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder);
                        } else if (ReturnType.HasDynamicElement) {
@@ -2222,6 +2373,12 @@ namespace Mono.CSharp {
                        return false;
                }
 
+               public override string GetSignatureForDocumentation ()
+               {
+                       // should not be called
+                       throw new NotSupportedException ();
+               }
+
                public override bool IsClsComplianceRequired()
                {
                        return false;
@@ -2235,9 +2392,6 @@ namespace Mono.CSharp {
                public override string DocCommentHeader {
                        get { throw new InvalidOperationException ("Unexpected attempt to get doc comment from " + this.GetType () + "."); }
                }
-
-               void IMethodData.EmitExtraSymbolInfo (SourceMethod source)
-               { }
        }
 
        public class Operator : MethodOrOperator {
@@ -2323,17 +2477,20 @@ namespace Mono.CSharp {
                        names [(int) OpType.Implicit] = new string [] { "implicit", "op_Implicit" };
                        names [(int) OpType.Explicit] = new string [] { "explicit", "op_Explicit" };
                }
-               
-               public Operator (DeclSpace parent, OpType type, FullNamedExpression ret_type,
-                                Modifiers mod_flags, ParametersCompiled parameters,
+
+               public Operator (TypeDefinition parent, OpType type, FullNamedExpression ret_type, Modifiers mod_flags, ParametersCompiled parameters,
                                 ToplevelBlock block, Attributes attrs, Location loc)
-                       : base (parent, null, ret_type, mod_flags, AllowedModifiers,
-                               new MemberName (GetMetadataName (type), loc), attrs, parameters)
+                       : base (parent, ret_type, mod_flags, AllowedModifiers, new MemberName (GetMetadataName (type), loc), attrs, parameters)
                {
                        OperatorType = type;
                        Block = block;
                }
 
+               public override void Accept (StructuralVisitor visitor)
+               {
+                       visitor.Visit (this);
+               }
+
                public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
                {
                        if (a.Type == pa.Conditional) {
@@ -2359,7 +2516,7 @@ namespace Mono.CSharp {
                                // Current method is turned into automatically generated
                                // wrapper which creates an instance of iterator
                                //
-                               Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags, Compiler);
+                               Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
                                ModFlags |= Modifiers.DEBUGGER_HIDDEN;
                        }
 
@@ -2374,12 +2531,12 @@ namespace Mono.CSharp {
                        TypeSpec first_arg_type = ParameterTypes [0];
                        
                        TypeSpec first_arg_type_unwrap = first_arg_type;
-                       if (TypeManager.IsNullableType (first_arg_type))
-                               first_arg_type_unwrap = TypeManager.GetTypeArguments (first_arg_type) [0];
+                       if (first_arg_type.IsNullableType)
+                               first_arg_type_unwrap = Nullable.NullableInfo.GetUnderlyingType (first_arg_type);
                        
                        TypeSpec return_type_unwrap = return_type;
-                       if (TypeManager.IsNullableType (return_type))
-                               return_type_unwrap = TypeManager.GetTypeArguments (return_type) [0];
+                       if (return_type.IsNullableType)
+                               return_type_unwrap = Nullable.NullableInfo.GetUnderlyingType (return_type);
 
                        //
                        // Rules for conversion operators
@@ -2402,7 +2559,7 @@ namespace Mono.CSharp {
                                        return false;
                                }
 
-                               if (conv_type == InternalType.Dynamic) {
+                               if (conv_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
                                        Report.Error (1964, Location,
                                                "User-defined conversion `{0}' cannot convert to or from the dynamic type",
                                                GetSignatureForError ());
@@ -2430,7 +2587,7 @@ namespace Mono.CSharp {
                                        }
                                }
                        } else if (OperatorType == OpType.LeftShift || OperatorType == OpType.RightShift) {
-                               if (first_arg_type != declaring_type || parameters.Types[1] != TypeManager.int32_type) {
+                               if (first_arg_type != declaring_type || parameters.Types[1].BuiltinType != BuiltinTypeSpec.Type.Int) {
                                        Report.Error (564, Location, "Overloaded shift operator must have the type of the first operand be the containing type, and the type of the second operand must be int");
                                        return false;
                                }
@@ -2457,7 +2614,7 @@ namespace Mono.CSharp {
                                }
 
                                if (OperatorType == OpType.True || OperatorType == OpType.False) {
-                                       if (return_type != TypeManager.bool_type) {
+                                       if (return_type.BuiltinType != BuiltinTypeSpec.Type.Bool) {
                                                Report.Error (
                                                        215, Location,
                                                        "The return type of operator True or False " +
@@ -2470,8 +2627,8 @@ namespace Mono.CSharp {
                                // Checks for Binary operators
 
                                var second_arg_type = ParameterTypes[1];
-                               if (TypeManager.IsNullableType (second_arg_type))
-                                       second_arg_type = TypeManager.GetTypeArguments (second_arg_type)[0];
+                               if (second_arg_type.IsNullableType)
+                                       second_arg_type = Nullable.NullableInfo.GetUnderlyingType (second_arg_type);
 
                                if (second_arg_type != declaring_type) {
                                        Report.Error (563, Location,
@@ -2492,7 +2649,7 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               protected override MemberSpec FindBaseMember (out MemberSpec bestCandidate)
+               protected override MemberSpec FindBaseMember (out MemberSpec bestCandidate, ref bool overrides)
                {
                        // Operator cannot be override
                        bestCandidate = null;
@@ -2561,12 +2718,23 @@ namespace Mono.CSharp {
                        }
                }
 
+               public override string GetSignatureForDocumentation ()
+               {
+                       string s = base.GetSignatureForDocumentation ();
+                       if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
+                               s = s + "~" + ReturnType.GetSignatureForDocumentation ();
+                       }
+
+                       return s;
+               }
+
                public override string GetSignatureForError ()
                {
                        StringBuilder sb = new StringBuilder ();
                        if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
                                sb.AppendFormat ("{0}.{1} operator {2}",
-                                       Parent.GetSignatureForError (), GetName (OperatorType), type_expr.GetSignatureForError ());
+                                       Parent.GetSignatureForError (), GetName (OperatorType),
+                                       member_type == null ? type_expr.GetSignatureForError () : member_type.GetSignatureForError ());
                        }
                        else {
                                sb.AppendFormat ("{0}.operator {1}", Parent.GetSignatureForError (), GetName (OperatorType));