2004-12-22 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / mcs / attribute.cs
index 6d9952e361d861d98690d542a5732827a4c6c29e..51de7ad253295cced2fd2671c83a9d2deed1796a 100644 (file)
@@ -33,11 +33,9 @@ namespace Mono.CSharp {
                /// </summary>
                Attributes attributes;
 
-               public Attributable(Attributes attrs)
+               public Attributable (Attributes attrs)
                {
                        attributes = attrs;
-                       if (attributes != null)
-                               attributes.CheckTargets (this);
                }
 
                public Attributes OptAttributes 
@@ -47,8 +45,6 @@ namespace Mono.CSharp {
                        }
                        set {
                                attributes = value;
-                               if (attributes != null)
-                                       attributes.CheckTargets (this);
                        }
                }
 
@@ -82,6 +78,8 @@ namespace Mono.CSharp {
 
                public Type Type;
 
+               bool resolve_error;
+
                // Is non-null if type is AttributeUsageAttribute
                AttributeUsageAttribute usage_attribute;
 
@@ -114,9 +112,9 @@ namespace Mono.CSharp {
 
                void Error_InvalidNamedArgument (string name)
                {
-                       Report.Error (617, Location, "'" + name + "' is not a valid named attribute " +
-                                     "argument. Named attribute arguments must be fields which are not " +
-                                     "readonly, static or const, or read-write properties which are not static.");
+                       Report.Error (617, Location, "Invalid attribute argument: '{0}'.  Argument must be fields " +
+                                     "fields which are not readonly, static or const;  or read-write instance properties.",
+                                     Name);
                }
 
                static void Error_AttributeArgumentNotValid (Location loc)
@@ -149,12 +147,18 @@ namespace Mono.CSharp {
                /// <summary>
                 ///   Tries to resolve the type of the attribute. Flags an error if it can't, and complain is true.
                 /// </summary>
-               protected virtual Type CheckAttributeType (EmitContext ec, bool complain)
+               protected virtual Type CheckAttributeType (EmitContext ec)
                {
-                       Type t1 = RootContext.LookupType (ec.DeclSpace, Name, true, Location);
+                       string NameAttribute = Name + "Attribute";
+
+                       Type t1 = ec.ResolvingTypeTree
+                               ? ec.DeclSpace.FindType (Location, Name)
+                               : RootContext.LookupType (ec.DeclSpace, Name, true, Location);
 
                        // FIXME: Shouldn't do this for quoted attributes: [@A]
-                       Type t2 = RootContext.LookupType (ec.DeclSpace, Name + "Attribute", true, Location);
+                       Type t2 = ec.ResolvingTypeTree
+                               ? ec.DeclSpace.FindType (Location, NameAttribute)
+                               : RootContext.LookupType (ec.DeclSpace, NameAttribute, true, Location);
 
                        String err0616 = null;
 
@@ -165,13 +169,13 @@ namespace Mono.CSharp {
                        if (t2 != null && ! t2.IsSubclassOf (TypeManager.attribute_type)) {
                                t2 = null;
                                err0616 = (err0616 != null) 
-                                       ? "Neither '" + Name + "' nor '" + Name + "Attribute' is an attribute class"
+                                       ? "Neither '" + Name + "' nor '" + NameAttribute +"' is an attribute class"
                                        : "'" + Name + "Attribute': is not an attribute class";
                        }
 
                        if (t1 != null && t2 != null) {
                                Report.Error(1614, Location, "'" + Name + "': is ambiguous; " 
-                                            + " use either '@" + Name + "' or '" + Name + "Attribute'");
+                                            + " use either '@" + Name + "' or '" + NameAttribute + "'");
                                return null;
                        }
                        if (t1 != null)
@@ -184,17 +188,18 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       if (complain)
-                               Report.Error (246, Location, 
-                                             "Could not find attribute '" + Name 
-                                             + "' (are you missing a using directive or an assembly reference ?)");
+                       Report.Error (246, Location, 
+                                     "Could not find attribute '" + Name 
+                                     + "' (are you missing a using directive or an assembly reference ?)");
+
+                       resolve_error = true;
                        return null;
                }
 
-               public Type ResolveType (EmitContext ec, bool complain)
+               public Type ResolveType (EmitContext ec)
                {
                        if (Type == null)
-                               Type = CheckAttributeType (ec, complain);
+                               Type = CheckAttributeType (ec);
                        return Type;
                }
 
@@ -261,12 +266,17 @@ namespace Mono.CSharp {
                        return false;
                }
                
-               public CustomAttributeBuilder Resolve (EmitContext ec)
+               public virtual CustomAttributeBuilder Resolve (EmitContext ec)
                {
+                       if (resolve_error)
+                               return null;
+
+                       resolve_error = true;
+
                        Type oldType = Type;
                        
                        // Sanity check.
-                       Type = CheckAttributeType (ec, true);
+                       Type = CheckAttributeType (ec);
 
                        if (oldType == null && Type == null)
                                return null;
@@ -343,6 +353,10 @@ namespace Mono.CSharp {
 
                                if (DoCompares){
                                        if (usage_attr) {
+                                               if ((int)val == 0) {
+                                                       Report.Error (591, Location, "Invalid value for argument to 'System.AttributeUsage' attribute");
+                                                       return null;
+                                               }
                                                usage_attribute = new AttributeUsageAttribute ((AttributeTargets)val);
                                        } else if (MethodImplAttr) {
                                                this.ImplOptions = (MethodImplOptions) val;
@@ -411,7 +425,13 @@ namespace Mono.CSharp {
                                        }
                                }
 
-                               if (member == null || !(member is PropertyExpr || member is FieldExpr)) {
+                               if (member == null){
+                                       Report.Error (117, Location, "Attribute `{0}' does not contain a definition for `{1}'",
+                                                     Type, member_name);
+                                       return null;
+                               }
+                               
+                               if (!(member is PropertyExpr || member is FieldExpr)) {
                                        Error_InvalidNamedArgument (member_name);
                                        return null;
                                }
@@ -484,10 +504,17 @@ namespace Mono.CSharp {
 
                        ParameterData pd = Invocation.GetParameterData (constructor);
 
-                       int group_in_params_array = Int32.MaxValue;
-                       int pc = pd.Count;
-                       if (pc > 0 && pd.ParameterModifier (pc-1) == Parameter.Modifier.PARAMS)
-                               group_in_params_array = pc-1;
+                       int last_real_param = pd.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) pos_args [j];
@@ -497,32 +524,24 @@ namespace Mono.CSharp {
                                        return null;
                                }
 
-                               if (j < group_in_params_array)
+                               if (j < last_real_param)
                                        continue;
                                
-                               if (j == group_in_params_array){
-                                       object v = pos_values [j];
-                                       int count = pos_arg_count - j;
-
-                                       object [] array = new object [count];
+                               if (j == last_real_param) {
+                                       object [] array = new object [pos_arg_count - last_real_param];
+                                       array [0] = pos_values [j];
                                        pos_values [j] = array;
-                                       array [0] = v;
-                               } else {
-                                       object [] array = (object []) pos_values [group_in_params_array];
-
-                                       array [j - group_in_params_array] = pos_values [j];
+                                       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 (group_in_params_array != Int32.MaxValue){
-                               int argc = group_in_params_array+1;
-                               object [] new_pos_values = new object [argc];
-
-                               for (int p = 0; p < argc; p++)
-                                       new_pos_values [p] = pos_values [p];
+                       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;
                        }
 
@@ -547,13 +566,6 @@ namespace Mono.CSharp {
                                else
                                        cb = new CustomAttributeBuilder (
                                                (ConstructorInfo) constructor, pos_values);
-                       } catch (NullReferenceException) {
-                               // 
-                               // Don't know what to do here
-                               //
-                               Report.Warning (
-                                       -101, Location, "NullReferenceException while trying to create attribute." +
-                                        "Something's wrong!");
                        } catch (Exception e) {
                                //
                                // Sample:
@@ -561,10 +573,11 @@ namespace Mono.CSharp {
                                // [DefaultValue (CollectionChangeAction.Add)]
                                // class X { static void Main () {} }
                                //
-                               Report.Warning (
-                                       -23, Location, "The compiler can not encode this attribute in .NET due to a bug in the .NET runtime. Try the Mono runtime. The exception was: " + e.Message);
+                               Error_AttributeArgumentNotValid (Location);
+                               return null;
                        }
                        
+                       resolve_error = false;
                        return cb;
                }
 
@@ -648,11 +661,10 @@ namespace Mono.CSharp {
                /// </summary>
                public string GetIndexerAttributeValue (EmitContext ec)
                {
-                       if (pos_values == null) {
+                       if (pos_values == null)
                                // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
                                // But because a lot of attribute class code must be rewritten will be better to wait...
                                Resolve (ec);
-                       }
 
                        return pos_values [0] as string;
                }
@@ -660,15 +672,12 @@ namespace Mono.CSharp {
                /// <summary>
                /// Returns condition of ConditionalAttribute
                /// </summary>
-               public string GetConditionalAttributeValue (DeclSpace ds)
+               public string GetConditionalAttributeValue (EmitContext ec)
                {
-                       if (pos_values == null) {
-                               EmitContext ec = new EmitContext (ds, ds, Location, null, null, 0, false);
-
+                       if (pos_values == null)
                                // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
                                // But because a lot of attribute class code must be rewritten will be better to wait...
                                Resolve (ec);
-                       }
 
                        // Some error occurred
                        if (pos_values [0] == null)
@@ -680,15 +689,12 @@ namespace Mono.CSharp {
                /// <summary>
                /// Creates the instance of ObsoleteAttribute from this attribute instance
                /// </summary>
-               public ObsoleteAttribute GetObsoleteAttribute (DeclSpace ds)
+               public ObsoleteAttribute GetObsoleteAttribute (EmitContext ec)
                {
-                       if (pos_values == null) {
-                               EmitContext ec = new EmitContext (ds, ds, Location, null, null, 0, false);
-
+                       if (pos_values == null)
                                // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
                                // But because a lot of attribute class code must be rewritten will be better to wait...
                                Resolve (ec);
-                       }
 
                        // Some error occurred
                        if (pos_values == null)
@@ -708,15 +714,12 @@ namespace Mono.CSharp {
                /// before ApplyAttribute. We need to resolve the arguments.
                /// This situation occurs when class deps is differs from Emit order.  
                /// </summary>
-               public bool GetClsCompliantAttributeValue (DeclSpace ds)
+               public bool GetClsCompliantAttributeValue (EmitContext ec)
                {
-                       if (pos_values == null) {
-                               EmitContext ec = new EmitContext (ds, ds, Location, null, null, 0, false);
-
+                       if (pos_values == null)
                                // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
                                // But because a lot of attribute class code must be rewritten will be better to wait...
                                Resolve (ec);
-                       }
 
                        // Some error occurred
                        if (pos_values [0] == null)
@@ -854,8 +857,14 @@ namespace Mono.CSharp {
                        return null;
                }
 
-               public UnmanagedMarshal GetMarshal ()
+               public UnmanagedMarshal GetMarshal (Attributable attr)
                {
+                       object value = GetFieldValue ("SizeParamIndex");
+                       if (value != null && UnmanagedType != UnmanagedType.LPArray) {
+                               Error_AttributeEmitError ("SizeParamIndex field is not valid for the specified unmanaged type");
+                               return null;
+                       }
+
                        object o = GetFieldValue ("ArraySubType");
                        UnmanagedType array_sub_type = o == null ? UnmanagedType.I4 : (UnmanagedType) o;
                        
@@ -863,8 +872,10 @@ namespace Mono.CSharp {
                        case UnmanagedType.CustomMarshaler:
                                MethodInfo define_custom = typeof (UnmanagedMarshal).GetMethod ("DefineCustom",
                                                                        BindingFlags.Static | BindingFlags.Public);
-                               if (define_custom == null)
+                               if (define_custom == null) {
+                                       Report.RuntimeMissingSupport (Location, "set marshal info");
                                        return null;
+                               }
                                
                                object [] args = new object [4];
                                args [0] = GetFieldValue ("MarshalTypeRef");
@@ -880,6 +891,11 @@ namespace Mono.CSharp {
                                return UnmanagedMarshal.DefineSafeArray (array_sub_type);
                        
                        case UnmanagedType.ByValArray:
+                               FieldMember fm = attr as FieldMember;
+                               if (fm == null) {
+                                       Error_AttributeEmitError ("Specified unmanaged type is only valid on fields");
+                                       return null;
+                               }
                                return UnmanagedMarshal.DefineByValArray ((int) GetFieldValue ("SizeConst"));
                        
                        case UnmanagedType.ByValTStr:
@@ -986,7 +1002,7 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       ResolveType (ec, true);
+                       ResolveType (ec);
                        if (Type == null)
                                return null;
                        
@@ -1148,12 +1164,47 @@ namespace Mono.CSharp {
                        ns = container.NamespaceEntry;
                }
 
-               protected override Type CheckAttributeType (EmitContext ec, bool complain)
+               protected override Type CheckAttributeType (EmitContext ec)
                {
-                       NamespaceEntry old = ec.DeclSpace.NamespaceEntry;
-                       if (old == null || old.NS == null || old.NS == Namespace.Root) 
+                       // RootContext.Tree.Types has a single NamespaceEntry which gets overwritten
+                       // each time a new file is parsed.  However, we need to use the NamespaceEntry
+                       // in effect where the attribute was used.  Since code elsewhere cannot assume
+                       // that the NamespaceEntry is right, just overwrite it.
+                       //
+                       // Precondition: RootContext.Tree.Types == null || RootContext.Tree.Types == ns.
+                       //               The second case happens when we are recursively invoked from inside Emit.
+
+                       NamespaceEntry old = null;
+                       if (ec.DeclSpace == RootContext.Tree.Types) {
+                               old = ec.DeclSpace.NamespaceEntry;
                                ec.DeclSpace.NamespaceEntry = ns;
-                       return base.CheckAttributeType (ec, complain);
+                               if (old != null && old != ns)
+                                       throw new InternalErrorException (Location + " non-null NamespaceEntry " + old);
+                       }
+
+                       Type retval = base.CheckAttributeType (ec);
+
+                       if (ec.DeclSpace == RootContext.Tree.Types)
+                               ec.DeclSpace.NamespaceEntry = old;
+
+                       return retval;
+               }
+
+               public override CustomAttributeBuilder Resolve (EmitContext ec)
+               {
+                       if (ec.DeclSpace == RootContext.Tree.Types) {
+                               NamespaceEntry old = ec.DeclSpace.NamespaceEntry;
+                               ec.DeclSpace.NamespaceEntry = ns;
+                               if (old != null)
+                                       throw new InternalErrorException (Location + " non-null NamespaceEntry " + old);
+                       }
+
+                       CustomAttributeBuilder retval = base.Resolve (ec);
+
+                       if (ec.DeclSpace == RootContext.Tree.Types)
+                               ec.DeclSpace.NamespaceEntry = null;
+
+                       return retval;
                }
        }
 
@@ -1179,7 +1230,7 @@ namespace Mono.CSharp {
                /// <summary>
                /// Checks whether attribute target is valid for the current element
                /// </summary>
-               public void CheckTargets (Attributable member)
+               public bool CheckTargets (Attributable member)
                {
                        string[] valid_targets = member.ValidAttributeTargets;
                        foreach (Attribute a in Attrs) {
@@ -1206,24 +1257,21 @@ namespace Mono.CSharp {
                                        sb.Append (", ");
                                }
                                sb.Remove (sb.Length - 2, 2);
-                               Report.Error (657, a.Location, "'{0}' is not a valid attribute location for this declaration. Valid attribute locations for this declaration are '{1}'", a.Target, sb.ToString ());
+                               Report.Error (657, a.Location, "'{0}' is not a valid attribute location for this declaration. Valid attribute locations for this declaration are '{1}'", a.ExplicitTarget, sb.ToString ());
+                               return false;
                        }
+                       return true;
                }
 
-               private Attribute Search (Type t, EmitContext ec, bool complain)
+               public Attribute Search (Type t, EmitContext ec)
                {
                        foreach (Attribute a in Attrs) {
-                               if (a.ResolveType (ec, complain) == t)
+                               if (a.ResolveType (ec) == t)
                                        return a;
                        }
                        return null;
                }
 
-               public Attribute Search (Type t, EmitContext ec)
-               {
-                       return Search (t, ec, true);
-               }
-
                /// <summary>
                /// Returns all attributes of type 't'. Use it when attribute is AllowMultiple = true
                /// </summary>
@@ -1232,7 +1280,7 @@ namespace Mono.CSharp {
                        ArrayList ar = null;
 
                        foreach (Attribute a in Attrs) {
-                               if (a.ResolveType (ec, false) == t) {
+                               if (a.ResolveType (ec) == t) {
                                        if (ar == null)
                                                ar = new ArrayList ();
                                        ar.Add (a);
@@ -1244,6 +1292,9 @@ namespace Mono.CSharp {
 
                public void Emit (EmitContext ec, Attributable ias)
                {
+                       if (!CheckTargets (ias))
+                               return;
+
                        ListDictionary ld = new ListDictionary ();
 
                        foreach (Attribute a in Attrs)
@@ -1254,26 +1305,6 @@ namespace Mono.CSharp {
                {
                         return Search (t, ec) != null;
                }
-
-               public Attribute GetClsCompliantAttribute (EmitContext ec)
-               {
-                       return Search (TypeManager.cls_compliant_attribute_type, ec, false);
-               }
-
-               /// <summary>
-               /// Pulls the IndexerName attribute from an Indexer if it exists.
-               /// </summary>
-               public Attribute GetIndexerNameAttribute (EmitContext ec)
-               {
-                       Attribute a = Search (TypeManager.indexer_name_type, ec, false);
-                       if (a == null)
-                               return null;
-
-                       // Remove the attribute from the list because it is not emitted
-                       Attrs.Remove (a);
-                       return a;
-               }
-
        }
 
        /// <summary>