Author: martin
Date: 2005-04-13 07:45:00 -0400 (Wed, 13 Apr 2005)
New Revision: 42910

Modified:
   trunk/mcs/gmcs/ChangeLog
   trunk/mcs/gmcs/attribute.cs
   trunk/mcs/gmcs/class.cs
   trunk/mcs/gmcs/decl.cs
Log:
**** Merged r41281 from MCS ****


Modified: trunk/mcs/gmcs/ChangeLog
===================================================================
--- trunk/mcs/gmcs/ChangeLog    2005-04-13 11:43:09 UTC (rev 42909)
+++ trunk/mcs/gmcs/ChangeLog    2005-04-13 11:45:00 UTC (rev 42910)
@@ -1,3 +1,27 @@
+2005-02-28  Marek Safar  <[EMAIL PROTECTED]>
+
+       * attribute.cs: Small refactoring (improved robustness).
+       (ImplOptions, UnmanagedType, UsageAttribute): Removed members.
+       (ValidateGuid): Removed.
+       (Resolve): Removed referenced to above mentioned.
+       (GetAttributeUsage): Made private and changed to work without
+       class assistance.
+       (GetIndexerAttributeValue): Don't crash.
+       (GetConditionalAttributeValue): Ditto.
+       (GetClsCompliantAttributeValue): Ditto.
+       (ExtractSecurityPermissionSet): All attributes exceptions are
+       error 648.
+       (GetPropertyValue): New helper.
+       (GetMethodImplOptions): New method.
+       (DefinePInvokeMethod): Reuse common code. Implemented handling of
+       some missing properties.
+       
+       * class.cs (ClassOrStruct.ApplyAttributeBuilder): Updated.
+       (Method.ApplyAttributeBuilder): Updated.
+       
+       * decl.cs (DeclSpace.ApplyAttributeBuilder): Don't catch shared
+       exception.
+
 2005-02-28  Raja R Harinath  <[EMAIL PROTECTED]>
 
        Fix #73052.

Modified: trunk/mcs/gmcs/attribute.cs
===================================================================
--- trunk/mcs/gmcs/attribute.cs 2005-04-13 11:43:09 UTC (rev 42909)
+++ trunk/mcs/gmcs/attribute.cs 2005-04-13 11:45:00 UTC (rev 42910)
@@ -83,11 +83,8 @@
                
                bool resolve_error;
 
-               public AttributeUsageAttribute UsageAttribute;
-               public static AttributeUsageAttribute DefaultUsageAttribute = 
new AttributeUsageAttribute (AttributeTargets.All);
+               static AttributeUsageAttribute DefaultUsageAttribute = new 
AttributeUsageAttribute (AttributeTargets.All);
 
-               MethodImplOptions ImplOptions;
-               UnmanagedType     UnmanagedType;
                CustomAttributeBuilder cb;
        
                // non-null if named args present after Resolve () is called
@@ -239,20 +236,6 @@
                        return Type;
                }
 
-               /// <summary>
-               ///   Validates the guid string
-               /// </summary>
-               bool ValidateGuid (string guid)
-               {
-                       try {
-                               new Guid (guid);
-                               return true;
-                       } catch {
-                               Report.Error (647, Location, "Format of GUID is 
invalid: " + guid);
-                               return false;
-                       }
-               }
-
                string GetFullMemberName (string member)
                {
                        return Type.FullName + '.' + member;
@@ -331,29 +314,6 @@
                                }
                        }
 
-                       bool MethodImplAttr = false;
-                       bool MarshalAsAttr = false;
-                       bool GuidAttr = false;
-                       bool usage_attr = false;
-
-                       bool DoCompares = true;
-
-                        //
-                        // If we are a certain special attribute, we
-                        // set the information accordingly
-                        //
-                        
-                       if (Type == TypeManager.attribute_usage_type)
-                               usage_attr = true;
-                       else if (Type == TypeManager.methodimpl_attr_type)
-                               MethodImplAttr = true;
-                       else if (Type == TypeManager.marshal_as_attr_type)
-                               MarshalAsAttr = true;
-                       else if (Type == TypeManager.guid_attr_type)
-                               GuidAttr = true;
-                       else
-                               DoCompares = false;
-
                        // Now we extract the positional and named arguments
                        
                        ArrayList pos_args = new ArrayList ();
