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
17 using System.Runtime.CompilerServices;
\r
20 namespace Mono.CSharp {
\r
22 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 MethodImplOptions ImplOptions;
\r
41 public UnmanagedType UnmanagedType;
\r
43 public Attribute (string name, ArrayList args, Location loc)
\r
50 void error617 (string name)
\r
52 Report.Error (617, Location, "'" + name + "' is not a valid named attribute " +
\r
53 "argument. Named attribute arguments must be fields which are not " +
\r
54 "readonly, static or const, or properties with a set accessor which "+
\r
60 Report.Error (182, Location,
\r
61 "An attribute argument must be a constant expression, typeof " +
\r
62 "expression or array creation expression");
\r
65 public CustomAttributeBuilder Resolve (EmitContext ec)
\r
68 bool MethodImplAttr = false;
\r
69 bool MarshalAsAttr = false;
\r
73 if (Name.IndexOf ("Attribute") == -1)
\r
74 name = Name + "Attribute";
\r
75 else if (Name.LastIndexOf ("Attribute") == 0)
\r
76 name = Name + "Attribute";
\r
78 Type = RootContext.LookupType (ec.DeclSpace, name, false, Location);
\r
82 246, Location, "Could not find attribute '" + Name + "' (are you" +
\r
83 " missing a using directive or an assembly reference ?)");
\r
87 if (Type == TypeManager.attribute_usage_type)
\r
89 if (Type == TypeManager.methodimpl_attr_type)
\r
90 MethodImplAttr = true;
\r
91 if (Type == TypeManager.marshal_as_attr_type)
\r
92 MarshalAsAttr = true;
\r
95 // Now we extract the positional and named arguments
\r
97 ArrayList pos_args = new ArrayList ();
\r
98 ArrayList named_args = new ArrayList ();
\r
100 if (Arguments != null) {
\r
101 pos_args = (ArrayList) Arguments [0];
\r
102 if (Arguments.Count > 1)
\r
103 named_args = (ArrayList) Arguments [1];
\r
106 object [] pos_values = new object [pos_args.Count];
\r
109 // First process positional arguments
\r
113 for (i = 0; i < pos_args.Count; i++) {
\r
114 Argument a = (Argument) pos_args [i];
\r
117 if (!a.Resolve (ec, Location))
\r
121 if (e is Constant) {
\r
122 pos_values [i] = ((Constant) e).GetValue ();
\r
125 this.Targets = (AttributeTargets) pos_values [0];
\r
127 if (MethodImplAttr)
\r
128 this.ImplOptions = (MethodImplOptions) pos_values [0];
\r
131 this.UnmanagedType = (System.Runtime.InteropServices.UnmanagedType) pos_values [0];
\r
140 // Now process named arguments
\r
143 ArrayList field_infos = new ArrayList ();
\r
144 ArrayList prop_infos = new ArrayList ();
\r
145 ArrayList field_values = new ArrayList ();
\r
146 ArrayList prop_values = new ArrayList ();
\r
148 for (i = 0; i < named_args.Count; i++) {
\r
149 DictionaryEntry de = (DictionaryEntry) named_args [i];
\r
150 string member_name = (string) de.Key;
\r
151 Argument a = (Argument) de.Value;
\r
154 if (!a.Resolve (ec, Location))
\r
157 Expression member = Expression.MemberLookup (
\r
158 ec, Type, member_name,
\r
159 MemberTypes.Field | MemberTypes.Property,
\r
160 BindingFlags.Public | BindingFlags.Instance,
\r
163 if (member == null || !(member is PropertyExpr || member is FieldExpr)) {
\r
164 error617 (member_name);
\r
169 if (member is PropertyExpr) {
\r
170 PropertyExpr pe = (PropertyExpr) member;
\r
171 PropertyInfo pi = pe.PropertyInfo;
\r
173 if (!pi.CanWrite) {
\r
174 error617 (member_name);
\r
178 if (e is Constant) {
\r
179 object o = ((Constant) e).GetValue ();
\r
180 prop_values.Add (o);
\r
183 if (member_name == "AllowMultiple")
\r
184 this.AllowMultiple = (bool) o;
\r
185 if (member_name == "Inherited")
\r
186 this.Inherited = (bool) o;
\r
194 prop_infos.Add (pi);
\r
196 } else if (member is FieldExpr) {
\r
197 FieldExpr fe = (FieldExpr) member;
\r
198 FieldInfo fi = fe.FieldInfo;
\r
200 if (fi.IsInitOnly) {
\r
201 error617 (member_name);
\r
206 field_values.Add (((Constant) e).GetValue ());
\r
212 field_infos.Add (fi);
\r
216 Expression mg = Expression.MemberLookup (
\r
217 ec, Type, ".ctor", MemberTypes.Constructor,
\r
218 BindingFlags.Public | BindingFlags.Instance, Location);
\r
223 "Could not find a constructor for this argument list.");
\r
227 MethodBase constructor = Invocation.OverloadResolve (
\r
228 ec, (MethodGroupExpr) mg, pos_args, Location);
\r
230 if (constructor == null) {
\r
233 "Could not find a constructor for this argument list.");
\r
237 PropertyInfo [] prop_info_arr = new PropertyInfo [prop_infos.Count];
\r
238 FieldInfo [] field_info_arr = new FieldInfo [field_infos.Count];
\r
239 object [] field_values_arr = new object [field_values.Count];
\r
240 object [] prop_values_arr = new object [prop_values.Count];
\r
242 field_infos.CopyTo (field_info_arr, 0);
\r
243 field_values.CopyTo (field_values_arr, 0);
\r
245 prop_values.CopyTo (prop_values_arr, 0);
\r
246 prop_infos.CopyTo (prop_info_arr, 0);
\r
248 CustomAttributeBuilder cb = new CustomAttributeBuilder (
\r
249 (ConstructorInfo) constructor, pos_values,
\r
250 prop_info_arr, prop_values_arr,
\r
251 field_info_arr, field_values_arr);
\r
256 static string GetValidPlaces (Attribute attr)
\r
258 StringBuilder sb = new StringBuilder ();
\r
259 AttributeTargets targets = 0;
\r
261 TypeContainer a = TypeManager.LookupAttr (attr.Type);
\r
264 System.Attribute [] attrs = System.Attribute.GetCustomAttributes (attr.Type);
\r
266 foreach (System.Attribute tmp in attrs)
\r
267 if (tmp is AttributeUsageAttribute)
\r
268 targets = ((AttributeUsageAttribute) tmp).ValidOn;
\r
270 targets = a.Targets;
\r
273 if ((targets & AttributeTargets.Assembly) != 0)
\r
274 sb.Append ("'assembly' ");
\r
276 if ((targets & AttributeTargets.Class) != 0)
\r
277 sb.Append ("'class' ");
\r
279 if ((targets & AttributeTargets.Constructor) != 0)
\r
280 sb.Append ("'constructor' ");
\r
282 if ((targets & AttributeTargets.Delegate) != 0)
\r
283 sb.Append ("'delegate' ");
\r
285 if ((targets & AttributeTargets.Enum) != 0)
\r
286 sb.Append ("'enum' ");
\r
288 if ((targets & AttributeTargets.Event) != 0)
\r
289 sb.Append ("'event' ");
\r
291 if ((targets & AttributeTargets.Field) != 0)
\r
292 sb.Append ("'field' ");
\r
294 if ((targets & AttributeTargets.Interface) != 0)
\r
295 sb.Append ("'interface' ");
\r
297 if ((targets & AttributeTargets.Method) != 0)
\r
298 sb.Append ("'method' ");
\r
300 if ((targets & AttributeTargets.Module) != 0)
\r
301 sb.Append ("'module' ");
\r
303 if ((targets & AttributeTargets.Parameter) != 0)
\r
304 sb.Append ("'parameter' ");
\r
306 if ((targets & AttributeTargets.Property) != 0)
\r
307 sb.Append ("'property' ");
\r
309 if ((targets & AttributeTargets.ReturnValue) != 0)
\r
310 sb.Append ("'return value' ");
\r
312 if ((targets & AttributeTargets.Struct) != 0)
\r
313 sb.Append ("'struct' ");
\r
315 return sb.ToString ();
\r
319 public static void error592 (Attribute a, Location loc)
\r
322 592, loc, "Attribute '" + a.Name +
\r
323 "' is not valid on this declaration type. " +
\r
324 "It is valid on " + GetValidPlaces (a) + "declarations only.");
\r
327 public static bool CheckAttribute (Attribute a, object element)
\r
329 TypeContainer attr = TypeManager.LookupAttr (a.Type);
\r
330 AttributeTargets targets = 0;
\r
332 if (attr == null) {
\r
333 System.Attribute [] attrs = System.Attribute.GetCustomAttributes (a.Type);
\r
335 foreach (System.Attribute tmp in attrs)
\r
336 if (tmp is AttributeUsageAttribute)
\r
337 targets = ((AttributeUsageAttribute) tmp).ValidOn;
\r
339 targets = attr.Targets;
\r
341 if (element is Class) {
\r
342 if ((targets & AttributeTargets.Class) != 0)
\r
347 } else if (element is Struct) {
\r
348 if ((targets & AttributeTargets.Struct) != 0)
\r
352 } else if (element is Constructor) {
\r
353 if ((targets & AttributeTargets.Constructor) != 0)
\r
357 } else if (element is Delegate) {
\r
358 if ((targets & AttributeTargets.Delegate) != 0)
\r
362 } else if (element is Enum) {
\r
363 if ((targets & AttributeTargets.Enum) != 0)
\r
367 } else if (element is Event) {
\r
368 if ((targets & AttributeTargets.Event) != 0)
\r
372 } else if (element is Field) {
\r
373 if ((targets & AttributeTargets.Field) != 0)
\r
377 } else if (element is Interface) {
\r
378 if ((targets & AttributeTargets.Interface) != 0)
\r
382 } else if (element is Method || element is Operator) {
\r
383 if ((targets & AttributeTargets.Method) != 0)
\r
387 } else if (element is ParameterBuilder) {
\r
388 if ((targets & AttributeTargets.Parameter) != 0)
\r
392 } else if (element is Property) {
\r
393 if ((targets & AttributeTargets.Property) != 0)
\r
397 } else if (element is AssemblyBuilder){
\r
398 if ((targets & AttributeTargets.Assembly) != 0)
\r
407 public static void ApplyAttributes (EmitContext ec, object builder, object kind,
\r
408 Attributes opt_attrs, Location loc)
\r
410 if (opt_attrs == null)
\r
413 if (opt_attrs.AttributeSections == null)
\r
416 foreach (AttributeSection asec in opt_attrs.AttributeSections) {
\r
418 if (asec.Attributes == null)
\r
421 foreach (Attribute a in asec.Attributes) {
\r
422 CustomAttributeBuilder cb = a.Resolve (ec);
\r
426 if (!(kind is TypeContainer))
\r
427 if (!CheckAttribute (a, kind)) {
\r
433 if (kind is Method || kind is Operator) {
\r
435 if (a.Type == TypeManager.methodimpl_attr_type) {
\r
436 if (a.ImplOptions == MethodImplOptions.InternalCall)
\r
437 ((MethodBuilder) builder).SetImplementationFlags (
\r
438 MethodImplAttributes.InternalCall |
\r
439 MethodImplAttributes.Runtime);
\r
440 } else if (a.Type != TypeManager.dllimport_type)
\r
441 ((MethodBuilder) builder).SetCustomAttribute (cb);
\r
443 } else if (kind is Constructor) {
\r
444 ((ConstructorBuilder) builder).SetCustomAttribute (cb);
\r
445 } else if (kind is Field) {
\r
446 ((FieldBuilder) builder).SetCustomAttribute (cb);
\r
447 } else if (kind is Property || kind is Indexer) {
\r
448 ((PropertyBuilder) builder).SetCustomAttribute (cb);
\r
449 } else if (kind is Event) {
\r
450 ((EventBuilder) builder).SetCustomAttribute (cb);
\r
451 } else if (kind is ParameterBuilder) {
\r
453 if (a.Type == TypeManager.marshal_as_attr_type) {
\r
454 UnmanagedMarshal marshal =
\r
455 UnmanagedMarshal.DefineUnmanagedMarshal (a.UnmanagedType);
\r
457 ((ParameterBuilder) builder).SetMarshal (marshal);
\r
459 ((ParameterBuilder) builder).SetCustomAttribute (cb);
\r
461 } else if (kind is Enum) {
\r
462 ((TypeBuilder) builder).SetCustomAttribute (cb);
\r
464 } else if (kind is TypeContainer) {
\r
466 TypeContainer tc = (TypeContainer) kind;
\r
469 tc.Targets = a.Targets;
\r
470 tc.AllowMultiple = a.AllowMultiple;
\r
471 tc.Inherited = a.Inherited;
\r
473 RootContext.TypeManager.RegisterAttrType (
\r
474 (TypeBuilder) builder, tc);
\r
476 } else if (a.Type == TypeManager.default_member_type) {
\r
477 if (tc.Indexers != null) {
\r
478 Report.Error (646, loc,
\r
479 "Cannot specify the DefaultMember attribute on " +
\r
480 " a type containing an indexer");
\r
485 if (!CheckAttribute (a, kind)) {
\r
491 ((TypeBuilder) builder).SetCustomAttribute (cb);
\r
493 } else if (kind is AssemblyBuilder){
\r
494 ((AssemblyBuilder) builder).SetCustomAttribute (cb);
\r
495 } else if (kind is ModuleBuilder) {
\r
496 ((ModuleBuilder) builder).SetCustomAttribute (cb);
\r
498 throw new Exception ("Unknown kind: " + kind);
\r
503 public MethodBuilder DefinePInvokeMethod (EmitContext ec, TypeBuilder builder, string name,
\r
504 MethodAttributes flags, Type ret_type, Type [] param_types)
\r
507 // We extract from the attribute the information we need
\r
510 if (Arguments == null) {
\r
511 Console.WriteLine ("Internal error : this is not supposed to happen !");
\r
515 string attr_name = Name;
\r
517 if (Name.IndexOf ("Attribute") == -1)
\r
518 attr_name = Name + "Attribute";
\r
519 else if (Name.LastIndexOf ("Attribute") == 0)
\r
520 attr_name = Name + "Attribute";
\r
522 Type = RootContext.LookupType (ec.DeclSpace, attr_name, false, Location);
\r
524 if (Type == null) {
\r
525 Report.Error (246, Location, "Could not find attribute '" + Name + "' (are you" +
\r
526 " missing a using directive or an assembly reference ?)");
\r
530 ArrayList named_args = new ArrayList ();
\r
532 ArrayList pos_args = (ArrayList) Arguments [0];
\r
533 if (Arguments.Count > 1)
\r
534 named_args = (ArrayList) Arguments [1];
\r
537 string dll_name = null;
\r
539 Argument tmp = (Argument) pos_args [0];
\r
541 if (!tmp.Resolve (ec, Location))
\r
544 if (tmp.Expr is Constant)
\r
545 dll_name = (string) ((Constant) tmp.Expr).GetValue ();
\r
551 // Now we process the named arguments
\r
552 CallingConvention cc = CallingConvention.Winapi;
\r
553 CharSet charset = CharSet.Ansi;
\r
554 bool preserve_sig = true;
\r
555 bool exact_spelling = false;
\r
556 bool set_last_err = false;
\r
557 string entry_point = null;
\r
559 for (int i = 0; i < named_args.Count; i++) {
\r
561 DictionaryEntry de = (DictionaryEntry) named_args [i];
\r
563 string member_name = (string) de.Key;
\r
564 Argument a = (Argument) de.Value;
\r
566 if (!a.Resolve (ec, Location))
\r
569 Expression member = Expression.MemberLookup (
\r
570 ec, Type, member_name,
\r
571 MemberTypes.Field | MemberTypes.Property,
\r
572 BindingFlags.Public | BindingFlags.Instance,
\r
575 if (member == null || !(member is FieldExpr)) {
\r
576 error617 (member_name);
\r
580 if (member is FieldExpr) {
\r
581 FieldExpr fe = (FieldExpr) member;
\r
582 FieldInfo fi = fe.FieldInfo;
\r
584 if (fi.IsInitOnly) {
\r
585 error617 (member_name);
\r
589 if (a.Expr is Constant) {
\r
590 Constant c = (Constant) a.Expr;
\r
592 if (member_name == "CallingConvention")
\r
593 cc = (CallingConvention) c.GetValue ();
\r
594 else if (member_name == "CharSet")
\r
595 charset = (CharSet) c.GetValue ();
\r
596 else if (member_name == "EntryPoint")
\r
597 entry_point = (string) c.GetValue ();
\r
598 else if (member_name == "SetLastError")
\r
599 set_last_err = (bool) c.GetValue ();
\r
600 else if (member_name == "ExactSpelling")
\r
601 exact_spelling = (bool) c.GetValue ();
\r
602 else if (member_name == "PreserveSig")
\r
603 preserve_sig = (bool) c.GetValue ();
\r
612 MethodBuilder mb = builder.DefinePInvokeMethod (
\r
613 name, dll_name, flags | MethodAttributes.HideBySig,
\r
614 CallingConventions.Standard,
\r
621 mb.SetImplementationFlags (MethodImplAttributes.PreserveSig);
\r
628 public class AttributeSection {
\r
630 public readonly string Target;
\r
631 public readonly ArrayList Attributes;
\r
633 public AttributeSection (string target, ArrayList attrs)
\r
636 Attributes = attrs;
\r
641 public class Attributes {
\r
642 public ArrayList AttributeSections;
\r
643 public Location Location;
\r
645 public Attributes (AttributeSection a, Location loc)
\r
647 AttributeSections = new ArrayList ();
\r
648 AttributeSections.Add (a);
\r
652 public void AddAttribute (AttributeSection a)
\r
655 AttributeSections.Add (a);
\r