Fix effective base class for value types
[mono.git] / mcs / mcs / method.cs
index 2932ae27837bc9a3c08adf772539b8eb3e1ba96f..a5d7bb24a8a41a736800a454dd89cb965d68e7fb 100644 (file)
 
 using System;
 using System.Collections.Generic;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Runtime.CompilerServices;
-using System.Runtime.InteropServices;
 using System.Security;
 using System.Security.Permissions;
 using System.Text;
@@ -28,6 +24,18 @@ using XmlElement = System.Object;
 using System.Xml;
 #endif
 
+#if STATIC
+using MetaType = IKVM.Reflection.Type;
+using SecurityType = System.Collections.Generic.List<IKVM.Reflection.Emit.CustomAttributeBuilder>;
+using IKVM.Reflection;
+using IKVM.Reflection.Emit;
+#else
+using MetaType = System.Type;
+using SecurityType = System.Collections.Generic.Dictionary<System.Security.Permissions.SecurityAction, System.Security.PermissionSet>;
+using System.Reflection;
+using System.Reflection.Emit;
+#endif
+
 using Mono.CompilerServices.SymbolWriter;
 
 namespace Mono.CSharp {
@@ -46,6 +54,12 @@ namespace Mono.CSharp {
                        this.parameters = parameters;
                }
 
+               public override Variance ExpectedMemberTypeVariance {
+                       get {
+                               return Variance.Covariant;
+                       }
+               }
+
                //
                //  Returns the System.Type array for the parameters of this method
                //
@@ -113,44 +127,44 @@ 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)
                {
-                       if (overload is MethodCore || overload is AbstractPropertyEventMethod) {
+                       if (overload is MethodCore) {
                                caching_flags |= Flags.MethodOverloadsExist;
                                return true;
                        }
 
+                       if (overload is AbstractPropertyEventMethod)
+                               return true;
+
                        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; }
                }
@@ -182,9 +196,9 @@ namespace Mono.CSharp {
 //             MethodInfo MakeGenericMethod (TypeSpec[] targs);
        }
 
