* MonoTouch/MonoPInvokeCallbackAttribute.cs: Added.
[mono.git] / mcs / mcs / class.cs
index 523855e102f925c1c736ddce8540bdc29f3189c6..48f22136149bce66ca18c110956166f26d44644c 100644 (file)
@@ -133,17 +133,17 @@ namespace Mono.CSharp {
                                                continue;
 
                                        Operator o = operators [i];
-                                       Report.Error (216, o.Location,
+                                       container.Report.Error (216, o.Location,
                                                "The operator `{0}' requires a matching operator `{1}' to also be defined",
                                                o.GetSignatureForError (), Operator.GetName (o.GetMatchingOperator ()));
                                }
 
-                               if (has_equality_or_inequality && Report.WarningLevel > 2) {
+                               if (has_equality_or_inequality && container.Report.WarningLevel > 2) {
                                        if (container.Methods == null || !container.HasEquals)
-                                               Report.Warning (660, 2, container.Location, "`{0}' defines operator == or operator != but does not override Object.Equals(object o)", container.GetSignatureForError ());
+                                               container.Report.Warning (660, 2, container.Location, "`{0}' defines operator == or operator != but does not override Object.Equals(object o)", container.GetSignatureForError ());
  
                                        if (container.Methods == null || !container.HasGetHashCode)
-                                               Report.Warning (661, 2, container.Location, "`{0}' defines operator == or operator != but does not override Object.GetHashCode()", container.GetSignatureForError ());
+                                               container.Report.Warning (661, 2, container.Location, "`{0}' defines operator == or operator != but does not override Object.GetHashCode()", container.GetSignatureForError ());
                                }
                        }
 
@@ -154,6 +154,80 @@ namespace Mono.CSharp {
                        }
                }
 