@@ -390,27 +350,9 @@
                                
                                pos_values [i] = val;
 
-                               if (DoCompares){
-                                       if (usage_attr) {
-                                               if ((int)val == 0) {
-                                                       Report.Error (591, 
Location, "Invalid value for argument to 'System.AttributeUsage' attribute");
-                                                       return null;
-                                               }
-                                               UsageAttribute = new 
AttributeUsageAttribute ((AttributeTargets)val);
-                                       } else if (MethodImplAttr) {
-                                               this.ImplOptions = 
(MethodImplOptions) val;
-                                       } else if (GuidAttr){
-                                               //
-                                               // we will later check the 
validity of the type
-                                               //
-                                               if (val is string){
-                                                       if (!ValidateGuid 
((string) val))
-                                                               return null;
-                                               }
-                                               
-                                       } else if (MarshalAsAttr)
-                                               this.UnmanagedType =
-                                               
(System.Runtime.InteropServices.UnmanagedType) val;
+                               if (i == 0 && Type == 
TypeManager.attribute_usage_type && (int)val == 0) {
+                                       Report.Error (591, Location, "Invalid 
value for argument to 'System.AttributeUsage' attribute");
+                                       return null;
                                }
                        }
 
@@ -495,13 +437,6 @@
                                        if (!GetAttributeArgumentExpression (e, 
Location, pi.PropertyType, out value))
                                                return null;
 
-                                       if (UsageAttribute != null) {
-                                               if (member_name == 
"AllowMultiple")
-                                                       
UsageAttribute.AllowMultiple = (bool) value;
-                                                       if (member_name == 
"Inherited")
-                                                       
UsageAttribute.Inherited = (bool) value;
-                                       }
-                                       
                                        prop_values.Add (value);
                                        prop_infos.Add (pi);
                                        
@@ -625,7 +560,7 @@
                                        if (pos_values.Length == 0)
                                                att_cache.Add (Type, cb);
                                }
-                       } catch (Exception e) {
+                       } catch (Exception) {
                                //
                                // Sample:
                                // using System.ComponentModel;
@@ -636,10 +571,6 @@
                                return null;
                        }
                        
-                       if (!usage_attr) {
-                               UsageAttribute = DefaultUsageAttribute;
-                       }
-
                        resolve_error = false;
                        return cb;
                }
@@ -650,7 +581,7 @@
                public string GetValidTargets ()
                {
                        StringBuilder sb = new StringBuilder ();
-                       AttributeTargets targets = UsageAttribute.ValidOn;
+                       AttributeTargets targets = GetAttributeUsage 
(null).ValidOn;
 
                        if ((targets & AttributeTargets.Assembly) != 0)
                                sb.Append ("'assembly' ");
@@ -701,7 +632,7 @@
                /// <summary>
                /// Returns AttributeUsage attribute for this type
                /// </summary>
-               public AttributeUsageAttribute GetAttributeUsage (EmitContext 
ec)
+               AttributeUsageAttribute GetAttributeUsage (EmitContext ec)
                {
                        AttributeUsageAttribute ua = usage_attr_cache [Type] as 
AttributeUsageAttribute;
                        if (ua != null)
@@ -716,11 +647,45 @@
                                return ua;
                        }
 
-                       ua = attr_class.ResolveAttributeUsage (ec);
+                       if (attr_class.OptAttributes == null) {
+                               usage_attr_cache.Add (Type, 
DefaultUsageAttribute);
+                               return DefaultUsageAttribute;
+                       }
+
+                       Attribute a = attr_class.OptAttributes.Search 
(TypeManager.attribute_usage_type, ec);
+                       if (a == null) {
+                               usage_attr_cache.Add (Type, 
DefaultUsageAttribute);
+                               return DefaultUsageAttribute;
+                       }
+
+                       ua = a.GetAttributeUsageAttribute (ec);
                        usage_attr_cache.Add (Type, ua);
                        return ua;
                }
 
