2004-08-04 Marek Safar <marek.safar@seznam.cz>
authorMarek Safar <marek.safar@gmail.com>
Wed, 4 Aug 2004 21:21:50 +0000 (21:21 -0000)
committerMarek Safar <marek.safar@gmail.com>
Wed, 4 Aug 2004 21:21:50 +0000 (21:21 -0000)
Fix bug 60333, 55971 in the more general way
* attribute.cs (Attribute.GetAttributeArgumentExpression):
Added arg_type argument for constant conversion.
(Attribute.Resolve): Reuse GetAttributeArgumentExpression.

svn path=/trunk/mcs/; revision=31902

mcs/mcs/ChangeLog
mcs/mcs/attribute.cs
mcs/mcs/expression.cs

index 2567e9369ac82fa033e0d9d551894b729029f499..3ac9807775942b1b1a99f68747e9fc0f56789d36 100755 (executable)
@@ -1,3 +1,10 @@
+2004-08-04  Marek Safar  <marek.safar@seznam.cz>
+
+       Fix bug 60333, 55971 in the more general way
+       * attribute.cs (Attribute.GetAttributeArgumentExpression):
+       Added arg_type argument for constant conversion.
+       (Attribute.Resolve): Reuse GetAttributeArgumentExpression.
+
 2004-08-04  Marek Safar  <marek.safar@seznam.cz>
 
        Fix bug #59760
index 314964bb78729c0c95145d21536e27fcc34750a4..3f2958ad2eef469bc9e22917677a12d132dae793 100644 (file)
@@ -207,10 +207,19 @@ namespace Mono.CSharp {
                // Given an expression, if the expression is a valid attribute-argument-expression
                // returns an object that can be used to encode it, or null on failure.
                //
-               public static bool GetAttributeArgumentExpression (Expression e, Location loc, out object result)
+               public static bool GetAttributeArgumentExpression (Expression e, Location loc, Type arg_type, out object result)
                {
-                       if (e is Constant) {
-                               result = ((Constant) e).GetValue ();
+                       Constant constant = e as Constant;
+                       if (constant != null) {
+                               if (e.Type != arg_type) {
+                                       constant = Const.ChangeType (loc, constant, arg_type);
+                                       if (constant == null) {
+                                               result = null;
+                                               Error_AttributeArgumentNotValid (loc);
+                                               return false;
+                                       }
+                               }
+                               result = constant.GetValue ();
                                return true;
                        } else if (e is TypeOf) {
                                result = ((TypeOf) e).TypeArg;
@@ -302,7 +311,7 @@ namespace Mono.CSharp {
                                e = a.Expr;
 
                                object val;
-                               if (!GetAttributeArgumentExpression (e, Location, out val))
+                               if (!GetAttributeArgumentExpression (e, Location, a.Type, out val))
                                        return null;
                                
                                pos_values [i] = val;
@@ -391,38 +400,18 @@ namespace Mono.CSharp {
                                                return null;
                                        }
 
-                                       Type type = e.Type;
-                                       EmptyCast ecast = e as EmptyCast;
-                                       if ((ecast != null) && (ecast.Child is Constant))
-                                               e = ecast.Child;
-
-                                       Constant c = e as Constant;
-                                       if (c != null) {
-                                               if (type != pi.PropertyType) {
-                                                       c = Const.ChangeType (Location, c, pi.PropertyType);
-                                                       if (c == null)
-                                                               return null;
-                                               }
-                                               
-                                               object o = c.GetValue ();
-                                               prop_values.Add (o);
-                                               
-                                               if (usage_attribute != null) {
-                                                       if (member_name == "AllowMultiple")
-                                                               usage_attribute.AllowMultiple = (bool) o;
-                                                       if (member_name == "Inherited")
-                                                               usage_attribute.Inherited = (bool) o;
-                                               }
-                                               
-                                       } else if (e is TypeOf) {
-                                               prop_values.Add (((TypeOf) e).TypeArg);
-                                       } else if (e is ArrayCreation) {
-                                               prop_values.Add (((ArrayCreation) e).EncodeAsAttribute());
-                                       } else {
-                                               Error_AttributeArgumentNotValid (Location);
+                                       object value;
+                                       if (!GetAttributeArgumentExpression (e, Location, pi.PropertyType, out value))
                                                return null;
+
+                                       if (usage_attribute != null) {
+                                               if (member_name == "AllowMultiple")
+                                                       usage_attribute.AllowMultiple = (bool) value;
+                                               if (member_name == "Inherited")
+                                                       usage_attribute.Inherited = (bool) value;
                                        }
-                                       
+
+                                       prop_values.Add (value);
                                        prop_infos.Add (pi);
                                        
                                } else if (member is FieldExpr) {
@@ -434,33 +423,11 @@ namespace Mono.CSharp {
                                                return null;
                                        }
 
-                                       Type type = e.Type;
-                                       EmptyCast ecast = e as EmptyCast;
-                                       if ((ecast != null) && (ecast.Child is Constant))
-                                               e = ecast.Child;
+                                       object value;
+                                       if (!GetAttributeArgumentExpression (e, Location, fi.FieldType, out value))
+                                               return null;
 
-                                       //
-                                       // Handle charset here, and set the TypeAttributes
-
-                                       Constant c = e as Constant;
-                                       if (c != null) {
-                                               if (type != fi.FieldType) {
-                                                       c = Const.ChangeType (Location, c, fi.FieldType);
-                                                       if (c == null)
-                                                               return null;
-                                               }                                       
-                                               
-                                               object value = c.GetValue ();
-                                               field_values.Add (value);
-                                       } else if (e is TypeOf) {
-                                               field_values.Add (((TypeOf) e).TypeArg);
-                                       } else if (e is ArrayCreation) {
-                                               field_values.Add (((ArrayCreation) e).EncodeAsAttribute());
-                                       } else {
-                                               Error_AttributeArgumentNotValid (Location);
-                                               return null;
-                                       }
-                                       
+                                       field_values.Add (value);                                       
                                        field_infos.Add (fi);
                                }
                        }
index 6748e1ac3e34270e0588a248747b4197138f3749..19e4f320ad722096a08641c6e334d1a24fc4446b 100755 (executable)
@@ -6457,7 +6457,7 @@ namespace Mono.CSharp {
                                if (e is NullLiteral)
                                        v = null;
                                else {
-                                       if (!Attribute.GetAttributeArgumentExpression (e, Location, out v))
+                                       if (!Attribute.GetAttributeArgumentExpression (e, Location, array_element_type, out v))
                                                return null;
                                }
                                ret [i++] = v;