+               //
+               // Different context is needed when resolving type container base
+               // types. Type names come from the parent scope but type parameter
+               // names from the container scope.
+               //
+               struct BaseContext : IMemberContext
+               {
+                       TypeContainer tc;
+
+                       public BaseContext (TypeContainer tc)
+                       {
+                               this.tc = tc;
+                       }
+
+                       #region IMemberContext Members
+
+                       public CompilerContext Compiler {
+                               get { return tc.Compiler; }
+                       }
+
+                       public Type CurrentType {
+                               get { return tc.Parent.CurrentType; }
+                       }
+
+                       public TypeParameter[] CurrentTypeParameters {
+                               get { return tc.PartialContainer.CurrentTypeParameters; }
+                       }
+
+                       public TypeContainer CurrentTypeDefinition {
+                               get { return tc.Parent.CurrentTypeDefinition; }
+                       }
+
+                       public bool IsObsolete {
+                               get { return tc.IsObsolete; }
+                       }
+
+                       public bool IsUnsafe {
+                               get { return tc.IsUnsafe; }
+                       }
+
+                       public bool IsStatic {
+                               get { return tc.IsStatic; }
+                       }
+
+                       public string GetSignatureForError ()
+                       {
+                               throw new NotImplementedException ();
+                       }
+
+                       public ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, string name, Location loc)
+                       {
+                               return null;
+                       }
+
+                       public FullNamedExpression LookupNamespaceAlias (string name)
+                       {
+                               return tc.Parent.LookupNamespaceAlias (name);
+                       }
+
+                       public FullNamedExpression LookupNamespaceOrType (string name, Location loc, bool ignore_cs0104)
+                       {
+                               TypeParameter[] tp = CurrentTypeParameters;
+                               if (tp != null) {
+                                       TypeParameter t = TypeParameter.FindTypeParameter (tp, name);
+                                       if (t != null)
+                                               return new TypeParameterExpr (t, loc);
+                               }
+
+                               return tc.Parent.LookupNamespaceOrType (name, loc, ignore_cs0104);
+                       }
+
+                       #endregion
+               }
+
                [Flags]
                enum CachedMethods
                {
@@ -229,9 +303,7 @@ namespace Mono.CSharp {
                TypeExpr base_type;
                TypeExpr[] iface_exprs;
                Type GenericType;
-#if GMCS_SOURCE                
                GenericTypeParameterBuilder[] nested_gen_params;
-#endif
 
                protected ArrayList type_bases;
 
@@ -547,16 +619,16 @@ namespace Mono.CSharp {
                        compiler_generated.Add (c);
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
-                       if (a.Type == TypeManager.default_member_type) {
+                       if (a.Type == pa.DefaultMember) {
                                if (Indexers != null) {
                                        Report.Error (646, a.Location, "Cannot specify the `DefaultMember' attribute on type containing an indexer");
                                        return;
                                }
                        }
                        
-                       base.ApplyAttributeBuilder (a, cb);
+                       base.ApplyAttributeBuilder (a, cb, pa);
                } 
 
                public override AttributeTargets AttributeTargets {
@@ -645,10 +717,10 @@ namespace Mono.CSharp {
 
                public bool IsComImport {
                        get {
-                               if (OptAttributes == null || TypeManager.comimport_attr_type == null)
+                               if (OptAttributes == null)
                                        return false;
 
-                               return OptAttributes.Contains (TypeManager.comimport_attr_type);
+                               return OptAttributes.Contains (PredefinedAttributes.Get.ComImport);
                        }
                }
 
@@ -669,7 +741,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               public void ResolveFieldInitializers (EmitContext ec)
+               public void ResolveFieldInitializers (BlockContext ec)
                {
                        if (partial_parts != null) {
                                foreach (TypeContainer part in partial_parts) {
@@ -679,7 +751,7 @@ namespace Mono.CSharp {
                        DoResolveFieldInitializers (ec);
                }
 
-               void DoResolveFieldInitializers (EmitContext ec)
+               void DoResolveFieldInitializers (BlockContext ec)
                {
                        if (ec.IsStatic) {
                                if (initialized_static_fields == null)
@@ -735,33 +807,6 @@ namespace Mono.CSharp {
                        }
                }
 
-               //
-               // Emits the instance field initializers
-               //
-               public bool EmitFieldInitializers (EmitContext ec)
-               {
-                       if (partial_parts != null) {
-                               foreach (TypeContainer part in partial_parts)
-                                       part.EmitFieldInitializers (ec);
-                       }
-
-                       ArrayList fields;
-                       
-                       if (ec.IsStatic){
-                               fields = initialized_static_fields;
-                       } else {
-                               fields = initialized_fields;
-                       }
-
-                       if (fields == null)
-                               return true;
-
-                       foreach (FieldInitializer f in fields) {
-                               f.EmitStatement (ec);
-                       }
-                       return true;
-               }
-               
                public override string DocComment {
                        get {
                                return comment;
@@ -811,6 +856,7 @@ namespace Mono.CSharp {
 
                        int count = type_bases.Count;
                        TypeExpr [] ifaces = null;
+                       IMemberContext base_context = new BaseContext (this);
                        for (int i = 0, j = 0; i < count; i++){
                                FullNamedExpression fne = (FullNamedExpression) type_bases [i];
 
@@ -819,12 +865,16 @@ namespace Mono.CSharp {
                                // it does ObsoleteAttribute and constraint checks which require
                                // base type to be set
                                //
-                               TypeExpr fne_resolved = fne.ResolveAsBaseTerminal (this, false);
+                               TypeExpr fne_resolved = fne.ResolveAsBaseTerminal (base_context, false);
                                if (fne_resolved == null)
                                        continue;
 
                                if (i == 0 && Kind == Kind.Class && !fne_resolved.Type.IsInterface) {
-                                       base_class = fne_resolved;
+                                       if (fne_resolved is DynamicTypeExpr)
+                                               Report.Error (1965, Location, "Class `{0}' cannot derive from the dynamic type",
+                                                       GetSignatureForError ());
+                                       else
+                                               base_class = fne_resolved;
                                        continue;
                                }
 
@@ -877,7 +927,7 @@ namespace Mono.CSharp {
                                        if (base_class == TypeManager.system_object_expr)
                                                base_class = new_base_class;
                                        else {
-                                               if (new_base_class != null && !new_base_class.Equals (base_class)) {
+                                               if (new_base_class != null && !TypeManager.IsEqual (new_base_class.Type, base_class.Type)) {
                                                        Report.SymbolRelatedToPreviousError (base_class.Location, "");
                                                        Report.Error (263, part.Location,
                                                                "Partial declarations of `{0}' must not specify different base classes",
@@ -945,6 +995,8 @@ namespace Mono.CSharp {
                                        default_parent = TypeManager.value_type;
                                else if (Kind == Kind.Enum)
                                        default_parent = TypeManager.enum_type;
+                               else if (Kind == Kind.Delegate)
+                                       default_parent = TypeManager.multicast_delegate_type;
 
                                //
                                // Sets .size to 1 for structs with no instance fields
@@ -952,11 +1004,12 @@ namespace Mono.CSharp {
                                int type_size = Kind == Kind.Struct && first_nonstatic_field == null ? 1 : 0;
 
                                if (IsTopLevel){
-                                       if (TypeManager.NamespaceClash (Name, Location)) {
+                                       if (GlobalRootNamespace.Instance.IsNamespace (Name)) {
+                                               Report.Error (519, Location, "`{0}' clashes with a predefined namespace", Name);
                                                return false;
                                        }
 
-                                       ModuleBuilder builder = CodeGen.Module.Builder;
+                                       ModuleBuilder builder = Module.Builder;
                                        TypeBuilder = builder.DefineType (
                                                Name, TypeAttr, default_parent, type_size);
                                } else {
@@ -972,15 +1025,18 @@ namespace Mono.CSharp {
 
                        TypeManager.AddUserType (this);
 
-#if GMCS_SOURCE
                        if (IsGeneric) {
                                string[] param_names = new string [TypeParameters.Length];
                                for (int i = 0; i < TypeParameters.Length; i++)
                                        param_names [i] = TypeParameters [i].Name;
 
+#if GMCS_SOURCE
                                GenericTypeParameterBuilder[] gen_params = TypeBuilder.DefineGenericParameters (param_names);
 
-                               int offset = CountTypeParameters - CurrentTypeParameters.Length;
+                               int offset = CountTypeParameters;
+                               if (CurrentTypeParameters != null)
+                                       offset -= CurrentTypeParameters.Length;
+
                                if (offset > 0) {
                                        nested_gen_params = new GenericTypeParameterBuilder [offset];
                                        Array.Copy (gen_params, nested_gen_params, offset);
@@ -988,8 +1044,11 @@ namespace Mono.CSharp {
 
                                for (int i = offset; i < gen_params.Length; i++)
                                        CurrentTypeParameters [i - offset].Define (gen_params [i]);
-                       }
+#else
+                               nested_gen_params = null;
+                               throw new NotSupportedException ();
 #endif
+                       }
 
                        return true;
                }
@@ -1011,7 +1070,7 @@ namespace Mono.CSharp {
                        // Let's do it as soon as possible, since code below can call DefineType() on classes
                        // that depend on us to be populated before they are.
                        //
-                       if (!(this is CompilerGeneratedClass))
+                       if (!(this is CompilerGeneratedClass) && !(this is Delegate))
                                RootContext.RegisterOrder (this); 
 
                        if (!CheckRecursiveDefinition (this))
@@ -1119,12 +1178,11 @@ namespace Mono.CSharp {
                        }
                }
 
-#if GMCS_SOURCE
                void UpdateTypeParameterConstraints (TypeContainer part)
                {
-                       TypeParameter[] current_params = CurrentTypeParameters;
+                       TypeParameter[] current_params = type_params;
                        for (int i = 0; i < current_params.Length; i++) {
-                               Constraints c = part.CurrentTypeParameters [i].Constraints;
+                               Constraints c = part.type_params [i].Constraints;
                                if (c == null)
                                        continue;
 
@@ -1137,7 +1195,6 @@ namespace Mono.CSharp {
                                        GetSignatureForError (), current_params [i].GetSignatureForError ());
                        }
                }
-#endif
 
                public bool ResolveType ()
                {
@@ -1155,7 +1212,6 @@ namespace Mono.CSharp {
 
                protected virtual bool DoResolveType ()
                {
-#if GMCS_SOURCE
                        if (!IsGeneric)
                                return true;
 
@@ -1163,17 +1219,18 @@ namespace Mono.CSharp {
                                throw new InternalErrorException ();
 
                        TypeExpr current_type = null;
-
-                       foreach (TypeParameter type_param in CurrentTypeParameters) {
-                               if (!type_param.Resolve (this)) {
-                                       error = true;
-                                       return false;
+                       if (CurrentTypeParameters != null) {
+                               foreach (TypeParameter type_param in CurrentTypeParameters) {
+                                       if (!type_param.Resolve (this)) {
+                                               error = true;
+                                               return false;
+                                       }
                                }
-                       }
 
-                       if (partial_parts != null && is_generic) {
-                               foreach (TypeContainer part in partial_parts)
-                                       UpdateTypeParameterConstraints (part);
+                               if (partial_parts != null) {
+                                       foreach (TypeContainer part in partial_parts)
+                                               UpdateTypeParameterConstraints (part);
+                               }
                        }
 
                        for (int i = 0; i < TypeParameters.Length; ++i) {
@@ -1201,8 +1258,7 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       CurrentType = current_type.Type;
-#endif                 
+                       currentType = current_type.Type;
                        return true;
                }
 
@@ -1267,6 +1323,12 @@ namespace Mono.CSharp {
                        return true;
                }
 
+               public override TypeParameter[] CurrentTypeParameters {
+                       get {
+                               return PartialContainer.type_params;
+                       }
+               }
+
                /// <summary>
                ///   Populates our TypeBuilder with fields and methods
                /// </summary>
@@ -1286,13 +1348,16 @@ namespace Mono.CSharp {
                        if (iface_exprs != null) {
                                foreach (TypeExpr iface in iface_exprs) {
                                        ObsoleteAttribute oa = AttributeTester.GetObsoleteAttribute (iface.Type);
-                                       if ((oa != null) && !IsInObsoleteScope)
+                                       if ((oa != null) && !IsObsolete)
                                                AttributeTester.Report_ObsoleteMessage (
-                                                       oa, iface.GetSignatureForError (), Location);
+                                                       oa, iface.GetSignatureForError (), Location, Report);
 
                                        GenericTypeExpr ct = iface as GenericTypeExpr;
                                        if (ct != null) {
-                                               if (!ct.CheckConstraints (this) || !ct.VerifyVariantTypeParameters ())
+                                               // TODO: passing `this' is wrong, should be base type iface instead
+                                               TypeManager.CheckTypeVariance (ct.Type, Variance.Covariant, this);
+
+                                               if (!ct.CheckConstraints (this))
                                                        return false;
                                        }
                                }
@@ -1300,8 +1365,8 @@ namespace Mono.CSharp {
 
                        if (base_type != null) {
                                ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (base_type.Type);
-                               if (obsolete_attr != null && !IsInObsoleteScope)
-                                       AttributeTester.Report_ObsoleteMessage (obsolete_attr, base_type.GetSignatureForError (), Location);
+                               if (obsolete_attr != null && !IsObsolete)
+                                       AttributeTester.Report_ObsoleteMessage (obsolete_attr, base_type.GetSignatureForError (), Location, Report);
 
                                GenericTypeExpr ct = base_type as GenericTypeExpr;
                                if ((ct != null) && !ct.CheckConstraints (this))
@@ -1356,7 +1421,7 @@ namespace Mono.CSharp {
 
                                if (requires_delayed_unmanagedtype_check) {
                                        requires_delayed_unmanagedtype_check = false;
-                                       foreach (Field f in fields) {
+                                       foreach (FieldBase f in fields) {
                                                if (f.MemberType != null && f.MemberType.IsPointer)
                                                        TypeManager.VerifyUnManaged (f.MemberType, f.Location);
                                        }
@@ -1382,15 +1447,13 @@ namespace Mono.CSharp {
                                GenericType = CurrentType;
                        }
 
-#if GMCS_SOURCE
                        //
                        // FIXME: This hack is needed because member cache does not work
                        // with generic types, we rely on runtime to inflate dynamic types.
                        // TODO: This hack requires member cache refactoring to be removed
                        //
-                       if (TypeBuilder.IsGenericType)
+                       if (TypeManager.IsGenericType (TypeBuilder))
                                member_cache = new MemberCache (this);
-#endif                 
 
                        return true;
                }
@@ -1442,23 +1505,12 @@ namespace Mono.CSharp {
                        if (!seen_normal_indexers)
                                return;
 
-                       if (TypeManager.default_member_ctor == null) {
-                               if (TypeManager.default_member_type == null) {
-                                       TypeManager.default_member_type = TypeManager.CoreLookupType (
-                                               "System.Reflection", "DefaultMemberAttribute", Kind.Class, true);
-
-                                       if (TypeManager.default_member_type == null)
-                                               return;
-                               }
-
-                               TypeManager.default_member_ctor = TypeManager.GetPredefinedConstructor (
-                                       TypeManager.default_member_type, Location, TypeManager.string_type);
-
-                               if (TypeManager.default_member_ctor == null)
-                                       return;
-                       }
+                       PredefinedAttribute pa = PredefinedAttributes.Get.DefaultMember;
+                       if (pa.Constructor == null &&
+                               !pa.ResolveConstructor (Location, TypeManager.string_type))
+                               return;
 
-                       CustomAttributeBuilder cb = new CustomAttributeBuilder (TypeManager.default_member_ctor, new string [] { IndexerName });
+                       CustomAttributeBuilder cb = new CustomAttributeBuilder (pa.Constructor, new string [] { IndexerName });
                        TypeBuilder.SetCustomAttribute (cb);
                }
 
@@ -2012,7 +2064,7 @@ namespace Mono.CSharp {
                        return;
                }
 
-               static void CheckMemberUsage (MemberCoreArrayList al, string member_type)
+               void CheckMemberUsage (MemberCoreArrayList al, string member_type)
                {
                        if (al == null)
                                return;
@@ -2177,7 +2229,7 @@ namespace Mono.CSharp {
                        }
 
                        if (pending != null)
-                               pending.VerifyPendingMethods ();
+                               pending.VerifyPendingMethods (Report);
 
                        if (Report.Errors > 0)
                                return;
@@ -2273,7 +2325,7 @@ namespace Mono.CSharp {
 
                        if (Kind == Kind.Struct){
                                if ((flags & va) != 0){
-                                       Modifiers.Error_InvalidModifier (mc.Location, "virtual or abstract");
+                                       Modifiers.Error_InvalidModifier (mc.Location, "virtual or abstract", Report);
                                        ok = false;
                                }
                        }
@@ -2508,7 +2560,7 @@ namespace Mono.CSharp {
                //
                internal override void GenerateDocComment (DeclSpace ds)
                {
-                       DocUtil.GenerateTypeDocComment (this, ds);
+                       DocUtil.GenerateTypeDocComment (this, ds, Report);
                }
 
                public override string DocCommentHeader {
@@ -2573,7 +2625,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
                        if (a.IsValidSecurityAttribute ()) {
                                if (declarative_security == null)
@@ -2583,14 +2635,14 @@ namespace Mono.CSharp {
                                return;
                        }
 
-                       if (a.Type == TypeManager.struct_layout_attribute_type) {
+                       if (a.Type == pa.StructLayout) {
                                PartialContainer.HasStructLayout = true;
 
                                if (a.GetLayoutKindValue () == LayoutKind.Explicit)
                                        PartialContainer.HasExplicitLayout = true;
                        }
 
-                       base.ApplyAttributeBuilder (a, cb);
+                       base.ApplyAttributeBuilder (a, cb, pa);
                }
 
                /// <summary>
@@ -2615,7 +2667,7 @@ namespace Mono.CSharp {
                                Location);
                        
                        AddConstructor (c);
-                       c.Block = new ToplevelBlock (ParametersCompiled.EmptyReadOnlyParameters, Location);
+                       c.Block = new ToplevelBlock (Compiler, ParametersCompiled.EmptyReadOnlyParameters, Location);
                }
 
                public override bool Define ()
@@ -2648,7 +2700,17 @@ namespace Mono.CSharp {
 
                public override ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, string name, Location loc)
                {
-                       return NamespaceEntry.LookupExtensionMethod (extensionType, this, name, loc);
+                       DeclSpace top_level = Parent;
+                       if (top_level != null) {
+                               while (top_level.Parent != null)
+                                       top_level = top_level.Parent;
+
+                               ArrayList candidates = NamespaceEntry.NS.LookupExtensionMethod (extensionType, this, name);
+                               if (candidates != null)
+                                       return new ExtensionMethodGroupExpr (candidates, NamespaceEntry, extensionType, loc);
+                       }
+
+                       return NamespaceEntry.LookupExtensionMethod (extensionType, name, loc);
                }
 
                protected override TypeAttributes TypeAttr {
@@ -2682,7 +2744,7 @@ namespace Mono.CSharp {
                        : base (ns, parent, name, attrs, Kind.Class)
                {
                        int accmods = Parent.Parent == null ? Modifiers.INTERNAL : Modifiers.PRIVATE;
-                       this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
+                       this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location, Report);
 
                        if (IsStatic && RootContext.Version == LanguageVersion.ISO_1) {
                                Report.FeatureIsNotAvailable (Location, "static classes");
@@ -2697,27 +2759,26 @@ namespace Mono.CSharp {
                        base.AddBasesForPart (part, bases);
                }
 
-               public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
-                       if (a.Type == TypeManager.attribute_usage_type) {
+                       if (a.Type == pa.AttributeUsage) {
                                if (!TypeManager.IsAttributeType (BaseType) &&
                                        TypeBuilder.FullName != "System.Attribute") {
                                        Report.Error (641, a.Location, "Attribute `{0}' is only valid on classes derived from System.Attribute", a.GetSignatureForError ());
                                }
                        }
 
-                       if (a.Type == TypeManager.conditional_attribute_type && !TypeManager.IsAttributeType (BaseType)) {
+                       if (a.Type == pa.Conditional && !TypeManager.IsAttributeType (BaseType)) {
                                Report.Error (1689, a.Location, "Attribute `System.Diagnostics.ConditionalAttribute' is only valid on methods or attribute classes");
                                return;
                        }
 
-                       if (a.Type == TypeManager.comimport_attr_type && TypeManager.guid_attr_type != null &&
-                               !attributes.Contains (TypeManager.guid_attr_type)) {
-                                       a.Error_MissingGuidAttribute ();
-                                       return;
+                       if (a.Type == pa.ComImport && !attributes.Contains (pa.Guid)) {
+                               a.Error_MissingGuidAttribute ();
+                               return;
                        }
 
-                       if (a.Type == TypeManager.extension_attribute_type) {
+                       if (a.Type == pa.Extension) {
                                a.Error_MisusedExtensionAttribute ();
                                return;
                        }
@@ -2725,7 +2786,7 @@ namespace Mono.CSharp {
                        if (AttributeTester.IsAttributeExcluded (a.Type, Location))
                                return;
 
-                       base.ApplyAttributeBuilder (a, cb);
+                       base.ApplyAttributeBuilder (a, cb, pa);
                }
 
                public override AttributeTargets AttributeTargets {
@@ -2805,10 +2866,8 @@ namespace Mono.CSharp {
                {
                        base.Emit ();
 
-#if GMCS_SOURCE
                        if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
-                               TypeBuilder.SetCustomAttribute (TypeManager.extension_attribute_attr);
-#endif                 
+                               PredefinedAttributes.Get.Extension.EmitAttribute (TypeBuilder);
                }
 
                protected override TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
@@ -2821,7 +2880,7 @@ namespace Mono.CSharp {
                                else if (Name != "System.Object")
                                        base_class = TypeManager.system_object_expr;
                        } else {
-                               if (Kind == Kind.Class && base_class is TypeParameterExpr){
+                               if (Kind == Kind.Class && TypeManager.IsGenericParameter (base_class.Type)){
                                        Report.Error (
                                                689, base_class.Location,
                                                "Cannot derive from `{0}' because it is a type parameter",
@@ -2889,11 +2948,7 @@ namespace Mono.CSharp {
                        if (OptAttributes == null)
                                return false;
 
-                       if (TypeManager.conditional_attribute_type == null)
-                               return false;
-
-                       Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type);
-
+                       Attribute[] attrs = OptAttributes.SearchMulti (PredefinedAttributes.Get.Conditional);
                        if (attrs == null)
                                return false;
 
@@ -2907,12 +2962,6 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               bool IsStatic {
-                       get {
-                               return (ModFlags & Modifiers.STATIC) != 0;
-                       }
-               }
-
                //
                // FIXME: How do we deal with the user specifying a different
                // layout?
@@ -2928,6 +2977,9 @@ namespace Mono.CSharp {
        }
 
        public sealed class Struct : ClassOrStruct {
+
+               bool is_unmanaged, has_unmanaged_check_done;
+
                // <summary>
                //   Modifiers allowed in a struct declaration
                // </summary>
@@ -2950,21 +3002,21 @@ namespace Mono.CSharp {
                        else
                                accmods = Modifiers.PRIVATE;
                        
-                       this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
+                       this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location, Report);
 
                        this.ModFlags |= Modifiers.SEALED;
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
-                       base.ApplyAttributeBuilder (a, cb);
+                       base.ApplyAttributeBuilder (a, cb, pa);
 
                        //
                        // When struct constains fixed fixed and struct layout has explicitly
                        // set CharSet, its value has to be propagated to compiler generated
                        // fixed field types
                        //
-                       if (a.Type == TypeManager.struct_layout_attribute_type && Fields != null && a.HasField ("CharSet")) {
+                       if (a.Type == pa.StructLayout && Fields != null && a.HasField ("CharSet")) {
                                for (int i = 0; i < Fields.Count; ++i) {
                                        FixedField ff = Fields [i] as FixedField;
                                        if (ff != null)
@@ -2987,6 +3039,11 @@ namespace Mono.CSharp {
                        if (requires_delayed_unmanagedtype_check)
                                return true;
 
+                       if (has_unmanaged_check_done)
+                               return is_unmanaged;
+
+                       has_unmanaged_check_done = true;
+
                        foreach (FieldBase f in fields) {
                                if ((f.ModFlags & Modifiers.STATIC) != 0)
                                        continue;
@@ -2995,6 +3052,7 @@ namespace Mono.CSharp {
                                // struct S { S* s; }
                                Type mt = f.MemberType;
                                if (mt == null) {
+                                       has_unmanaged_check_done = false;
                                        requires_delayed_unmanagedtype_check = true;
                                        return true;
                                }
@@ -3011,6 +3069,7 @@ namespace Mono.CSharp {
                                return false;
                        }
 
+                       is_unmanaged = true;
                        return true;
                }
 
@@ -3041,8 +3100,7 @@ namespace Mono.CSharp {
                        get {
                                const TypeAttributes DefaultTypeAttributes =
                                        TypeAttributes.SequentialLayout |
-                                       TypeAttributes.Sealed |
-                                       TypeAttributes.BeforeFieldInit;
+                                       TypeAttributes.Sealed;
 
                                return base.TypeAttr | DefaultTypeAttributes;
                        }
@@ -3087,17 +3145,17 @@ namespace Mono.CSharp {
                        else
                                accmods = Modifiers.PRIVATE;
 
-                       this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, name.Location);
+                       this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, name.Location, Report);
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
-                       if (a.Type == TypeManager.comimport_attr_type && TypeManager.guid_attr_type != null &&
-                               !attributes.Contains (TypeManager.guid_attr_type)) {
-                                       a.Error_MissingGuidAttribute ();
-                                       return;
+                       if (a.Type == pa.ComImport && !attributes.Contains (pa.Guid)) {
+                               a.Error_MissingGuidAttribute ();
+                               return;
                        }
-                       base.ApplyAttributeBuilder (a, cb);
+
+                       base.ApplyAttributeBuilder (a, cb, pa);
                }
 
 
@@ -3242,7 +3300,7 @@ namespace Mono.CSharp {
                //
                internal override void OnGenerateDocComment (XmlElement el)
                {
-                       DocUtil.OnMethodGenerateDocComment (this, el);
+                       DocUtil.OnMethodGenerateDocComment (this, el, Report);
                }
 
                //
@@ -3350,13 +3408,13 @@ namespace Mono.CSharp {
                                if ((ModFlags & Modifiers.OVERRIDE) != 0) {
                                        ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (base_method);
                                        if (oa != null) {
-                                               if (OptAttributes == null || TypeManager.obsolete_attribute_type == null || !OptAttributes.Contains (TypeManager.obsolete_attribute_type)) {
+                                               if (OptAttributes == null || !OptAttributes.Contains (PredefinedAttributes.Get.Obsolete)) {
                                                        Report.SymbolRelatedToPreviousError (base_method);
                                                                Report.Warning (672, 1, Location, "Member `{0}' overrides obsolete member `{1}'. Add the Obsolete attribute to `{0}'",
                                                                        GetSignatureForError (), TypeManager.CSharpSignature (base_method));
                                                }
                                        } else {
-                                               if (OptAttributes != null && TypeManager.obsolete_attribute_type != null && OptAttributes.Contains (TypeManager.obsolete_attribute_type)) {
+                                               if (OptAttributes != null && OptAttributes.Contains (PredefinedAttributes.Get.Obsolete)) {
                                                        Report.SymbolRelatedToPreviousError (base_method);
                                                        Report.Warning (809, 1, Location, "Obsolete member `{0}' overrides non-obsolete member `{1}'",
                                                                GetSignatureForError (), TypeManager.CSharpSignature (base_method));
@@ -3405,7 +3463,7 @@ namespace Mono.CSharp {
                protected virtual bool CheckForDuplications ()
                {
                        return Parent.MemberCache.CheckExistingMembersOverloads (
-                               this, GetFullName (MemberName), ParametersCompiled.EmptyReadOnlyParameters);
+                               this, GetFullName (MemberName), ParametersCompiled.EmptyReadOnlyParameters, Report);
                }
 
                //
@@ -3468,6 +3526,11 @@ namespace Mono.CSharp {
                                        if (!IsInterface && (base_method.IsVirtual || base_method.IsAbstract)) {
                                                Report.Warning (114, 2, Location, "`{0}' hides inherited member `{1}'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword",
                                                        GetSignatureForError (), TypeManager.CSharpSignature (base_method));
+                                               if (base_method.IsAbstract){
+                                                       Report.Error (533, Location, "`{0}' hides inherited abstract member `{1}'",
+                                                                     GetSignatureForError (), TypeManager.CSharpSignature (base_method));
+                                                       ok = false;
+                                               }
                                        } else {
                                                Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
                                                        GetSignatureForError (), TypeManager.CSharpSignature (base_method));
@@ -3556,7 +3619,7 @@ namespace Mono.CSharp {
                                        Parent.PartialContainer.VerifyImplements (this);
                                }
 
-                               Modifiers.Check (Modifiers.AllowedExplicitImplFlags, explicit_mod_flags, 0, Location);
+                               Modifiers.Check (Modifiers.AllowedExplicitImplFlags, explicit_mod_flags, 0, Location, Report);
                        }
 
                        return base.Define ();
@@ -3564,14 +3627,16 @@ namespace Mono.CSharp {
 
                protected bool DefineParameters (ParametersCompiled parameters)
                {
-                       IResolveContext rc = GenericMethod == null ? this : (IResolveContext)ds;
-
-                       if (!parameters.Resolve (rc))
+                       if (!parameters.Resolve (this))
                                return false;
 
                        bool error = false;
                        for (int i = 0; i < parameters.Count; ++i) {
                                Parameter p = parameters [i];
+
+                               if (p.HasDefaultValue && (IsExplicitImpl || this is Operator || (this is Indexer && parameters.Count == 1)))
+                                       p.Warning_UselessOptionalParameter (Report);
+
                                if (p.CheckAccessibility (this))
                                        continue;
 
@@ -3726,13 +3791,13 @@ namespace Mono.CSharp {
                {
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
                        if (a.Target == AttributeTargets.ReturnValue) {
                                if (return_attributes == null)
                                        return_attributes = new ReturnParameter (MethodBuilder, Location);
 
-                               return_attributes.ApplyAttributeBuilder (a, cb);
+                               return_attributes.ApplyAttributeBuilder (a, cb, pa);
                                return;
                        }
 
@@ -3740,7 +3805,7 @@ namespace Mono.CSharp {
                                is_external_implementation = true;
                        }
 
-                       if (a.Type == TypeManager.dllimport_type) {
+                       if (a.Type == pa.DllImport) {
                                const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
                                if ((ModFlags & extern_static) != extern_static) {
                                        Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
@@ -3771,13 +3836,13 @@ namespace Mono.CSharp {
                        if (MemberName.IsGeneric)
                                name = MemberName.MakeName (name, MemberName.TypeArguments);
 
-                       return Parent.MemberCache.CheckExistingMembersOverloads (this, name, Parameters);
+                       return Parent.MemberCache.CheckExistingMembersOverloads (this, name, Parameters, Report);
                }
 
-               public virtual EmitContext CreateEmitContext (DeclSpace tc, ILGenerator ig)
+               public virtual EmitContext CreateEmitContext (ILGenerator ig)
                {
                        return new EmitContext (
-                               this, tc, this.ds, Location, ig, MemberType, ModFlags, false);
+                               this, ig, MemberType);
                }
 
                protected override bool ResolveMemberType ()
@@ -3806,7 +3871,7 @@ namespace Mono.CSharp {
                                // Current method is turned into automatically generated
                                // wrapper which creates an instance of iterator
                                //
-                               Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
+                               Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags, Compiler);
                                ModFlags |= Modifiers.DEBUGGER_HIDDEN;
                        }
 
@@ -3826,15 +3891,13 @@ namespace Mono.CSharp {
                        MethodData = new MethodData (
                                this, ModFlags, flags, this, MethodBuilder, GenericMethod, base_method);
 
-                       if (!MethodData.Define (Parent.PartialContainer, GetFullName (MemberName)))
+                       if (!MethodData.Define (Parent.PartialContainer, GetFullName (MemberName), Report))
                                return false;
                                        
                        MethodBuilder = MethodData.MethodBuilder;
 
-#if GMCS_SOURCE                                                
-                       if (MethodBuilder.IsGenericMethod)
+                       if (TypeManager.IsGenericMethod (MethodBuilder))
                                Parent.MemberCache.AddGenericMember (MethodBuilder, this);
-#endif                 
                        
                        Parent.MemberCache.AddMember (MethodBuilder, this);
 
@@ -3849,11 +3912,14 @@ namespace Mono.CSharp {
 
                        if ((ModFlags & Modifiers.PARTIAL) != 0) {
                                for (int i = 0; i < Parameters.Count; ++i) {
-                                       if (Parameters.FixedParameters[i].ModFlags == Parameter.Modifier.OUT) {
+                                       IParameterData p = Parameters.FixedParameters [i];
+                                       if (p.ModFlags == Parameter.Modifier.OUT) {
                                                Report.Error (752, Location, "`{0}': A partial method parameters cannot use `out' modifier",
                                                        GetSignatureForError ());
-                                               break;
                                        }
+
+                                       if (p.HasDefaultValue && IsPartialImplementation)
+                                               ((Parameter) p).Warning_UselessOptionalParameter (Report);
                                }
                        }
                }
@@ -3871,12 +3937,16 @@ namespace Mono.CSharp {
 
                public override void Emit ()
                {
-#if GMCS_SOURCE                        
                        if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
-                               MethodBuilder.SetCustomAttribute (TypeManager.GetCompilerGeneratedAttribute (Location));
+                               PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (MethodBuilder);
                        if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
-                               MethodBuilder.SetCustomAttribute (TypeManager.GetDebuggerHiddenAttribute (Location));
-#endif
+                               PredefinedAttributes.Get.DebuggerHidden.EmitAttribute (MethodBuilder);
+
+                       if (TypeManager.IsDynamicType (ReturnType)) {
+                               return_attributes = new ReturnParameter (MethodBuilder, Location);
+                               return_attributes.EmitPredefined (PredefinedAttributes.Get.Dynamic, Location);
+                       }
+
                        if (OptAttributes != null)
                                OptAttributes.Emit ();
 
@@ -3947,10 +4017,7 @@ namespace Mono.CSharp {
                                if (OptAttributes == null)
                                        return false;
 
-                               if (TypeManager.conditional_attribute_type == null)
-                                       return false;
-
-                               Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type);
+                               Attribute[] attrs = OptAttributes.SearchMulti (PredefinedAttributes.Get.Conditional);
 
                                if (attrs == null)
                                        return false;
@@ -4076,6 +4143,8 @@ namespace Mono.CSharp {
                const int AllowedInterfaceModifiers =
                        Modifiers.NEW | Modifiers.UNSAFE;
 
+               Method partialMethodImplementation;
+
                public Method (DeclSpace parent, GenericMethod generic,
                               FullNamedExpression return_type, int mod,
                               MemberName name, ParametersCompiled parameters, Attributes attrs)
@@ -4096,7 +4165,7 @@ namespace Mono.CSharp {
                        return base.GetSignatureForError () + Parameters.GetSignatureForError ();
                }
 
-               static void Error_DuplicateEntryPoint (Method b)
+               void Error_DuplicateEntryPoint (Method b)
                {
                        Report.Error (17, b.Location,
                                "Program `{0}' has more than one entry point defined: `{1}'",
@@ -4121,9 +4190,21 @@ namespace Mono.CSharp {
                                        (Parameters[0].ModFlags & ~Parameter.Modifier.PARAMS) == Parameter.Modifier.NONE;
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               public override FullNamedExpression LookupNamespaceOrType (string name, Location loc, bool ignore_cs0104)
+               {
+                       TypeParameter[] tp = CurrentTypeParameters;
+                       if (tp != null) {
+                               TypeParameter t = TypeParameter.FindTypeParameter (tp, name);
+                               if (t != null)
+                                       return new TypeParameterExpr (t, loc);
+                       }
+
+                       return base.LookupNamespaceOrType (name, loc, ignore_cs0104);
+               }
+
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
-                       if (a.Type == TypeManager.conditional_attribute_type) {
+                       if (a.Type == pa.Conditional) {
                                if (IsExplicitImpl) {
                                        Error_ConditionalAttributeIsNotValid ();
                                        return;
@@ -4159,12 +4240,12 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       if (a.Type == TypeManager.extension_attribute_type) {
+                       if (a.Type == pa.Extension) {
                                a.Error_MisusedExtensionAttribute ();
                                return;
                        }
 
-                       base.ApplyAttributeBuilder (a, cb);
+                       base.ApplyAttributeBuilder (a, cb, pa);
                }
 
                protected override bool CheckForDuplications ()
@@ -4206,6 +4287,15 @@ namespace Mono.CSharp {
                        return true;
                }
 
+               public override TypeParameter[] CurrentTypeParameters {
+                       get {
+                               if (GenericMethod != null)
+                                       return GenericMethod.CurrentTypeParameters;
+
+                               return null;
+                       }
+               }
+
                //
                // Creates the type
                //
@@ -4218,8 +4308,11 @@ namespace Mono.CSharp {
                        if (!base.Define ())
                                return false;
 
+                       if (partialMethodImplementation != null && IsPartialDefinition)
+                               MethodBuilder = partialMethodImplementation.MethodBuilder;
+
                        if (RootContext.StdLib && TypeManager.IsSpecialType (ReturnType)) {
-                               Error1599 (Location, ReturnType);
+                               Error1599 (Location, ReturnType, Report);
                                return false;
                        }
 
@@ -4234,21 +4327,16 @@ namespace Mono.CSharp {
                                return true;
 
                        if (Parameters.HasExtensionMethodType) {
-                               if (Parent.IsStaticClass && !Parent.IsGeneric) {
+                               if (Parent.PartialContainer.IsStaticClass && !Parent.IsGeneric) {
                                        if (!Parent.IsTopLevel)
                                                Report.Error (1109, Location, "`{0}': Extension methods cannot be defined in a nested class",
                                                        GetSignatureForError ());
 
-                                       if (TypeManager.extension_attribute_type == null) {
+                                       PredefinedAttribute pa = PredefinedAttributes.Get.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",
                                                        GetSignatureForError ());
-                                       } else if (TypeManager.extension_attribute_attr == null) {
-                                               ConstructorInfo ci = TypeManager.GetPredefinedConstructor (
-                                                       TypeManager.extension_attribute_type, Location, System.Type.EmptyTypes);
-
-                                               if (ci != null)
-                                                       TypeManager.extension_attribute_attr = new CustomAttributeBuilder (ci, new object [0]);
                                        }
 
                                        ModFlags |= Modifiers.METHOD_EXTENSION;
@@ -4299,25 +4387,21 @@ namespace Mono.CSharp {
                                Report.Debug (64, "METHOD EMIT", this, MethodBuilder, Location, Block, MethodData);
                                if (IsPartialDefinition) {
                                        //
-                                       // Do attribute checks only when partial implementation does not exist
+                                       // Use partial method implementation builder for partial method declaration attributes
                                        //
-                                       if (MethodBuilder == null)
-                                               base.Emit ();
-
-                                       return;
-                               }
-
-                               if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.PartialDefinitionExists) == 0)
+                                       if (partialMethodImplementation != null) {
+                                               MethodBuilder = partialMethodImplementation.MethodBuilder;
+                                               return;
+                                       }
+                               } else 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 ());
+                               }
 
                                base.Emit ();
                                
-#if GMCS_SOURCE                                
                                if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
-                                       MethodBuilder.SetCustomAttribute (TypeManager.extension_attribute_attr);
-#endif
-
+                                       PredefinedAttributes.Get.Extension.EmitAttribute (MethodBuilder);
                        } catch {
                                Console.WriteLine ("Internal compiler error at {0}: exception caught while emitting {1}",
                                                   Location, MethodBuilder);
@@ -4337,7 +4421,7 @@ namespace Mono.CSharp {
                        return base.EnableOverloadChecks (overload);
                }
 
-               public static void Error1599 (Location loc, Type t)
+               public static void Error1599 (Location loc, Type t, Report Report)
                {
                        Report.Error (1599, loc, "Method or delegate cannot return type `{0}'", TypeManager.CSharpName (t));
                }
@@ -4360,7 +4444,12 @@ namespace Mono.CSharp {
                public void SetPartialDefinition (Method methodDefinition)
                {
                        caching_flags |= Flags.PartialDefinitionExists;
-                       methodDefinition.MethodBuilder = MethodBuilder;
+                       methodDefinition.partialMethodImplementation = this;
+
+                       for (int i = 0; i < methodDefinition.Parameters.Count; ++i ) {
+                               Parameters [i].DefaultValue = methodDefinition.Parameters [i].DefaultValue;
+                       }
+
                        if (methodDefinition.attributes == null)
                                return;
 
@@ -4379,7 +4468,7 @@ namespace Mono.CSharp {
                        if (!Parameters.IsEmpty) {
                                ArrayList al = (ArrayList)Parent.PartialContainer.MemberCache.Members [Name];
                                if (al.Count > 1)
-                                       MemberCache.VerifyClsParameterConflict (al, this, MethodBuilder);
+                                       MemberCache.VerifyClsParameterConflict (al, this, MethodBuilder, Report);
                        }
 
                        return true;
@@ -4388,44 +4477,59 @@ namespace Mono.CSharp {
 
        public abstract class ConstructorInitializer : ExpressionStatement
        {
-               ArrayList argument_list;
+               Arguments argument_list;
                MethodGroupExpr base_constructor_group;
-               
-               public ConstructorInitializer (ArrayList argument_list, Location loc)
+
+               public ConstructorInitializer (Arguments argument_list, Location loc)
                {
                        this.argument_list = argument_list;
                        this.loc = loc;
                }
 
-               public ArrayList Arguments {
+               public Arguments Arguments {
                        get {
                                return argument_list;
                        }
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        throw new NotSupportedException ("ET");
                }
 
-               public bool Resolve (ConstructorBuilder caller_builder, EmitContext ec)
+               public override Expression DoResolve (ResolveContext ec)
                {
-                       if (argument_list != null){
-                               foreach (Argument a in argument_list){
-                                       if (!a.Resolve (ec, loc))
-                                               return false;
+                       eclass = ExprClass.Value;
+
+                       // TODO: ec.GetSignatureForError ()
+                       ConstructorBuilder caller_builder = ((Constructor) ec.MemberContext).ConstructorBuilder;
+
+                       if (argument_list != null) {
+                               bool dynamic;
+
+                               //
+                               // Spec mandates that constructor initializer will not have `this' access
+                               //
+                               using (ec.Set (ResolveContext.Options.BaseInitializer)) {
+                                       argument_list.Resolve (ec, out dynamic);
+                               }
+
+                               if (dynamic) {
+                                       SimpleName ctor = new SimpleName (ConstructorBuilder.ConstructorName, loc);
+                                       return new DynamicInvocation (ctor, argument_list, loc).Resolve (ec) as ExpressionStatement;
                                }
                        }
 
+                       type = ec.CurrentType;
                        if (this is ConstructorBaseInitializer) {
-                               if (ec.ContainerType.BaseType == null)
-                                       return true;
+                               if (ec.CurrentType.BaseType == null)
+                                       return this;
 
-                               type = ec.ContainerType.BaseType;
-                               if (TypeManager.IsStruct (ec.ContainerType)) {
-                                       Report.Error (522, loc,
+                               type = ec.CurrentType.BaseType;
+                               if (TypeManager.IsStruct (ec.CurrentType)) {
+                                       ec.Report.Error (522, loc,
                                                "`{0}': Struct constructors cannot call base constructors", TypeManager.CSharpSignature (caller_builder));
-                                       return false;
+                                       return this;
                                }
                        } else {
                                //
@@ -4434,10 +4538,8 @@ namespace Mono.CSharp {
                                //
                                // struct D { public D (int a) : this () {}
                                //
-                               if (TypeManager.IsStruct (ec.ContainerType) && argument_list == null)
-                                       return true;
-                               
-                               type = ec.ContainerType;
+                               if (TypeManager.IsStruct (ec.CurrentType) && argument_list == null)
+                                       return this;                    
                        }
 
                        base_constructor_group = MemberLookupFinal (
@@ -4446,26 +4548,24 @@ namespace Mono.CSharp {
                                loc) as MethodGroupExpr;
                        
                        if (base_constructor_group == null)
-                               return false;
+                               return this;
                        
                        base_constructor_group = base_constructor_group.OverloadResolve (
                                ec, ref argument_list, false, loc);
                        
                        if (base_constructor_group == null)
-                               return false;
+                               return this;
+
+                       if (!ec.IsStatic)
+                               base_constructor_group.InstanceExpression = ec.GetThis (loc);
                        
                        ConstructorInfo base_ctor = (ConstructorInfo)base_constructor_group;
-                       
+
                        if (base_ctor == caller_builder){
-                               Report.Error (516, loc, "Constructor `{0}' cannot call itself", TypeManager.CSharpSignature (caller_builder));
+                               ec.Report.Error (516, loc, "Constructor `{0}' cannot call itself", TypeManager.CSharpSignature (caller_builder));
                        }
                                                
-                       return true;
-               }
-
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       throw new NotSupportedException ();
+                       return this;
                }
 
                public override void Emit (EmitContext ec)
@@ -4475,9 +4575,7 @@ namespace Mono.CSharp {
                                return;
                        
                        ec.Mark (loc);
-                       if (!ec.IsStatic)
-                               base_constructor_group.InstanceExpression = ec.GetThis (loc);
-                       
+
                        base_constructor_group.EmitCall (ec, argument_list);
                }
 
@@ -4488,7 +4586,7 @@ namespace Mono.CSharp {
        }
 
        public class ConstructorBaseInitializer : ConstructorInitializer {
-               public ConstructorBaseInitializer (ArrayList argument_list, Location l) :
+               public ConstructorBaseInitializer (Arguments argument_list, Location l) :
                        base (argument_list, l)
                {
                }
@@ -4502,7 +4600,7 @@ namespace Mono.CSharp {
        }
 
        public class ConstructorThisInitializer : ConstructorInitializer {
-               public ConstructorThisInitializer (ArrayList argument_list, Location l) :
+               public ConstructorThisInitializer (Arguments argument_list, Location l) :
                        base (argument_list, l)
                {
                }
@@ -4561,7 +4659,7 @@ namespace Mono.CSharp {
                                        (Initializer.Arguments == null);
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
                        if (a.IsValidSecurityAttribute ()) {
                                if (declarative_security == null) {
@@ -4597,7 +4695,7 @@ namespace Mono.CSharp {
 
                        if ((caching_flags & Flags.MethodOverloadsExist) != 0)
                                Parent.MemberCache.CheckExistingMembersOverloads (this, ConstructorInfo.ConstructorName,
-                                       Parameters);
+                                       Parameters, Report);
 
                        if (Parent.PartialContainer.Kind == Kind.Struct) {
                                if (Parameters.Count == 0) {
@@ -4666,7 +4764,7 @@ namespace Mono.CSharp {
                        // It's here only to report an error
                        if (block != null && block.IsIterator) {
                                member_type = TypeManager.void_type;
-                               Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
+                               Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags, Compiler);
                        }
 
                        return true;
@@ -4678,15 +4776,13 @@ namespace Mono.CSharp {
                public override void Emit ()
                {
                        if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
-                               ConstructorBuilder.SetCustomAttribute (TypeManager.GetDebuggerHiddenAttribute (Location));
+                               PredefinedAttributes.Get.DebuggerHidden.EmitAttribute (ConstructorBuilder);
 
                        if (OptAttributes != null)
                                OptAttributes.Emit ();
 
                        base.Emit ();
 
-                       EmitContext ec = CreateEmitContext (null, null);
-
                        //
                        // If we use a "this (...)" constructor initializer, then
                        // do not emit field initializers, they are initialized in the other constructor
@@ -4694,8 +4790,11 @@ namespace Mono.CSharp {
                        bool emit_field_initializers = ((ModFlags & Modifiers.STATIC) != 0) ||
                                !(Initializer is ConstructorThisInitializer);
 
+                       BlockContext bc = new BlockContext (this, block, TypeManager.void_type);
+                       bc.Set (ResolveContext.Options.ConstructorScope);
+
                        if (emit_field_initializers)
-                               Parent.PartialContainer.ResolveFieldInitializers (ec);
+                               Parent.PartialContainer.ResolveFieldInitializers (bc);
 
                        if (block != null) {
                                // If this is a non-static `struct' constructor and doesn't have any
@@ -4704,44 +4803,32 @@ namespace Mono.CSharp {
                                        ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
                                        block.AddThisVariable (Parent, Location);
 
-                               if (!block.ResolveMeta (ec, Parameters))
-                                       block = null;
-
                                if (block != null && (ModFlags & Modifiers.STATIC) == 0){
                                        if (Parent.PartialContainer.Kind == Kind.Class && Initializer == null)
                                                Initializer = new GeneratedBaseInitializer (Location);
 
-                                       //
-                                       // Spec mandates that Initializers will not have `this' access
-                                       //
                                        if (Initializer != null) {
-                                               ec.IsStatic = true;
-                                               Initializer.Resolve (ConstructorBuilder, ec);
-                                               ec.IsStatic = false;
                                                block.AddScopeStatement (new StatementExpression (Initializer));
                                        }
                                }
                        }
 
                        Parameters.ApplyAttributes (ConstructorBuilder);
-                       
-                       SourceMethod source = null;
-                       if (block == null)
-                               ec.OmitDebuggingInfo = true;
-                       else
-                               source = SourceMethod.Create (Parent, ConstructorBuilder, block);
 
-                       bool unreachable = false;
-                       if (block != null) {
-                               if (!ec.ResolveTopBlock (null, block, Parameters, this, out unreachable))
-                                       return;
+                       SourceMethod source = SourceMethod.Create (Parent, ConstructorBuilder, block);
 
-                               ec.EmitMeta (block);
+                       if (block != null) {
+                               if (block.Resolve (null, bc, Parameters, this)) {
+                                       EmitContext ec = new EmitContext (this, ConstructorBuilder.GetILGenerator (), bc.ReturnType);
+                                       ec.With (EmitContext.Options.ConstructorScope, true);
 
-                               if (Report.Errors > 0)
-                                       return;
+                                       if (!ec.HasReturnLabel && bc.HasReturnLabel) {
+                                               ec.ReturnLabel = bc.ReturnLabel;
+                                               ec.HasReturnLabel = true;
+                                       }
 
-                               ec.EmitResolvedTopBlock (block, unreachable);
+                                       block.Emit (ec);
+                               }
                        }
 
                        if (source != null)
@@ -4782,7 +4869,7 @@ namespace Mono.CSharp {
                        if (!Parameters.IsEmpty) {
                                ArrayList al = (ArrayList)Parent.MemberCache.Members [ConstructorInfo.ConstructorName];
                                if (al.Count > 2)
-                                       MemberCache.VerifyClsParameterConflict (al, this, ConstructorBuilder);
+                                       MemberCache.VerifyClsParameterConflict (al, this, ConstructorBuilder, Report);
  
                                if (TypeManager.IsSubclassOf (Parent.TypeBuilder, TypeManager.attribute_type)) {
                                        foreach (Type param in Parameters.Types) {
@@ -4810,12 +4897,9 @@ namespace Mono.CSharp {
                        }
                }
 
-               public EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
+               public EmitContext CreateEmitContext (ILGenerator ig)
                {
-                       ILGenerator ig_ = ConstructorBuilder.GetILGenerator ();
-                       EmitContext ec = new EmitContext (this, Parent, Location, ig_, TypeManager.void_type, ModFlags, true);
-                       ec.CurrentBlock = block;
-                       return ec;
+                       throw new NotImplementedException ();
                }
 
                public bool IsExcluded()
@@ -4850,7 +4934,7 @@ namespace Mono.CSharp {
                Attributes OptAttributes { get; }
                ToplevelBlock Block { get; set; }
 
-               EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig);
+               EmitContext CreateEmitContext (ILGenerator ig);
                ObsoleteAttribute GetObsoleteAttribute ();
                string GetSignatureForError ();
                bool IsExcluded ();
@@ -4863,9 +4947,7 @@ namespace Mono.CSharp {
        // Encapsulates most of the Method's state
        //
        public class MethodData {
-#if GMCS_SOURCE
                static FieldInfo methodbuilder_attrs_field;
-#endif
                public readonly IMethodData method;
 
                public readonly GenericMethod GenericMethod;
@@ -4918,7 +5000,7 @@ namespace Mono.CSharp {
                        this.parent_method = parent_method;
                }
 
-               public bool Define (DeclSpace parent, string method_full_name)
+               public bool Define (DeclSpace parent, string method_full_name, Report Report)
                {
                        string name = method.MethodName.Basename;
 
@@ -5070,8 +5152,7 @@ namespace Mono.CSharp {
                                if (implementing != null)
                                        parent_method = implementing;
 
-                               EmitContext ec = method.CreateEmitContext (container, null);
-                               if (!GenericMethod.DefineType (ec, builder, parent_method, is_override))
+                               if (!GenericMethod.DefineType (GenericMethod, builder, parent_method, is_override))
                                        return false;
                        }
 
@@ -5087,7 +5168,8 @@ namespace Mono.CSharp {
                        if (builder == null) {
                                builder = container.TypeBuilder.DefineMethod (
                                        method_name, flags, method.CallingConventions,
-                                       method.ReturnType, param.GetEmitTypes ());
+                                       TypeManager.TypeToReflectionType (method.ReturnType),
+                                       param.GetEmitTypes ());
                                return;
                        }
 
@@ -5098,19 +5180,16 @@ namespace Mono.CSharp {
                        //
                        builder.SetParameters (param.GetEmitTypes ());
                        builder.SetReturnType (method.ReturnType);
-
+#endif
                        if (builder.Attributes != flags) {
                                try {
                                        if (methodbuilder_attrs_field == null)
                                                methodbuilder_attrs_field = typeof (MethodBuilder).GetField ("attrs", BindingFlags.NonPublic | BindingFlags.Instance);
                                        methodbuilder_attrs_field.SetValue (builder, flags);
                                } catch {
-                                       Report.RuntimeMissingSupport (method.Location, "Generic method MethodAttributes");
+                                       RootContext.ToplevelTypes.Compiler.Report.RuntimeMissingSupport (method.Location, "Generic method MethodAttributes");
                                }
                        }
-#else
-                       throw new InternalErrorException ();
-#endif
                }
 
                //
@@ -5118,14 +5197,6 @@ namespace Mono.CSharp {
                // 
                public void Emit (DeclSpace parent)
                {
-                       ToplevelBlock block = method.Block;
-
-                       EmitContext ec;
-                       if (block != null)
-                               ec = method.CreateEmitContext (parent, builder.GetILGenerator ());
-                       else
-                               ec = method.CreateEmitContext (parent, null);
-
                        method.ParameterInfo.ApplyAttributes (MethodBuilder);
 
                        if (GenericMethod != null)
@@ -5140,7 +5211,19 @@ namespace Mono.CSharp {
 
                        SourceMethod source = SourceMethod.Create (parent, MethodBuilder, method.Block);
 
-                       ec.EmitTopBlock (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)) {
+                                       EmitContext ec = method.CreateEmitContext (MethodBuilder.GetILGenerator ());
+                                       if (!ec.HasReturnLabel && bc.HasReturnLabel) {
+                                               ec.ReturnLabel = bc.ReturnLabel;
+                                               ec.HasReturnLabel = true;
+                                       }
+
+                                       block.Emit (ec);
+                               }
+                       }
 
                        if (source != null) {
                                method.EmitExtraSymbolInfo (source);
@@ -5167,14 +5250,14 @@ namespace Mono.CSharp {
                        ModFlags |= Modifiers.PROTECTED;
                }
 
-               public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
-                       if (a.Type == TypeManager.conditional_attribute_type) {
+                       if (a.Type == pa.Conditional) {
                                Error_ConditionalAttributeIsNotValid ();
                                return;
                        }
 
-                       base.ApplyAttributeBuilder (a, cb);
+                       base.ApplyAttributeBuilder (a, cb, pa);
                }
 
                protected override bool CheckBase ()
@@ -5189,14 +5272,14 @@ namespace Mono.CSharp {
 
                        Type base_type = Parent.PartialContainer.BaseCache.Container.Type;
                        if (base_type != null && Block != null) {
-                               MethodGroupExpr method_expr = Expression.MethodLookup (Parent.TypeBuilder, base_type, MetadataName, Location);
+                               MethodGroupExpr method_expr = Expression.MethodLookup (Parent.Module.Compiler, Parent.TypeBuilder, base_type, MetadataName, Location);
                                if (method_expr == null)
                                        throw new NotImplementedException ();
 
                                method_expr.IsBase = true;
                                method_expr.InstanceExpression = new CompilerGeneratedThis (Parent.TypeBuilder, Location);
 
-                               ToplevelBlock new_block = new ToplevelBlock (Block.StartLocation);
+                               ToplevelBlock new_block = new ToplevelBlock (Compiler, Block.StartLocation);
                                new_block.EndLocation = Block.EndLocation;
 
                                Block finaly_block = new ExplicitBlock (new_block, Location, Location);
@@ -5207,7 +5290,7 @@ namespace Mono.CSharp {
                                // TODO: Should use AddScopeStatement or something else which emits correct
                                // debugger scope
                                //
-                               finaly_block.AddStatement (new StatementExpression (new Invocation (method_expr, new ArrayList (0))));
+                               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;
@@ -5248,7 +5331,7 @@ namespace Mono.CSharp {
                {
                        this.ds = generic != null ? generic : (DeclSpace) parent;
                        this.type_name = type;
-                       ModFlags = Modifiers.Check (allowed_mod, mod, def_mod, Location);
+                       ModFlags = Modifiers.Check (allowed_mod, mod, def_mod, Location, Report);
                        GenericMethod = generic;
                        if (GenericMethod != null)
                                GenericMethod.ModFlags = ModFlags;
@@ -5319,11 +5402,9 @@ namespace Mono.CSharp {
                                                      "accessible than field `" + GetSignatureForError () + "'");
                                }
                        }
-#if GMCS_SOURCE
-                       if (MemberType.IsGenericParameter && (MemberType.GenericParameterAttributes & GenericParameterAttributes.Contravariant) != 0) {
-                               Report.Error (-33, Location, "Contravariant type parameters can only be used in input positions");
-                       }
-#endif
+
+                       Variance variance = this is Event ? Variance.Contravariant : Variance.Covariant;
+                       TypeManager.CheckTypeVariance (MemberType, variance, this);
                }
 
                protected bool IsTypePermitted ()
@@ -5351,8 +5432,7 @@ namespace Mono.CSharp {
                        if (member_type != null)
                                throw new InternalErrorException ("Multi-resolve");
 
-                       IResolveContext rc = GenericMethod == null ? this : (IResolveContext) ds;
-                       TypeExpr te = type_name.ResolveAsTypeTerminal (rc, false);
+                       TypeExpr te = type_name.ResolveAsTypeTerminal (this, false);
                        if (te == null)
                                return false;
                        
@@ -5396,9 +5476,9 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
-                       if (a.Type == TypeManager.field_offset_attribute_type) {
+                       if (a.Type == pa.FieldOffset) {
                                status |= Status.HAS_OFFSET;
 
                                if (!Parent.PartialContainer.HasExplicitLayout) {
@@ -5413,14 +5493,14 @@ namespace Mono.CSharp {
                        }
 
 #if NET_2_0
-                       if (a.Type == TypeManager.fixed_buffer_attr_type) {
+                       if (a.Type == pa.FixedBuffer) {
                                Report.Error (1716, Location, "Do not use 'System.Runtime.CompilerServices.FixedBuffer' attribute. Use the 'fixed' field modifier instead");
                                return;
                        }
 #endif
 
 #if !NET_2_0
-                       if (a.Type == TypeManager.marshal_as_attr_type) {
+                       if (a.Type == pa.MarshalAs) {
                                UnmanagedMarshal marshal = a.GetMarshal (this);
                                if (marshal != null) {
                                        FieldBuilder.SetMarshal (marshal);
@@ -5449,7 +5529,7 @@ namespace Mono.CSharp {
                                return true;
                        }
  
-                       if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0) {
+                       if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.BACKING_FIELD)) == 0) {
                                Report.SymbolRelatedToPreviousError (conflict_symbol);
                                Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
                                        GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
@@ -5466,7 +5546,7 @@ namespace Mono.CSharp {
                                return;
 
                        if (MemberType.IsSealed && MemberType.IsAbstract) {
-                               Error_VariableOfStaticClass (Location, GetSignatureForError (), MemberType);
+                               Error_VariableOfStaticClass (Location, GetSignatureForError (), MemberType, Report);
                        }
 
                        CheckBase ();
@@ -5482,10 +5562,11 @@ namespace Mono.CSharp {
 
                public override void Emit ()
                {
-#if GMCS_SOURCE
+                       if (TypeManager.IsDynamicType (member_type))
+                               PredefinedAttributes.Get.Dynamic.EmitAttribute (FieldBuilder);
+
                        if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
-                               FieldBuilder.SetCustomAttribute (TypeManager.GetCompilerGeneratedAttribute (Location));
-#endif
+                               PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (FieldBuilder);
 
                        if (OptAttributes != null) {
                                OptAttributes.Emit ();
@@ -5498,7 +5579,7 @@ namespace Mono.CSharp {
                        base.Emit ();
                }
 
-               public static void Error_VariableOfStaticClass (Location loc, string variable_name, Type static_class)
+               public static void Error_VariableOfStaticClass (Location loc, string variable_name, Type static_class, Report Report)
                {
                        Report.SymbolRelatedToPreviousError (static_class);
                        Report.Error (723, loc, "`{0}': cannot declare variables of static types",
@@ -5622,7 +5703,7 @@ namespace Mono.CSharp {
                        
                        // Create nested fixed buffer container
                        string name = String.Format ("<{0}>__FixedBuffer{1}", Name, GlobalCounter++);
-                       fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name, CodeGen.Module.DefaultCharSetType |
+                       fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name, Parent.Module.DefaultCharSetType |
                                TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit, TypeManager.value_type);
                        
                        element = fixed_buffer_type.DefineField (FixedElementName, MemberType, FieldAttributes.Public);
@@ -5639,8 +5720,8 @@ namespace Mono.CSharp {
                {
                        base.DoMemberTypeIndependentChecks ();
 
-                       if (!Parent.IsInUnsafeScope)
-                               Expression.UnsafeError (Location);
+                       if (!Parent.IsUnsafe)
+                               Expression.UnsafeError (Report, Location);
 
                        if (Parent.PartialContainer.Kind != Kind.Struct) {
                                Report.Error (1642, Location, "`{0}': Fixed size buffer fields may only be members of structs",
@@ -5650,12 +5731,12 @@ namespace Mono.CSharp {
 
                public override void Emit()
                {
-                       EmitContext ec = new EmitContext (this, Parent, Location, null, TypeManager.void_type, ModFlags);
-                       Constant c = size_expr.ResolveAsConstant (ec, this);
+                       ResolveContext rc = new ResolveContext (this);
+                       Constant c = size_expr.ResolveAsConstant (rc, this);
                        if (c == null)
                                return;
                        
-                       IntConstant buffer_size_const = c.ImplicitConversionRequired (ec, TypeManager.int32_type, Location) as IntConstant;
+                       IntConstant buffer_size_const = c.ImplicitConversionRequired (rc, TypeManager.int32_type, Location) as IntConstant;
                        if (buffer_size_const == null)
                                return;
 
@@ -5677,53 +5758,27 @@ namespace Mono.CSharp {
                        buffer_size *= type_size;
                        EmitFieldSize (buffer_size);
 
-#if GMCS_SOURCE
-                       //
-                       // Emit compiler generated fixed buffer type attribute
-                       //
-                       CustomAttributeBuilder cab = TypeManager.unsafe_value_type_attr;
-                       if (cab == null) {
-                               Type attr_type = TypeManager.CoreLookupType (
-                                       "System.Runtime.CompilerServices", "UnsafeValueTypeAttribute", Kind.Class, true);
-
-                               if (attr_type != null) {
-                                       ConstructorInfo ci = TypeManager.GetPredefinedConstructor (attr_type, Location);
-                                       if (ci != null) {
-                                               cab = new CustomAttributeBuilder (ci, new object [0]);
-                                               TypeManager.unsafe_value_type_attr = cab;
-                                       }
-                               }
-                       }
+                       PredefinedAttributes.Get.UnsafeValueType.EmitAttribute (fixed_buffer_type);
 
-                       if (cab != null)
-                               fixed_buffer_type.SetCustomAttribute (cab);
-#endif
                        base.Emit ();
                }
 
                void EmitFieldSize (int buffer_size)
                {
-                       if (TypeManager.struct_layout_attribute_type == null) {
-                               TypeManager.struct_layout_attribute_type = TypeManager.CoreLookupType (
-                                       "System.Runtime.InteropServices", "StructLayoutAttribute", Kind.Class, true);
+                       CustomAttributeBuilder cab;
+                       PredefinedAttribute pa;
 
-                               if (TypeManager.struct_layout_attribute_type == null)
+                       pa = PredefinedAttributes.Get.StructLayout;
+                       if (pa.Constructor == null &&
+                               !pa.ResolveConstructor (Location, TypeManager.short_type))
                                        return;
-                       }
 
+                       // TODO: It's not cleared
                        if (fi == null)
-                               fi = new FieldInfo [] { TypeManager.struct_layout_attribute_type.GetField ("Size") };
-
-                       object [] fi_val = new object [] { buffer_size };
+                               fi = new FieldInfo[] { pa.Type.GetField ("Size") };
 
-                       if (TypeManager.struct_layout_attribute_ctor == null) {
-                               TypeManager.struct_layout_attribute_ctor = TypeManager.GetPredefinedConstructor (
-                                       TypeManager.struct_layout_attribute_type, Location, TypeManager.short_type);
-                               if (TypeManager.struct_layout_attribute_ctor == null)
-                                       return;
-                       }
-
-                       CustomAttributeBuilder cab = new CustomAttributeBuilder (TypeManager.struct_layout_attribute_ctor,
+                       object[] fi_val = new object[] { buffer_size };
+                       cab = new CustomAttributeBuilder (pa.Constructor,
                                ctor_args, fi, fi_val);
                        fixed_buffer_type.SetCustomAttribute (cab);
                        
@@ -5731,25 +5786,14 @@ namespace Mono.CSharp {
                        // Don't emit FixedBufferAttribute attribute for private types
                        //
                        if ((ModFlags & Modifiers.PRIVATE) != 0)
-                               return; 
-
-                       if (TypeManager.fixed_buffer_attr_ctor == null) {
-                               if (TypeManager.fixed_buffer_attr_type == null) {
-                                       TypeManager.fixed_buffer_attr_type = TypeManager.CoreLookupType (
-                                               "System.Runtime.CompilerServices", "FixedBufferAttribute", Kind.Class, true);
-
-                                       if (TypeManager.fixed_buffer_attr_type == null)
-                                               return;
-                               }
+                               return;
 
-                               TypeManager.fixed_buffer_attr_ctor = TypeManager.GetPredefinedConstructor (TypeManager.fixed_buffer_attr_type,
-                                       Location, TypeManager.type_type, TypeManager.int32_type);
-                               
-                               if (TypeManager.fixed_buffer_attr_ctor == null)
-                                       return;
-                       }
+                       pa = PredefinedAttributes.Get.FixedBuffer;
+                       if (pa.Constructor == null &&
+                               !pa.ResolveConstructor (Location, TypeManager.type_type, TypeManager.int32_type))
+                               return;
 
-                       cab = new CustomAttributeBuilder (TypeManager.fixed_buffer_attr_ctor, new object [] { MemberType, buffer_size });
+                       cab = new CustomAttributeBuilder (pa.Constructor, new object[] { MemberType, buffer_size });
                        FieldBuilder.SetCustomAttribute (cab);
                }
 
@@ -5830,7 +5874,8 @@ namespace Mono.CSharp {
                                MemberType == TypeManager.sbyte_type || MemberType == TypeManager.byte_type ||
                                MemberType == TypeManager.short_type || MemberType == TypeManager.ushort_type ||
                                MemberType == TypeManager.int32_type || MemberType == TypeManager.uint32_type ||
-                               MemberType == TypeManager.float_type)
+                               MemberType == TypeManager.float_type ||
+                               MemberType == TypeManager.intptr_type || MemberType == TypeManager.uintptr_type)
                                return true;
 
                        if (TypeManager.IsEnumType (MemberType))
@@ -5876,7 +5921,7 @@ namespace Mono.CSharp {
                                Type[] required_modifier = null;
                                if ((ModFlags & Modifiers.VOLATILE) != 0) {
                                        if (TypeManager.isvolatile_type == null)
-                                               TypeManager.isvolatile_type = TypeManager.CoreLookupType (
+                                               TypeManager.isvolatile_type = TypeManager.CoreLookupType (Compiler,
                                                        "System.Runtime.CompilerServices", "IsVolatile", Kind.Class, true);
 
                                        if (TypeManager.isvolatile_type != null)
@@ -5967,7 +6012,7 @@ namespace Mono.CSharp {
                        Attributes = attrs;
                        Location = loc;
                        Parameters = p;
-                       ModFlags = Modifiers.Check (AllowedModifiers, mod, 0, loc);
+                       ModFlags = Modifiers.Check (AllowedModifiers, mod, 0, loc, RootContext.ToplevelTypes.Compiler.Report);
                }
        }
 
@@ -6031,6 +6076,11 @@ namespace Mono.CSharp {
                        }
                }
 
+               public EmitContext CreateEmitContext (ILGenerator ig)
+               {
+                       return new EmitContext (this, ig, ReturnType);
+               }
+
                public bool IsExcluded ()
                {
                        return false;
@@ -6056,14 +6106,12 @@ namespace Mono.CSharp {
 
                public abstract ParametersCompiled ParameterInfo { get ; }
                public abstract Type ReturnType { get; }
-               public abstract EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig);
 
                #endregion
 
-               public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
-                       if (a.Type == TypeManager.cls_compliant_attribute_type || a.Type == TypeManager.obsolete_attribute_type ||
-                                       a.Type == TypeManager.conditional_attribute_type) {
+                       if (a.Type == pa.CLSCompliant || a.Type == pa.Obsolete || a.Type == pa.Conditional) {
                                Report.Error (1667, a.Location,
                                        "Attribute `{0}' is not valid on property or event accessors. It is valid on `{1}' declarations only",
                                        TypeManager.CSharpName (a.Type), a.GetValidTargets ());
@@ -6086,14 +6134,14 @@ namespace Mono.CSharp {
                                if (return_attributes == null)
                                        return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
 
-                               return_attributes.ApplyAttributeBuilder (a, cb);
+                               return_attributes.ApplyAttributeBuilder (a, cb, pa);
                                return;
                        }
 
-                       ApplyToExtraTarget (a, cb);
+                       ApplyToExtraTarget (a, cb, pa);
                }
 
-               virtual protected void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb)
+               protected virtual void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
                        throw new NotSupportedException ("You forgot to define special attribute target handling");
                }
@@ -6108,12 +6156,16 @@ namespace Mono.CSharp {
                {
                        method_data.Emit (parent);
 
-#if GMCS_SOURCE                        
                        if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
-                               method_data.MethodBuilder.SetCustomAttribute (TypeManager.GetCompilerGeneratedAttribute (Location));
+                               PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (method_data.MethodBuilder);
                        if (((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0))
-                               method_data.MethodBuilder.SetCustomAttribute (TypeManager.GetDebuggerHiddenAttribute (Location));
-#endif                 
+                               PredefinedAttributes.Get.DebuggerHidden.EmitAttribute (method_data.MethodBuilder);
+
+                       if (TypeManager.IsDynamicType (ReturnType)) {
+                               return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
+                               return_attributes.EmitPredefined (PredefinedAttributes.Get.Dynamic, Location);
+                       }
+
                        if (OptAttributes != null)
                                OptAttributes.Emit ();
 
@@ -6215,7 +6267,7 @@ namespace Mono.CSharp {
                                
                                method_data = new MethodData (method, ModFlags, flags, this);
 
-                               if (!method_data.Define (parent, method.GetFullName (MemberName)))
+                               if (!method_data.Define (parent, method.GetFullName (MemberName), Report))
                                        return null;
 
                                return method_data.MethodBuilder;
@@ -6259,17 +6311,17 @@ namespace Mono.CSharp {
                                this.parameters = accessor.Parameters;
                        }
 
-                       protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
+                       protected override void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                        {
                                if (a.Target == AttributeTargets.Parameter) {
                                        if (param_attr == null)
                                                param_attr = new ImplicitParameter (method_data.MethodBuilder);
 
-                                       param_attr.ApplyAttributeBuilder (a, cb);
+                                       param_attr.ApplyAttributeBuilder (a, cb, pa);
                                        return;
                                }
 
-                               base.ApplyAttributeBuilder (a, cb);
+                               base.ApplyAttributeBuilder (a, cb, pa);
                        }
 
                        public override ParametersCompiled ParameterInfo {
@@ -6280,7 +6332,7 @@ namespace Mono.CSharp {
 
                        public override MethodBuilder Define (DeclSpace parent)
                        {
-                               parameters.Resolve (ResolveContext);
+                               parameters.Resolve (this);
                                
                                base.Define (parent);
 
@@ -6289,7 +6341,7 @@ namespace Mono.CSharp {
 
                                method_data = new MethodData (method, ModFlags, flags, this);
 
-                               if (!method_data.Define (parent, method.GetFullName (MemberName)))
+                               if (!method_data.Define (parent, method.GetFullName (MemberName), Report))
                                        return null;
 
                                return method_data.MethodBuilder;
@@ -6334,13 +6386,13 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+                       public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                        {
                                if (a.IsInternalMethodImplAttribute) {
                                        method.is_external_implementation = true;
                                }
 
-                               base.ApplyAttributeBuilder (a, cb);
+                               base.ApplyAttributeBuilder (a, cb, pa);
                        }
 
                        public override AttributeTargets AttributeTargets {
@@ -6398,7 +6450,7 @@ namespace Mono.CSharp {
                                CheckAbstractAndExtern (block != null);
 
                                if (block != null && block.IsIterator)
-                                       Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
+                                       Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags, Compiler);
 
                                return null;
                        }
@@ -6415,13 +6467,6 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       public override EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
-                       {
-                               return new EmitContext (this,
-                                       ds, method.ds, method.Location, ig, ReturnType,
-                                       method.ModFlags, false);
-                       }
-
                        public override ObsoleteAttribute GetObsoleteAttribute ()
                        {
                                return method.GetObsoleteAttribute ();
@@ -6462,7 +6507,7 @@ namespace Mono.CSharp {
                                if ((caching_flags & Flags.MethodOverloadsExist) == 0)
                                        return true;
 
-                               return Parent.MemberCache.CheckExistingMembersOverloads (this, Name, ParameterInfo);
+                               return Parent.MemberCache.CheckExistingMembersOverloads (this, Name, ParameterInfo, Report);
                        }
                }
 
@@ -6480,7 +6525,7 @@ namespace Mono.CSharp {
                         this.define_set_first = define_set_first;
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
                        if (a.HasSecurityAttribute) {
                                a.Error_InvalidSecurityParent ();
@@ -6622,8 +6667,13 @@ namespace Mono.CSharp {
                        // case, we do not actually emit the ".property", so there is nowhere to
                        // put the attribute
                        //
-                       if (PropertyBuilder != null && OptAttributes != null)
-                               OptAttributes.Emit ();
+                       if (PropertyBuilder != null) {
+                               if (OptAttributes != null)
+                                       OptAttributes.Emit ();
+
+                               if (TypeManager.IsDynamicType (member_type))
+                                       PredefinedAttributes.Get.Dynamic.EmitAttribute (PropertyBuilder);
+                       }
 
                        if (!Get.IsDummy)
                                Get.Emit (Parent);
@@ -6761,12 +6811,12 @@ namespace Mono.CSharp {
                                fe.InstanceExpression = new CompilerGeneratedThis (fe.Type, Location);
 
                        // Create get block
-                       Get.Block = new ToplevelBlock (ParametersCompiled.EmptyReadOnlyParameters, Location);
+                       Get.Block = new ToplevelBlock (Compiler, ParametersCompiled.EmptyReadOnlyParameters, Location);
                        Return r = new Return (fe, Location);
                        Get.Block.AddStatement (r);
 
                        // Create set block
-                       Set.Block = new ToplevelBlock (Set.ParameterInfo, Location);
+                       Set.Block = new ToplevelBlock (Compiler, Set.ParameterInfo, Location);
                        Assign a = new SimpleAssign (fe, new SimpleName ("value", Location));
                        Set.Block.AddStatement (new StatementExpression (a));
                }
@@ -6997,7 +7047,7 @@ namespace Mono.CSharp {
                sealed class AddDelegateMethod: AEventPropertyAccessor
                {
                        public AddDelegateMethod (Event method, Accessor accessor):
-                               base (method, accessor, "add_")
+                               base (method, accessor, AddPrefix)
                        {
                        }
                }
@@ -7005,7 +7055,7 @@ namespace Mono.CSharp {
                sealed class RemoveDelegateMethod: AEventPropertyAccessor
                {
                        public RemoveDelegateMethod (Event method, Accessor accessor):
-                               base (method, accessor, "remove_")
+                               base (method, accessor, RemovePrefix)
                        {
                        }
                }
@@ -7063,7 +7113,7 @@ namespace Mono.CSharp {
                                        if ((method.ModFlags & Modifiers.STATIC) == 0)
                                                f_expr.InstanceExpression = new CompilerGeneratedThis (field_info.FieldType, Location);
 
-                                       block = new ToplevelBlock (ParameterInfo, Location);
+                                       block = new ToplevelBlock (Compiler, ParameterInfo, Location);
                                        block.AddStatement (new StatementExpression (
                                                new CompoundAssign (Operation,
                                                        f_expr,
@@ -7079,7 +7129,7 @@ namespace Mono.CSharp {
                sealed class AddDelegateMethod: EventFieldAccessor
                {
                        public AddDelegateMethod (Event method):
-                               base (method, "add_")
+                               base (method, AddPrefix)
                        {
                        }
 
@@ -7091,7 +7141,7 @@ namespace Mono.CSharp {
                sealed class RemoveDelegateMethod: EventFieldAccessor
                {
                        public RemoveDelegateMethod (Event method):
-                               base (method, "remove_")
+                               base (method, RemovePrefix)
                        {
                        }
 
@@ -7114,22 +7164,22 @@ namespace Mono.CSharp {
                        Remove = new RemoveDelegateMethod (this);
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
                        if (a.Target == AttributeTargets.Field) {
-                               BackingField.ApplyAttributeBuilder (a, cb);
+                               BackingField.ApplyAttributeBuilder (a, cb, pa);
                                return;
                        }
 
                        if (a.Target == AttributeTargets.Method) {
                                int errors = Report.Errors;
-                               Add.ApplyAttributeBuilder (a, cb);
+                               Add.ApplyAttributeBuilder (a, cb, pa);
                                if (errors == Report.Errors)
-                                       Remove.ApplyAttributeBuilder (a, cb);
+                                       Remove.ApplyAttributeBuilder (a, cb, pa);
                                return;
                        }
 
-                       base.ApplyAttributeBuilder (a, cb);
+                       base.ApplyAttributeBuilder (a, cb, pa);
                }
 
                public override bool Define()
@@ -7142,14 +7192,16 @@ namespace Mono.CSharp {
                                        GetSignatureForError ());
                        }
 
-                       if (!HasBackingField)
+                       if (!HasBackingField) {
+                               SetMemberIsUsed ();
                                return true;
+                       }
 
                        // FIXME: We are unable to detect whether generic event is used because
                        // we are using FieldExpr instead of EventExpr for event access in that
                        // case.  When this issue will be fixed this hack can be removed.
                        if (TypeManager.IsGenericType (MemberType))
-                               SetMemberIsUsed();
+                               SetMemberIsUsed ();
 
                        if (Add.IsInterfaceImplementation)
                                SetMemberIsUsed ();
@@ -7191,6 +7243,9 @@ namespace Mono.CSharp {
 
                        static readonly string[] attribute_targets = new string [] { "method", "param", "return" };
 
+                       public const string AddPrefix = "add_";
+                       public const string RemovePrefix = "remove_";
+
                        protected AEventAccessor (Event method, string prefix)
                                : base (method, prefix)
                        {
@@ -7209,26 +7264,26 @@ namespace Mono.CSharp {
                                get { return method_data.implementing != null; }
                        }
 
-                       public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+                       public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                        {
                                if (a.IsInternalMethodImplAttribute) {
                                        method.is_external_implementation = true;
                                }
 
-                               base.ApplyAttributeBuilder (a, cb);
+                               base.ApplyAttributeBuilder (a, cb, pa);
                        }
 
-                       protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
+                       protected override void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                        {
                                if (a.Target == AttributeTargets.Parameter) {
                                        if (param_attr == null)
                                                param_attr = new ImplicitParameter (method_data.MethodBuilder);
 
-                                       param_attr.ApplyAttributeBuilder (a, cb);
+                                       param_attr.ApplyAttributeBuilder (a, cb, pa);
                                        return;
                                }
 
-                               base.ApplyAttributeBuilder (a, cb);
+                               base.ApplyAttributeBuilder (a, cb, pa);
                        }
 
                        public override AttributeTargets AttributeTargets {
@@ -7247,7 +7302,7 @@ namespace Mono.CSharp {
                                method_data = new MethodData (method, method.ModFlags,
                                        method.flags | MethodAttributes.HideBySig | MethodAttributes.SpecialName, this);
 
-                               if (!method_data.Define (parent, method.GetFullName (MemberName)))
+                               if (!method_data.Define (parent, method.GetFullName (MemberName), Report))
                                        return null;
 
                                MethodBuilder mb = method_data.MethodBuilder;
@@ -7261,13 +7316,6 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       public override EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
-                       {
-                               return new EmitContext (
-                                       this, method.Parent, Location, ig, ReturnType,
-                                       method.ModFlags, false);
-                       }
-
                        public override ObsoleteAttribute GetObsoleteAttribute ()
                        {
                                return method.GetObsoleteAttribute ();
@@ -7317,7 +7365,7 @@ namespace Mono.CSharp {
                {
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
                        if ((a.HasSecurityAttribute)) {
                                a.Error_InvalidSecurityParent ();
@@ -7441,7 +7489,7 @@ namespace Mono.CSharp {
 
                        public override MethodBuilder Define (DeclSpace parent)
                        {
-                               parameters.Resolve (ResolveContext);
+                               parameters.Resolve (this);
                                return base.Define (parent);
                        }
                        
@@ -7527,7 +7575,7 @@ namespace Mono.CSharp {
 
                protected override bool CheckForDuplications ()
                {
-                       return Parent.MemberCache.CheckExistingMembersOverloads (this, GetFullName (MemberName), parameters);
+                       return Parent.MemberCache.CheckExistingMembersOverloads (this, GetFullName (MemberName), parameters, Report);
                }
                
                public override bool Define ()
@@ -7538,8 +7586,8 @@ namespace Mono.CSharp {
                        if (!DefineParameters (parameters))
                                return false;
 
-                       if (OptAttributes != null && TypeManager.indexer_name_type != null) {
-                               Attribute indexer_attr = OptAttributes.Search (TypeManager.indexer_name_type);
+                       if (OptAttributes != null) {
+                               Attribute indexer_attr = OptAttributes.Search (PredefinedAttributes.Get.IndexerName);
                                if (indexer_attr != null) {
                                        // Remove the attribute from the list because it is not emitted
                                        OptAttributes.Attrs.Remove (indexer_attr);
@@ -7742,14 +7790,14 @@ namespace Mono.CSharp {
                        Block = block;
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
-                       if (a.Type == TypeManager.conditional_attribute_type) {
+                       if (a.Type == pa.Conditional) {
                                Error_ConditionalAttributeIsNotValid ();
                                return;
                        }
 
-                       base.ApplyAttributeBuilder (a, cb);
+                       base.ApplyAttributeBuilder (a, cb, pa);
                }
                
                public override bool Define ()
@@ -7764,9 +7812,9 @@ namespace Mono.CSharp {
 
                        // imlicit and explicit operator of same types are not allowed
                        if (OperatorType == OpType.Explicit)
-                               Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Implicit), Parameters);
+                               Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Implicit), Parameters, Report);
                        else if (OperatorType == OpType.Implicit)
-                               Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Explicit), Parameters);
+                               Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Explicit), Parameters, Report);
 
                        Type declaring_type = MethodData.DeclaringType;
                        Type return_type = MemberType;
@@ -7774,11 +7822,19 @@ namespace Mono.CSharp {
                        
                        Type first_arg_type_unwrap = first_arg_type;
                        if (TypeManager.IsNullableType (first_arg_type))
-                               first_arg_type_unwrap = TypeManager.GetTypeArguments (first_arg_type) [0];
+                               first_arg_type_unwrap = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (first_arg_type) [0]);
                        
                        Type return_type_unwrap = return_type;
                        if (TypeManager.IsNullableType (return_type))
-                               return_type_unwrap = TypeManager.GetTypeArguments (return_type) [0];                    
+                               return_type_unwrap = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (return_type) [0]);
+
+                       if (TypeManager.IsDynamicType (return_type) || TypeManager.IsDynamicType (first_arg_type)) {
+                               Report.Error (1964, Location,
+                                       "User-defined operator `{0}' cannot convert to or from the dynamic type",
+                                       GetSignatureForError ());
+
+                               return false;
+                       }
 
                        //
                        // Rules for conversion operators