-       public class MethodSpec : MemberSpec, IParametersMember
+       public sealed class MethodSpec : MemberSpec, IParametersMember
        {
-               MethodBase metaInfo;
+               MethodBase metaInfo, inflatedMetaInfo;
                AParametersCollection parameters;
                TypeSpec returnType;
 
@@ -290,28 +304,76 @@ namespace Mono.CSharp {
 
                public MethodBase GetMetaInfo ()
                {
-                       if ((state & StateFlags.PendingMetaInflate) != 0) {
-                               if (DeclaringType.IsTypeBuilder) {
-                                       if (IsConstructor)
-                                               metaInfo = TypeBuilder.GetConstructor (DeclaringType.GetMetaInfo (), (ConstructorInfo) metaInfo);
-                                       else
-                                               metaInfo = TypeBuilder.GetMethod (DeclaringType.GetMetaInfo (), (MethodInfo) metaInfo);
+                       //
+                       // inflatedMetaInfo is extra field needed for cases where we
+                       // inflate method but another nested type can later inflate
+                       // again (the cache would be build with inflated metaInfo) and
+                       // TypeBuilder can work with method definitions only
+                       //
+                       if (inflatedMetaInfo == null) {
+                               if ((state & StateFlags.PendingMetaInflate) != 0) {
+                                       var dt_meta = DeclaringType.GetMetaInfo ();
+
+                                       if (DeclaringType.IsTypeBuilder) {
+                                               if (IsConstructor)
+                                                       inflatedMetaInfo = TypeBuilder.GetConstructor (dt_meta, (ConstructorInfo) metaInfo);
+                                               else
+                                                       inflatedMetaInfo = TypeBuilder.GetMethod (dt_meta, (MethodInfo) metaInfo);
+                                       } else {
+#if STATIC
+                                               // it should not be reached
+                                               throw new NotImplementedException ();
+#else
+                                               inflatedMetaInfo = MethodInfo.GetMethodFromHandle (metaInfo.MethodHandle, dt_meta.TypeHandle);
+#endif
+                                       }
+
+                                       state &= ~StateFlags.PendingMetaInflate;
                                } else {
-                                       metaInfo = MethodInfo.GetMethodFromHandle (metaInfo.MethodHandle, DeclaringType.GetMetaInfo ().TypeHandle);
+                                       inflatedMetaInfo = metaInfo;
                                }
-
-                               state &= ~StateFlags.PendingMetaInflate;
                        }
 
                        if ((state & StateFlags.PendingMakeMethod) != 0) {
-                               metaInfo = ((MethodInfo) metaInfo).MakeGenericMethod (targs.Select (l => l.GetMetaInfo ()).ToArray ());
+                               var sre_targs = new MetaType[targs.Length];
+                               for (int i = 0; i < sre_targs.Length; ++i)
+                                       sre_targs[i] = targs[i].GetMetaInfo ();
+
+                               inflatedMetaInfo = ((MethodInfo) inflatedMetaInfo).MakeGenericMethod (sre_targs);
                                state &= ~StateFlags.PendingMakeMethod;
                        }
 
-                       if (Kind == MemberKind.FakeMethod)
-                               throw new InternalErrorException ("Emitting fake method");
+                       return inflatedMetaInfo;
+               }
 
-                       return metaInfo;
+               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 ()
@@ -354,15 +416,16 @@ namespace Mono.CSharp {
                public override MemberSpec InflateMember (TypeParameterInflator inflator)
                {
                        var ms = (MethodSpec) base.InflateMember (inflator);
+                       ms.inflatedMetaInfo = null;
                        ms.returnType = inflator.Inflate (returnType);
                        ms.parameters = parameters.Inflate (inflator);
                        if (IsGeneric)
-                               ms.constraints = TypeParameterSpec.InflateConstraints (inflator, GenericDefinition.TypeParameters);
+                               ms.constraints = TypeParameterSpec.InflateConstraints (inflator, Constraints);
 
                        return ms;
                }
 
-               public MethodSpec MakeGenericMethod (params TypeSpec[] targs)
+               public MethodSpec MakeGenericMethod (IMemberContext context, params TypeSpec[] targs)
                {
                        if (targs == null)
                                throw new ArgumentNullException ();
@@ -373,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;
@@ -406,9 +469,7 @@ namespace Mono.CSharp {
 
                        var ms = (MethodSpec) MemberwiseClone ();
                        if (decl != DeclaringType) {
-                               // Gets back MethodInfo in case of metaInfo was inflated
-                               ms.metaInfo = MemberCache.GetMember (DeclaringType.GetDefinition (), this).metaInfo;
-
+                               ms.inflatedMetaInfo = null;
                                ms.declaringType = decl;
                                ms.state |= StateFlags.PendingMetaInflate;
                        }
@@ -421,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)
@@ -434,10 +512,10 @@ namespace Mono.CSharp {
        {
                public MethodBuilder MethodBuilder;
                ReturnParameter return_attributes;
-               Dictionary<SecurityAction, PermissionSet> declarative_security;
+               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,
@@ -457,8 +535,8 @@ namespace Mono.CSharp {
                                return;
                        }
 
-                       if (a.IsInternalMethodImplAttribute) {
-                               is_external_implementation = true;
+                       if (a.Type == pa.MethodImpl) {
+                               is_external_implementation = a.IsInternalCall ();
                        }
 
                        if (a.Type == pa.DllImport) {
@@ -470,9 +548,7 @@ namespace Mono.CSharp {
                        }
 
                        if (a.IsValidSecurityAttribute ()) {
-                               if (declarative_security == null)
-                                       declarative_security = new Dictionary<SecurityAction, PermissionSet> ();
-                               a.ExtractSecurityPermissionSet (declarative_security);
+                               a.ExtractSecurityPermissionSet (ctor, ref declarative_security);
                                return;
                        }
 
@@ -576,23 +652,16 @@ namespace Mono.CSharp {
                public override void Emit ()
                {
                        if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
-                               PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (MethodBuilder);
+                               Module.PredefinedAttributes.CompilerGenerated.EmitAttribute (MethodBuilder);
                        if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
-                               PredefinedAttributes.Get.DebuggerHidden.EmitAttribute (MethodBuilder);
+                               Module.PredefinedAttributes.DebuggerHidden.EmitAttribute (MethodBuilder);
 
-                       if (ReturnType == InternalType.Dynamic) {
+                       if (ReturnType.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
                                return_attributes = new ReturnParameter (this, MethodBuilder, Location);
-                               PredefinedAttributes.Get.Dynamic.EmitAttribute (return_attributes.Builder);
-                       } else {
-                               var trans_flags = TypeManager.HasDynamicTypeUsed (ReturnType);
-                               if (trans_flags != null) {
-                                       var pa = PredefinedAttributes.Get.DynamicTransform;
-                                       if (pa.Constructor != null || pa.ResolveConstructor (Location, ArrayContainer.MakeType (TypeManager.bool_type))) {
-                                               return_attributes = new ReturnParameter (this, MethodBuilder, Location);
-                                               return_attributes.Builder.SetCustomAttribute (
-                                                       new CustomAttributeBuilder (pa.Constructor, new object [] { trans_flags }));
-                                       }
-                               }
+                               Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder);
+                       } else if (ReturnType.HasDynamicElement) {
+                               return_attributes = new ReturnParameter (this, MethodBuilder, Location);
+                               Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder, ReturnType, Location);
                        }
 
                        if (OptAttributes != null)
@@ -600,15 +669,22 @@ namespace Mono.CSharp {
 
                        if (declarative_security != null) {
                                foreach (var de in declarative_security) {
+#if STATIC
+                                       MethodBuilder.__AddDeclarativeSecurity (de);
+#else
                                        MethodBuilder.AddDeclarativeSecurity (de.Key, de.Value);
+#endif
                                }
                        }
 
-                       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;
                }
@@ -640,6 +716,12 @@ namespace Mono.CSharp {
 
                #region IMethodData Members
 
+               bool IMethodData.IsAccessor {
+                       get {
+                               return false;
+                       }
+               }
+
                public TypeSpec ReturnType {
                        get {
                                return MemberType;
@@ -670,7 +752,7 @@ namespace Mono.CSharp {
                                if (OptAttributes == null)
                                        return null;
 
-                               Attribute[] attrs = OptAttributes.SearchMulti (PredefinedAttributes.Get.Conditional);
+                               Attribute[] attrs = OptAttributes.SearchMulti (Module.PredefinedAttributes.Conditional);
                                if (attrs == null)
                                        return null;
 
@@ -718,12 +800,17 @@ namespace Mono.CSharp {
 
                public int Token {
                        get {
-                               if (method is MethodBuilder)
-                                       return ((MethodBuilder) method).GetToken ().Token;
-                               else if (method is ConstructorBuilder)
-                                       return ((ConstructorBuilder) method).GetToken ().Token;
+                               MethodToken token;
+                               var mb = method as MethodBuilder;
+                               if (mb != null)
+                                       token = mb.GetToken ();
                                else
-                                       throw new NotSupportedException ();
+                                       token = ((ConstructorBuilder) method).GetToken ();
+#if STATIC
+                               if (token.IsPseudoToken)
+                                       return ((ModuleBuilder) method.Module).ResolvePseudoToken (token.Token);
+#endif
+                               return token.Token;
                        }
                }
 
@@ -759,33 +846,15 @@ namespace Mono.CSharp {
 
        public class Method : MethodOrOperator, IGenericMethodDefinition
        {
-               /// <summary>
-               ///   Modifiers allowed in a class declaration
-               /// </summary>
-               const Modifiers AllowedModifiers =
-                       Modifiers.NEW |
-                       Modifiers.PUBLIC |
-                       Modifiers.PROTECTED |
-                       Modifiers.INTERNAL |
-                       Modifiers.PRIVATE |
-                       Modifiers.STATIC |
-                       Modifiers.VIRTUAL |
-                       Modifiers.SEALED |
-                       Modifiers.OVERRIDE |
-                       Modifiers.ABSTRACT |
-                       Modifiers.UNSAFE |
-                       Modifiers.EXTERN;
-
-               const Modifiers AllowedInterfaceModifiers = 
-                       Modifiers.NEW | Modifiers.UNSAFE;
-
                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,
-                               parent.PartialContainer.Kind == MemberKind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
+                               parent.PartialContainer.Kind == MemberKind.Interface ? AllowedModifiersInterface :
+                               parent.PartialContainer.Kind == MemberKind.Struct ? AllowedModifiersStruct :
+                               AllowedModifiersClass,
                                name, attrs, parameters)
                {
                }
@@ -796,7 +865,7 @@ namespace Mono.CSharp {
                {
                }
 
-#region Properties
+               #region Properties
 
                public override TypeParameter[] CurrentTypeParameters {
                        get {
@@ -807,25 +876,9 @@ namespace Mono.CSharp {
                        }
                }
 
-               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;
-                       }
-               }
-
                public TypeParameterSpec[] TypeParameters {
                        get {
+                               // TODO: Cache this
                                return CurrentTypeParameters.Select (l => l.Type).ToArray ();
                        }
                }
@@ -847,13 +900,12 @@ namespace Mono.CSharp {
                {
                        Report.Error (17, b.Location,
                                "Program `{0}' has more than one entry point defined: `{1}'",
-                               CodeGen.FileName, b.GetSignatureForError ());
+                               b.Module.Builder.ScopeName, b.GetSignatureForError ());
                }
 
                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)
@@ -863,11 +915,11 @@ 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;
@@ -878,7 +930,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       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)
@@ -889,13 +941,13 @@ namespace Mono.CSharp {
                                        return;
                                }
 
-                               if (ReturnType != TypeManager.void_type) {
-                                       Report.Error (578, Location, "Conditional not valid on `{0}' because its return type is not void", GetSignatureForError ());
+                               if ((ModFlags & Modifiers.OVERRIDE) != 0) {
+                                       Report.Error (243, Location, "Conditional not valid on `{0}' because it is an override method", GetSignatureForError ());
                                        return;
                                }
 
-                               if ((ModFlags & Modifiers.OVERRIDE) != 0) {
-                                       Report.Error (243, Location, "Conditional not valid on `{0}' because it is an override method", GetSignatureForError ());
+                               if (ReturnType.Kind != MemberKind.Void) {
+                                       Report.Error (578, Location, "Conditional not valid on `{0}' because its return type is not void", GetSignatureForError ());
                                        return;
                                }
 
@@ -927,42 +979,66 @@ namespace Mono.CSharp {
                        base.ApplyAttributeBuilder (a, ctor, cdata, pa);
                }
 
-               protected override bool CheckForDuplications ()
-               {
-                       if (!base.CheckForDuplications ())
-                               return false;
-
-                       var ar = Parent.PartialContainer.Properties;
-                       if (ar != null) {
-                               for (int i = 0; i < ar.Count; ++i) {
-                                       PropertyBase pb = (PropertyBase) ar [i];
-                                       if (pb.AreAccessorsDuplicateImplementation (this))
-                                               return false;
-                               }
-                       }
-
-                       ar = Parent.PartialContainer.Indexers;
-                       if (ar != null) {
-                               for (int i = 0; i < ar.Count; ++i) {
-                                       PropertyBase pb = (PropertyBase) ar [i];
-                                       if (pb.AreAccessorsDuplicateImplementation (this))
-                                               return false;
-                               }
-                       }
-
-                       return true;
-               }
-
                protected virtual void DefineTypeParameters ()
                {
                        var tparams = CurrentTypeParameters;
 
                        TypeParameterSpec[] base_tparams = null;
+                       TypeParameterSpec[] base_decl_tparams = TypeParameterSpec.EmptyTypes;
+                       TypeSpec[] base_targs = TypeSpec.EmptyTypes;
                        if (((ModFlags & Modifiers.OVERRIDE) != 0 || IsExplicitImpl)) {
-                               if (base_method != null)
+                               if (base_method != null) {
                                        base_tparams = base_method.GenericDefinition.TypeParameters;
-                               else if (MethodData.implementing != null)
+                               
+                                       if (base_method.DeclaringType.IsGeneric) {
+                                               base_decl_tparams = base_method.DeclaringType.MemberDefinition.TypeParameters;
+
+                                               var base_type_parent = CurrentType;
+                                               while (base_type_parent.BaseType != base_method.DeclaringType) {
+                                                       base_type_parent = base_type_parent.BaseType;
+                                               }
+
+                                               base_targs = base_type_parent.BaseType.TypeArguments;
+                                       }
+
+                                       if (base_method.IsGeneric) {
+                                               ObsoleteAttribute oa;
+                                               foreach (var base_tp in base_tparams) {
+                                                       oa = base_tp.BaseType.GetAttributeObsolete ();
+                                                       if (oa != null) {
+                                                               AttributeTester.Report_ObsoleteMessage (oa, base_tp.BaseType.GetSignatureForError (), Location, Report);
+                                                       }
+
+                                                       if (base_tp.InterfacesDefined != null) {
+                                                               foreach (var iface in base_tp.InterfacesDefined) {
+                                                                       oa = iface.GetAttributeObsolete ();
+                                                                       if (oa != null) {
+                                                                               AttributeTester.Report_ObsoleteMessage (oa, iface.GetSignatureForError (), Location, Report);
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+
+                                               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 ();
+                                               } else {
+                                                       base_decl_tparams = base_tparams;
+                                                       base_targs = tparams.Select (l => l.Type).ToArray ();
+                                               }
+                                       }
+                               } else if (MethodData.implementing != null) {
                                        base_tparams = MethodData.implementing.GenericDefinition.TypeParameters;
+                                       if (MethodData.implementing.DeclaringType.IsGeneric) {
+                                               base_decl_tparams = MethodData.implementing.DeclaringType.MemberDefinition.TypeParameters;
+                                               foreach (var iface in Parent.CurrentType.Interfaces) {
+                                                       if (iface == MethodData.implementing.DeclaringType) {
+                                                               base_targs = iface.TypeArguments;
+                                                               break;
+                                                       }
+                                               }
+                                       }
+                               }
                        }
 
                        for (int i = 0; i < tparams.Length; ++i) {
@@ -976,13 +1052,38 @@ namespace Mono.CSharp {
                                //
                                if (base_tparams != null) {
                                        var base_tparam = base_tparams[i];
-                                       tp.Type.SpecialConstraint = base_tparam.SpecialConstraint;
-                                       tp.Type.TypeArguments = base_tparam.TypeArguments;
+                                       var local_tparam = tp.Type;
+                                       local_tparam.SpecialConstraint = base_tparam.SpecialConstraint;
 
-                                       // TODO MemberCache: Inflate with different MVAR ?
-                                       tp.Type.Interfaces = base_tparam.Interfaces;
-                                       tp.Type.BaseType = base_tparam.BaseType;
-                               } else if (MethodData.implementing != null) {
+                                       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
+                                       // introduced by inflating inherited constraints in this context
+                                       //
+                                       // Conflict example:
+                                       //
+                                       // class A<T> { virtual void Foo<U> () where U : class, T {} }
+                                       // class B : A<int> { override void Foo<U> {} }
+                                       //
+                                       var local_tparam_targs = local_tparam.TypeArguments;
+                                       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);
+                                                       }
+                                               }
+                                       }
+
+                                       continue;
+                               }
+                               
+                               if (MethodData.implementing != null) {
                                        var base_tp = MethodData.implementing.Constraints[i];
                                        if (!tp.Type.HasSameConstraintsImplementation (base_tp)) {
                                                Report.SymbolRelatedToPreviousError (MethodData.implementing);
@@ -999,24 +1100,25 @@ namespace Mono.CSharp {
                //
                public override bool Define ()
                {
-                       if (type_expr == TypeManager.system_void_expr && parameters.IsEmpty && 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 (parameters.Count == 1 && ParameterTypes[0].BuiltinType == BuiltinTypeSpec.Type.Object && Name == "Equals")
                                                Parent.PartialContainer.Mark_HasEquals ();
                                        else if (parameters.IsEmpty && Name == "GetHashCode")
                                                Parent.PartialContainer.Mark_HasGetHashCode ();
@@ -1026,12 +1128,12 @@ namespace Mono.CSharp {
                                DefineTypeParameters ();
                        }
 
-                       if (block != null && block.IsIterator && !(Parent is IteratorStorey)) {
+                       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);
+                               Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
                                ModFlags |= Modifiers.DEBUGGER_HIDDEN;
                        }
 
@@ -1044,7 +1146,7 @@ namespace Mono.CSharp {
                                                Report.Error (1109, Location, "`{0}': Extension methods cannot be defined in a nested class",
                                                        GetSignatureForError ());
 
-                                       PredefinedAttribute pa = PredefinedAttributes.Get.Extension;
+                                       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",
@@ -1054,7 +1156,7 @@ namespace Mono.CSharp {
                                        ModFlags |= Modifiers.METHOD_EXTENSION;
                                        Parent.PartialContainer.ModFlags |= Modifiers.METHOD_EXTENSION;
                                        Spec.DeclaringType.SetExtensionMethodContainer ();
-                                       CodeGen.Assembly.HasExtensionMethods = true;
+                                       Parent.Module.HasExtensionMethod = true;
                                } else {
                                        Report.Error (1106, Location, "`{0}': Extension methods must be defined in a non-generic static class",
                                                GetSignatureForError ());
@@ -1064,22 +1166,19 @@ 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 && Name == "Main" && (settings.MainClass == null || settings.MainClass == Parent.TypeBuilder.FullName)) {
                                if (IsEntryPoint ()) {
-
-                                       if (RootContext.EntryPoint == null) {
+                                       if (Parent.DeclaringAssembly.EntryPoint == null) {
                                                if (Parent.IsGeneric || MemberName.IsGeneric) {
                                                        Report.Warning (402, 4, Location, "`{0}': an entry point cannot be generic or in a generic type",
                                                                GetSignatureForError ());
                                                } else {
                                                        SetIsUsed ();
-                                                       RootContext.EntryPoint = this;
+                                                       Parent.DeclaringAssembly.EntryPoint = this;
                                                }
                                        } else {
-                                               Error_DuplicateEntryPoint (RootContext.EntryPoint);
+                                               Error_DuplicateEntryPoint (Parent.DeclaringAssembly.EntryPoint);
                                                Error_DuplicateEntryPoint (this);
                                        }
                                } else {
@@ -1097,34 +1196,26 @@ namespace Mono.CSharp {
                public override void Emit ()
                {
                        try {
-                               Report.Debug (64, "METHOD EMIT", this, MethodBuilder, Location, Block, MethodData);
                                if (IsPartialDefinition) {
                                        //
                                        // Use partial method implementation builder for partial method declaration attributes
                                        //
                                        if (partialMethodImplementation != null) {
                                                MethodBuilder = partialMethodImplementation.MethodBuilder;
-                                               return;
                                        }
-                               } else if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.PartialDefinitionExists) == 0) {
+
+                                       return;
+                               }
+                               
+                               if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.PartialDefinitionExists) == 0) {
                                        Report.Error (759, Location, "A partial method `{0}' implementation is missing a partial method declaration",
                                                GetSignatureForError ());
                                }
 
                                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) {
                                                var tp = CurrentTypeParameters [i];
-                                               tp.CheckGenericConstraints ();
+                                               tp.CheckGenericConstraints (false);
                                                tp.Emit ();
                                        }
                                }
@@ -1132,7 +1223,7 @@ namespace Mono.CSharp {
                                base.Emit ();
                                
                                if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
-                                       PredefinedAttributes.Get.Extension.EmitAttribute (MethodBuilder);
+                                       Module.PredefinedAttributes.Extension.EmitAttribute (MethodBuilder);
                        } catch {
                                Console.WriteLine ("Internal compiler error at {0}: exception caught while emitting {1}",
                                                   Location, MethodBuilder);
@@ -1193,7 +1284,7 @@ namespace Mono.CSharp {
        public abstract class ConstructorInitializer : ExpressionStatement
        {
                Arguments argument_list;
-               MethodGroupExpr base_constructor_group;
+               MethodSpec base_ctor;
 
                public ConstructorInitializer (Arguments argument_list, Location loc)
                {
@@ -1219,65 +1310,47 @@ namespace Mono.CSharp {
                        // FIXME: Hack
                        var caller_builder = (Constructor) ec.MemberContext;
 
-                       if (argument_list != null) {
-                               bool dynamic;
-
-                               //
-                               // Spec mandates that constructor initializer will not have `this' access
-                               //
-                               using (ec.Set (ResolveContext.Options.BaseInitializer)) {
+                       //
+                       // Spec mandates that constructor initializer will not have `this' access
+                       //
+                       using (ec.Set (ResolveContext.Options.BaseInitializer)) {
+                               if (argument_list != null) {
+                                       bool dynamic;
                                        argument_list.Resolve (ec, out dynamic);
-                               }
 
-                               if (dynamic) {
-                                       ec.Report.Error (1975, loc,
-                                               "The constructor call cannot be dynamically dispatched within constructor initializer");
+                                       if (dynamic) {
+                                               ec.Report.Error (1975, loc,
+                                                       "The constructor call cannot be dynamically dispatched within constructor initializer");
 
-                                       return null;
+                                               return null;
+                                       }
                                }
-                       }
 
-                       type = ec.CurrentType;
-                       if (this is ConstructorBaseInitializer) {
-                               if (ec.CurrentType.BaseType == null)
-                                       return this;
+                               type = ec.CurrentType;
+                               if (this is ConstructorBaseInitializer) {
+                                       if (ec.CurrentType.BaseType == null)
+                                               return this;
 
-                               type = ec.CurrentType.BaseType;
-                               if (ec.CurrentType.IsStruct) {
-                                       ec.Report.Error (522, loc,
-                                               "`{0}': Struct constructors cannot call base constructors", caller_builder.GetSignatureForError ());
-                                       return this;
+                                       type = ec.CurrentType.BaseType;
+                                       if (ec.CurrentType.IsStruct) {
+                                               ec.Report.Error (522, loc,
+                                                       "`{0}': Struct constructors cannot call base constructors", caller_builder.GetSignatureForError ());
+                                               return this;
+                                       }
+                               } else {
+                                       //
+                                       // It is legal to have "this" initializers that take no arguments
+                                       // in structs, they are just no-ops.
+                                       //
+                                       // struct D { public D (int a) : this () {}
+                                       //
+                                       if (ec.CurrentType.IsStruct && argument_list == null)
+                                               return this;
                                }
-                       } else {
-                               //
-                               // It is legal to have "this" initializers that take no arguments
-                               // in structs, they are just no-ops.
-                               //
-                               // struct D { public D (int a) : this () {}
-                               //
-                               if (TypeManager.IsStruct (ec.CurrentType) && argument_list == null)
-                                       return this;                    
-                       }
-
-                       base_constructor_group = MemberLookupFinal (
-                               ec, null, type, ConstructorBuilder.ConstructorName, 0, MemberKind.Constructor,
-                               BindingRestriction.AccessibleOnly | BindingRestriction.DeclaredOnly,
-                               loc) as MethodGroupExpr;
-                       
-                       if (base_constructor_group == null)
-                               return this;
-                       
-                       base_constructor_group = base_constructor_group.OverloadResolve (
-                               ec, ref argument_list, false, loc);
-                       
-                       if (base_constructor_group == null)
-                               return this;
-
-                       if (!ec.IsStatic)
-                               base_constructor_group.InstanceExpression = ec.GetThis (loc);
-                       
-                       var base_ctor = base_constructor_group.BestCandidate;
 
+                               base_ctor = ConstructorLookup (ec, type, ref argument_list, loc);
+                       }
+       
                        // TODO MemberCache: Does it work for inflated types ?
                        if (base_ctor == caller_builder.Spec){
                                ec.Report.Error (516, loc, "Constructor `{0}' cannot call itself",
@@ -1290,12 +1363,12 @@ namespace Mono.CSharp {
                public override void Emit (EmitContext ec)
                {
                        // It can be null for static initializers
-                       if (base_constructor_group == null)
+                       if (base_ctor == null)
                                return;
                        
                        ec.Mark (loc);
 
-                       base_constructor_group.EmitCall (ec, argument_list);
+                       Invocation.EmitCall (ec, new CompilerGeneratedThis (type, loc), base_ctor, argument_list, loc);
                }
 
                public override void EmitStatement (EmitContext ec)
@@ -1328,7 +1401,7 @@ namespace Mono.CSharp {
        public class Constructor : MethodCore, IMethodData {
                public ConstructorBuilder ConstructorBuilder;
                public ConstructorInitializer Initializer;
-               Dictionary<SecurityAction, PermissionSet> declarative_security;
+               SecurityType declarative_security;
                bool has_compliant_args;
 
                // <summary>
@@ -1345,6 +1418,9 @@ namespace Mono.CSharp {
 
                static readonly string[] attribute_targets = new string [] { "method" };
 
+               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.
@@ -1362,7 +1438,15 @@ namespace Mono.CSharp {
                }
 
                public override AttributeTargets AttributeTargets {
-                       get { return AttributeTargets.Constructor; }
+                       get {
+                               return AttributeTargets.Constructor;
+                       }
+               }
+
+               bool IMethodData.IsAccessor {
+                       get {
+                               return false;
+                       }
                }
 
                //
@@ -1381,15 +1465,12 @@ namespace Mono.CSharp {
                public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
                {
                        if (a.IsValidSecurityAttribute ()) {
-                               if (declarative_security == null) {
-                                       declarative_security = new Dictionary<SecurityAction, PermissionSet> ();
-                               }
-                               a.ExtractSecurityPermissionSet (declarative_security);
+                               a.ExtractSecurityPermissionSet (ctor, ref declarative_security);
                                return;
                        }
 
-                       if (a.IsInternalMethodImplAttribute) {
-                               is_external_implementation = true;
+                       if (a.Type == pa.MethodImpl) {
+                               is_external_implementation = a.IsInternalCall ();
                        }
 
                        ConstructorBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
@@ -1453,14 +1534,14 @@ namespace Mono.CSharp {
                                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;
@@ -1476,16 +1557,21 @@ namespace Mono.CSharp {
                                        Report.Error (669, Location, "`{0}': A class with the ComImport attribute cannot have a user-defined constructor",
                                                Parent.GetSignatureForError ());
                                }
+
+                               // Set as internal implementation and reset block data
+                               // to ensure no IL is generated
                                ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall);
+                               block = null;
                        }
 
                        if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
-                               PredefinedAttributes.Get.DebuggerHidden.EmitAttribute (ConstructorBuilder);
+                               Module.PredefinedAttributes.DebuggerHidden.EmitAttribute (ConstructorBuilder);
 
                        if (OptAttributes != null)
                                OptAttributes.Emit ();
 
                        base.Emit ();
+                       parameters.ApplyAttributes (this, ConstructorBuilder);
 
                        //
                        // If we use a "this (...)" constructor initializer, then
@@ -1494,7 +1580,7 @@ namespace Mono.CSharp {
                        bool emit_field_initializers = ((ModFlags & Modifiers.STATIC) != 0) ||
                                !(Initializer is ConstructorThisInitializer);
 
-                       BlockContext bc = new BlockContext (this, block, TypeManager.void_type);
+                       BlockContext bc = new BlockContext (this, block, Compiler.BuiltinTypes.Void);
                        bc.Set (ResolveContext.Options.ConstructorScope);
 
                        if (emit_field_initializers)
@@ -1505,7 +1591,7 @@ namespace Mono.CSharp {
                                // initializer, it must initialize all of the struct's fields.
                                if ((Parent.PartialContainer.Kind == MemberKind.Struct) &&
                                        ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
-                                       block.AddThisVariable (Parent, Location);
+                                       block.AddThisVariable (bc, Parent, Location);
 
                                if (block != null && (ModFlags & Modifiers.STATIC) == 0){
                                        if (Parent.PartialContainer.Kind == MemberKind.Class && Initializer == null)
@@ -1517,12 +1603,10 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       parameters.ApplyAttributes (ConstructorBuilder);
-
                        SourceMethod source = SourceMethod.Create (Parent, ConstructorBuilder, block);
 
                        if (block != null) {
-                               if (block.Resolve (null, bc, parameters, this)) {
+                               if (block.Resolve (null, bc, this)) {
                                        EmitContext ec = new EmitContext (this, ConstructorBuilder.GetILGenerator (), bc.ReturnType);
                                        ec.With (EmitContext.Options.ConstructorScope, true);
 
@@ -1540,20 +1624,29 @@ namespace Mono.CSharp {
 
                        if (declarative_security != null) {
                                foreach (var de in declarative_security) {
+#if STATIC
+                                       ConstructorBuilder.__AddDeclarativeSecurity (de);
+#else
                                        ConstructorBuilder.AddDeclarativeSecurity (de.Key, de.Value);
+#endif
                                }
                        }
 
                        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 ();
@@ -1622,7 +1715,7 @@ namespace Mono.CSharp {
        /// <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; }
@@ -1631,20 +1724,24 @@ namespace Mono.CSharp {
                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;
+#endif
+
                public readonly IMethodData method;
 
                public readonly GenericMethod GenericMethod;
@@ -1702,24 +1799,25 @@ namespace Mono.CSharp {
                        TypeContainer container = parent.PartialContainer;
 
                        PendingImplementation pending = container.PendingImplementations;
-                       if (pending != null){
-                               implementing = pending.IsInterfaceMethod (method.MethodName, member.InterfaceType, this);
+                       MethodSpec ambig_iface_method;
+                       if (pending != null) {
+                               implementing = pending.IsInterfaceMethod (method.MethodName, member.InterfaceType, this, out ambig_iface_method);
 
-                               if (member.InterfaceType != null){
-                                       if (implementing == null){
+                               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 ('.')));
+                                                                         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);
+                                                                         "`{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)) {
+                                       if (implementing.IsAccessor && !method.IsAccessor) {
                                                Report.SymbolRelatedToPreviousError (implementing);
                                                Report.Error (683, method.Location, "`{0}' explicit method implementation cannot implement `{1}' because it is an accessor",
                                                        member.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
@@ -1727,8 +1825,7 @@ namespace Mono.CSharp {
                                        }
                                } 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}'",
@@ -1739,18 +1836,19 @@ namespace Mono.CSharp {
                                                                Report.SymbolRelatedToPreviousError (implementing);
                                                                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 ());
-                                                               return false;
-                                                       }
-                                                       PropertyBase.PropertyMethod pm = prop_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}'",
-                                                                       method.GetSignatureForError (), implementing.GetSignatureForError ());
-                                                               return false;
+                                                       } else {
+                                                               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}'",
+                                                                               method.GetSignatureForError (), implementing.GetSignatureForError ());
+                                                               }
                                                        }
                                                }
                                        }
                                }
+                       } else {
+                               ambig_iface_method = null;
                        }
 
                        //
@@ -1770,8 +1868,16 @@ namespace Mono.CSharp {
                                                Report.SymbolRelatedToPreviousError (implementing);
                                                Report.Error (466, method.Location, "`{0}': the explicit interface implementation cannot introduce the params modifier",
                                                        method.GetSignatureForError ());
-                                               return false;
                                        }
+
+                                       if (ambig_iface_method != null) {
+                                               Report.SymbolRelatedToPreviousError (ambig_iface_method);
+                                               Report.SymbolRelatedToPreviousError (implementing);
+                                               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 {
                                        if (implementing.DeclaringType.IsInterface) {
                                                //
@@ -1814,13 +1920,24 @@ namespace Mono.CSharp {
                                        if ((modifiers & Modifiers.OVERRIDE) == 0)
                                                flags |= MethodAttributes.NewSlot;
                                }
-                               flags |=
-                                       MethodAttributes.Virtual |
-                                       MethodAttributes.HideBySig;
+
+                               flags |= MethodAttributes.Virtual | MethodAttributes.HideBySig;
 
                                // Set Final unless we're virtual, abstract or already overriding a method.
                                if ((modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) == 0)
                                        flags |= MethodAttributes.Final;
+
+                               //
+                               // clear the pending implementation flag (requires explicit methods to be defined first)
+                               //
+                               parent.PartialContainer.PendingImplementations.ImplementMethod (method.MethodName,
+                                       member.InterfaceType, this, member.IsExplicitImpl, out ambig_iface_method);
+
+                               //
+                               // Update indexer accessor name to match implementing abstract accessor
+                               //
+                               if (!implementing.DeclaringType.IsInterface && !member.IsExplicitImpl && implementing.IsAccessor)
+                                       method_full_name = implementing.MemberDefinition.Name;
                        }
 
                        DefineMethodBuilder (container, method_full_name, method.ParameterInfo);
@@ -1863,6 +1980,9 @@ namespace Mono.CSharp {
                        builder.SetParameters (p_types);
                        builder.SetReturnType (return_type);
                        if (builder.Attributes != flags) {
+#if STATIC
+                               builder.__SetAttributes (flags);
+#else
                                try {
                                        if (methodbuilder_attrs_field == null)
                                                methodbuilder_attrs_field = typeof (MethodBuilder).GetField ("attrs", BindingFlags.NonPublic | BindingFlags.Instance);
@@ -1870,6 +1990,7 @@ namespace Mono.CSharp {
                                } catch {
                                        container.Compiler.Report.RuntimeMissingSupport (method.Location, "Generic method MethodAttributes");
                                }
+#endif
                        }
                }
 
@@ -1881,21 +2002,16 @@ namespace Mono.CSharp {
                        if (GenericMethod != null)
                                GenericMethod.EmitAttributes ();
 
-                       method.ParameterInfo.ApplyAttributes (MethodBuilder);
+                       var mc = (IMemberContext) method;
 
-                       //
-                       // clear the pending implementation flag
-                       //
-                       if (implementing != null)
-                               parent.PartialContainer.PendingImplementations.ImplementMethod (method.MethodName,
-                                       member.InterfaceType, this, member.IsExplicitImpl);
+                       method.ParameterInfo.ApplyAttributes (mc, MethodBuilder);
 
                        SourceMethod source = SourceMethod.Create (parent, MethodBuilder, method.Block);
 
                        ToplevelBlock block = method.Block;
                        if (block != null) {
-                               BlockContext bc = new BlockContext ((IMemberContext) method, block, method.ReturnType);
-                               if (block.Resolve (null, bc, method.ParameterInfo, method)) {
+                               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;
@@ -1924,7 +2040,7 @@ namespace Mono.CSharp {
                public static readonly string MetadataName = "Finalize";
 
                public Destructor (DeclSpace parent, Modifiers mod, ParametersCompiled parameters, Attributes attrs, Location l)
-                       : base (parent, null, TypeManager.system_void_expr, mod, AllowedModifiers,
+                       : base (parent, null, null, mod, AllowedModifiers,
                                new MemberName (MetadataName, l), attrs, parameters)
                {
                        ModFlags &= ~Modifiers.PRIVATE;
@@ -1943,26 +2059,25 @@ namespace Mono.CSharp {
 
                protected override bool CheckBase ()
                {
-                       if (!base.CheckBase ())
-                               return false;
+                       // Don't check base, destructors have special syntax
+                       return true;
+               }
 
+               public override void Emit()
+               {
                        var base_type = Parent.PartialContainer.BaseType;
-                       if (base_type == null)
-                               return true;
+                       if (base_type != null && Block != null) {
+                               var base_dtor = MemberCache.FindMember (base_type,
+                                       new MemberFilter (MetadataName, 0, MemberKind.Destructor, null, null), BindingRestriction.InstanceOnly) as MethodSpec;
 
-                       if (Block != null) {
-                               MethodGroupExpr method_expr = Expression.MethodLookup (Parent.Module.Compiler, Parent.Definition, base_type, MemberKind.Destructor, MetadataName, 0, Location);
-                               if (method_expr == null)
+                               if (base_dtor == null)
                                        throw new NotImplementedException ();
 
-                               method_expr.IsBase = true;
-                               method_expr.InstanceExpression = new CompilerGeneratedThis (Parent.Definition, Location);
-
-                               ToplevelBlock new_block = new ToplevelBlock (Compiler, Block.StartLocation);
-                               new_block.EndLocation = Block.EndLocation;
+                               MethodGroupExpr method_expr = MethodGroupExpr.CreatePredefined (base_dtor, base_type, Location);
+                               method_expr.InstanceExpression = new BaseThis (base_type, Location);
 
-                               Block finaly_block = new ExplicitBlock (new_block, Location, Location);
-                               Block try_block = new Block (new_block, block);
+                               var try_block = new ExplicitBlock (block, block.StartLocation, block.EndLocation);
+                               var finaly_block = new ExplicitBlock (block, Location, Location);
 
                                //
                                // 0-size arguments to avoid CS0250 error
@@ -1970,12 +2085,12 @@ namespace Mono.CSharp {
                                // debugger scope
                                //
                                finaly_block.AddStatement (new StatementExpression (new Invocation (method_expr, new Arguments (0))));
-                               new_block.AddStatement (new TryFinally (try_block, finaly_block, Location));
 
-                               block = new_block;
+                               var tf = new TryFinally (try_block, finaly_block, Location);
+                               block.WrapIntoDestructor (tf, try_block);
                        }
 
-                       return true;
+                       base.Emit ();
                }
 
                public override string GetSignatureForError ()
@@ -1983,6 +2098,12 @@ namespace Mono.CSharp {
                        return Parent.GetSignatureForError () + ".~" + Parent.MemberName.Name + "()";
                }
 
+               protected override bool ResolveMemberType ()
+               {
+                       member_type = Compiler.BuiltinTypes.Void;
+                       return true;
+               }
+
                public override string[] ValidAttributeTargets {
                        get {
                                return attribute_targets;
@@ -1995,31 +2116,16 @@ namespace Mono.CSharp {
        public abstract class AbstractPropertyEventMethod : MemberCore, IMethodData {
                protected MethodData method_data;
                protected ToplevelBlock block;
-               protected Dictionary<SecurityAction, PermissionSet> declarative_security;
-
-               // The accessor are created even if they are not wanted.
-               // But we need them because their names are reserved.
-               // Field says whether accessor will be emited or not
-               public readonly bool IsDummy;
+               protected SecurityType declarative_security;
 
                protected readonly string prefix;
 
                ReturnParameter return_attributes;
 
-               public AbstractPropertyEventMethod (PropertyBasedMember member, string prefix)
-                       : base (member.Parent, SetupName (prefix, member, member.Location), null)
+               public AbstractPropertyEventMethod (InterfaceMemberBase member, string prefix, Attributes attrs, Location loc)
+                       : base (member.Parent, SetupName (prefix, member, loc), attrs)
                {
                        this.prefix = prefix;
-                       IsDummy = true;
-               }
-
-               public AbstractPropertyEventMethod (InterfaceMemberBase member, Accessor accessor,
-                                                   string prefix)
-                       : base (member.Parent, SetupName (prefix, member, accessor.Location),
-                               accessor.Attributes)
-               {
-                       this.prefix = prefix;
-                       this.block = accessor.Block;
                }
 
                static MemberName SetupName (string prefix, InterfaceMemberBase member, Location loc)
@@ -2055,6 +2161,12 @@ namespace Mono.CSharp {
                        return new EmitContext (this, ig, ReturnType);
                }
 
+               public bool IsAccessor {
+                       get {
+                               return true;
+                       }
+               }
+
                public bool IsExcluded ()
                {
                        return false;
@@ -2093,9 +2205,7 @@ namespace Mono.CSharp {
                        }
 
                        if (a.IsValidSecurityAttribute ()) {
-                               if (declarative_security == null)
-                                       declarative_security = new Dictionary<SecurityAction, PermissionSet> ();
-                               a.ExtractSecurityPermissionSet (declarative_security);
+                               a.ExtractSecurityPermissionSet (ctor, ref declarative_security);
                                return;
                        }
 
@@ -2131,23 +2241,16 @@ namespace Mono.CSharp {
                        method_data.Emit (parent);
 
                        if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
-                               PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (method_data.MethodBuilder);
+                               Module.PredefinedAttributes.CompilerGenerated.EmitAttribute (method_data.MethodBuilder);
                        if (((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0))
-                               PredefinedAttributes.Get.DebuggerHidden.EmitAttribute (method_data.MethodBuilder);
+                               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);
-                               PredefinedAttributes.Get.Dynamic.EmitAttribute (return_attributes.Builder);
-                       } else {
-                               var trans_flags = TypeManager.HasDynamicTypeUsed (ReturnType);
-                               if (trans_flags != null) {
-                                       var pa = PredefinedAttributes.Get.DynamicTransform;
-                                       if (pa.Constructor != null || pa.ResolveConstructor (Location, ArrayContainer.MakeType (TypeManager.bool_type))) {
-                                               return_attributes = new ReturnParameter (this, method_data.MethodBuilder, Location);
-                                               return_attributes.Builder.SetCustomAttribute (
-                                                       new CustomAttributeBuilder (pa.Constructor, new object [] { trans_flags }));
-                                       }
-                               }
+                               Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder);
+                       } else if (ReturnType.HasDynamicElement) {
+                               return_attributes = new ReturnParameter (this, method_data.MethodBuilder, Location);
+                               Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder, ReturnType, Location);
                        }
 
                        if (OptAttributes != null)
@@ -2155,7 +2258,11 @@ namespace Mono.CSharp {
 
                        if (declarative_security != null) {
                                foreach (var de in declarative_security) {
+#if STATIC
+                                       method_data.MethodBuilder.__AddDeclarativeSecurity (de);
+#else
                                        method_data.MethodBuilder.AddDeclarativeSecurity (de.Key, de.Value);
+#endif
                                }
                        }
 
@@ -2164,50 +2271,28 @@ namespace Mono.CSharp {
 
                public override bool EnableOverloadChecks (MemberCore overload)
                {
+                       if (overload is MethodCore) {
+                               caching_flags |= Flags.MethodOverloadsExist;
+                               return true;
+                       }
+
                        // This can only happen with indexers and it will
                        // be catched as indexer difference
                        if (overload is AbstractPropertyEventMethod)
                                return true;
 
-                       if (overload is MethodCore) {
-                               caching_flags |= Flags.MethodOverloadsExist;
-                               return true;
-                       }
                        return false;
                }
 
-               public override bool IsClsComplianceRequired()
+               public override string GetSignatureForDocumentation ()
                {
-                       return false;
+                       // should not be called
+                       throw new NotSupportedException ();
                }
 
-               public bool IsDuplicateImplementation (MethodCore method)
+               public override bool IsClsComplianceRequired()
                {
-                       if (!MemberName.Equals (method.MemberName))
-                               return false;
-
-                       TypeSpec[] param_types = method.ParameterTypes;
-
-                       if (param_types == null || param_types.Length != ParameterTypes.Length)
-                               return false;
-
-                       for (int i = 0; i < param_types.Length; i++)
-                               if (param_types [i] != ParameterTypes [i])
-                                       return false;
-
-                       Report.SymbolRelatedToPreviousError (method);
-                       Report.Error (82, Location, "A member `{0}' is already reserved",
-                               method.GetSignatureForError ());
-                       return true;
-               }
-
-               public override bool IsUsed {
-                       get {
-                               if (IsDummy)
-                                       return false;
-
-                               return base.IsUsed;
-                       }
+                       return false;
                }
 
                public MethodSpec Spec { get; protected set; }
@@ -2337,12 +2422,12 @@ namespace Mono.CSharp {
                        if (!base.Define ())
                                return false;
 
-                       if (block != null && block.IsIterator && !(Parent is IteratorStorey)) {
+                       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);
+                               Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
                                ModFlags |= Modifiers.DEBUGGER_HIDDEN;
                        }
 
@@ -2357,68 +2442,63 @@ 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 == InternalType.Dynamic || first_arg_type == InternalType.Dynamic) {
-                               Report.Error (1964, Location,
-                                       "User-defined operator `{0}' cannot convert to or from the dynamic type",
-                                       GetSignatureForError ());
-
-                               return false;
-                       }
+                       if (return_type.IsNullableType)
+                               return_type_unwrap = Nullable.NullableInfo.GetUnderlyingType (return_type);
 
                        //
                        // Rules for conversion operators
                        //
                        if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
-                               if (first_arg_type_unwrap == return_type_unwrap && first_arg_type_unwrap == declaring_type){
+                               if (first_arg_type_unwrap == return_type_unwrap && first_arg_type_unwrap == declaring_type) {
                                        Report.Error (555, Location,
                                                "User-defined operator cannot take an object of the enclosing type and convert to an object of the enclosing type");
                                        return false;
                                }
-                               
+
                                TypeSpec conv_type;
-                               if (TypeManager.IsEqual (declaring_type, return_type) || declaring_type == return_type_unwrap) {
+                               if (declaring_type == return_type || declaring_type == return_type_unwrap) {
                                        conv_type = first_arg_type;
-                               } else if (TypeManager.IsEqual (declaring_type, first_arg_type) || declaring_type == first_arg_type_unwrap) {
+                               } else if (declaring_type == first_arg_type || declaring_type == first_arg_type_unwrap) {
                                        conv_type = return_type;
                                } else {
-                                       Report.Error (556, Location, 
+                                       Report.Error (556, Location,
                                                "User-defined conversion must convert to or from the enclosing type");
                                        return false;
                                }
 
-                               //
-                               // Because IsInterface and IsClass are not supported
-                               //
-                               if (!TypeManager.IsGenericParameter (conv_type)) {
-                                       if (conv_type.IsInterface) {
-                                               Report.Error (552, Location, "User-defined conversion `{0}' cannot convert to or from an interface type",
+                               if (conv_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
+                                       Report.Error (1964, Location,
+                                               "User-defined conversion `{0}' cannot convert to or from the dynamic type",
+                                               GetSignatureForError ());
+
+                                       return false;
+                               }
+
+                               if (conv_type.IsInterface) {
+                                       Report.Error (552, Location, "User-defined conversion `{0}' cannot convert to or from an interface type",
+                                               GetSignatureForError ());
+                                       return false;
+                               }
+
+                               if (conv_type.IsClass) {
+                                       if (TypeSpec.IsBaseClass (declaring_type, conv_type, true)) {
+                                               Report.Error (553, Location, "User-defined conversion `{0}' cannot convert to or from a base class",
                                                        GetSignatureForError ());
                                                return false;
                                        }
 
-                                       if (conv_type.IsClass) {
-                                               if (TypeManager.IsSubclassOf (declaring_type, conv_type)) {
-                                                       Report.Error (553, Location, "User-defined conversion `{0}' cannot convert to or from a base class",
-                                                               GetSignatureForError ());
-                                                       return false;
-                                               }
-
-                                               if (TypeManager.IsSubclassOf (conv_type, declaring_type)) {
-                                                       Report.Error (554, Location, "User-defined conversion `{0}' cannot convert to or from a derived class",
-                                                               GetSignatureForError ());
-                                                       return false;
-                                               }
+                                       if (TypeSpec.IsBaseClass (conv_type, declaring_type, false)) {
+                                               Report.Error (554, Location, "User-defined conversion `{0}' cannot convert to or from a derived class",
+                                                       GetSignatureForError ());
+                                               return false;
                                        }
                                }
                        } 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;
                                }
@@ -2426,7 +2506,7 @@ namespace Mono.CSharp {
                                // Checks for Unary operators
 
                                if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
-                                       if (return_type != declaring_type && !TypeManager.IsSubclassOf (return_type, declaring_type)) {
+                                       if (return_type != declaring_type && !TypeSpec.IsBaseClass (return_type, declaring_type, false)) {
                                                Report.Error (448, Location,
                                                        "The return type for ++ or -- operator must be the containing type or derived from the containing type");
                                                return false;
@@ -2437,15 +2517,15 @@ namespace Mono.CSharp {
                                                return false;
                                        }
                                }
-                               
-                               if (!TypeManager.IsEqual (first_arg_type_unwrap, declaring_type)){
+
+                               if (first_arg_type_unwrap != declaring_type) {
                                        Report.Error (562, Location,
                                                "The parameter type of a unary operator must be the containing type");
                                        return false;
                                }
-                               
+
                                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 " +
@@ -2453,15 +2533,15 @@ namespace Mono.CSharp {
                                                return false;
                                        }
                                }
-                               
-                       } else if (!TypeManager.IsEqual (first_arg_type_unwrap, declaring_type)) {
+
+                       } else if (first_arg_type_unwrap != declaring_type) {
                                // 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];
+                               var second_arg_type = ParameterTypes[1];
+                               if (second_arg_type.IsNullableType)
+                                       second_arg_type = Nullable.NullableInfo.GetUnderlyingType (second_arg_type);
 
-                               if (!TypeManager.IsEqual (second_arg_type, declaring_type)) {
+                               if (second_arg_type != declaring_type) {
                                        Report.Error (563, Location,
                                                "One of the parameters of a binary operator must be the containing type");
                                        return false;
@@ -2480,7 +2560,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;
@@ -2549,12 +2629,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));