merge r98600
[mono.git] / mcs / mcs / attribute.cs
index c92254c81d07442e755f1f219939389aba30922d..84e29b78a80ec8eb7df6f90bce12f7699a6a9c28 100644 (file)
@@ -75,7 +75,8 @@ namespace Mono.CSharp {
                public abstract string[] ValidAttributeTargets { get; }
        };
 
-       public class Attribute {
+       public class Attribute : Expression
+       {
                public readonly string ExplicitTarget;
                public AttributeTargets Target;
 
@@ -84,18 +85,14 @@ namespace Mono.CSharp {
                public readonly Expression LeftExpr;
                public readonly string Identifier;
 
-               readonly ArrayList PosArguments;
+               ArrayList PosArguments;
                ArrayList NamedArguments;
 
-               public readonly Location Location;
-
-               public Type Type;
-
                bool resolve_error;
                readonly bool nameEscaped;
 
                // It can contain more onwers when the attribute is applied to multiple fiels.
-               Attributable[] owners;
+               protected Attributable[] owners;
 
                static readonly AttributeUsageAttribute DefaultUsageAttribute = new AttributeUsageAttribute (AttributeTargets.All);
                static Assembly orig_sec_assembly;
@@ -121,7 +118,7 @@ namespace Mono.CSharp {
                                PosArguments = (ArrayList)args [0];
                                NamedArguments = (ArrayList)args [1];                           
                        }
-                       Location = loc;
+                       this.loc = loc;
                        ExplicitTarget = target;
                        this.nameEscaped = nameEscaped;
                }
@@ -137,7 +134,7 @@ namespace Mono.CSharp {
                        att_cache = new PtrHashtable ();
                }
 
-               public void AttachTo (Attributable owner)
+               public virtual void AttachTo (Attributable owner)
                {
                        if (this.owners == null) {
                                this.owners = new Attributable[1] { owner };
@@ -288,7 +285,7 @@ namespace Mono.CSharp {
                        return Type;
                }
 
-               public string GetSignatureForError ()
+               public override string GetSignatureForError ()
                {
                        if (Type != null)
                                return TypeManager.CSharpName (Type);
@@ -296,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;
                }
@@ -363,8 +372,12 @@ namespace Mono.CSharp {
                        }
 
                        Attributable owner = Owner;
-                       EmitContext ec = new EmitContext (owner.ResolveContext, owner.ResolveContext.DeclContainer, owner.ResolveContext.DeclContainer,
-                               Location, null, null, owner.ResolveContext.DeclContainer.ModFlags, false);
+                       DeclSpace ds = owner.ResolveContext as DeclSpace;
+                       if (ds == null)
+                               ds = owner.ResolveContext.DeclContainer;
+                       
+                       EmitContext ec = new EmitContext (owner.ResolveContext, ds, owner.ResolveContext.DeclContainer,
+                               Location, null, typeof (Attribute), owner.ResolveContext.DeclContainer.ModFlags, false);
                        ec.IsAnonymousMethodAllowed = false;
 
                        ConstructorInfo ctor = ResolveConstructor (ec);
@@ -381,6 +394,10 @@ namespace Mono.CSharp {
 
                        CustomAttributeBuilder cb;
                        try {
+                               // SRE does not allow private ctor but we want to report all source code errors
+                               if (ctor.IsPrivate)
+                                       return null;
+
                                if (NamedArguments == null) {
                                        cb = new CustomAttributeBuilder (ctor, pos_values);
 
@@ -418,21 +435,22 @@ namespace Mono.CSharp {
                                                return null;
                                }
                        }
-
-                       Expression mg = Expression.MemberLookup (ec.ContainerType,
+                       
+                       MethodGroupExpr mg = MemberLookupFinal (ec, ec.ContainerType,
                                Type, ".ctor", MemberTypes.Constructor,
                                BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
-                               Location);
+                               Location) as MethodGroupExpr;
 
                        if (mg == null)
                                return null;
 
-                       MethodBase constructor = ((MethodGroupExpr)mg).OverloadResolve (
-                               ec, PosArguments, false, Location);
-
-                       if (constructor == null)
+                       mg = mg.OverloadResolve (ec, ref PosArguments, false, Location);
+                       if (mg == null)
                                return null;
+                       
+                       ConstructorInfo constructor = (ConstructorInfo)mg;
 
+                       // TODO: move to OverloadResolve
                        ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (constructor);
                        if (oa != null && !Owner.ResolveContext.IsInObsoleteScope) {
                                AttributeTester.Report_ObsoleteMessage (oa, mg.GetSignatureForError (), mg.Location);
@@ -440,52 +458,18 @@ namespace Mono.CSharp {
 
                        if (PosArguments == null) {
                                pos_values = EmptyObject;
-                               return (ConstructorInfo)constructor;
+                               return constructor;
                        }
 
                        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.
@@ -521,7 +505,7 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       return (ConstructorInfo)constructor;
+                       return constructor;
                }
 
                protected virtual bool ResolveNamedArguments (EmitContext ec)
@@ -592,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);
@@ -619,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);
@@ -663,7 +647,7 @@ namespace Mono.CSharp {
                public string GetValidTargets ()
                {
                        StringBuilder sb = new StringBuilder ();
-                       AttributeTargets targets = GetAttributeUsage ().ValidOn;
+                       AttributeTargets targets = GetAttributeUsage (Type).ValidOn;
 
                        if ((targets & AttributeTargets.Assembly) != 0)
                                sb.Append ("assembly, ");
@@ -715,32 +699,37 @@ namespace Mono.CSharp {
                }
 
                /// <summary>
-               /// Returns AttributeUsage attribute for this type
+               /// Returns AttributeUsage attribute based on types hierarchy
                /// </summary>
-               AttributeUsageAttribute GetAttributeUsage ()
+               static AttributeUsageAttribute GetAttributeUsage (Type type)
                {
-                       AttributeUsageAttribute ua = usage_attr_cache [Type] as AttributeUsageAttribute;
+                       AttributeUsageAttribute ua = usage_attr_cache [type] as AttributeUsageAttribute;
                        if (ua != null)
                                return ua;
 
-                       Class attr_class = TypeManager.LookupClass (Type);
+                       Class attr_class = TypeManager.LookupClass (type);
 
                        if (attr_class == null) {
-                               object[] usage_attr = Type.GetCustomAttributes (TypeManager.attribute_usage_type, true);
+                               object[] usage_attr = type.GetCustomAttributes (TypeManager.attribute_usage_type, true);
                                ua = (AttributeUsageAttribute)usage_attr [0];
-                               usage_attr_cache.Add (Type, ua);
+                               usage_attr_cache.Add (type, ua);
                                return ua;
                        }
 
-                       Attribute a = attr_class.OptAttributes == null
-                               ? null
-                               : attr_class.OptAttributes.Search (TypeManager.attribute_usage_type);
+                       Attribute a = null;
+                       if (attr_class.OptAttributes != null)
+                               a = attr_class.OptAttributes.Search (TypeManager.attribute_usage_type);
 
-                       ua = a == null
-                               ? DefaultUsageAttribute 
-                               : a.GetAttributeUsageAttribute ();
+                       if (a == null) {
+                               if (attr_class.TypeBuilder.BaseType != TypeManager.attribute_type)
+                                       ua = GetAttributeUsage (attr_class.TypeBuilder.BaseType);
+                               else
+                                       ua = DefaultUsageAttribute;
+                       } else {
+                               ua = a.GetAttributeUsageAttribute ();
+                       }
 
-                       usage_attr_cache.Add (Type, ua);
+                       usage_attr_cache.Add (type, ua);
                        return ua;
                }
 
@@ -884,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 ();
 
@@ -1185,7 +1174,7 @@ namespace Mono.CSharp {
                        if (cb == null)
                                return;
 
-                       AttributeUsageAttribute usage_attr = GetAttributeUsage ();
+                       AttributeUsageAttribute usage_attr = GetAttributeUsage (Type);
                        if ((usage_attr.ValidOn & Target) == 0) {
                                Report.Error (592, Location, "The attribute `{0}' is not valid on this declaration type. " +
                                              "It is valid on `{1}' declarations only",
@@ -1262,16 +1251,16 @@ namespace Mono.CSharp {
                public string GetString () 
                {
                        Expression e = GetValue ();
-                       if (e is StringLiteral)
-                               return (e as StringLiteral).Value;
+                       if (e is StringConstant)
+                               return ((StringConstant)e).Value;
                        return null;
                }
 
                public bool GetBoolean () 
                {
                        Expression e = GetValue ();
-                       if (e is BoolLiteral)
-                               return (e as BoolLiteral).Value;
+                       if (e is BoolConstant)
+                               return ((BoolConstant)e).Value;
                        return false;
                }
 
@@ -1282,6 +1271,16 @@ namespace Mono.CSharp {
                                return null;
                        return e.TypeArgument;
                }
+
+               public override Expression DoResolve (EmitContext ec)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       throw new NotImplementedException ();
+               }
        }
        
 
@@ -1298,6 +1297,20 @@ namespace Mono.CSharp {
                        base (target, left_expr, identifier, args, loc, nameEscaped)
                {
                        this.ns = ns;
+                       this.owners = new Attributable[1];
+               }
+               
+               public override void AttachTo (Attributable owner)
+               {
+                       if (ExplicitTarget == "assembly") {
+                               owners [0] = CodeGen.Assembly;
+                               return;
+                       }
+                       if (ExplicitTarget == "module") {
+                               owners [0] = CodeGen.Module;
+                               return;
+                       }
+                       throw new NotImplementedException ("Unknown global explicit target " + ExplicitTarget);
                }
 
                void Enter ()
@@ -1315,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;
@@ -1438,7 +1456,8 @@ namespace Mono.CSharp {
                                        Report.SymbolRelatedToPreviousError (collision.Location, "");
 
                                Attribute a = (Attribute)d.Key;
-                               Report.Error (579, a.Location, "The attribute `{0}' cannot be applied multiple times", a.GetSignatureForError ());
+                               Report.Error (579, a.Location, "The attribute `{0}' cannot be applied multiple times",
+                                       a.GetSignatureForError ());
                        }
                }
 
@@ -1457,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 ();
@@ -1480,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 {
@@ -1564,8 +1578,10 @@ namespace Mono.CSharp {
                        }
 
                        bool result;
-                       if (type.IsArray || type.IsByRef)       {
+                       if (type.IsArray || type.IsByRef) {
                                result = IsClsCompliant (TypeManager.GetElementType (type));
+                       } else if (TypeManager.IsNullableType (type)) {
+                               result = IsClsCompliant (TypeManager.GetTypeArguments (type) [0]);
                        } else {
                                result = AnalyzeTypeCompliance (type);
                        }
@@ -1586,11 +1602,16 @@ namespace Mono.CSharp {
                        if (fb != null) {
                                return fb as IFixedBuffer;
                        }
+                       
+                       if (TypeManager.GetConstant (fi) != null)
+                               return null;
 
-#if NET_2_0
                        object o = fixed_buffer_cache [fi];
                        if (o == null) {
-                               if (System.Attribute.GetCustomAttribute (fi, TypeManager.fixed_buffer_attr_type) == 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;
                                }
@@ -1604,9 +1625,6 @@ namespace Mono.CSharp {
                                return null;
 
                        return (IFixedBuffer)o;
-#else
-                       return null;
-#endif         
                }
 
                public static void VerifyModulesClsCompliance ()
@@ -1618,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;
@@ -1639,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)
@@ -1659,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
@@ -1695,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 ();
                                }
@@ -1747,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);
@@ -1763,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;
                        }
@@ -1772,14 +1798,13 @@ namespace Mono.CSharp {
 
                public static bool IsConditionalMethodExcluded (MethodBase mb)
                {
-                       mb = TypeManager.DropGenericMethodArguments (mb);
-                       if ((mb is MethodBuilder) || (mb is ConstructorBuilder))
-                               return false;
-
                        object excluded = analyzed_method_excluded [mb];
                        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) {
@@ -1810,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))
@@ -1826,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);