+               AttributeUsageAttribute GetAttributeUsageAttribute (EmitContext 
ec)
+               {
+                       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);
+
+                       if (resolve_error)
+                               return DefaultUsageAttribute;
+
+                       AttributeUsageAttribute usage_attribute = new 
AttributeUsageAttribute ((AttributeTargets)pos_values [0]);
+
+                       object field = GetPropertyValue ("AllowMultiple");
+                       if (field != null)
+                               usage_attribute.AllowMultiple = (bool)field;
+
+                       field = GetPropertyValue ("Inherited");
+                       if (field != null)
+                               usage_attribute.Inherited = (bool)field;
+
+                       return usage_attribute;
+               }
+
                /// <summary>
                /// Returns custom name of indexer
                /// </summary>
@@ -731,6 +696,9 @@
                                // But because a lot of attribute class code 
must be rewritten will be better to wait...
                                Resolve (ec);
                        
+                       if (resolve_error)
+                               return null;
+
                        return pos_values [0] as string;
                }
 
@@ -744,8 +712,7 @@
                                // 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)
+                       if (resolve_error)
                                return null;
 
                        return (string)pos_values [0];
@@ -761,7 +728,6 @@
                                // But because a lot of attribute class code 
must be rewritten will be better to wait...
                                Resolve (ec);
 
-                       // Some error occurred
                        if (resolve_error)
                                return null;
 
@@ -786,8 +752,7 @@
                                // 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)
+                       if (resolve_error)
                                return false;
 
                        return (bool)pos_values [0];
@@ -865,13 +830,7 @@
                        }
 
                        IPermission perm;
