2 // attribute.cs: Attribute Handler
\r
4 // Author: Ravi Pratap (ravi@ximian.com)
\r
6 // Licensed under the terms of the GNU GPL
\r
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)
\r
13 using System.Collections;
\r
14 using System.Reflection;
\r
15 using System.Reflection.Emit;
\r
16 using System.Runtime.InteropServices;
\r
19 namespace Mono.CSharp {
\r
21 public class Attribute {
\r
23 public readonly string Name;
\r
24 public readonly ArrayList Arguments;
\r
31 // The following are only meaningful when the attribute
\r
32 // being emitted is one of the builtin ones
\r
34 public AttributeTargets Targets;
\r
35 public bool AllowMultiple;
\r
36 public bool Inherited;
\r
38 public bool UsageAttr = false;
\r
40 public Attribute (string name, ArrayList args, Location loc)
\r
47 void error617 (string name)
\r
49 Report.Error (617, Location, "'" + name + "' is not a valid named attribute " +
\r
50 "argument. Named attribute arguments must be fields which are not " +
\r
51 "readonly, static or const, or properties with a set accessor which "+
\r
57 Report.Error (182, Location,
\r
58 "An attribute argument must be a constant expression, typeof " +
\r
59 "expression or array creation expression");
\r
62 public CustomAttributeBuilder Resolve (EmitContext ec)
\r
68 if (Name.IndexOf ("Attribute") == -1)
\r
69 name = Name + "Attribute";
\r
70 else if (Name.LastIndexOf ("Attribute") == 0)
\r
71 name = Name + "Attribute";
\r
73 Type = ec.TypeContainer.LookupType (name, false);
\r
76 Report.Error (246, Location, "Could not find attribute '" + Name + "' (are you" +
\r
77 " missing a using directive or an assembly reference ?)");
\r
81 if (Type == TypeManager.attribute_usage_type)
\r
84 // Now we extract the positional and named arguments
\r
86 ArrayList pos_args = new ArrayList ();
\r
87 ArrayList named_args = new ArrayList ();
\r
89 if (Arguments != null) {
\r
90 pos_args = (ArrayList) Arguments [0];
\r
91 if (Arguments.Count > 1)
\r
92 named_args = (ArrayList) Arguments [1];
\r
95 object [] pos_values = new object [pos_args.Count];
\r
98 // First process positional arguments
\r
102 for (i = 0; i < pos_args.Count; i++) {
\r
104 Argument a = (Argument) pos_args [i];
\r
106 if (!a.Resolve (ec, Location))
\r
109 Expression e = Expression.Reduce (ec, a.Expr);
\r
111 if (e is Literal) {
\r
112 pos_values [i] = ((Literal) e).GetValue ();
\r
115 this.Targets = (AttributeTargets) pos_values [0];
\r
123 // Now process named arguments
\r
126 ArrayList field_infos = new ArrayList ();
\r
127 ArrayList prop_infos = new ArrayList ();
\r
128 ArrayList field_values = new ArrayList ();
\r
129 ArrayList prop_values = new ArrayList ();
\r
131 for (i = 0; i < named_args.Count; i++) {
\r
133 DictionaryEntry de = (DictionaryEntry) named_args [i];
\r
135 string member_name = (string) de.Key;
\r
136 Argument a = (Argument) de.Value;
\r
138 if (!a.Resolve (ec, Location))
\r
141 Expression member = Expression.MemberLookup (ec, Type, member_name, false,
\r
142 MemberTypes.Field | MemberTypes.Property,
\r
143 BindingFlags.Public | BindingFlags.Instance,
\r
146 if (member == null || !(member is PropertyExpr || member is FieldExpr)) {
\r
147 error617 (member_name);
\r
151 if (member is PropertyExpr) {
\r
152 PropertyExpr pe = (PropertyExpr) member;
\r
153 PropertyInfo pi = pe.PropertyInfo;
\r
155 if (!pi.CanWrite) {
\r
156 error617 (member_name);
\r
160 Expression e = Expression.Reduce (ec, a.Expr);
\r
162 if (e is Literal) {
\r
163 object o = ((Literal) e).GetValue ();
\r
164 prop_values.Add (o);
\r
167 if (member_name == "AllowMultiple")
\r
168 this.AllowMultiple = (bool) o;
\r
169 if (member_name == "Inherited")
\r
170 this.Inherited = (bool) o;
\r
178 prop_infos.Add (pi);
\r
180 } else if (member is FieldExpr) {
\r
181 FieldExpr fe = (FieldExpr) member;
\r
182 FieldInfo fi = fe.FieldInfo;
\r
184 if (fi.IsInitOnly) {
\r
185 error617 (member_name);
\r
189 Expression e = Expression.Reduce (ec, a.Expr);
\r
192 field_values.Add (((Literal) e).GetValue ());
\r
198 field_infos.Add (fi);
\r
202 Expression mg = Expression.MemberLookup (ec, Type, ".ctor", false,
\r
203 MemberTypes.Constructor,
\r
204 BindingFlags.Public | BindingFlags.Instance,
\r
208 Report.Error (-6, Location, "Could not find a constructor for this argument list.");
\r
212 MethodBase constructor = Invocation.OverloadResolve (ec, (MethodGroupExpr) mg, pos_args, Location);
\r
214 if (constructor == null) {
\r
215 Report.Error (-6, Location, "Could not find a constructor for this argument list.");
\r
219 PropertyInfo [] prop_info_arr = new PropertyInfo [prop_infos.Count];
\r
220 FieldInfo [] field_info_arr = new FieldInfo [field_infos.Count];
\r
221 object [] field_values_arr = new object [field_values.Count];
\r
222 object [] prop_values_arr = new object [prop_values.Count];
\r
224 field_infos.CopyTo (field_info_arr, 0);
\r
225 field_values.CopyTo (field_values_arr, 0);
\r
227 prop_values.CopyTo (prop_values_arr, 0);
\r
228 prop_infos.CopyTo (prop_info_arr, 0);
\r
230 CustomAttributeBuilder cb = new CustomAttributeBuilder ((ConstructorInfo) constructor, pos_values,
\r
231 prop_info_arr, prop_values_arr,
\r
232 field_info_arr, field_values_arr);
\r
237 static string GetValidPlaces (Attribute attr)
\r
239 StringBuilder sb = new StringBuilder ();
\r
240 AttributeTargets targets = 0;
\r
242 TypeContainer a = TypeManager.LookupAttr (attr.Type);
\r
245 System.Attribute [] attrs = System.Attribute.GetCustomAttributes (attr.Type);
\r
247 foreach (System.Attribute tmp in attrs)
\r
248 if (tmp is AttributeUsageAttribute)
\r
249 targets = ((AttributeUsageAttribute) tmp).ValidOn;
\r
251 targets = a.Targets;
\r
254 if ((targets & AttributeTargets.Assembly) != 0)
\r
255 sb.Append ("'assembly' ");
\r
257 if ((targets & AttributeTargets.Class) != 0)
\r
258 sb.Append ("'class' ");
\r
260 if ((targets & AttributeTargets.Constructor) != 0)
\r
261 sb.Append ("'constructor' ");
\r
263 if ((targets & AttributeTargets.Delegate) != 0)
\r
264 sb.Append ("'delegate' ");
\r
266 if ((targets & AttributeTargets.Enum) != 0)
\r
267 sb.Append ("'enum' ");
\r
269 if ((targets & AttributeTargets.Event) != 0)
\r
270 sb.Append ("'event' ");
\r
272 if ((targets & AttributeTargets.Field) != 0)
\r
273 sb.Append ("'field' ");
\r
275 if ((targets & AttributeTargets.Interface) != 0)
\r
276 sb.Append ("'interface' ");
\r
278 if ((targets & AttributeTargets.Method) != 0)
\r
279 sb.Append ("'method' ");
\r
281 if ((targets & AttributeTargets.Module) != 0)
\r
282 sb.Append ("'module' ");
\r
284 if ((targets & AttributeTargets.Parameter) != 0)
\r
285 sb.Append ("'parameter' ");
\r
287 if ((targets & AttributeTargets.Property) != 0)
\r
288 sb.Append ("'property' ");
\r
290 if ((targets & AttributeTargets.ReturnValue) != 0)
\r
291 sb.Append ("'return value' ");
\r
293 if ((targets & AttributeTargets.Struct) != 0)
\r
294 sb.Append ("'struct' ");
\r
296 return sb.ToString ();
\r
300 public static void Error592 (Attribute a, Location loc)
\r
302 Report.Error (592, loc, "Attribute '" + a.Name + "' is not valid on this declaration type. " +
\r
303 "It is valid on " + GetValidPlaces (a) + "declarations only.");
\r
306 public static bool CheckAttribute (Attribute a, object element)
\r
308 TypeContainer attr = TypeManager.LookupAttr (a.Type);
\r
309 AttributeTargets targets = 0;
\r
311 if (attr == null) {
\r
312 System.Attribute [] attrs = System.Attribute.GetCustomAttributes (a.Type);
\r
314 foreach (System.Attribute tmp in attrs)
\r
315 if (tmp is AttributeUsageAttribute)
\r
316 targets = ((AttributeUsageAttribute) tmp).ValidOn;
\r
318 targets = attr.Targets;
\r
320 if (element is Class) {
\r
321 if ((targets & AttributeTargets.Class) != 0)
\r
326 } else if (element is Struct) {
\r
327 if ((targets & AttributeTargets.Struct) != 0)
\r
331 } else if (element is Constructor) {
\r
332 if ((targets & AttributeTargets.Constructor) != 0)
\r
336 } else if (element is Delegate) {
\r
337 if ((targets & AttributeTargets.Delegate) != 0)
\r
341 } else if (element is Enum) {
\r
342 if ((targets & AttributeTargets.Enum) != 0)
\r
346 } else if (element is Event) {
\r
347 if ((targets & AttributeTargets.Event) != 0)
\r
351 } else if (element is Field) {
\r
352 if ((targets & AttributeTargets.Field) != 0)
\r
356 } else if (element is Interface) {
\r
357 if ((targets & AttributeTargets.Interface) != 0)
\r
361 } else if (element is Method) {
\r
362 if ((targets & AttributeTargets.Method) != 0)
\r
366 } else if (element is Parameter) {
\r
367 if ((targets & AttributeTargets.Parameter) != 0)
\r
371 } else if (element is Property) {
\r
372 if ((targets & AttributeTargets.Property) != 0)
\r
381 public MethodBuilder DefinePInvokeMethod (EmitContext ec, TypeBuilder builder, string name,
\r
382 MethodAttributes flags, Type ret_type, Type [] param_types)
\r
385 // We extract from the attribute the information we need from the attribute
\r
388 if (Arguments == null) {
\r
389 Console.WriteLine ("Internal error : this is not supposed to happen !");
\r
393 ArrayList named_args = new ArrayList ();
\r
395 ArrayList pos_args = (ArrayList) Arguments [0];
\r
396 if (Arguments.Count > 1)
\r
397 named_args = (ArrayList) Arguments [1];
\r
400 string dll_name = null;
\r
402 Argument tmp = (Argument) pos_args [0];
\r
404 if (!tmp.Resolve (ec, Location))
\r
407 Expression exp = Expression.Reduce (ec, tmp.Expr);
\r
409 if (exp is Literal)
\r
410 dll_name = (string) ((Literal) exp).GetValue ();
\r
416 // FIXME : We need to process CallingConvention and other named
\r
417 // args but for now, we ignore them.
\r
419 MethodBuilder mb = builder.DefinePInvokeMethod (
\r
420 name, dll_name, flags,
\r
421 CallingConventions.Standard,
\r
424 CallingConvention.StdCall,
\r
432 public class AttributeSection {
\r
434 public readonly string Target;
\r
435 public readonly ArrayList Attributes;
\r
437 public AttributeSection (string target, ArrayList attrs)
\r
440 Attributes = attrs;
\r
445 public class Attributes {
\r
447 public ArrayList AttributeSections;
\r
449 public Attributes (AttributeSection a)
\r
451 AttributeSections = new ArrayList ();
\r
452 AttributeSections.Add (a);
\r
456 public void AddAttribute (AttributeSection a)
\r
459 AttributeSections.Add (a);
\r