2002-11-16 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / mcs / attribute.cs
index 5709c7fcaaae683cec1a9e89369fa06533cfc833..b939cf436ba46b09f46ef580e6a2f015cf3f41a5 100644 (file)
@@ -10,6 +10,7 @@
 //\r
 \r
 using System;\r
+using System.Diagnostics;\r
 using System.Collections;\r
 using System.Reflection;\r
 using System.Reflection.Emit;\r
@@ -36,8 +37,6 @@ namespace Mono.CSharp {
                bool Inherited;\r
 \r
                bool UsageAttr = false;\r
-               public bool StructLayout = false;\r
-               public TypeAttributes StructLayoutAttributes = 0;\r
                \r
                MethodImplOptions ImplOptions;\r
                UnmanagedType     UnmanagedType;\r
@@ -50,7 +49,7 @@ namespace Mono.CSharp {
                        Location = loc;\r
                }\r
 \r
-               void error617 (string name)\r
+               void Error_InvalidNamedArgument (string name)\r
                {\r
                        Report.Error (617, Location, "'" + name + "' is not a valid named attribute " +\r
                                      "argument. Named attribute arguments must be fields which are not " +\r
@@ -58,13 +57,20 @@ namespace Mono.CSharp {
                                      "are not static.");\r
                }\r
 \r
-               void error182 ()\r
+               void Error_AttributeArgumentNotValid ()\r
                {\r
                        Report.Error (182, Location,\r
                                      "An attribute argument must be a constant expression, typeof " +\r
                                      "expression or array creation expression");\r
                }\r
 \r
+               static void Error_AttributeConstructorMismatch (Location loc)\r
+               {\r
+                       Report.Error (\r
+                                       -6, loc,\r
+                                       "Could not find a constructor for this argument list.");\r
+               }\r
+               \r
                private Type CheckAttributeType (EmitContext ec) {\r
                        Type t;\r
                        bool isattributeclass = true;\r
@@ -94,56 +100,54 @@ namespace Mono.CSharp {
                        return null;\r
                }\r
 \r
+               public Type ResolveType (EmitContext ec)\r
+               {\r
+                       Type = CheckAttributeType (ec);\r
+                       return Type;\r
+               }\r
+\r
+               \r
                public CustomAttributeBuilder Resolve (EmitContext ec)\r
                {\r
-                       //\r
-                       // We might call resolve twice, because Structs will call\r
-                       // things in advance, so they can find out any attribute information\r
-                       // that needs to be encoded in the TypeAttribute (TypeAttribute.AnsiClass,\r
-                       // TypeAttribute.UnicodeClass, or TypeAttribute.AutoClass\r
-                       //\r
-                       if (cb != null)\r
-                               return cb;\r
-                       \r
+                       if (Type == null)\r
+                               Type = CheckAttributeType (ec);\r
+                       if (Type == null)\r
+                               return null;\r
+\r
                        bool MethodImplAttr = false;\r
                        bool MarshalAsAttr = false;\r
 \r
                        UsageAttr = false;\r
 \r
-                       Type = CheckAttributeType (ec);\r
-                       if (Type == null)\r
-                               return null;\r
-\r
                        if (Type == TypeManager.attribute_usage_type)\r
                                UsageAttr = true;\r
                        if (Type == TypeManager.methodimpl_attr_type)\r
                                MethodImplAttr = true;\r
                        if (Type == TypeManager.marshal_as_attr_type)\r
                                MarshalAsAttr = true;\r
-                       if (Type == TypeManager.structlayout_type){\r
-                               StructLayout = true;\r
-                               StructLayoutAttributes = TypeAttributes.AnsiClass;\r
-                       }\r
-                       \r
+\r
                        // Now we extract the positional and named arguments\r
                        \r
                        ArrayList pos_args = new ArrayList ();\r
                        ArrayList named_args = new ArrayList ();\r
+                       int pos_arg_count = 0;\r
                        \r
                        if (Arguments != null) {\r
                                pos_args = (ArrayList) Arguments [0];\r
+                               if (pos_args != null)\r
+                                       pos_arg_count = pos_args.Count;\r
                                if (Arguments.Count > 1)\r
                                        named_args = (ArrayList) Arguments [1];\r
                        }\r
-                               \r
-                       object [] pos_values = new object [pos_args.Count];\r
+\r
+                       object [] pos_values = new object [pos_arg_count];\r
 \r
                        //\r
                        // First process positional arguments \r
                        //\r
-                       \r
+\r
                        int i;\r
-                       for (i = 0; i < pos_args.Count; i++) {\r
+                       for (i = 0; i < pos_arg_count; i++) {\r
                                Argument a = (Argument) pos_args [i];\r
                                Expression e;\r
 \r
@@ -151,12 +155,13 @@ namespace Mono.CSharp {
                                        return null;\r
 \r
                                e = a.Expr;\r
+\r
                                if (e is Constant) {\r
                                        pos_values [i] = ((Constant) e).GetValue ();\r
                                } else if (e is TypeOf) {\r
                                        pos_values [i] = ((TypeOf) e).TypeArg;\r
                                } else {\r
-                                       error182 ();\r
+                                       Error_AttributeArgumentNotValid ();\r
                                        return null;\r
                                }\r
                                \r
@@ -196,7 +201,7 @@ namespace Mono.CSharp {
                                        Location);\r
 \r
                                if (member == null || !(member is PropertyExpr || member is FieldExpr)) {\r
-                                       error617 (member_name);\r
+                                       Error_InvalidNamedArgument (member_name);\r
                                        return null;\r
                                }\r
 \r
@@ -206,7 +211,7 @@ namespace Mono.CSharp {
                                        PropertyInfo pi = pe.PropertyInfo;\r
 \r
                                        if (!pi.CanWrite) {\r
-                                               error617 (member_name);\r
+                                               Error_InvalidNamedArgument (member_name);\r
                                                return null;\r
                                        }\r
 \r
@@ -221,8 +226,10 @@ namespace Mono.CSharp {
                                                                this.Inherited = (bool) o;\r
                                                }\r
                                                \r
-                                       } else { \r
-                                               error182 ();\r
+                                       } else if (e is TypeOf) {\r
+                                               prop_values.Add (((TypeOf) e).TypeArg);\r
+                                       } else {\r
+                                               Error_AttributeArgumentNotValid ();\r
                                                return null;\r
                                        }\r
                                        \r
@@ -233,7 +240,7 @@ namespace Mono.CSharp {
                                        FieldInfo fi = fe.FieldInfo;\r
 \r
                                        if (fi.IsInitOnly) {\r
-                                               error617 (member_name);\r
+                                               Error_InvalidNamedArgument (member_name);\r
                                                return null;\r
                                        }\r
 \r
@@ -243,44 +250,24 @@ namespace Mono.CSharp {
                                        if (e is Constant){\r
                                                object value = ((Constant) e).GetValue ();\r
                                                \r
-                                               if (StructLayout && fi.Name == "CharSet"){\r
-                                                       CharSet cs = (CharSet) value;\r
-\r
-                                                       switch (cs){\r
-                                                       case CharSet.Ansi:\r
-                                                       case CharSet.None:\r
-                                                               StructLayoutAttributes =\r
-                                                               TypeAttributes.AnsiClass;\r
-                                                               break;\r
-                                                       case CharSet.Auto:\r
-                                                               StructLayoutAttributes =\r
-                                                               TypeAttributes.AutoClass;\r
-                                                               break;\r
-                                                       case CharSet.Unicode:\r
-                                                               StructLayoutAttributes =\r
-                                                               TypeAttributes.UnicodeClass;\r
-                                                               break;\r
-                                                       }\r
-                                                       continue;\r
-                                               }\r
                                                field_values.Add (value);\r
-                                       } else { \r
-                                               error182 ();\r
+                                       } else if (e is TypeOf) {\r
+                                               field_values.Add (((TypeOf) e).TypeArg);\r
+                                       } else {\r
+                                               Error_AttributeArgumentNotValid ();\r
                                                return null;\r
                                        }\r
                                        \r
                                        field_infos.Add (fi);\r
                                }\r
                        }\r
-                       \r
+\r
                        Expression mg = Expression.MemberLookup (\r
                                ec, Type, ".ctor", MemberTypes.Constructor,\r
                                BindingFlags.Public | BindingFlags.Instance, Location);\r
 \r
                        if (mg == null) {\r
-                               Report.Error (\r
-                                       -6, Location,\r
-                                       "Could not find a constructor for this argument list.");\r
+                               Error_AttributeConstructorMismatch (Location);\r
                                return null;\r
                        }\r
 \r
@@ -288,11 +275,26 @@ namespace Mono.CSharp {
                                ec, (MethodGroupExpr) mg, pos_args, Location);\r
 \r
                        if (constructor == null) {\r
-                               Report.Error (\r
-                                       -6, Location,\r
-                                       "Could not find a constructor for this argument list.");\r
+                               Error_AttributeConstructorMismatch (Location);\r
                                return null;\r
                        }\r
+\r
+                       //\r
+                       // Now we perform some checks on the positional args as they\r
+                       // cannot be null for a constructor which expects a parameter\r
+                       // of type object\r
+                       //\r
+\r
+                       ParameterData pd = Invocation.GetParameterData (constructor);\r
+\r
+                       for (int j = 0; j < pos_arg_count; ++j) {\r
+                               Argument a = (Argument) pos_args [j];\r
+                               \r
+                               if (a.Expr is NullLiteral && pd.ParameterType (j) == TypeManager.object_type) {\r
+                                       Error_AttributeArgumentNotValid ();\r
+                                       return null;\r
+                               }\r
+                       }\r
                        \r
                        PropertyInfo [] prop_info_arr = new PropertyInfo [prop_infos.Count];\r
                        FieldInfo [] field_info_arr = new FieldInfo [field_infos.Count];\r
@@ -304,11 +306,31 @@ namespace Mono.CSharp {
 \r
                        prop_values.CopyTo  (prop_values_arr, 0);\r
                        prop_infos.CopyTo   (prop_info_arr, 0);\r
-                       \r
-                       cb = new CustomAttributeBuilder (\r
-                               (ConstructorInfo) constructor, pos_values,\r
-                               prop_info_arr, prop_values_arr,\r
-                               field_info_arr, field_values_arr); \r
+\r
+                       try {\r
+                               cb = new CustomAttributeBuilder (\r
+                                       (ConstructorInfo) constructor, pos_values,\r
+                                       prop_info_arr, prop_values_arr,\r
+                                       field_info_arr, field_values_arr); \r
+\r
+                       } catch (NullReferenceException) {\r
+                               // \r
+                               // Don't know what to do here\r
+                               //\r
+                               Report.Warning (\r
+                                       -100, Location, "NullReferenceException while trying to create attribute. Something's wrong!");\r
+                       } catch {\r
+                               //\r
+                               // Sample:\r
+                               // using System.ComponentModel;\r
+                               // [DefaultValue (CollectionChangeAction.Add)]\r
+                               // class X { static void Main () {} }\r
+                               //\r
+                               Report.Warning (\r
+                                       -23, Location,\r
+                                       "The compiler can not encode this attribute in .NET due to\n" +\r
+                                       "\ta bug in the .NET runtime.  Try the Mono runtime");\r
+                       }\r
                        \r
                        return cb;\r
                }\r
@@ -335,8 +357,10 @@ namespace Mono.CSharp {
                                }\r
                                        \r
                                foreach (System.Attribute tmp in attrs)\r
-                                       if (tmp is AttributeUsageAttribute) \r
+                                       if (tmp is AttributeUsageAttribute) {\r
                                                targets = ((AttributeUsageAttribute) tmp).ValidOn;\r
+                                               break;\r
+                                       }\r
                        } else\r
                                targets = a.Targets;\r
 \r
@@ -447,12 +471,12 @@ namespace Mono.CSharp {
                                        return true;\r
                                else\r
                                        return false;\r
-                       } else if (element is Event) {\r
+                       } else if (element is Event || element is InterfaceEvent) {\r
                                if ((targets & AttributeTargets.Event) != 0)\r
                                        return true;\r
                                else\r
                                        return false;\r
-                       } else if (element is Field) {\r
+                       } else if (element is Field || element is FieldBuilder) {\r
                                if ((targets & AttributeTargets.Field) != 0)\r
                                        return true;\r
                                else\r
@@ -462,7 +486,7 @@ namespace Mono.CSharp {
                                        return true;\r
                                else\r
                                        return false;\r
-                       } else if (element is Method || element is Operator) {\r
+                       } else if (element is Method || element is Operator || element is InterfaceMethod || element is Accessor) {\r
                                if ((targets & AttributeTargets.Method) != 0)\r
                                        return true;\r
                                else\r
@@ -472,7 +496,8 @@ namespace Mono.CSharp {
                                        return true;\r
                                else\r
                                        return false;\r
-                       } else if (element is Property || element is Indexer) {\r
+                       } else if (element is Property || element is Indexer ||\r
+                                  element is InterfaceProperty || element is InterfaceIndexer) {\r
                                if ((targets & AttributeTargets.Property) != 0)\r
                                        return true;\r
                                else\r
@@ -488,46 +513,139 @@ namespace Mono.CSharp {
                }\r
 \r
                //\r
-               // Returns TypeAttributes that might be encoded on the attributes.  Ugly, ugly\r
+               // This method should be invoked to pull the IndexerName attribute from an\r
+               // Indexer if it exists.\r
                //\r
-               public static TypeAttributes GetExtraTypeInfo (EmitContext ec, Attributes opt_attrs)\r
+               public static string ScanForIndexerName (EmitContext ec, Attributes opt_attrs)\r
                {\r
+                       if (opt_attrs == null)\r
+                               return null;\r
                        if (opt_attrs.AttributeSections == null)\r
-                               return 0;\r
+                               return null;\r
 \r
                        foreach (AttributeSection asec in opt_attrs.AttributeSections) {\r
-\r
                                if (asec.Attributes == null)\r
                                        continue;\r
 \r
-                               if (asec.Target == "assembly")\r
-                                       continue;\r
-                               \r
-                               foreach (Attribute a in asec.Attributes) {\r
-                                       CustomAttributeBuilder cb = a.Resolve (ec);\r
-\r
-                                       if (cb == null)\r
+                               foreach (Attribute a in asec.Attributes){\r
+                                       if (a.ResolveType (ec) == null)\r
+                                               return null;\r
+                                       \r
+                                       if (a.Type != TypeManager.indexer_name_type)\r
                                                continue;\r
 \r
-                                       if (a.StructLayout)\r
-                                               return a.StructLayoutAttributes;\r
+                                       //\r
+                                       // So we have found an IndexerName, pull the data out.\r
+                                       //\r
+                                       if (a.Arguments == null || a.Arguments [0] == null){\r
+                                               Error_AttributeConstructorMismatch (a.Location);\r
+                                               return null;\r
+                                       }\r
+                                       ArrayList pos_args = (ArrayList) a.Arguments [0];\r
+                                       if (pos_args.Count == 0){\r
+                                               Error_AttributeConstructorMismatch (a.Location);\r
+                                               return null;\r
+                                       }\r
+                                       \r
+                                       Argument arg = (Argument) pos_args [0];\r
+                                       if (!arg.Resolve (ec, a.Location))\r
+                                               return null;\r
+                                       \r
+                                       Expression e = arg.Expr;\r
+                                       if (!(e is StringConstant)){\r
+                                               Error_AttributeConstructorMismatch (a.Location);\r
+                                               return null;\r
+                                       }\r
+\r
+                                       //\r
+                                       // Remove the attribute from the list\r
+                                       //\r
+                                       asec.Attributes.Remove (a);\r
+\r
+                                       return (((StringConstant) e).Value);\r
                                }\r
                        }\r
+                       return null;\r
+               }\r
+\r
+               //\r
+               // This pulls the condition name out of a Conditional attribute\r
+               //\r
+               public string Conditional_GetConditionName ()\r
+               {\r
+                       //\r
+                       // So we have a Conditional, pull the data out.\r
+                       //\r
+                       if (Arguments == null || Arguments [0] == null){\r
+                               Error_AttributeConstructorMismatch (Location);\r
+                               return null;\r
+                       }\r
 \r
-                       return 0;\r
+                       ArrayList pos_args = (ArrayList) Arguments [0];\r
+                       if (pos_args.Count != 1){\r
+                               Error_AttributeConstructorMismatch (Location);\r
+                               return null;\r
+                       }\r
+\r
+                       Argument arg = (Argument) pos_args [0]; \r
+                       if (!(arg.Expr is StringConstant)){\r
+                               Error_AttributeConstructorMismatch (Location);\r
+                               return null;\r
+                       }\r
+\r
+                       return ((StringConstant) arg.Expr).Value;\r
                }\r
-               \r
+\r
+               //\r
+               // This pulls the obsolete message and error flag out of an Obsolete attribute\r
+               //\r
+               public string Obsolete_GetObsoleteMessage (out bool is_error)\r
+               {\r
+                       is_error = false;\r
+                       //\r
+                       // So we have an Obsolete, pull the data out.\r
+                       //\r
+                       if (Arguments == null || Arguments [0] == null)\r
+                               return "";\r
+\r
+                       ArrayList pos_args = (ArrayList) Arguments [0];\r
+                       if (pos_args.Count == 0)\r
+                               return "";\r
+                       else if (pos_args.Count > 2){\r
+                               Error_AttributeConstructorMismatch (Location);\r
+                               return null;\r
+                       }\r
+\r
+                       Argument arg = (Argument) pos_args [0]; \r
+                       if (!(arg.Expr is StringConstant)){\r
+                               Error_AttributeConstructorMismatch (Location);\r
+                               return null;\r
+                       }\r
+\r
+                       if (pos_args.Count == 2){\r
+                               Argument arg2 = (Argument) pos_args [1];\r
+                               if (!(arg2.Expr is BoolConstant)){\r
+                                       Error_AttributeConstructorMismatch (Location);\r
+                                       return null;\r
+                               }\r
+                               is_error = ((BoolConstant) arg2.Expr).Value;\r
+                       }\r
+\r
+                       return ((StringConstant) arg.Expr).Value;\r
+               }\r
+\r
+               //\r
+               // Applies the attributes to the `builder'.\r
+               //\r
                public static void ApplyAttributes (EmitContext ec, object builder, object kind,\r
                                                    Attributes opt_attrs, Location loc)\r
                {\r
                        if (opt_attrs == null)\r
                                return;\r
-\r
                        if (opt_attrs.AttributeSections == null)\r
                                return;\r
 \r
                        foreach (AttributeSection asec in opt_attrs.AttributeSections) {\r
-\r
                                if (asec.Attributes == null)\r
                                        continue;\r
 \r
@@ -542,28 +660,29 @@ namespace Mono.CSharp {
 \r
                                        if (!(kind is TypeContainer))\r
                                                if (!CheckAttribute (a, kind)) {\r
-                                                               Console.WriteLine ("Kind is: " + kind);\r
                                                        Error_AttributeNotValidForElement (a, loc);\r
                                                        return;\r
                                                }\r
 \r
-                                       if (kind is Method || kind is Operator) {\r
-\r
+                                       if (kind is Method || kind is Operator || kind is InterfaceMethod ||\r
+                                           kind is Accessor) {\r
                                                if (a.Type == TypeManager.methodimpl_attr_type) {\r
                                                        if (a.ImplOptions == MethodImplOptions.InternalCall)\r
-                                                               ((MethodBuilder) builder).SetImplementationFlags (\r
-                                                                          MethodImplAttributes.InternalCall |\r
-                                                                          MethodImplAttributes.Runtime);\r
-                                               } else if (a.Type != TypeManager.dllimport_type)\r
+                                                               ((MethodBuilder) builder).\r
+                                                               SetImplementationFlags (\r
+                                                                       MethodImplAttributes.InternalCall |\r
+                                                                       MethodImplAttributes.Runtime);\r
+                                               } else if (a.Type != TypeManager.dllimport_type){\r
                                                        ((MethodBuilder) builder).SetCustomAttribute (cb);\r
-\r
+                                               }\r
                                        } else if (kind is Constructor) {\r
                                                ((ConstructorBuilder) builder).SetCustomAttribute (cb);\r
                                        } else if (kind is Field) {\r
                                                ((FieldBuilder) builder).SetCustomAttribute (cb);\r
-                                       } else if (kind is Property || kind is Indexer) {\r
+                                       } else if (kind is Property || kind is Indexer ||\r
+                                                  kind is InterfaceProperty || kind is InterfaceIndexer) {\r
                                                ((PropertyBuilder) builder).SetCustomAttribute (cb);\r
-                                       } else if (kind is Event) {\r
+                                       } else if (kind is Event || kind is InterfaceEvent) {\r
                                                ((MyEventBuilder) builder).SetCustomAttribute (cb);\r
                                        } else if (kind is ParameterBuilder) {\r
 \r
@@ -600,13 +719,43 @@ namespace Mono.CSharp {
                                                                return;\r
                                                        }\r
                                                }\r
+\r
+                                               try {\r
+                                                       ((TypeBuilder) builder).SetCustomAttribute (cb);\r
+                                               } catch (System.ArgumentException) {\r
+                                                       Report.Warning (\r
+                                                               -21, loc,\r
+                                               "The CharSet named property on StructLayout\n"+\r
+                                               "\tdoes not work correctly on Microsoft.NET\n"+\r
+                                               "\tYou might want to remove the CharSet declaration\n"+\r
+                                               "\tor compile using the Mono runtime instead of the\n"+\r
+                                               "\tMicrosoft .NET runtime");\r
+                                               }\r
                                                \r
+                                       } else if (kind is Interface) {\r
+                                               Interface iface = (Interface) kind;\r
+\r
+                                               if ((a.Type == TypeManager.default_member_type) &&\r
+                                                   (iface.InterfaceIndexers != null)) {\r
+                                                       Report.Error (\r
+                                                               646, loc,\r
+                                                               "Cannot specify the DefaultMember attribute on" +\r
+                                                               " a type containing an indexer");\r
+                                                       return;\r
+                                               }\r
+\r
+                                               if (!CheckAttribute (a, kind)) {\r
+                                                       Error_AttributeNotValidForElement (a, loc);\r
+                                                       return;\r
+                                               }\r
+\r
                                                ((TypeBuilder) builder).SetCustomAttribute (cb);\r
-                                               \r
                                        } else if (kind is AssemblyBuilder){\r
                                                ((AssemblyBuilder) builder).SetCustomAttribute (cb);\r
                                        } else if (kind is ModuleBuilder) {\r
                                                ((ModuleBuilder) builder).SetCustomAttribute (cb);\r
+                                       } else if (kind is FieldBuilder) {\r
+                                               ((FieldBuilder) builder).SetCustomAttribute (cb);\r
                                        } else\r
                                                throw new Exception ("Unknown kind: " + kind);\r
                                }\r
@@ -646,7 +795,7 @@ namespace Mono.CSharp {
                        if (tmp.Expr is Constant)\r
                                dll_name = (string) ((Constant) tmp.Expr).GetValue ();\r
                        else { \r
-                               error182 ();\r
+                               Error_AttributeArgumentNotValid ();\r
                                return null;\r
                        }\r
 \r
@@ -675,7 +824,7 @@ namespace Mono.CSharp {
                                        Location);\r
 \r
                                if (member == null || !(member is FieldExpr)) {\r
-                                       error617 (member_name);\r
+                                       Error_InvalidNamedArgument (member_name);\r
                                        return null;\r
                                }\r
 \r
@@ -684,7 +833,7 @@ namespace Mono.CSharp {
                                        FieldInfo fi = fe.FieldInfo;\r
 \r
                                        if (fi.IsInitOnly) {\r
-                                               error617 (member_name);\r
+                                               Error_InvalidNamedArgument (member_name);\r
                                                return null;\r
                                        }\r
 \r
@@ -704,15 +853,18 @@ namespace Mono.CSharp {
                                                else if (member_name == "PreserveSig")\r
                                                        preserve_sig = (bool) c.GetValue ();\r
                                        } else { \r
-                                               error182 ();\r
+                                               Error_AttributeArgumentNotValid ();\r
                                                return null;\r
                                        }\r
                                        \r
                                }\r
                        }\r
 \r
+                       if (entry_point == null)\r
+                               entry_point = name;\r
+                       \r
                        MethodBuilder mb = builder.DefinePInvokeMethod (\r
-                               name, dll_name, flags | MethodAttributes.HideBySig,\r
+                               name, dll_name, entry_point, flags | MethodAttributes.HideBySig,\r
                                CallingConventions.Standard,\r
                                ret_type,\r
                                param_types,\r