-                       try {
-                               perm = sa.CreatePermission ();
-                       }
-                       catch (Exception e) {
-                               Error_AttributeEmitError (String.Format ("{0} 
was thrown during attribute processing: {1}", e.GetType (), e.Message));
-                               return;
-                       }
+                       perm = sa.CreatePermission ();
                        SecurityAction action = GetSecurityActionValue ();
 
                        // IS is correct because for corlib we are using an 
instance from old corlib
@@ -912,13 +871,21 @@
                                return value;                           
                }
 
-               public object GetPositionalValue (int i)
+               object GetPropertyValue (string name)
                {
-                       return (pos_values == null) ? null : pos_values[i];
+                       if (prop_info_arr == null)
+                               return null;
+
+                       for (int i = 0; i < prop_info_arr.Length; ++i) {
+                               if (prop_info_arr [i].Name == name)
+                                       return prop_values_arr [i];
+                       }
+
+                       return null;
                }
 
                object GetFieldValue (string name)
-                {
+               {
                        int i;
                        if (field_info_arr == null)
                                return null;
@@ -933,6 +900,8 @@
 
                public UnmanagedMarshal GetMarshal (Attributable attr)
                {
+                       UnmanagedType UnmanagedType = 
(UnmanagedType)System.Enum.Parse (typeof (UnmanagedType), pos_values 
[0].ToString ());
+
                        object value = GetFieldValue ("SizeParamIndex");
                        if (value != null && UnmanagedType != 
UnmanagedType.LPArray) {
                                Error_AttributeEmitError ("SizeParamIndex field 
is not valid for the specified unmanaged type");
@@ -980,11 +949,16 @@
                        }
                }
 
-               public bool IsInternalCall
+               public MethodImplOptions GetMethodImplOptions ()
                {
-                       get { return ImplOptions == 
MethodImplOptions.InternalCall; }
+                       return (MethodImplOptions)System.Enum.Parse (typeof 
(MethodImplOptions), pos_values [0].ToString ());
                }
 
+               public LayoutKind GetLayoutKindValue ()
+               {
+                       return (LayoutKind)System.Enum.Parse (typeof 
(LayoutKind), pos_values [0].ToString ());
+               }
+
                /// <summary>
                /// Emit attribute for Attributable symbol
                /// </summary>
@@ -1000,7 +974,13 @@
                                return;
                        }
 
-                       ias.ApplyAttributeBuilder (this, cb);
+                       try {
+                               ias.ApplyAttributeBuilder (this, cb);
+                       }
+                       catch (Exception e) {
+                               Error_AttributeEmitError (e.Message);
+                               return;
+                       }
 
                        if (!usage_attr.AllowMultiple) {
                                ArrayList emitted_targets = 
(ArrayList)emitted_attr [Type];
@@ -1055,7 +1035,7 @@
                        }
                }
 
-               public object GetValue (EmitContext ec, Constant c, Type target)
+               object GetValue (EmitContext ec, Constant c, Type target)
                {
                        if (Convert.ImplicitConversionExists (ec, c, target))
                                return c.GetValue ();
@@ -1067,134 +1047,70 @@
                public MethodBuilder DefinePInvokeMethod (EmitContext ec, 
TypeBuilder builder, string name,
                                                          MethodAttributes 
flags, Type ret_type, Type [] param_types)
                {
-                       //
-                       // We extract from the attribute the information we 
need 
-                       //
+                       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);
 
-                       if (Arguments == null) {
-                               Console.WriteLine ("Internal error : this is 
not supposed to happen !");
+                       if (resolve_error)
                                return null;
-                       }
-
-                       ResolveType (ec);
-                       if (Type == null)
-                               return null;
                        
-                       ArrayList named_args = new ArrayList ();
-                       
-                       ArrayList pos_args = (ArrayList) Arguments [0];
-                       if (Arguments.Count > 1)
-                               named_args = (ArrayList) Arguments [1];
-                       
+                       string dll_name = (string)pos_values [0];
 
-                       string dll_name = null;
-                       
-                       Argument tmp = (Argument) pos_args [0];
-
-                       if (!tmp.Resolve (ec, Location))
-                               return null;
-                       
-                       if (tmp.Expr is Constant)
-                               dll_name = (string) ((Constant) 
tmp.Expr).GetValue ();
-                       else { 
-                               Error_AttributeArgumentNotValid (Location);
-                               return null;
-                       }
-                       if (dll_name == null || dll_name == ""){
-                               Error_AttributeArgumentNotValid (": DllImport 
requires a non-empty string", Location);
-                               return null;
-                       }
-                       
-                       // Now we process the named arguments
+                       // Default settings
                        CallingConvention cc = CallingConvention.Winapi;
                        CharSet charset = CharSet.Ansi;
                        bool preserve_sig = true;
-#if FIXME
-                       bool exact_spelling = false;
-#endif
-                       bool set_last_err = false;
-                       string entry_point = null;
+                       string entry_point = name;
 
-                       for (int i = 0; i < named_args.Count; i++) {
+                       if (prop_info_arr != null) {
+                               int char_set_extra = 0;
 
-                               DictionaryEntry de = (DictionaryEntry) 
named_args [i];
-
-                               string member_name = (string) de.Key;
-                               Argument a  = (Argument) de.Value;
-
-                               if (!a.Resolve (ec, Location))
-                                       return null;
-
-                               Expression member = Expression.MemberLookup (
-                                       ec, Type, member_name, 
-                                       MemberTypes.Field | 
MemberTypes.Property,
-                                       BindingFlags.Public | 
BindingFlags.Instance,
-                                       Location);
-
-                               if (member == null || !(member is FieldExpr)) {
-                                       Error_InvalidNamedArgument 
(member_name);
-                                       return null;
-                               }
-
-                               if (member is FieldExpr) {
-                                       FieldExpr fe = (FieldExpr) member;
-                                       FieldInfo fi = fe.FieldInfo;
-
-                                       if (fi.IsInitOnly) {
-                                               Error_InvalidNamedArgument 
(member_name);
-                                               return null;
+                               for (int i = 0; i < prop_info_arr.Length; i++) {
+                                       switch (prop_info_arr [i].Name) {
+                                               case "BestFitMapping":
+                                                       throw new 
NotImplementedException ();
+                                               case "CallingConvention":
+                                                       cc = 
(CallingConvention) prop_values_arr [i];
+                                                       break;
+                                               case "CharSet":
+                                                       charset = (CharSet) 
prop_values_arr [i];
+                                                       break;
+                                               case "EntryPoint":
+                                                       entry_point = (string) 
prop_values_arr [i];
+                                                       break;
+                                               case "ExactSpelling":
+                                                       char_set_extra |= 0x01;
+                                                       break;
+                                               case "PreserveSig":
+                                                       preserve_sig = (bool) 
prop_values_arr [i];
+                                                       break;
+                                               case "SetLastError":
+                                                       char_set_extra |= 0x40;
+                                                       break;
+                                               case "ThrowOnUnmappableChar":
+                                                       throw new 
NotImplementedException ();
+                                               default: 
+                                                       throw new 
InternalErrorException (prop_info_arr [i].ToString ());
                                        }
-
-                                       if (a.Expr is Constant) {
-                                               Constant c = (Constant) a.Expr;
-
-                                               try {
-                                                       if (member_name == 
"CallingConvention"){
-                                                               object val = 
GetValue (ec, c, typeof (CallingConvention));
-                                                               if (val == null)
-                                                                       return 
null;
-                                                               cc = 
(CallingConvention) val;
-                                                       } else if (member_name 
== "CharSet"){
-                                                               charset = 
(CharSet) c.GetValue ();
-                                                       } else if (member_name 
== "EntryPoint")
-                                                               entry_point = 
(string) c.GetValue ();
-                                                       else if (member_name == 
"SetLastError")
-                                                               set_last_err = 
(bool) c.GetValue ();
-#if FIXME
-                                                       else if (member_name == 
"ExactSpelling")
-                                                               exact_spelling 
= (bool) c.GetValue ();
-#endif
-                                                       else if (member_name == 
"PreserveSig")
-                                                               preserve_sig = 
(bool) c.GetValue ();
-                                               } catch (InvalidCastException){
-                                                       
Error_InvalidNamedArgument (member_name);
-                                                       
Error_AttributeArgumentNotValid (Location);
-                                               }
-                                       } else { 
-                                               Error_AttributeArgumentNotValid 
(Location);
-                                               return null;
-                                       }
-                                       
                                }
+                               charset |= (CharSet)char_set_extra;
                        }
 
-                       if (entry_point == null)
-                               entry_point = name;
-                       if (set_last_err)
-                               charset = (CharSet)((int)charset | 0x40);
-                       
-                       MethodBuilder mb = builder.DefinePInvokeMethod (
-                               name, dll_name, entry_point, flags | 
MethodAttributes.HideBySig,
-                               CallingConventions.Standard,
-                               ret_type,
-                               param_types,
-                               cc,
-                               charset);
+                       try {
+                               MethodBuilder mb = builder.DefinePInvokeMethod (
+                                       name, dll_name, entry_point, flags | 
MethodAttributes.HideBySig | MethodAttributes.PinvokeImpl,
+                                       CallingConventions.Standard, ret_type, 
param_types, cc, charset);
 
-                       if (preserve_sig)
-                               mb.SetImplementationFlags 
(MethodImplAttributes.PreserveSig);
+                               if (preserve_sig)
+                                       mb.SetImplementationFlags 
(MethodImplAttributes.PreserveSig);
                        
-                       return mb;
+                               return mb;
+                       }
+                       catch (ArgumentException e) {
+                               Error_AttributeEmitError (e.Message);
+                               return null;
+                       }
                }
 
                private Expression GetValue () 

Modified: trunk/mcs/gmcs/class.cs
===================================================================
--- trunk/mcs/gmcs/class.cs     2005-04-13 11:43:09 UTC (rev 42909)
+++ trunk/mcs/gmcs/class.cs     2005-04-13 11:45:00 UTC (rev 42910)
@@ -2923,8 +2923,8 @@
                                return;
                        }
 
-                       if (a.Type == TypeManager.struct_layout_attribute_type
-                           && (LayoutKind) a.GetPositionalValue (0) == 
LayoutKind.Explicit)
+                       if (a.Type == TypeManager.struct_layout_attribute_type 
&&
+                           a.GetLayoutKindValue () == LayoutKind.Explicit)
                                hasExplicitLayout = true;
 
                        base.ApplyAttributeBuilder (a, cb);
@@ -3073,28 +3073,6 @@
                        base.ApplyAttributeBuilder (a, cb);
                }
 
-               /// <summary>
-               /// Resolves AttributeUsageAttribute for attribute class
-               /// </summary>
-               public AttributeUsageAttribute ResolveAttributeUsage 
(EmitContext ec)
-               {
-                       if (OptAttributes == null)
-                               return Attribute.DefaultUsageAttribute;
-
-                       Attribute a = OptAttributes.Search 
(TypeManager.attribute_usage_type, ec);
-                       if (a == null)
-                               return Attribute.DefaultUsageAttribute;
-
-                       // Because our namespace model is so weird.
-                       // When GlobalAttribute is resolved and it has 
AttributeUsage attribute
-                       // we need to switch from global namespace to local
-                       DeclSpace old_ds = ec.DeclSpace;
-                       ec.DeclSpace = this;
-                       a.Resolve (ec);
-                       ec.DeclSpace = old_ds;
-                       return a.UsageAttribute;
-               }
-
                public const TypeAttributes DefaultTypeAttributes =
                        TypeAttributes.AutoLayout | TypeAttributes.Class;
 
@@ -3954,7 +3932,8 @@
                                return;
                        }
 
-                       if (a.Type == TypeManager.methodimpl_attr_type && 
a.IsInternalCall) {
+                       if (a.Type == TypeManager.methodimpl_attr_type &&
+                               (a.GetMethodImplOptions () & 
MethodImplOptions.InternalCall) != 0) {
                                MethodBuilder.SetImplementationFlags 
(MethodImplAttributes.InternalCall | MethodImplAttributes.Runtime);
                        }
 

Modified: trunk/mcs/gmcs/decl.cs
===================================================================
--- trunk/mcs/gmcs/decl.cs      2005-04-13 11:43:09 UTC (rev 42909)
+++ trunk/mcs/gmcs/decl.cs      2005-04-13 11:45:00 UTC (rev 42910)
@@ -1291,17 +1291,7 @@
 
                public override void ApplyAttributeBuilder (Attribute a, 
CustomAttributeBuilder cb)
                {
-                       try {
-                               TypeBuilder.SetCustomAttribute (cb);
-                       } catch (System.ArgumentException e) {
-                               Report.Warning (-21, a.Location,
-                                               "The CharSet named property on 
StructLayout\n"+
-                                               "\tdoes not work correctly on 
Microsoft.NET\n"+
-                                               "\tYou might want to remove the 
CharSet declaration\n"+
-                                               "\tor compile using the Mono 
runtime instead of the\n"+
-                                               "\tMicrosoft .NET runtime\n"+
-                                               "\tThe runtime gave the error: 
" + e);
-                       }
+                       TypeBuilder.SetCustomAttribute (cb);
                }
 
                /// <summary>

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to