Add more incomplete statements to AST. Fixes #4361.
[mono.git] / mcs / mcs / attribute.cs
index 57b04be27f1496ab3e1e587c644156d348c524b5..ae9627e93be4b709c507235b19a347622c154bc1 100644 (file)
@@ -8,6 +8,7 @@
 //
 // Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
 // Copyright 2003-2008 Novell, Inc.
+// Copyright 2011 Xamarin Inc
 //
 
 using System;
@@ -21,10 +22,12 @@ using System.IO;
 
 #if STATIC
 using SecurityType = System.Collections.Generic.List<IKVM.Reflection.Emit.CustomAttributeBuilder>;
+using BadImageFormat = IKVM.Reflection.BadImageFormatException;
 using IKVM.Reflection;
 using IKVM.Reflection.Emit;
 #else
 using SecurityType = System.Collections.Generic.Dictionary<System.Security.Permissions.SecurityAction, System.Security.PermissionSet>;
+using BadImageFormat = System.BadImageFormatException;
 using System.Reflection;
 using System.Reflection.Emit;
 #endif
@@ -48,9 +51,9 @@ namespace Mono.CSharp {
                        if (attributes == null)
                                attributes = attrs;
                        else
-                               throw new NotImplementedException ();
+                               attributes.AddAttributes (attrs.Attrs);
 
-                       attributes.AttachTo (this, context);
+                       attrs.AttachTo (this, context);
                }
 
                public Attributes OptAttributes {
@@ -81,23 +84,24 @@ namespace Mono.CSharp {
                public abstract string[] ValidAttributeTargets { get; }
        };
 
-       public class Attribute : Expression
+       public class Attribute
        {
                public readonly string ExplicitTarget;
                public AttributeTargets Target;
                readonly ATypeNameExpression expression;
 
-               Arguments PosArguments;
-               Arguments NamedArguments;
+               Arguments pos_args, named_args;
 
                bool resolve_error;
                bool arg_resolved;
                readonly bool nameEscaped;
+               readonly Location loc;
+               public TypeSpec Type;   
 
                //
                // An attribute can be attached to multiple targets (e.g. multiple fields)
                //
-               protected Attributable[] targets;
+               Attributable[] targets;
 
                //
                // A member context for the attribute, it's much easier to hold it here
@@ -114,14 +118,38 @@ namespace Mono.CSharp {
                {
                        this.expression = expr;
                        if (args != null) {
-                               PosArguments = args [0];
-                               NamedArguments = args [1];                              
+                               pos_args = args[0];
+                               named_args = args[1];
                        }
                        this.loc = loc;
                        ExplicitTarget = target;
                        this.nameEscaped = nameEscaped;
                }
 
+               public Location Location {
+                       get {
+                               return loc;
+                       }
+               }
+
+               public Arguments NamedArguments {
+                       get {
+                               return named_args;
+                       }
+               }
+
+               public Arguments PositionalArguments {
+                       get {
+                               return pos_args;
+                       }
+               }
+
+               public ATypeNameExpression TypeExpression {
+                       get {
+                               return expression;
+                       }
+               }
+
                void AddModuleCharSet (ResolveContext rc)
                {
                        const string dll_import_char_set = "CharSet";
@@ -137,7 +165,7 @@ namespace Mono.CSharp {
                        }
 
                        if (NamedArguments == null)
-                               NamedArguments = new Arguments (1);
+                               named_args = new Arguments (1);
 
                        var value = Constant.CreateConstant (rc.Module.PredefinedTypes.CharSet.TypeSpec, rc.Module.DefaultCharSet, Location);
                        NamedArguments.Add (new NamedArgument (dll_import_char_set, loc, value));
@@ -146,8 +174,8 @@ namespace Mono.CSharp {
                public Attribute Clone ()
                {
                        Attribute a = new Attribute (ExplicitTarget, expression, null, loc, nameEscaped);
-                       a.PosArguments = PosArguments;
-                       a.NamedArguments = NamedArguments;
+                       a.pos_args = pos_args;
+                       a.named_args = NamedArguments;
                        return a;
                }
 
@@ -164,6 +192,13 @@ namespace Mono.CSharp {
                                return;
                        }
 
+                       // When re-attaching global attributes
+                       if (context is NamespaceContainer) {
+                               this.targets[0] = target;
+                               this.context = context;
+                               return;
+                       }
+
                        // Resize target array
                        Attributable[] new_array = new Attributable [this.targets.Length + 1];
                        targets.CopyTo (new_array, 0);
@@ -238,37 +273,28 @@ namespace Mono.CSharp {
                        }
                }
 
-               TypeSpec ResolvePossibleAttributeType (ATypeNameExpression expr, ref bool is_attr)
-               {
-                       TypeExpr te = expr.ResolveAsTypeTerminal (context, false);
-                       if (te == null)
-                               return null;
-
-                       TypeSpec t = te.Type;
-                       if (t.IsAttribute) {
-                               is_attr = true;
-                       } else {
-                               Report.SymbolRelatedToPreviousError (t);
-                               Report.Error (616, Location, "`{0}': is not an attribute class", TypeManager.CSharpName (t));
-                       }
-                       return t;
-               }
-
                /// <summary>
                ///   Tries to resolve the type of the attribute. Flags an error if it can't, and complain is true.
                /// </summary>
                void ResolveAttributeType ()
                {
                        SessionReportPrinter resolve_printer = new SessionReportPrinter ();
-                       ReportPrinter prev_recorder = context.Module.Compiler.Report.SetPrinter (resolve_printer);
+                       ReportPrinter prev_recorder = Report.SetPrinter (resolve_printer);
 
                        bool t1_is_attr = false;
                        bool t2_is_attr = false;
                        TypeSpec t1, t2;
                        ATypeNameExpression expanded = null;
 
+                       // TODO: Additional warnings such as CS0436 are swallowed because we don't
+                       // print on success
+
                        try {
-                               t1 = ResolvePossibleAttributeType (expression, ref t1_is_attr);
+                               t1 = expression.ResolveAsType (context);
+                               if (t1 != null)
+                                       t1_is_attr = t1.IsAttribute;
+
+                               resolve_printer.EndSession ();
 
                                if (nameEscaped) {
                                        t2 = null;
@@ -276,15 +302,15 @@ namespace Mono.CSharp {
                                        expanded = (ATypeNameExpression) expression.Clone (null);
                                        expanded.Name += "Attribute";
 
-                                       t2 = ResolvePossibleAttributeType (expanded, ref t2_is_attr);
+                                       t2 = expanded.ResolveAsType (context);
+                                       if (t2 != null)
+                                               t2_is_attr = t2.IsAttribute;
                                }
-
-                               resolve_printer.EndSession ();
                        } finally {
                                context.Module.Compiler.Report.SetPrinter (prev_recorder);
                        }
 
-                       if (t1_is_attr && t2_is_attr) {
+                       if (t1_is_attr && t2_is_attr && t1 != t2) {
                                Report.Error (1614, Location, "`{0}' is ambiguous between `{1}' and `{2}'. Use either `@{0}' or `{0}Attribute'",
                                        GetSignatureForError (), expression.GetSignatureForError (), expanded.GetSignatureForError ());
                                resolve_error = true;
@@ -301,8 +327,23 @@ namespace Mono.CSharp {
                                return;
                        }
 
-                       resolve_printer.Merge (prev_recorder);
                        resolve_error = true;
+
+                       if (t1 != null) {
+                               resolve_printer.Merge (prev_recorder);
+
+                               Report.SymbolRelatedToPreviousError (t1);
+                               Report.Error (616, Location, "`{0}': is not an attribute class", t1.GetSignatureForError ());
+                               return;
+                       }
+
+                       if (t2 != null) {
+                               Report.SymbolRelatedToPreviousError (t2);
+                               Report.Error (616, Location, "`{0}': is not an attribute class", t2.GetSignatureForError ());
+                               return;
+                       }
+
+                       resolve_printer.Merge (prev_recorder);
                }
 
                public TypeSpec ResolveType ()
@@ -312,7 +353,7 @@ namespace Mono.CSharp {
                        return Type;
                }
 
-               public override string GetSignatureForError ()
+               public string GetSignatureForError ()
                {
                        if (Type != null)
                                return TypeManager.CSharpName (Type);
@@ -323,13 +364,13 @@ namespace Mono.CSharp {
                public bool HasSecurityAttribute {
                        get {
                                PredefinedAttribute pa = context.Module.PredefinedAttributes.Security;
-                               return pa.IsDefined && TypeSpec.IsBaseClass (type, pa.TypeSpec, false);
+                               return pa.IsDefined && TypeSpec.IsBaseClass (Type, pa.TypeSpec, false);
                        }
                }
 
                public bool IsValidSecurityAttribute ()
                {
-                       return HasSecurityAttribute && IsSecurityActionValid (false);
+                       return HasSecurityAttribute && IsSecurityActionValid ();
                }
 
                static bool IsValidArgumentType (TypeSpec t)
@@ -403,14 +444,14 @@ namespace Mono.CSharp {
 
                        MethodSpec ctor;
                        // Try if the attribute is simple and has been resolved before
-                       if (PosArguments != null || !context.Module.AttributeConstructorCache.TryGetValue (Type, out ctor)) {
+                       if (pos_args != null || !context.Module.AttributeConstructorCache.TryGetValue (Type, out ctor)) {
                                rc = CreateResolveContext ();
                                ctor = ResolveConstructor (rc);
                                if (ctor == null) {
                                        return null;
                                }
 
-                               if (PosArguments == null && ctor.Parameters.IsEmpty)
+                               if (pos_args == null && ctor.Parameters.IsEmpty)
                                        context.Module.AttributeConstructorCache.Add (Type, ctor);
                        }
 
@@ -418,7 +459,7 @@ namespace Mono.CSharp {
                        // Add [module: DefaultCharSet] to all DllImport import attributes
                        //
                        var module = context.Module;
-                       if (Type == module.PredefinedAttributes.DllImport && module.HasDefaultCharSet) {
+                       if ((Type == module.PredefinedAttributes.DllImport || Type == module.PredefinedAttributes.UnmanagedFunctionPointer) && module.HasDefaultCharSet) {
                                if (rc == null)
                                        rc = CreateResolveContext ();
 
@@ -439,16 +480,16 @@ namespace Mono.CSharp {
 
                MethodSpec ResolveConstructor (ResolveContext ec)
                {
-                       if (PosArguments != null) {
+                       if (pos_args != null) {
                                bool dynamic;
-                               PosArguments.Resolve (ec, out dynamic);
+                               pos_args.Resolve (ec, out dynamic);
                                if (dynamic) {
                                        Error_AttributeArgumentIsDynamic (ec.MemberContext, loc);
                                        return null;
                                }
                        }
 
-                       return ConstructorLookup (ec, Type, ref PosArguments, loc);
+                       return Expression.ConstructorLookup (ec, Type, ref pos_args, loc);
                }
 
                bool ResolveNamedArguments (ResolveContext ec)
@@ -469,10 +510,10 @@ namespace Mono.CSharp {
 
                                a.Resolve (ec);
 
-                               Expression member = Expression.MemberLookup (ec, false, Type, name, 0, MemberLookupRestrictions.ExactArity, loc);
+                               Expression member = Expression.MemberLookup (ec, false, Type, name, 0, Expression.MemberLookupRestrictions.ExactArity, loc);
 
                                if (member == null) {
-                                       member = Expression.MemberLookup (ec, true, Type, name, 0, MemberLookupRestrictions.ExactArity, loc);
+                                       member = Expression.MemberLookup (ec, true, Type, name, 0, Expression.MemberLookupRestrictions.ExactArity, loc);
 
                                        if (member != null) {
                                                // TODO: ec.Report.SymbolRelatedToPreviousError (member);
@@ -608,7 +649,7 @@ namespace Mono.CSharp {
                        if (resolve_error)
                                return DefaultUsageAttribute;
 
-                       AttributeUsageAttribute usage_attribute = new AttributeUsageAttribute ((AttributeTargets)((Constant) PosArguments [0].Expr).GetValue ());
+                       AttributeUsageAttribute usage_attribute = new AttributeUsageAttribute ((AttributeTargets) ((Constant) pos_args[0].Expr).GetValue ());
 
                        var field = GetNamedValue ("AllowMultiple") as BoolConstant;
                        if (field != null)
@@ -631,10 +672,10 @@ namespace Mono.CSharp {
                                // But because a lot of attribute class code must be rewritten will be better to wait...
                                Resolve ();
 
-                       if (resolve_error)
+                       if (resolve_error || pos_args.Count != 1 || !(pos_args[0].Expr is Constant))
                                return null;
 
-                       return ((Constant) PosArguments [0].Expr).GetValue () as string;
+                       return ((Constant) pos_args[0].Expr).GetValue () as string;
                }
 
                /// <summary>
@@ -650,7 +691,7 @@ namespace Mono.CSharp {
                        if (resolve_error)
                                return null;
 
-                       return ((Constant) PosArguments[0].Expr).GetValue () as string;
+                       return ((Constant) pos_args[0].Expr).GetValue () as string;
                }
 
                /// <summary>
@@ -660,7 +701,7 @@ namespace Mono.CSharp {
                {
                        if (!arg_resolved) {
                                // corlib only case when obsolete is used before is resolved
-                               var c = type.MemberDefinition as Class;
+                               var c = Type.MemberDefinition as Class;
                                if (c != null && !c.HasMembersDefined)
                                        c.Define ();
                                
@@ -672,14 +713,14 @@ namespace Mono.CSharp {
                        if (resolve_error)
                                return null;
 
-                       if (PosArguments == null)
+                       if (pos_args == null)
                                return new ObsoleteAttribute ();
 
-                       string msg = ((Constant) PosArguments[0].Expr).GetValue () as string;
-                       if (PosArguments.Count == 1)
+                       string msg = ((Constant) pos_args[0].Expr).GetValue () as string;
+                       if (pos_args.Count == 1)
                                return new ObsoleteAttribute (msg);
 
-                       return new ObsoleteAttribute (msg, ((BoolConstant) PosArguments[1].Expr).Value);
+                       return new ObsoleteAttribute (msg, ((BoolConstant) pos_args[1].Expr).Value);
                }
 
                /// <summary>
@@ -697,7 +738,7 @@ namespace Mono.CSharp {
                        if (resolve_error)
                                return false;
 
-                       return ((BoolConstant) PosArguments[0].Expr).Value;
+                       return ((BoolConstant) pos_args[0].Expr).Value;
                }
 
                public TypeSpec GetCoClassAttributeValue ()
@@ -747,9 +788,10 @@ namespace Mono.CSharp {
                /// <summary>
                /// Tests permitted SecurityAction for assembly or other types
                /// </summary>
-               protected virtual bool IsSecurityActionValid (bool for_assembly)
+               bool IsSecurityActionValid ()
                {
                        SecurityAction action = GetSecurityActionValue ();
+                       bool for_assembly = Target == AttributeTargets.Assembly || Target == AttributeTargets.Module;
 
                        switch (action) {
 #pragma warning disable 618
@@ -782,7 +824,7 @@ namespace Mono.CSharp {
 
                System.Security.Permissions.SecurityAction GetSecurityActionValue ()
                {
-                       return (SecurityAction) ((Constant) PosArguments[0].Expr).GetValue ();
+                       return (SecurityAction) ((Constant) pos_args[0].Expr).GetValue ();
                }
 
                /// <summary>
@@ -792,9 +834,9 @@ namespace Mono.CSharp {
                public void ExtractSecurityPermissionSet (MethodSpec ctor, ref SecurityType permissions)
                {
 #if STATIC
-                       object[] values = new object [PosArguments.Count];
+                       object[] values = new object[pos_args.Count];
                        for (int i = 0; i < values.Length; ++i)
-                               values [i] = ((Constant) PosArguments [i].Expr).GetValue ();
+                               values[i] = ((Constant) pos_args[i].Expr).GetValue ();
 
                        PropertyInfo[] prop;
                        object[] prop_values;
@@ -835,7 +877,7 @@ namespace Mono.CSharp {
 
                public CharSet GetCharSetValue ()
                {
-                       return (CharSet)System.Enum.Parse (typeof (CharSet), ((Constant) PosArguments [0].Expr).GetValue ().ToString ());
+                       return (CharSet) System.Enum.Parse (typeof (CharSet), ((Constant) pos_args[0].Expr).GetValue ().ToString ());
                }
 
                public bool HasField (string fieldName)
@@ -855,16 +897,21 @@ namespace Mono.CSharp {
                // Returns true for MethodImplAttribute with MethodImplOptions.InternalCall value
                // 
                public bool IsInternalCall ()
+               {
+                       return (GetMethodImplOptions () & MethodImplOptions.InternalCall) != 0;
+               }
+
+               public MethodImplOptions GetMethodImplOptions ()
                {
                        MethodImplOptions options = 0;
-                       if (PosArguments.Count == 1) {
-                               options = (MethodImplOptions) System.Enum.Parse (typeof (MethodImplOptions), ((Constant) PosArguments[0].Expr).GetValue ().ToString ());
+                       if (pos_args.Count == 1) {
+                               options = (MethodImplOptions) System.Enum.Parse (typeof (MethodImplOptions), ((Constant) pos_args[0].Expr).GetValue ().ToString ());
                        } else if (HasField ("Value")) {
                                var named = GetNamedValue ("Value");
                                options = (MethodImplOptions) System.Enum.Parse (typeof (MethodImplOptions), named.GetValue ().ToString ());
                        }
 
-                       return (options & MethodImplOptions.InternalCall) != 0;
+                       return options;
                }
 
                //
@@ -872,19 +919,19 @@ namespace Mono.CSharp {
                // 
                public bool IsExplicitLayoutKind ()
                {
-                       if (PosArguments == null || PosArguments.Count != 1)
+                       if (pos_args == null || pos_args.Count != 1)
                                return false;
 
-                       var value = (LayoutKind) System.Enum.Parse (typeof (LayoutKind), ((Constant) PosArguments[0].Expr).GetValue ().ToString ());
+                       var value = (LayoutKind) System.Enum.Parse (typeof (LayoutKind), ((Constant) pos_args[0].Expr).GetValue ().ToString ());
                        return value == LayoutKind.Explicit;
                }
 
                public Expression GetParameterDefaultValue ()
                {
-                       if (PosArguments == null)
+                       if (pos_args == null)
                                return null;
 
-                       return PosArguments[0].Expr;
+                       return pos_args[0].Expr;
                }
 
                public override bool Equals (object obj)
@@ -898,7 +945,7 @@ namespace Mono.CSharp {
 
                public override int GetHashCode ()
                {
-                       return type.GetHashCode () ^ Target.GetHashCode ();
+                       return Type.GetHashCode () ^ Target.GetHashCode ();
                }
 
                /// <summary>
@@ -921,22 +968,23 @@ namespace Mono.CSharp {
                        }
 
                        byte[] cdata;
-                       if (PosArguments == null && named_values == null) {
+                       if (pos_args == null && named_values == null) {
                                cdata = AttributeEncoder.Empty;
                        } else {
                                AttributeEncoder encoder = new AttributeEncoder ();
 
-                               if (PosArguments != null) {
+                               if (pos_args != null) {
                                        var param_types = ctor.Parameters.Types;
-                                       for (int j = 0; j < PosArguments.Count; ++j) {
+                                       for (int j = 0; j < pos_args.Count; ++j) {
                                                var pt = param_types[j];
-                                               var arg_expr = PosArguments[j].Expr;
+                                               var arg_expr = pos_args[j].Expr;
                                                if (j == 0) {
-                                                       if (Type == predefined.IndexerName || Type == predefined.Conditional) {
-                                                               string v = ((StringConstant) arg_expr).Value;
-                                                               if (!Tokenizer.IsValidIdentifier (v) || Tokenizer.IsKeyword (v)) {
+                                                       if ((Type == predefined.IndexerName || Type == predefined.Conditional) && arg_expr is Constant) {
+                                                               string v = ((Constant) arg_expr).GetValue () as string;
+                                                               if (!Tokenizer.IsValidIdentifier (v) || (Type == predefined.IndexerName && Tokenizer.IsKeyword (v))) {
                                                                        context.Module.Compiler.Report.Error (633, arg_expr.Location,
                                                                                "The argument to the `{0}' attribute must be a valid identifier", GetSignatureForError ());
+                                                                       return;
                                                                }
                                                        } else if (Type == predefined.Guid) {
                                                                try {
@@ -953,15 +1001,15 @@ namespace Mono.CSharp {
                                                                                "System.AttributeUsage");
                                                                }
                                                        } else if (Type == predefined.MarshalAs) {
-                                                               if (PosArguments.Count == 1) {
-                                                                       var u_type = (UnmanagedType) System.Enum.Parse (typeof (UnmanagedType), ((Constant) PosArguments[0].Expr).GetValue ().ToString ());
+                                                               if (pos_args.Count == 1) {
+                                                                       var u_type = (UnmanagedType) System.Enum.Parse (typeof (UnmanagedType), ((Constant) pos_args[0].Expr).GetValue ().ToString ());
                                                                        if (u_type == UnmanagedType.ByValArray && !(Owner is FieldBase)) {
                                                                                Error_AttributeEmitError ("Specified unmanaged type is only valid on fields");
                                                                        }
                                                                }
                                                        } else if (Type == predefined.DllImport) {
-                                                               if (PosArguments.Count == 1) {
-                                                                       var value = ((Constant) PosArguments[0].Expr).GetValue () as string;
+                                                               if (pos_args.Count == 1 && pos_args[0].Expr is Constant) {
+                                                                       var value = ((Constant) pos_args[0].Expr).GetValue () as string;
                                                                        if (string.IsNullOrEmpty (value))
                                                                                Error_AttributeEmitError ("DllName cannot be empty");
                                                                }
@@ -999,6 +1047,9 @@ namespace Mono.CSharp {
                                foreach (Attributable target in targets)
                                        target.ApplyAttributeBuilder (this, ctor, cdata, predefined);
                        } catch (Exception e) {
+                               if (e is BadImageFormat && Report.Errors > 0)
+                                       return;
+
                                Error_AttributeEmitError (e.Message);
                                return;
                        }
@@ -1021,8 +1072,8 @@ namespace Mono.CSharp {
 
                        // Here we are testing attribute arguments for array usage (error 3016)
                        if (Owner.IsClsComplianceRequired ()) {
-                               if (PosArguments != null)
-                                       PosArguments.CheckArrayAsAttribute (context.Module.Compiler);
+                               if (pos_args != null)
+                                       pos_args.CheckArrayAsAttribute (context.Module.Compiler);
                        
                                if (NamedArguments == null)
                                        return;
@@ -1033,10 +1084,10 @@ namespace Mono.CSharp {
 
                private Expression GetValue () 
                {
-                       if (PosArguments == null || PosArguments.Count < 1)
+                       if (pos_args == null || pos_args.Count < 1)
                                return null;
 
-                       return PosArguments [0].Expr;
+                       return pos_args[0].Expr;
                }
 
                public string GetString () 
@@ -1062,42 +1113,10 @@ namespace Mono.CSharp {
                                return null;
                        return e.TypeArgument;
                }
-
-               public override Expression CreateExpressionTree (ResolveContext ec)
-               {
-                       throw new NotSupportedException ("ET");
-               }
-
-               protected override Expression DoResolve (ResolveContext ec)
-               {
-                       throw new NotImplementedException ();
-               }
-
-               public override void Emit (EmitContext ec)
-               {
-                       throw new NotImplementedException ();
-               }
        }
        
-
-       /// <summary>
-       /// For global attributes (assembly, module) we need special handling.
-       /// Attributes can be located in the several files
-       /// </summary>
-       public class GlobalAttribute : Attribute
+       public class Attributes
        {
-               public GlobalAttribute (string target, ATypeNameExpression expression, Arguments[] args, Location loc, bool nameEscaped)
-                       : base (target, expression, args, loc, nameEscaped)
-               {
-               }
-
-               protected override bool IsSecurityActionValid (bool for_assembly)
-               {
-                       return base.IsSecurityActionValid (true);
-               }
-       }
-
-       public class Attributes {
                public readonly List<Attribute> Attrs;
 
                public Attributes (Attribute a)
@@ -1111,6 +1130,11 @@ namespace Mono.CSharp {
                        Attrs = attrs;
                }
 
+               public void AddAttribute (Attribute attr)
+               {
+                       Attrs.Add (attr);
+               }
+
                public void AddAttributes (List<Attribute> attrs)
                {
                        Attrs.AddRange (attrs);
@@ -1144,6 +1168,31 @@ namespace Mono.CSharp {
                        return true;
                }
 
+               public void ConvertGlobalAttributes (TypeContainer member, NamespaceContainer currentNamespace, bool isGlobal)
+               {
+                       var member_explicit_targets = member.ValidAttributeTargets;
+                       for (int i = 0; i < Attrs.Count; ++i) {
+                               var attr = Attrs[0];
+                               if (attr.ExplicitTarget == null)
+                                       continue;
+
+                               int ii;
+                               for (ii = 0; ii < member_explicit_targets.Length; ++ii) {
+                                       if (attr.ExplicitTarget == member_explicit_targets[ii]) {
+                                               ii = -1;
+                                               break;
+                                       }
+                               }
+
+                               if (ii < 0 || !isGlobal)
+                                       continue;
+
+                               member.Module.AddAttribute (attr, currentNamespace);
+                               Attrs.RemoveAt (i);
+                               --i;
+                       }
+               }
+
                public Attribute Search (PredefinedAttribute t)
                {
                        return Search (null, t);
@@ -1494,56 +1543,6 @@ namespace Mono.CSharp {
        /// </summary>
        static class AttributeTester
        {
-               public enum Result {
-                       Ok,
-                       RefOutArrayError,
-                       ArrayArrayError
-               }
-
-               /// <summary>
-               /// Returns true if parameters of two compared methods are CLS-Compliant.
-               /// It tests differing only in ref or out, or in array rank.
-               /// </summary>
-               public static Result AreOverloadedMethodParamsClsCompliant (AParametersCollection pa, AParametersCollection pb) 
-               {
-                       TypeSpec [] types_a = pa.Types;
-                       TypeSpec [] types_b = pb.Types;
-                       if (types_a == null || types_b == null)
-                               return Result.Ok;
-
-                       if (types_a.Length != types_b.Length)
-                               return Result.Ok;
-
-                       Result result = Result.Ok;
-                       for (int i = 0; i < types_b.Length; ++i) {
-                               TypeSpec aType = types_a [i];
-                               TypeSpec bType = types_b [i];
-
-                               var ac_a = aType as ArrayContainer;
-                               var ac_b = aType as ArrayContainer;
-
-                               if (ac_a != null && ac_b != null) {
-                                       if (ac_a.Rank != ac_b.Rank && ac_a.Element == ac_b.Element) {
-                                               result = Result.RefOutArrayError;
-                                               continue;
-                                       }
-
-                                       if (ac_a.Element.IsArray || ac_b.Element.IsArray) {
-                                               result = Result.ArrayArrayError;
-                                               continue;
-                                       }
-                               }
-
-                               if (aType != bType)
-                                       return Result.Ok;
-
-                               const Parameter.Modifier out_ref_mod = (Parameter.Modifier.OUTMASK | Parameter.Modifier.REFMASK);
-                               if ((pa.FixedParameters[i].ModFlags & out_ref_mod) != (pb.FixedParameters[i].ModFlags & out_ref_mod))
-                                       result = Result.RefOutArrayError;
-                       }
-                       return result;
-               }
-
                /// <summary>
                /// Common method for Obsolete error/warning reporting.
                /// </summary>
@@ -1594,8 +1593,6 @@ namespace Mono.CSharp {
                public readonly PredefinedAttribute DefaultParameterValue;
                public readonly PredefinedAttribute OptionalParameter;
                public readonly PredefinedAttribute UnverifiableCode;
-
-               // New in .NET 2.0
                public readonly PredefinedAttribute DefaultCharset;
                public readonly PredefinedAttribute TypeForwarder;
                public readonly PredefinedAttribute FixedBuffer;
@@ -1604,6 +1601,8 @@ namespace Mono.CSharp {
                public readonly PredefinedAttribute RuntimeCompatibility;
                public readonly PredefinedAttribute DebuggerHidden;
                public readonly PredefinedAttribute UnsafeValueType;
+               public readonly PredefinedAttribute UnmanagedFunctionPointer;
+               public readonly PredefinedDebuggerBrowsableAttribute DebuggerBrowsable;
 
                // New in .NET 3.5
                public readonly PredefinedAttribute Extension;
@@ -1618,13 +1617,16 @@ namespace Mono.CSharp {
                public readonly PredefinedDecimalAttribute DecimalConstant;
                public readonly PredefinedAttribute StructLayout;
                public readonly PredefinedAttribute FieldOffset;
+               public readonly PredefinedAttribute CallerMemberNameAttribute;
+               public readonly PredefinedAttribute CallerLineNumberAttribute;
+               public readonly PredefinedAttribute CallerFilePathAttribute;
 
                public PredefinedAttributes (ModuleContainer module)
                {
                        ParamArray = new PredefinedAttribute (module, "System", "ParamArrayAttribute");
                        Out = new PredefinedAttribute (module, "System.Runtime.InteropServices", "OutAttribute");
-                       ParamArray.Resolve (Location.Null);
-                       Out.Resolve (Location.Null);
+                       ParamArray.Resolve ();
+                       Out.Resolve ();
 
                        Obsolete = new PredefinedAttribute (module, "System", "ObsoleteAttribute");
                        DllImport = new PredefinedAttribute (module, "System.Runtime.InteropServices", "DllImportAttribute");
@@ -1657,6 +1659,8 @@ namespace Mono.CSharp {
                        RuntimeCompatibility = new PredefinedAttribute (module, "System.Runtime.CompilerServices", "RuntimeCompatibilityAttribute");
                        DebuggerHidden = new PredefinedAttribute (module, "System.Diagnostics", "DebuggerHiddenAttribute");
                        UnsafeValueType = new PredefinedAttribute (module, "System.Runtime.CompilerServices", "UnsafeValueTypeAttribute");
+                       UnmanagedFunctionPointer = new PredefinedAttribute (module, "System.Runtime.InteropServices", "UnmanagedFunctionPointerAttribute");
+                       DebuggerBrowsable = new PredefinedDebuggerBrowsableAttribute (module, "System.Diagnostics", "DebuggerBrowsableAttribute");
 
                        Extension = new PredefinedAttribute (module, "System.Runtime.CompilerServices", "ExtensionAttribute");
 
@@ -1667,6 +1671,10 @@ namespace Mono.CSharp {
                        StructLayout = new PredefinedAttribute (module, "System.Runtime.InteropServices", "StructLayoutAttribute");
                        FieldOffset = new PredefinedAttribute (module, "System.Runtime.InteropServices", "FieldOffsetAttribute");
 
+                       CallerMemberNameAttribute = new PredefinedAttribute (module, "System.Runtime.CompilerServices", "CallerMemberNameAttribute");
+                       CallerLineNumberAttribute = new PredefinedAttribute (module, "System.Runtime.CompilerServices", "CallerLineNumberAttribute");
+                       CallerFilePathAttribute = new PredefinedAttribute (module, "System.Runtime.CompilerServices", "CallerFilePathAttribute");
+
                        // TODO: Should define only attributes which are used for comparison
                        const System.Reflection.BindingFlags all_fields = System.Reflection.BindingFlags.Public |
                                System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly;
@@ -1740,22 +1748,12 @@ namespace Mono.CSharp {
                                builder.SetCustomAttribute (GetCtorMetaInfo (), AttributeEncoder.Empty);
                }
 
-               public void EmitAttribute (FieldBuilder builder, AttributeEncoder argsEncoded)
-               {
-                       builder.SetCustomAttribute (GetCtorMetaInfo (), argsEncoded.ToArray ());
-               }
-
                public void EmitAttribute (TypeBuilder builder)
                {
                        if (ResolveBuilder ())
                                builder.SetCustomAttribute (GetCtorMetaInfo (), AttributeEncoder.Empty);
                }
 
-               public void EmitAttribute (TypeBuilder builder, AttributeEncoder argsEncoded)
-               {
-                       builder.SetCustomAttribute (GetCtorMetaInfo (), argsEncoded.ToArray ());
-               }
-
                public void EmitAttribute (AssemblyBuilder builder)
                {
                        if (ResolveBuilder ())
@@ -1774,11 +1772,6 @@ namespace Mono.CSharp {
                                builder.SetCustomAttribute (GetCtorMetaInfo (), AttributeEncoder.Empty);
                }
 
-               public void EmitAttribute (ParameterBuilder builder, AttributeEncoder argsEncoded)
-               {
-                       builder.SetCustomAttribute (GetCtorMetaInfo (), argsEncoded.ToArray ());
-               }
-
                ConstructorInfo GetCtorMetaInfo ()
                {
                        return (ConstructorInfo) ctor.GetMetaInfo ();
@@ -1800,6 +1793,27 @@ namespace Mono.CSharp {
                }
        }
 
+       public class PredefinedDebuggerBrowsableAttribute : PredefinedAttribute
+       {
+               public PredefinedDebuggerBrowsableAttribute (ModuleContainer module, string ns, string name)
+                       : base (module, ns, name)
+               {
+               }
+
+               public void EmitAttribute (FieldBuilder builder, System.Diagnostics.DebuggerBrowsableState state)
+               {
+                       var ctor = module.PredefinedMembers.DebuggerBrowsableAttributeCtor.Get ();
+                       if (ctor == null)
+                               return;
+
+                       AttributeEncoder encoder = new AttributeEncoder ();
+                       encoder.Encode ((int) state);
+                       encoder.EncodeEmptyNamedArguments ();
+
+                       builder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), encoder.ToArray ());
+               }
+       }
+
        public class PredefinedDecimalAttribute : PredefinedAttribute
        {
                public PredefinedDecimalAttribute (ModuleContainer module, string ns, string name)