merge r98600
[mono.git] / mcs / mcs / attribute.cs
index f14f0477115183f8a93a66d5fc3b84867d148821..84e29b78a80ec8eb7df6f90bce12f7699a6a9c28 100644 (file)
@@ -85,7 +85,7 @@ namespace Mono.CSharp {
                public readonly Expression LeftExpr;
                public readonly string Identifier;
 
-               readonly ArrayList PosArguments;
+               ArrayList PosArguments;
                ArrayList NamedArguments;
 
                bool resolve_error;
@@ -293,14 +293,26 @@ namespace Mono.CSharp {
                        return LeftExpr == null ? Identifier : LeftExpr.GetSignatureForError () + "." + Identifier;
                }
 
-               bool IsValidArgumentType (Type t)
+               public bool HasSecurityAttribute {
+                       get {
+                               return TypeManager.security_attr_type != null &&
+                               TypeManager.IsSubclassOf (type, TypeManager.security_attr_type);
+                       }
+               }
+
+               public bool IsValidSecurityAttribute ()
+               {
+                       return HasSecurityAttribute && IsSecurityActionValid (false);
+               }
+
+               static bool IsValidArgumentType (Type t)
                {
                        if (t.IsArray)
-                               t = t.GetElementType ();
+                               t = TypeManager.GetElementType (t);
 
-                       return TypeManager.IsPrimitiveType (t) ||
+                       return t == TypeManager.string_type ||
+                               TypeManager.IsPrimitiveType (t) ||
                                TypeManager.IsEnumType (t) ||
-                               t == TypeManager.string_type ||
                                t == TypeManager.object_type ||
                                t == TypeManager.type_type;
                }
@@ -432,7 +444,7 @@ namespace Mono.CSharp {
                        if (mg == null)
                                return null;
 
-                       mg = mg.OverloadResolve (ec, PosArguments, false, Location);
+                       mg = mg.OverloadResolve (ec, ref PosArguments, false, Location);
                        if (mg == null)
                                return null;
                        
@@ -452,46 +464,12 @@ namespace Mono.CSharp {
                        ParameterData pd = TypeManager.GetParameterData (constructor);
 
                        int pos_arg_count = PosArguments.Count;
-                       int last_real_param = pd.Count;
-
                        pos_values = new object [pos_arg_count];
-
-                       if (pd.HasParams) {
-                               // When the params is not filled we need to put one
-                               if (last_real_param > pos_arg_count) {
-                                       object [] new_pos_values = new object [pos_arg_count + 1];
-                                       pos_values.CopyTo (new_pos_values, 0);
-                                       new_pos_values [pos_arg_count] = new object [] {} ;
-                                       pos_values = new_pos_values;
-                               }
-                               last_real_param--;
-                       }
-
                        for (int j = 0; j < pos_arg_count; ++j) {
                                Argument a = (Argument) PosArguments [j];
 
                                if (!a.Expr.GetAttributableValue (a.Type, out pos_values [j]))
                                        return null;
-                               
-                               if (j < last_real_param)
-                                       continue;
-                               
-                               if (j == last_real_param) {
-                                       object [] array = new object [pos_arg_count - last_real_param];
-                                       array [0] = pos_values [j];
-                                       pos_values [j] = array;
-                                       continue;
-                               }
-
-                               object [] params_array = (object []) pos_values [last_real_param];
-                               params_array [j - last_real_param] = pos_values [j];
-                       }
-
-                       // Adjust the size of the pos_values if it had params
-                       if (last_real_param != pos_arg_count) {
-                               object [] new_pos_values = new object [last_real_param + 1];
-                               Array.Copy (pos_values, new_pos_values, last_real_param + 1);
-                               pos_values = new_pos_values;
                        }
 
                        // Here we do the checks which should be done by corlib or by runtime.
@@ -527,7 +505,7 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       return (ConstructorInfo)constructor;
+                       return constructor;
                }
 
                protected virtual bool ResolveNamedArguments (EmitContext ec)
@@ -598,14 +576,14 @@ namespace Mono.CSharp {
                                                return false;
                                        }
 
-                                       if (!IsValidArgumentType (pi.PropertyType)) {
+                                       if (!IsValidArgumentType (member.Type)) {
                                                Report.SymbolRelatedToPreviousError (pi);
                                                Error_InvalidNamedAgrumentType (member_name);
                                                return false;
                                        }
 
                                        object value;
-                                       if (!a.Expr.GetAttributableValue (pi.PropertyType, out value))
+                                       if (!a.Expr.GetAttributableValue (member.Type, out value))
                                                return false;
 
                                        PropertyBase pb = TypeManager.GetProperty (pi);
@@ -625,14 +603,14 @@ namespace Mono.CSharp {
                                                return false;
                                        }
 
-                                       if (!IsValidArgumentType (fi.FieldType)) {
+                                       if (!IsValidArgumentType (member.Type)) {
                                                Report.SymbolRelatedToPreviousError (fi);
                                                Error_InvalidNamedAgrumentType (member_name);
                                                return false;
                                        }
 
                                        object value;
-                                       if (!a.Expr.GetAttributableValue (fi.FieldType, out value))
+                                       if (!a.Expr.GetAttributableValue (member.Type, out value))
                                                return false;
 
                                        FieldBase fb = TypeManager.GetField (fi);
@@ -895,7 +873,7 @@ namespace Mono.CSharp {
                /// <summary>
                /// Tests permitted SecurityAction for assembly or other types
                /// </summary>
-               public bool CheckSecurityActionValidity (bool for_assembly)
+               protected virtual bool IsSecurityActionValid (bool for_assembly)
                {
                        SecurityAction action = GetSecurityActionValue ();
 
@@ -1350,6 +1328,11 @@ namespace Mono.CSharp {
                        RootContext.ToplevelTypes.NamespaceEntry = ns;
                }
 
+               protected override bool IsSecurityActionValid (bool for_assembly)
+               {
+                       return base.IsSecurityActionValid (true);
+               }
+
                void Leave ()
                {
                        RootContext.ToplevelTypes.NamespaceEntry = null;
@@ -1493,10 +1476,7 @@ namespace Mono.CSharp {
                static PtrHashtable analyzed_types_obsolete;
                static PtrHashtable analyzed_member_obsolete;
                static PtrHashtable analyzed_method_excluded;
-
-#if NET_2_0
                static PtrHashtable fixed_buffer_cache;
-#endif
 
                static object TRUE = new object ();
                static object FALSE = new object ();
@@ -1516,9 +1496,7 @@ namespace Mono.CSharp {
                        analyzed_types_obsolete = new PtrHashtable ();
                        analyzed_member_obsolete = new PtrHashtable ();
                        analyzed_method_excluded = new PtrHashtable ();
-#if NET_2_0
                        fixed_buffer_cache = new PtrHashtable ();
-#endif
                }
 
                public enum Result {
@@ -1628,9 +1606,11 @@ namespace Mono.CSharp {
                        if (TypeManager.GetConstant (fi) != null)
                                return null;
 
-#if NET_2_0
                        object o = fixed_buffer_cache [fi];
                        if (o == null) {
+                               if (TypeManager.fixed_buffer_attr_type == null)
+                                       return null;
+
                                if (!fi.IsDefined (TypeManager.fixed_buffer_attr_type, false)) {
                                        fixed_buffer_cache.Add (fi, FALSE);
                                        return null;
@@ -1645,9 +1625,6 @@ namespace Mono.CSharp {
                                return null;
 
                        return (IFixedBuffer)o;
-#else
-                       return null;
-#endif         
                }
 
                public static void VerifyModulesClsCompliance ()
@@ -1659,7 +1636,7 @@ namespace Mono.CSharp {
                        // The first module is generated assembly
                        for (int i = 1; i < modules.Length; ++i) {
                                Module module = modules [i];
-                               if (!IsClsCompliant (module)) {
+                               if (!GetClsCompliantAttributeValue (module, null)) {
                                        Report.Error (3013, "Added modules must be marked with the CLSCompliant attribute " +
                                                      "to match the assembly", module.Name);
                                        return;
@@ -1680,13 +1657,20 @@ namespace Mono.CSharp {
                        return null;
                }
 
-               static bool IsClsCompliant (ICustomAttributeProvider attribute_provider
+               static bool GetClsCompliantAttributeValue (ICustomAttributeProvider attribute_provider, Assembly a
                {
-                       object[] CompliantAttribute = attribute_provider.GetCustomAttributes (TypeManager.cls_compliant_attribute_type, false);
-                       if (CompliantAttribute.Length == 0)
+                       if (TypeManager.cls_compliant_attribute_type == null)
                                return false;
 
-                       return ((CLSCompliantAttribute)CompliantAttribute[0]).IsCompliant;
+                       object[] cls_attr = attribute_provider.GetCustomAttributes (TypeManager.cls_compliant_attribute_type, false);
+                       if (cls_attr.Length == 0) {
+                               if (a == null)
+                                       return false;
+
+                               return GetClsCompliantAttributeValue (a, null);
+                       }
+                       
+                       return ((CLSCompliantAttribute)cls_attr [0]).IsCompliant;
                }
 
                static bool AnalyzeTypeCompliance (Type type)
@@ -1700,11 +1684,7 @@ namespace Mono.CSharp {
                        if (TypeManager.IsGenericParameter (type))
                                return true;
 
-                       object[] CompliantAttribute = type.GetCustomAttributes (TypeManager.cls_compliant_attribute_type, false);
-                       if (CompliantAttribute.Length == 0) 
-                               return IsClsCompliant (type.Assembly);
-
-                       return ((CLSCompliantAttribute)CompliantAttribute[0]).IsCompliant;
+                       return GetClsCompliantAttributeValue (type, type.Assembly);
                }
 
                // Registers the core type as we assume that they will never be obsolete which
@@ -1736,9 +1716,11 @@ namespace Mono.CSharp {
 
                                // Type is external, we can get attribute directly
                                if (type_ds == null) {
-                                       object[] attribute = type.GetCustomAttributes (TypeManager.obsolete_attribute_type, false);
-                                       if (attribute.Length == 1)
-                                               result = (ObsoleteAttribute)attribute [0];
+                                       if (TypeManager.obsolete_attribute_type != null) {
+                                               object [] attribute = type.GetCustomAttributes (TypeManager.obsolete_attribute_type, false);
+                                               if (attribute.Length == 1)
+                                                       result = (ObsoleteAttribute) attribute [0];
+                                       }
                                } else {
                                        result = type_ds.GetObsoleteAttribute ();
                                }
@@ -1788,6 +1770,9 @@ namespace Mono.CSharp {
                        if ((mi.DeclaringType is TypeBuilder) || TypeManager.IsGenericType (mi.DeclaringType))
                                return null;
 
+                       if (TypeManager.obsolete_attribute_type == null)
+                               return null;
+
                        ObsoleteAttribute oa = System.Attribute.GetCustomAttribute (mi, TypeManager.obsolete_attribute_type, false)
                                as ObsoleteAttribute;
                        analyzed_member_obsolete.Add (mi, oa == null ? FALSE : oa);
@@ -1804,7 +1789,7 @@ namespace Mono.CSharp {
                                return;
                        }
 
-                       if (oa.Message == null) {
+                       if (oa.Message == null || oa.Message.Length == 0) {
                                Report.Warning (612, 1, loc, "`{0}' is obsolete", member);
                                return;
                        }
@@ -1817,6 +1802,9 @@ namespace Mono.CSharp {
                        if (excluded != null)
                                return excluded == TRUE ? true : false;
 
+                       if (TypeManager.conditional_attribute_type == null)
+                               return false;
+
                        ConditionalAttribute[] attrs = mb.GetCustomAttributes (TypeManager.conditional_attribute_type, true)
                                as ConditionalAttribute[];
                        if (attrs.Length == 0) {
@@ -1847,7 +1835,7 @@ namespace Mono.CSharp {
 
                        // TODO: add caching
                        // TODO: merge all Type bases attribute caching to one cache to save memory
-                       if (class_decl == null) {
+                       if (class_decl == null && TypeManager.conditional_attribute_type != null) {
                                object[] attributes = type.GetCustomAttributes (TypeManager.conditional_attribute_type, false);
                                foreach (ConditionalAttribute ca in attributes) {
                                        if (RootContext.AllDefines.Contains (ca.ConditionString))
@@ -1863,13 +1851,16 @@ namespace Mono.CSharp {
                {
                        TypeContainer tc = TypeManager.LookupInterface (type);
                        if (tc == null) {
+                               if (TypeManager.coclass_attr_type == null)
+                                       return null;
+
                                object[] o = type.GetCustomAttributes (TypeManager.coclass_attr_type, false);
                                if (o.Length < 1)
                                        return null;
                                return ((System.Runtime.InteropServices.CoClassAttribute)o[0]).CoClass;
                        }
 
-                       if (tc.OptAttributes == null)
+                       if (tc.OptAttributes == null || TypeManager.coclass_attr_type == null)
                                return null;
 
                        Attribute a = tc.OptAttributes.Search (TypeManager.coclass_attr_type);