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
22 public readonly string Name;
\r
23 public readonly ArrayList Arguments;
\r
30 // The following are only meaningful when the attribute
\r
31 // being emitted is one of the builtin ones
\r
33 public AttributeTargets Targets;
\r
34 public bool AllowMultiple;
\r
35 public bool Inherited;
\r
37 public bool UsageAttr = false;
\r
39 public Attribute (string name, ArrayList args, Location loc)
\r
46 void error617 (string name)
\r
48 Report.Error (617, Location, "'" + name + "' is not a valid named attribute " +
\r
49 "argument. Named attribute arguments must be fields which are not " +
\r
50 "readonly, static or const, or properties with a set accessor which "+
\r
56 Report.Error (182, Location,
\r
57 "An attribute argument must be a constant expression, typeof " +
\r
58 "expression or array creation expression");
\r
61 public CustomAttributeBuilder Resolve (EmitContext ec)
\r
67 if (Name.IndexOf ("Attribute") == -1)
\r
68 name = Name + "Attribute";
\r
69 else if (Name.LastIndexOf ("Attribute") == 0)
\r
70 name = Name + "Attribute";
\r
72 Type = RootContext.LookupType (ec.TypeContainer, name, false, Location);
\r
76 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
103 Argument a = (Argument) pos_args [i];
\r
106 if (!a.Resolve (ec, Location))
\r
110 if (e is Constant) {
\r
111 pos_values [i] = ((Constant) e).GetValue ();
\r
114 this.Targets = (AttributeTargets) pos_values [0];
\r
122 // Now process named arguments
\r
125 ArrayList field_infos = new ArrayList ();
\r
126 ArrayList prop_infos = new ArrayList ();
\r
127 ArrayList field_values = new ArrayList ();
\r
128 ArrayList prop_values = new ArrayList ();
\r
130 for (i = 0; i < named_args.Count; i++) {
\r
131 DictionaryEntry de = (DictionaryEntry) named_args [i];
\r
132 string member_name = (string) de.Key;
\r
133 Argument a = (Argument) de.Value;
\r
136 if (!a.Resolve (ec, Location))
\r
139 Expression member = Expression.MemberLookup (
\r
140 ec, Type, member_name, false,
\r
141 MemberTypes.Field | MemberTypes.Property,
\r
142 BindingFlags.Public | BindingFlags.Instance,
\r
145 if (member == null || !(member is PropertyExpr || member is FieldExpr)) {
\r
146 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 if (e is Constant) {
\r
161 object o = ((Constant) e).GetValue ();
\r
162 prop_values.Add (o);
\r
165 if (member_name == "AllowMultiple")
\r
166 this.AllowMultiple = (bool) o;
\r
167 if (member_name == "Inherited")
\r
168 this.Inherited = (bool) o;
\r
176 prop_infos.Add (pi);
\r
178 } else if (member is FieldExpr) {
\r
179 FieldExpr fe = (FieldExpr) member;
\r
180 FieldInfo fi = fe.FieldInfo;
\r
182 if (fi.IsInitOnly) {
\r
183 error617 (member_name);
\r
188 field_values.Add (((Constant) e).GetValue ());
\r
194 field_infos.Add (fi);
\r
198 Expression mg = Expression.MemberLookup (
\r
199 ec, Type, ".ctor", false, MemberTypes.Constructor,
\r
200 BindingFlags.Public | BindingFlags.Instance, Location);
\r
205 "Could not find a constructor for this argument list.");
\r
209 MethodBase constructor = Invocation.OverloadResolve (
\r
210 ec, (MethodGroupExpr) mg, pos_args, Location);
\r
212 if (constructor == null) {
\r
215 "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 (
\r
231 (ConstructorInfo) constructor, pos_values,
\r
232 prop_info_arr, prop_values_arr,
\r
233 field_info_arr, field_values_arr);
\r
238 static string GetValidPlaces (Attribute attr)
\r
240 StringBuilder sb = new StringBuilder ();
\r
241 AttributeTargets targets = 0;
\r
243 TypeContainer a = TypeManager.LookupAttr (attr.Type);
\r
246 System.Attribute [] attrs = System.Attribute.GetCustomAttributes (attr.Type);
\r
248 foreach (System.Attribute tmp in attrs)
\r
249 if (tmp is AttributeUsageAttribute)
\r
250 targets = ((AttributeUsageAttribute) tmp).ValidOn;
\r
252 targets = a.Targets;
\r
255 if ((targets & AttributeTargets.Assembly) != 0)
\r
256 sb.Append ("'assembly' ");
\r
258 if ((targets & AttributeTargets.Class) != 0)
\r
259 sb.Append ("'class' ");
\r
261 if ((targets & AttributeTargets.Constructor) != 0)
\r
262 sb.Append ("'constructor' ");
\r
264 if ((targets & AttributeTargets.Delegate) != 0)
\r
265 sb.Append ("'delegate' ");
\r
267 if ((targets & AttributeTargets.Enum) != 0)
\r
268 sb.Append ("'enum' ");
\r
270 if ((targets & AttributeTargets.Event) != 0)
\r
271 sb.Append ("'event' ");
\r
273 if ((targets & AttributeTargets.Field) != 0)
\r
274 sb.Append ("'field' ");
\r
276 if ((targets & AttributeTargets.Interface) != 0)
\r
277 sb.Append ("'interface' ");
\r
279 if ((targets & AttributeTargets.Method) != 0)
\r
280 sb.Append ("'method' ");
\r
282 if ((targets & AttributeTargets.Module) != 0)
\r
283 sb.Append ("'module' ");
\r
285 if ((targets & AttributeTargets.Parameter) != 0)
\r
286 sb.Append ("'parameter' ");
\r
288 if ((targets & AttributeTargets.Property) != 0)
\r
289 sb.Append ("'property' ");
\r
291 if ((targets & AttributeTargets.ReturnValue) != 0)
\r
292 sb.Append ("'return value' ");
\r
294 if ((targets & AttributeTargets.Struct) != 0)
\r
295 sb.Append ("'struct' ");
\r
297 return sb.ToString ();
\r
301 public static void error592 (Attribute a, Location loc)
\r
303 Report.Error (592, loc, "Attribute '" + a.Name + "' is not valid on this declaration type. " +
\r
304 "It is valid on " + GetValidPlaces (a) + "declarations only.");
\r
307 public static bool CheckAttribute (Attribute a, object element)
\r
309 TypeContainer attr = TypeManager.LookupAttr (a.Type);
\r
310 AttributeTargets targets = 0;
\r
312 if (attr == null) {
\r
313 System.Attribute [] attrs = System.Attribute.GetCustomAttributes (a.Type);
\r
315 foreach (System.Attribute tmp in attrs)
\r
316 if (tmp is AttributeUsageAttribute)
\r
317 targets = ((AttributeUsageAttribute) tmp).ValidOn;
\r
319 targets = attr.Targets;
\r
321 if (element is Class) {
\r
322 if ((targets & AttributeTargets.Class) != 0)
\r
327 } else if (element is Struct) {
\r
328 if ((targets & AttributeTargets.Struct) != 0)
\r
332 } else if (element is Constructor) {
\r
333 if ((targets & AttributeTargets.Constructor) != 0)
\r
337 } else if (element is Delegate) {
\r
338 if ((targets & AttributeTargets.Delegate) != 0)
\r
342 } else if (element is Enum) {
\r
343 if ((targets & AttributeTargets.Enum) != 0)
\r
347 } else if (element is Event) {
\r
348 if ((targets & AttributeTargets.Event) != 0)
\r
352 } else if (element is Field) {
\r
353 if ((targets & AttributeTargets.Field) != 0)
\r
357 } else if (element is Interface) {
\r
358 if ((targets & AttributeTargets.Interface) != 0)
\r
362 } else if (element is Method) {
\r
363 if ((targets & AttributeTargets.Method) != 0)
\r
367 } else if (element is Parameter) {
\r
368 if ((targets & AttributeTargets.Parameter) != 0)
\r
372 } else if (element is Property) {
\r
373 if ((targets & AttributeTargets.Property) != 0)
\r
382 public static void ApplyAttributes (EmitContext ec, object builder, object kind,
\r
383 Attributes opt_attrs, Location loc)
\r
385 if (opt_attrs == null)
\r
388 if (opt_attrs.AttributeSections == null)
\r
391 foreach (AttributeSection asec in opt_attrs.AttributeSections) {
\r
393 if (asec.Attributes == null)
\r
396 foreach (Attribute a in asec.Attributes) {
\r
397 CustomAttributeBuilder cb = a.Resolve (ec);
\r
401 if (!(kind is TypeContainer))
\r
402 if (!CheckAttribute (a, kind)) {
\r
408 if (kind is Method) {
\r
409 if (a.Type != TypeManager.dllimport_type)
\r
410 ((MethodBuilder) builder).SetCustomAttribute (cb);
\r
412 } else if (kind is Constructor) {
\r
413 ((ConstructorBuilder) builder).SetCustomAttribute (cb);
\r
415 } else if (kind is Field) {
\r
416 ((FieldBuilder) builder).SetCustomAttribute (cb);
\r
418 } else if (kind is Property || kind is Indexer) {
\r
419 ((PropertyBuilder) builder).SetCustomAttribute (cb);
\r
421 } else if (kind is Event) {
\r
422 ((EventBuilder) builder).SetCustomAttribute (cb);
\r
424 } else if (kind is Operator) {
\r
425 ((MethodBuilder) builder).SetCustomAttribute (cb);
\r
427 } else if (kind is Enum) {
\r
428 ((TypeBuilder) builder).SetCustomAttribute (cb);
\r
430 } else if (kind is TypeContainer) {
\r
432 TypeContainer tc = (TypeContainer) kind;
\r
435 tc.Targets = a.Targets;
\r
436 tc.AllowMultiple = a.AllowMultiple;
\r
437 tc.Inherited = a.Inherited;
\r
439 RootContext.TypeManager.RegisterAttrType (
\r
440 (TypeBuilder) builder, tc);
\r
443 if (!CheckAttribute (a, kind)) {
\r
449 ((TypeBuilder) builder).SetCustomAttribute (cb);
\r
456 public MethodBuilder DefinePInvokeMethod (EmitContext ec, TypeBuilder builder, string name,
\r
457 MethodAttributes flags, Type ret_type, Type [] param_types)
\r
460 // We extract from the attribute the information we need
\r
463 if (Arguments == null) {
\r
464 Console.WriteLine ("Internal error : this is not supposed to happen !");
\r
468 string attr_name = Name;
\r
470 if (Name.IndexOf ("Attribute") == -1)
\r
471 attr_name = Name + "Attribute";
\r
472 else if (Name.LastIndexOf ("Attribute") == 0)
\r
473 attr_name = Name + "Attribute";
\r
475 Type = RootContext.LookupType (ec.TypeContainer, attr_name, false, Location);
\r
477 if (Type == null) {
\r
478 Report.Error (246, Location, "Could not find attribute '" + Name + "' (are you" +
\r
479 " missing a using directive or an assembly reference ?)");
\r
483 ArrayList named_args = new ArrayList ();
\r
485 ArrayList pos_args = (ArrayList) Arguments [0];
\r
486 if (Arguments.Count > 1)
\r
487 named_args = (ArrayList) Arguments [1];
\r
490 string dll_name = null;
\r
492 Argument tmp = (Argument) pos_args [0];
\r
494 if (!tmp.Resolve (ec, Location))
\r
497 if (tmp.Expr is Constant)
\r
498 dll_name = (string) ((Constant) tmp.Expr).GetValue ();
\r
504 // Now we process the named arguments
\r
505 CallingConvention cc = 0;
\r
506 CharSet charset = 0;
\r
507 bool preserve_sig = false;
\r
508 bool exact_spelling = false;
\r
509 bool set_last_err = false;
\r
510 string entry_point = null;
\r
512 for (int i = 0; i < named_args.Count; i++) {
\r
514 DictionaryEntry de = (DictionaryEntry) named_args [i];
\r
516 string member_name = (string) de.Key;
\r
517 Argument a = (Argument) de.Value;
\r
519 if (!a.Resolve (ec, Location))
\r
522 Expression member = Expression.MemberLookup (ec, Type, member_name, false,
\r
523 MemberTypes.Field | MemberTypes.Property,
\r
524 BindingFlags.Public | BindingFlags.Instance,
\r
527 if (member == null || !(member is FieldExpr)) {
\r
528 error617 (member_name);
\r
532 if (member is FieldExpr) {
\r
533 FieldExpr fe = (FieldExpr) member;
\r
534 FieldInfo fi = fe.FieldInfo;
\r
536 if (fi.IsInitOnly) {
\r
537 error617 (member_name);
\r
541 if (a.Expr is Constant) {
\r
542 Constant c = (Constant) a.Expr;
\r
544 if (member_name == "CallingConvention")
\r
545 cc = (CallingConvention) c.GetValue ();
\r
546 else if (member_name == "CharSet")
\r
547 charset = (CharSet) c.GetValue ();
\r
548 else if (member_name == "EntryPoint")
\r
549 entry_point = (string) c.GetValue ();
\r
550 else if (member_name == "SetLastError")
\r
551 set_last_err = (bool) c.GetValue ();
\r
552 else if (member_name == "ExactSpelling")
\r
553 exact_spelling = (bool) c.GetValue ();
\r
554 else if (member_name == "PreserveSig")
\r
555 preserve_sig = (bool) c.GetValue ();
\r
564 MethodBuilder mb = builder.DefinePInvokeMethod (
\r
565 name, dll_name, flags,
\r
566 CallingConventions.Standard,
\r
577 public class AttributeSection {
\r
579 public readonly string Target;
\r
580 public readonly ArrayList Attributes;
\r
582 public AttributeSection (string target, ArrayList attrs)
\r
585 Attributes = attrs;
\r
590 public class Attributes {
\r
592 public ArrayList AttributeSections;
\r
594 public Attributes (AttributeSection a)
\r
596 AttributeSections = new ArrayList ();
\r
597 AttributeSections.Add (a);
\r
601 public void AddAttribute (AttributeSection a)
\r
604 AttributeSections.Add (a);
\r