2 // attribute.cs: Attribute Handler
4 // Author: Ravi Pratap (ravi@ximian.com)
5 // Marek Safar (marek.safar@seznam.cz)
7 // Licensed under the terms of the GNU GPL
9 // (C) 2001 Ximian, Inc (http://www.ximian.com)
14 using System.Diagnostics;
15 using System.Collections;
16 using System.Collections.Specialized;
17 using System.Reflection;
18 using System.Reflection.Emit;
19 using System.Runtime.InteropServices;
20 using System.Runtime.CompilerServices;
21 using System.Security;
22 using System.Security.Permissions;
25 namespace Mono.CSharp {
28 /// Base class for objects that can have Attributes applied to them.
30 public abstract class Attributable {
32 /// Attributes for this type
34 Attributes attributes;
36 public Attributable (Attributes attrs)
41 public Attributes OptAttributes
52 /// Use member-specific procedure to apply attribute @a in @cb to the entity being built in @builder
54 public abstract void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb);
57 /// Returns one AttributeTarget for this element.
59 public abstract AttributeTargets AttributeTargets { get; }
61 public abstract bool IsClsCompliaceRequired (DeclSpace ds);
64 /// Gets list of valid attribute targets for explicit target declaration.
65 /// The first array item is default target. Don't break this rule.
67 public abstract string[] ValidAttributeTargets { get; }
70 public class Attribute {
71 public readonly string ExplicitTarget;
72 public AttributeTargets Target;
74 public readonly string Name;
75 public readonly Expression LeftExpr;
76 public readonly string Identifier;
78 public readonly ArrayList Arguments;
80 public readonly Location Location;
86 static AttributeUsageAttribute DefaultUsageAttribute = new AttributeUsageAttribute (AttributeTargets.All);
88 // non-null if named args present after Resolve () is called
89 PropertyInfo [] prop_info_arr;
90 FieldInfo [] field_info_arr;
91 object [] field_values_arr;
92 object [] prop_values_arr;
95 static PtrHashtable usage_attr_cache = new PtrHashtable ();
97 public Attribute (string target, Expression left_expr, string identifier, ArrayList args, Location loc)
100 Identifier = identifier;
101 Name = LeftExpr == null ? identifier : LeftExpr + "." + identifier;
104 ExplicitTarget = target;
107 void Error_InvalidNamedArgument (string name)
109 Report.Error (617, Location, "`{0}' is not a valid named attribute argument. Named attribute arguments must be fields which are not readonly, static, const or read-write properties which are public and not static",
113 void Error_InvalidNamedAgrumentType (string name)
115 Report.Error (655, Location, "`{0}' is not a valid named attribute argument because it is not a valid attribute parameter type", name);
118 static void Error_AttributeArgumentNotValid (string extra, Location loc)
120 Report.Error (182, loc,
121 "An attribute argument must be a constant expression, typeof " +
122 "expression or array creation expression" + extra);
125 static void Error_AttributeArgumentNotValid (Location loc)
127 Error_AttributeArgumentNotValid ("", loc);
132 /// This is rather hack. We report many emit attribute error with same error to be compatible with
133 /// csc. But because csc has to report them this way because error came from ilasm we needn't.
135 public void Error_AttributeEmitError (string inner)
137 Report.Error (647, Location, "Error during emitting `{0}' attribute. The reason is `{1}'", TypeManager.CSharpName (Type), inner);
140 public void Error_InvalidSecurityParent ()
142 Error_AttributeEmitError ("it is attached to invalid parent");
145 void Error_AttributeConstructorMismatch ()
147 Report.Error (-6, Location,
148 "Could not find a constructor for this argument list.");
152 protected virtual FullNamedExpression ResolveAsTypeStep (Expression expr, EmitContext ec)
154 return expr.ResolveAsTypeStep (ec);
157 void ResolvePossibleAttributeTypes (EmitContext ec, out Type t1, out Type t2)
162 FullNamedExpression n1 = null;
163 FullNamedExpression n2 = null;
164 string IdentifierAttribute = Identifier + "Attribute";
165 if (LeftExpr == null) {
166 n1 = ResolveAsTypeStep (new SimpleName (Identifier, Location), ec);
168 // FIXME: Shouldn't do this for quoted attributes: [@A]
169 n2 = ResolveAsTypeStep (new SimpleName (IdentifierAttribute, Location), ec);
171 FullNamedExpression l = ResolveAsTypeStep (LeftExpr, ec);
173 NamespaceEntry.Error_NamespaceNotFound (Location, LeftExpr.GetSignatureForError ());
176 n1 = new MemberAccess (l, Identifier, Location).ResolveNamespaceOrType (ec, true);
178 // FIXME: Shouldn't do this for quoted attributes: [X.@A]
179 n2 = new MemberAccess (l, IdentifierAttribute, Location).ResolveNamespaceOrType (ec, true);
182 TypeExpr te1 = n1 == null ? null : n1 as TypeExpr;
183 TypeExpr te2 = n2 == null ? null : n2 as TypeExpr;
186 t1 = te1.ResolveType (ec);
188 t2 = te2.ResolveType (ec);
192 /// Tries to resolve the type of the attribute. Flags an error if it can't, and complain is true.
194 Type CheckAttributeType (EmitContext ec)
198 ResolvePossibleAttributeTypes (ec, out t1, out t2);
200 String err0616 = null;
202 if (t1 != null && ! t1.IsSubclassOf (TypeManager.attribute_type)) {
204 err0616 = "`{0}' is not an attribute class";
206 if (t2 != null && ! t2.IsSubclassOf (TypeManager.attribute_type)) {
208 err0616 = (err0616 != null)
209 ? "Neither `{0}' nor `{0}Attribute' is an attribute class"
210 : "`{0}Attribute': is not an attribute class";
213 if (t1 != null && t2 != null) {
214 Report.Error (1614, Location, "`{0}' is ambiguous between `{0}' and `{0}Attribute'. Use either `@{0}' or `{0}Attribute'", Name);
215 resolve_error = true;
223 if (err0616 != null) {
224 Report.Error (616, Location, err0616, Name);
228 NamespaceEntry.Error_NamespaceNotFound (Location, Name);
230 resolve_error = true;
234 public virtual Type ResolveType (EmitContext ec)
236 if (Type == null && !resolve_error)
237 Type = CheckAttributeType (ec);
241 string GetFullMemberName (string member)
243 return Type.FullName + '.' + member;
247 // Given an expression, if the expression is a valid attribute-argument-expression
248 // returns an object that can be used to encode it, or null on failure.
250 public static bool GetAttributeArgumentExpression (Expression e, Location loc, Type arg_type, out object result)
252 if (e is EnumConstant) {
253 if (RootContext.StdLib)
254 result = ((EnumConstant)e).GetValueAsEnumType ();
256 result = ((EnumConstant)e).GetValue ();
261 Constant constant = e as Constant;
262 if (constant != null) {
263 if (e.Type != arg_type) {
264 constant = Const.ChangeType (loc, constant, arg_type);
265 if (constant == null) {
267 Error_AttributeArgumentNotValid (loc);
271 result = constant.GetValue ();
273 } else if (e is TypeOf) {
274 result = ((TypeOf) e).TypeArg;
276 } else if (e is ArrayCreation){
277 result = ((ArrayCreation) e).EncodeAsAttribute ();
280 } else if (e is EmptyCast) {
281 Expression child = ((EmptyCast)e).Child;
282 return GetAttributeArgumentExpression (child, loc, child.Type, out result);
286 Error_AttributeArgumentNotValid (loc);
290 bool IsValidArgumentType (Type t)
292 return TypeManager.IsPrimitiveType (t) ||
293 (t.IsArray && TypeManager.IsPrimitiveType (t.GetElementType ())) ||
294 TypeManager.IsEnumType (t) ||
295 t == TypeManager.string_type ||
296 t == TypeManager.object_type ||
297 t == TypeManager.type_type;
300 // Cache for parameter-less attributes
301 static PtrHashtable att_cache = new PtrHashtable ();
303 public CustomAttributeBuilder Resolve (EmitContext ec)
308 resolve_error = true;
311 Type = CheckAttributeType (ec);
317 if (Type.IsAbstract) {
318 Report.Error (653, Location, "Cannot apply attribute class `{0}' because it is abstract", Name);
322 if (Arguments == null) {
323 object o = att_cache [Type];
325 resolve_error = false;
326 return (CustomAttributeBuilder)o;
330 ConstructorInfo ctor = ResolveArguments (ec);
331 CustomAttributeBuilder cb;
334 if (prop_info_arr != null || field_info_arr != null) {
335 cb = new CustomAttributeBuilder (
337 prop_info_arr, prop_values_arr,
338 field_info_arr, field_values_arr);
340 cb = new CustomAttributeBuilder (
343 if (pos_values.Length == 0)
344 att_cache.Add (Type, cb);
348 Error_AttributeArgumentNotValid (Location);
352 resolve_error = false;
356 protected virtual ConstructorInfo ResolveArguments (EmitContext ec)
358 // Now we extract the positional and named arguments
360 ArrayList pos_args = null;
361 ArrayList named_args = null;
362 int pos_arg_count = 0;
363 int named_arg_count = 0;
365 if (Arguments != null) {
366 pos_args = (ArrayList) Arguments [0];
367 if (pos_args != null)
368 pos_arg_count = pos_args.Count;
369 if (Arguments.Count > 1) {
370 named_args = (ArrayList) Arguments [1];
371 named_arg_count = named_args.Count;
375 pos_values = new object [pos_arg_count];
378 // First process positional arguments
382 for (i = 0; i < pos_arg_count; i++) {
383 Argument a = (Argument) pos_args [i];
386 if (!a.Resolve (ec, Location))
392 if (!GetAttributeArgumentExpression (e, Location, a.Type, out val))
395 pos_values [i] = val;
397 if (i == 0 && Type == TypeManager.attribute_usage_type && (int)val == 0) {
398 Report.Error (591, Location, "Invalid value for argument to 'System.AttributeUsage' attribute");
404 // Now process named arguments
407 ArrayList field_infos = null;
408 ArrayList prop_infos = null;
409 ArrayList field_values = null;
410 ArrayList prop_values = null;
411 Hashtable seen_names = null;
413 if (named_arg_count > 0) {
414 field_infos = new ArrayList ();
415 prop_infos = new ArrayList ();
416 field_values = new ArrayList ();
417 prop_values = new ArrayList ();
419 seen_names = new Hashtable();
422 for (i = 0; i < named_arg_count; i++) {
423 DictionaryEntry de = (DictionaryEntry) named_args [i];
424 string member_name = (string) de.Key;
425 Argument a = (Argument) de.Value;
428 if (seen_names.Contains(member_name)) {
429 Report.Error(643, Location, "'" + member_name + "' duplicate named attribute argument");
432 seen_names.Add(member_name, 1);
434 if (!a.Resolve (ec, Location))
437 Expression member = Expression.MemberLookup (
438 ec, Type, member_name,
439 MemberTypes.Field | MemberTypes.Property,
440 BindingFlags.Public | BindingFlags.Instance,
443 if (member == null) {
444 member = Expression.MemberLookup (ec, Type, member_name,
445 MemberTypes.Field | MemberTypes.Property, BindingFlags.NonPublic | BindingFlags.Instance,
448 if (member != null) {
449 Expression.ErrorIsInaccesible (Location, GetFullMemberName (member_name));
455 Report.Error (117, Location, "`{0}' does not contain a definition for `{1}'",
460 if (!(member is PropertyExpr || member is FieldExpr)) {
461 Error_InvalidNamedArgument (member_name);
466 if (member is PropertyExpr) {
467 PropertyExpr pe = (PropertyExpr) member;
468 PropertyInfo pi = pe.PropertyInfo;
470 if (!pi.CanWrite || !pi.CanRead) {
471 Report.SymbolRelatedToPreviousError (pi);
472 Error_InvalidNamedArgument (member_name);
476 if (!IsValidArgumentType (pi.PropertyType)) {
477 Report.SymbolRelatedToPreviousError (pi);
478 Error_InvalidNamedAgrumentType (member_name);
483 if (!GetAttributeArgumentExpression (e, Location, pi.PropertyType, out value))
486 prop_values.Add (value);
489 } else if (member is FieldExpr) {
490 FieldExpr fe = (FieldExpr) member;
491 FieldInfo fi = fe.FieldInfo;
494 Error_InvalidNamedArgument (member_name);
498 if (!IsValidArgumentType (fi.FieldType)) {
499 Report.SymbolRelatedToPreviousError (fi);
500 Error_InvalidNamedAgrumentType (member_name);
505 if (!GetAttributeArgumentExpression (e, Location, fi.FieldType, out value))
508 field_values.Add (value);
509 field_infos.Add (fi);
513 Expression mg = Expression.MemberLookup (
514 ec, Type, ".ctor", MemberTypes.Constructor,
515 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
519 Error_AttributeConstructorMismatch ();
523 MethodBase constructor = Invocation.OverloadResolve (
524 ec, (MethodGroupExpr) mg, pos_args, false, Location);
526 if (constructor == null) {
531 // Here we do the checks which should be done by corlib or by runtime.
532 // However Zoltan doesn't like it and every Mono compiler has to do it again.
534 if (Type == TypeManager.guid_attr_type) {
536 new Guid ((string)pos_values [0]);
538 catch (Exception e) {
539 Error_AttributeEmitError (e.Message);
543 // if (Type == TypeManager.methodimpl_attr_type &&
544 // pos_values.Length == 1 && ((Argument)pos_args [0]).Type == TypeManager.short_type &&
545 // !System.Enum.IsDefined (TypeManager.method_impl_options, pos_values [0])) {
546 // Error_AttributeEmitError ("Incorrect argument value.");
550 // Now we perform some checks on the positional args as they
551 // cannot be null for a constructor which expects a parameter
555 ParameterData pd = TypeManager.GetParameterData (constructor);
557 int last_real_param = pd.Count;
559 // When the params is not filled we need to put one
560 if (last_real_param > pos_arg_count) {
561 object [] new_pos_values = new object [pos_arg_count + 1];
562 pos_values.CopyTo (new_pos_values, 0);
563 new_pos_values [pos_arg_count] = new object [] {} ;
564 pos_values = new_pos_values;
569 for (int j = 0; j < pos_arg_count; ++j) {
570 Argument a = (Argument) pos_args [j];
572 if (a.Expr is NullLiteral && pd.ParameterType (j) == TypeManager.object_type) {
573 Error_AttributeArgumentNotValid (Location);
577 object value = pos_values [j];
578 if (value != null && a.Type != value.GetType () && TypeManager.IsPrimitiveType (a.Type)) {
580 pos_values [j] = TypeManager.ChangeType (value, a.Type, out fail);
582 // TODO: Can failed ?
583 throw new NotImplementedException ();
587 if (j < last_real_param)
590 if (j == last_real_param) {
591 object [] array = new object [pos_arg_count - last_real_param];
592 array [0] = pos_values [j];
593 pos_values [j] = array;
597 object [] params_array = (object []) pos_values [last_real_param];
598 params_array [j - last_real_param] = pos_values [j];
601 // Adjust the size of the pos_values if it had params
602 if (last_real_param != pos_arg_count) {
603 object [] new_pos_values = new object [last_real_param + 1];
604 Array.Copy (pos_values, new_pos_values, last_real_param + 1);
605 pos_values = new_pos_values;
608 if (named_arg_count > 0) {
609 prop_info_arr = new PropertyInfo [prop_infos.Count];
610 field_info_arr = new FieldInfo [field_infos.Count];
611 field_values_arr = new object [field_values.Count];
612 prop_values_arr = new object [prop_values.Count];
614 field_infos.CopyTo (field_info_arr, 0);
615 field_values.CopyTo (field_values_arr, 0);
617 prop_values.CopyTo (prop_values_arr, 0);
618 prop_infos.CopyTo (prop_info_arr, 0);
621 return (ConstructorInfo) constructor;
625 /// Get a string containing a list of valid targets for the attribute 'attr'
627 public string GetValidTargets ()
629 StringBuilder sb = new StringBuilder ();
630 AttributeTargets targets = GetAttributeUsage (null).ValidOn;
632 if ((targets & AttributeTargets.Assembly) != 0)
633 sb.Append ("assembly, ");
635 if ((targets & AttributeTargets.Module) != 0)
636 sb.Append ("module, ");
638 if ((targets & AttributeTargets.Class) != 0)
639 sb.Append ("class, ");
641 if ((targets & AttributeTargets.Struct) != 0)
642 sb.Append ("struct, ");
644 if ((targets & AttributeTargets.Enum) != 0)
645 sb.Append ("enum, ");
647 if ((targets & AttributeTargets.Constructor) != 0)
648 sb.Append ("constructor, ");
650 if ((targets & AttributeTargets.Method) != 0)
651 sb.Append ("method, ");
653 if ((targets & AttributeTargets.Property) != 0)
654 sb.Append ("property, indexer, ");
656 if ((targets & AttributeTargets.Field) != 0)
657 sb.Append ("field, ");
659 if ((targets & AttributeTargets.Event) != 0)
660 sb.Append ("event, ");
662 if ((targets & AttributeTargets.Interface) != 0)
663 sb.Append ("interface, ");
665 if ((targets & AttributeTargets.Parameter) != 0)
666 sb.Append ("parameter, ");
668 if ((targets & AttributeTargets.Delegate) != 0)
669 sb.Append ("delegate, ");
671 if ((targets & AttributeTargets.ReturnValue) != 0)
672 sb.Append ("return, ");
675 if ((targets & AttributeTargets.GenericParameter) != 0)
676 sb.Append ("type parameter, ");
678 return sb.Remove (sb.Length - 2, 2).ToString ();
682 /// Returns AttributeUsage attribute for this type
684 AttributeUsageAttribute GetAttributeUsage (EmitContext ec)
686 AttributeUsageAttribute ua = usage_attr_cache [Type] as AttributeUsageAttribute;
690 Class attr_class = TypeManager.LookupClass (Type);
692 if (attr_class == null) {
693 object[] usage_attr = Type.GetCustomAttributes (TypeManager.attribute_usage_type, true);
694 ua = (AttributeUsageAttribute)usage_attr [0];
695 usage_attr_cache.Add (Type, ua);
699 Attribute a = attr_class.OptAttributes == null
701 : attr_class.OptAttributes.Search (TypeManager.attribute_usage_type, attr_class.EmitContext);
704 ? DefaultUsageAttribute
705 : a.GetAttributeUsageAttribute (attr_class.EmitContext);
707 usage_attr_cache.Add (Type, ua);
711 AttributeUsageAttribute GetAttributeUsageAttribute (EmitContext ec)
713 if (pos_values == null)
714 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
715 // But because a lot of attribute class code must be rewritten will be better to wait...
719 return DefaultUsageAttribute;
721 AttributeUsageAttribute usage_attribute = new AttributeUsageAttribute ((AttributeTargets)pos_values [0]);
723 object field = GetPropertyValue ("AllowMultiple");
725 usage_attribute.AllowMultiple = (bool)field;
727 field = GetPropertyValue ("Inherited");
729 usage_attribute.Inherited = (bool)field;
731 return usage_attribute;
735 /// Returns custom name of indexer
737 public string GetIndexerAttributeValue (EmitContext ec)
739 if (pos_values == null)
740 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
741 // But because a lot of attribute class code must be rewritten will be better to wait...
747 return pos_values [0] as string;
751 /// Returns condition of ConditionalAttribute
753 public string GetConditionalAttributeValue (EmitContext ec)
755 if (pos_values == null)
756 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
757 // But because a lot of attribute class code must be rewritten will be better to wait...
763 return (string)pos_values [0];
767 /// Creates the instance of ObsoleteAttribute from this attribute instance
769 public ObsoleteAttribute GetObsoleteAttribute (EmitContext ec)
771 if (pos_values == null)
772 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
773 // But because a lot of attribute class code must be rewritten will be better to wait...
779 if (pos_values == null || pos_values.Length == 0)
780 return new ObsoleteAttribute ();
782 if (pos_values.Length == 1)
783 return new ObsoleteAttribute ((string)pos_values [0]);
785 return new ObsoleteAttribute ((string)pos_values [0], (bool)pos_values [1]);
789 /// Returns value of CLSCompliantAttribute contructor parameter but because the method can be called
790 /// before ApplyAttribute. We need to resolve the arguments.
791 /// This situation occurs when class deps is differs from Emit order.
793 public bool GetClsCompliantAttributeValue (EmitContext ec)
795 if (pos_values == null)
796 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
797 // But because a lot of attribute class code must be rewritten will be better to wait...
803 return (bool)pos_values [0];
807 /// Tests permitted SecurityAction for assembly or other types
809 public bool CheckSecurityActionValidity (bool for_assembly)
811 SecurityAction action = GetSecurityActionValue ();
813 if ((action == SecurityAction.RequestMinimum || action == SecurityAction.RequestOptional || action == SecurityAction.RequestRefuse) && for_assembly)
817 if (action < SecurityAction.Demand || action > SecurityAction.InheritanceDemand) {
818 Error_AttributeEmitError ("SecurityAction is out of range");
822 if ((action != SecurityAction.RequestMinimum && action != SecurityAction.RequestOptional && action != SecurityAction.RequestRefuse) && !for_assembly)
826 Error_AttributeEmitError (String.Concat ("SecurityAction '", action, "' is not valid for this declaration"));
830 System.Security.Permissions.SecurityAction GetSecurityActionValue ()
832 return (SecurityAction)pos_values [0];
836 /// Creates instance of SecurityAttribute class and add result of CreatePermission method to permission table.
838 /// <returns></returns>
839 public void ExtractSecurityPermissionSet (ListDictionary permissions)
841 if (TypeManager.LookupDeclSpace (Type) != null && RootContext.StdLib) {
842 Error_AttributeEmitError ("security custom attributes can not be referenced from defining assembly");
846 SecurityAttribute sa;
847 // For all assemblies except corlib we can avoid all hacks
848 if (RootContext.StdLib) {
849 sa = (SecurityAttribute) Activator.CreateInstance (Type, pos_values);
851 if (prop_info_arr != null) {
852 for (int i = 0; i < prop_info_arr.Length; ++i) {
853 PropertyInfo pi = prop_info_arr [i];
854 pi.SetValue (sa, prop_values_arr [i], null);
858 Type temp_type = Type.GetType (Type.FullName);
859 // HACK: All mscorlib attributes have same ctor syntax
860 sa = (SecurityAttribute) Activator.CreateInstance (temp_type, new object[] { GetSecurityActionValue () } );
862 // All types are from newly created corlib but for invocation with old we need to convert them
863 if (prop_info_arr != null) {
864 for (int i = 0; i < prop_info_arr.Length; ++i) {
865 PropertyInfo emited_pi = prop_info_arr [i];
866 PropertyInfo pi = temp_type.GetProperty (emited_pi.Name, emited_pi.PropertyType);
868 object old_instance = pi.PropertyType.IsEnum ?
869 System.Enum.ToObject (pi.PropertyType, prop_values_arr [i]) :
872 pi.SetValue (sa, old_instance, null);
878 perm = sa.CreatePermission ();
879 SecurityAction action = GetSecurityActionValue ();
881 // IS is correct because for corlib we are using an instance from old corlib
882 if (!(perm is System.Security.CodeAccessPermission)) {
884 case SecurityAction.Demand:
885 action = (SecurityAction)13;
887 case SecurityAction.LinkDemand:
888 action = (SecurityAction)14;
890 case SecurityAction.InheritanceDemand:
891 action = (SecurityAction)15;
896 PermissionSet ps = (PermissionSet)permissions [action];
898 if (sa is PermissionSetAttribute)
899 ps = new PermissionSet (sa.Unrestricted ? PermissionState.Unrestricted : PermissionState.None);
901 ps = new PermissionSet (PermissionState.None);
903 permissions.Add (action, ps);
904 } else if (!ps.IsUnrestricted () && (sa is PermissionSetAttribute) && sa.Unrestricted) {
905 ps = ps.Union (new PermissionSet (PermissionState.Unrestricted));
906 permissions [action] = ps;
908 ps.AddPermission (perm);
911 object GetValue (object value)
913 if (value is EnumConstant)
914 return ((EnumConstant) value).GetValue ();
919 object GetPropertyValue (string name)
921 if (prop_info_arr == null)
924 for (int i = 0; i < prop_info_arr.Length; ++i) {
925 if (prop_info_arr [i].Name == name)
926 return prop_values_arr [i];
932 object GetFieldValue (string name)
935 if (field_info_arr == null)
938 foreach (FieldInfo fi in field_info_arr) {
940 return GetValue (field_values_arr [i]);
946 public UnmanagedMarshal GetMarshal (Attributable attr)
948 UnmanagedType UnmanagedType = (UnmanagedType)System.Enum.Parse (typeof (UnmanagedType), pos_values [0].ToString ());
950 object value = GetFieldValue ("SizeParamIndex");
951 if (value != null && UnmanagedType != UnmanagedType.LPArray) {
952 Error_AttributeEmitError ("SizeParamIndex field is not valid for the specified unmanaged type");
956 object o = GetFieldValue ("ArraySubType");
957 UnmanagedType array_sub_type = o == null ? UnmanagedType.I4 : (UnmanagedType) o;
959 switch (UnmanagedType) {
960 case UnmanagedType.CustomMarshaler: {
961 MethodInfo define_custom = typeof (UnmanagedMarshal).GetMethod ("DefineCustom",
962 BindingFlags.Static | BindingFlags.Public);
963 if (define_custom == null) {
964 Report.RuntimeMissingSupport (Location, "set marshal info");
968 object [] args = new object [4];
969 args [0] = GetFieldValue ("MarshalTypeRef");
970 args [1] = GetFieldValue ("MarshalCookie");
971 args [2] = GetFieldValue ("MarshalType");
972 args [3] = Guid.Empty;
973 return (UnmanagedMarshal) define_custom.Invoke (null, args);
975 case UnmanagedType.LPArray: {
976 object size_const = GetFieldValue ("SizeConst");
977 object size_param_index = GetFieldValue ("SizeParamIndex");
979 if ((size_const != null) || (size_param_index != null)) {
980 MethodInfo define_array = typeof (UnmanagedMarshal).GetMethod ("DefineLPArrayInternal", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
981 if (define_array == null) {
982 Report.RuntimeMissingSupport (Location, "set marshal info");
986 object [] args = new object [3];
987 args [0] = array_sub_type;
988 args [1] = size_const == null ? -1 : size_const;
989 args [2] = size_param_index == null ? -1 : size_param_index;
990 return (UnmanagedMarshal) define_array.Invoke (null, args);
993 return UnmanagedMarshal.DefineLPArray (array_sub_type);
995 case UnmanagedType.SafeArray:
996 return UnmanagedMarshal.DefineSafeArray (array_sub_type);
998 case UnmanagedType.ByValArray:
999 FieldMember fm = attr as FieldMember;
1001 Error_AttributeEmitError ("Specified unmanaged type is only valid on fields");
1004 return UnmanagedMarshal.DefineByValArray ((int) GetFieldValue ("SizeConst"));
1006 case UnmanagedType.ByValTStr:
1007 return UnmanagedMarshal.DefineByValTStr ((int) GetFieldValue ("SizeConst"));
1010 return UnmanagedMarshal.DefineUnmanagedMarshal (UnmanagedType);
1014 public CharSet GetCharSetValue ()
1016 return (CharSet)System.Enum.Parse (typeof (CharSet), pos_values [0].ToString ());
1019 public MethodImplOptions GetMethodImplOptions ()
1021 return (MethodImplOptions)System.Enum.Parse (typeof (MethodImplOptions), pos_values [0].ToString ());
1024 public LayoutKind GetLayoutKindValue ()
1026 return (LayoutKind)System.Enum.Parse (typeof (LayoutKind), pos_values [0].ToString ());
1030 /// Emit attribute for Attributable symbol
1032 public void Emit (EmitContext ec, Attributable ias, ListDictionary emitted_attr)
1034 CustomAttributeBuilder cb = Resolve (ec);
1038 AttributeUsageAttribute usage_attr = GetAttributeUsage (ec);
1039 if ((usage_attr.ValidOn & Target) == 0) {
1040 Report.Error (592, Location, "Attribute `{0}' is not valid on this declaration type. It is valid on `{1}' declarations only",
1041 Name, GetValidTargets ());
1046 ias.ApplyAttributeBuilder (this, cb);
1048 catch (Exception e) {
1049 Error_AttributeEmitError (e.Message);
1053 if (!usage_attr.AllowMultiple) {
1054 ArrayList emitted_targets = (ArrayList)emitted_attr [Type];
1055 if (emitted_targets == null) {
1056 emitted_targets = new ArrayList ();
1057 emitted_attr.Add (Type, emitted_targets);
1058 } else if (emitted_targets.Contains (Target)) {
1059 Report.Error (579, Location, "Duplicate '" + Name + "' attribute");
1062 emitted_targets.Add (Target);
1065 if (!RootContext.VerifyClsCompliance)
1068 // Here we are testing attribute arguments for array usage (error 3016)
1069 if (ias.IsClsCompliaceRequired (ec.DeclSpace)) {
1070 if (Arguments == null)
1073 ArrayList pos_args = (ArrayList) Arguments [0];
1074 if (pos_args != null) {
1075 foreach (Argument arg in pos_args) {
1076 // Type is undefined (was error 246)
1077 if (arg.Type == null)
1080 if (arg.Type.IsArray) {
1081 Report.Error (3016, Location, "Arrays as attribute arguments are not CLS-compliant");
1087 if (Arguments.Count < 2)
1090 ArrayList named_args = (ArrayList) Arguments [1];
1091 foreach (DictionaryEntry de in named_args) {
1092 Argument arg = (Argument) de.Value;
1094 // Type is undefined (was error 246)
1095 if (arg.Type == null)
1098 if (arg.Type.IsArray) {
1099 Report.Error (3016, Location, "Arrays as attribute arguments are not CLS-compliant");
1106 public MethodBuilder DefinePInvokeMethod (EmitContext ec, TypeBuilder builder, string name,
1107 MethodAttributes flags, Type ret_type, Type [] param_types)
1109 if (pos_values == null)
1110 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
1111 // But because a lot of attribute class code must be rewritten will be better to wait...
1117 string dll_name = (string)pos_values [0];
1120 CallingConvention cc = CallingConvention.Winapi;
1121 CharSet charset = CodeGen.Module.DefaultCharSet;
1122 bool preserve_sig = true;
1123 string entry_point = name;
1124 bool best_fit_mapping = false;
1125 bool throw_on_unmappable = false;
1126 bool exact_spelling = false;
1127 bool set_last_error = false;
1129 bool best_fit_mapping_set = false;
1130 bool throw_on_unmappable_set = false;
1131 bool exact_spelling_set = false;
1132 bool set_last_error_set = false;
1134 MethodInfo set_best_fit = null;
1135 MethodInfo set_throw_on = null;
1136 MethodInfo set_exact_spelling = null;
1137 MethodInfo set_set_last_error = null;
1139 if (field_info_arr != null) {
1141 for (int i = 0; i < field_info_arr.Length; i++) {
1142 switch (field_info_arr [i].Name) {
1143 case "BestFitMapping":
1144 best_fit_mapping = (bool) field_values_arr [i];
1145 best_fit_mapping_set = true;
1147 case "CallingConvention":
1148 cc = (CallingConvention) field_values_arr [i];
1151 charset = (CharSet) field_values_arr [i];
1154 entry_point = (string) field_values_arr [i];
1156 case "ExactSpelling":
1157 exact_spelling = (bool) field_values_arr [i];
1158 exact_spelling_set = true;
1161 preserve_sig = (bool) field_values_arr [i];
1163 case "SetLastError":
1164 set_last_error = (bool) field_values_arr [i];
1165 set_last_error_set = true;
1167 case "ThrowOnUnmappableChar":
1168 throw_on_unmappable = (bool) field_values_arr [i];
1169 throw_on_unmappable_set = true;
1172 throw new InternalErrorException (field_info_arr [i].ToString ());
1177 if (throw_on_unmappable_set || best_fit_mapping_set || exact_spelling_set || set_last_error_set) {
1178 set_best_fit = typeof (MethodBuilder).GetMethod ("set_BestFitMapping", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
1179 set_throw_on = typeof (MethodBuilder).GetMethod ("set_ThrowOnUnmappableChar", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
1180 set_exact_spelling = typeof (MethodBuilder).GetMethod ("set_ExactSpelling", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
1181 set_set_last_error = typeof (MethodBuilder).GetMethod ("set_SetLastError", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
1183 if ((set_best_fit == null) || (set_throw_on == null) || (set_exact_spelling == null) || (set_set_last_error == null)) {
1184 Report.Error (-1, Location,
1185 "The ThrowOnUnmappableChar, BestFitMapping, SetLastError, and ExactSpelling attributes can only be emitted when running on the mono runtime.");
1191 MethodBuilder mb = builder.DefinePInvokeMethod (
1192 name, dll_name, entry_point, flags | MethodAttributes.HideBySig | MethodAttributes.PinvokeImpl,
1193 CallingConventions.Standard, ret_type, param_types, cc, charset);
1196 mb.SetImplementationFlags (MethodImplAttributes.PreserveSig);
1198 if (throw_on_unmappable_set)
1199 set_throw_on.Invoke (mb, 0, null, new object [] { throw_on_unmappable }, null);
1200 if (best_fit_mapping_set)
1201 set_best_fit.Invoke (mb, 0, null, new object [] { best_fit_mapping }, null);
1202 if (exact_spelling_set)
1203 set_exact_spelling.Invoke (mb, 0, null, new object [] { exact_spelling }, null);
1204 if (set_last_error_set)
1205 set_set_last_error.Invoke (mb, 0, null, new object [] { set_last_error }, null);
1209 catch (ArgumentException e) {
1210 Error_AttributeEmitError (e.Message);
1215 private Expression GetValue ()
1217 if ((Arguments == null) || (Arguments.Count < 1))
1219 ArrayList al = (ArrayList) Arguments [0];
1220 if ((al == null) || (al.Count < 1))
1222 Argument arg = (Argument) al [0];
1223 if ((arg == null) || (arg.Expr == null))
1228 public string GetString ()
1230 Expression e = GetValue ();
1231 if (e is StringLiteral)
1232 return (e as StringLiteral).Value;
1236 public bool GetBoolean ()
1238 Expression e = GetValue ();
1239 if (e is BoolLiteral)
1240 return (e as BoolLiteral).Value;
1247 /// For global attributes (assembly, module) we need special handling.
1248 /// Attributes can be located in the several files
1250 public class GlobalAttribute: Attribute
1252 public readonly NamespaceEntry ns;
1254 public GlobalAttribute (TypeContainer container, string target,
1255 Expression left_expr, string identifier, ArrayList args, Location loc):
1256 base (target, left_expr, identifier, args, loc)
1258 ns = container.NamespaceEntry;
1263 // RootContext.Tree.Types has a single NamespaceEntry which gets overwritten
1264 // each time a new file is parsed. However, we need to use the NamespaceEntry
1265 // in effect where the attribute was used. Since code elsewhere cannot assume
1266 // that the NamespaceEntry is right, just overwrite it.
1268 // Precondition: RootContext.Tree.Types == null
1270 if (RootContext.Tree.Types.NamespaceEntry != null)
1271 throw new InternalErrorException (Location + " non-null NamespaceEntry");
1273 RootContext.Tree.Types.NamespaceEntry = ns;
1278 RootContext.Tree.Types.NamespaceEntry = null;
1281 protected override FullNamedExpression ResolveAsTypeStep (Expression expr, EmitContext ec)
1285 return base.ResolveAsTypeStep (expr, ec);
1292 protected override ConstructorInfo ResolveArguments (EmitContext ec)
1296 return base.ResolveArguments (ec);
1304 public class Attributes {
1305 public ArrayList Attrs;
1307 public Attributes (Attribute a)
1309 Attrs = new ArrayList ();
1313 public Attributes (ArrayList attrs)
1318 public void AddAttributes (ArrayList attrs)
1320 Attrs.AddRange (attrs);
1324 /// Checks whether attribute target is valid for the current element
1326 public bool CheckTargets (Attributable member)
1328 string[] valid_targets = member.ValidAttributeTargets;
1329 foreach (Attribute a in Attrs) {
1330 if (a.ExplicitTarget == null || a.ExplicitTarget == valid_targets [0]) {
1331 a.Target = member.AttributeTargets;
1335 // TODO: we can skip the first item
1336 if (((IList) valid_targets).Contains (a.ExplicitTarget)) {
1337 switch (a.ExplicitTarget) {
1338 case "return": a.Target = AttributeTargets.ReturnValue; continue;
1339 case "param": a.Target = AttributeTargets.Parameter; continue;
1340 case "field": a.Target = AttributeTargets.Field; continue;
1341 case "method": a.Target = AttributeTargets.Method; continue;
1342 case "property": a.Target = AttributeTargets.Property; continue;
1344 throw new InternalErrorException ("Unknown explicit target: " + a.ExplicitTarget);
1347 StringBuilder sb = new StringBuilder ();
1348 foreach (string s in valid_targets) {
1352 sb.Remove (sb.Length - 2, 2);
1353 Report.Error (657, a.Location, "`{0}' is not a valid attribute location for this declaration. Valid attribute locations for this declaration are `{1}'", a.ExplicitTarget, sb.ToString ());
1359 public Attribute Search (Type t, EmitContext ec)
1361 foreach (Attribute a in Attrs) {
1362 if (a.ResolveType (ec) == t)
1369 /// Returns all attributes of type 't'. Use it when attribute is AllowMultiple = true
1371 public Attribute[] SearchMulti (Type t, EmitContext ec)
1373 ArrayList ar = null;
1375 foreach (Attribute a in Attrs) {
1376 if (a.ResolveType (ec) == t) {
1378 ar = new ArrayList ();
1383 return ar == null ? null : ar.ToArray (typeof (Attribute)) as Attribute[];
1386 public void Emit (EmitContext ec, Attributable ias)
1390 ListDictionary ld = new ListDictionary ();
1392 foreach (Attribute a in Attrs)
1393 a.Emit (ec, ias, ld);
1396 public bool Contains (Type t, EmitContext ec)
1398 return Search (t, ec) != null;
1403 /// Helper class for attribute verification routine.
1405 sealed class AttributeTester
1407 static PtrHashtable analyzed_types = new PtrHashtable ();
1408 static PtrHashtable analyzed_types_obsolete = new PtrHashtable ();
1409 static PtrHashtable analyzed_member_obsolete = new PtrHashtable ();
1410 static PtrHashtable analyzed_method_excluded = new PtrHashtable ();
1413 static PtrHashtable fixed_buffer_cache = new PtrHashtable ();
1416 static object TRUE = new object ();
1417 static object FALSE = new object ();
1419 private AttributeTester ()
1423 public enum Result {
1430 /// Returns true if parameters of two compared methods are CLS-Compliant.
1431 /// It tests differing only in ref or out, or in array rank.
1433 public static Result AreOverloadedMethodParamsClsCompliant (Type[] types_a, Type[] types_b)
1435 if (types_a == null || types_b == null)
1438 if (types_a.Length != types_b.Length)
1441 Result result = Result.Ok;
1442 for (int i = 0; i < types_b.Length; ++i) {
1443 Type aType = types_a [i];
1444 Type bType = types_b [i];
1446 if (aType.IsArray && bType.IsArray) {
1447 Type a_el_type = aType.GetElementType ();
1448 Type b_el_type = bType.GetElementType ();
1449 if (aType.GetArrayRank () != bType.GetArrayRank () && a_el_type == b_el_type) {
1450 result = Result.RefOutArrayError;
1454 if (a_el_type.IsArray || b_el_type.IsArray) {
1455 result = Result.ArrayArrayError;
1460 Type aBaseType = aType;
1461 bool is_either_ref_or_out = false;
1463 if (aType.IsByRef || aType.IsPointer) {
1464 aBaseType = aType.GetElementType ();
1465 is_either_ref_or_out = true;
1468 Type bBaseType = bType;
1469 if (bType.IsByRef || bType.IsPointer)
1471 bBaseType = bType.GetElementType ();
1472 is_either_ref_or_out = !is_either_ref_or_out;
1475 if (aBaseType != bBaseType)
1478 if (is_either_ref_or_out)
1479 result = Result.RefOutArrayError;
1485 /// Goes through all parameters and test if they are CLS-Compliant.
1487 public static bool AreParametersCompliant (Parameter[] fixedParameters, Location loc)
1489 if (fixedParameters == null)
1492 foreach (Parameter arg in fixedParameters) {
1493 if (!AttributeTester.IsClsCompliant (arg.ParameterType)) {
1494 Report.Error (3001, loc, "Argument type `{0}' is not CLS-compliant", arg.GetSignatureForError ());
1503 /// This method tests the CLS compliance of external types. It doesn't test type visibility.
1505 public static bool IsClsCompliant (Type type)
1510 object type_compliance = analyzed_types[type];
1511 if (type_compliance != null)
1512 return type_compliance == TRUE;
1514 if (type.IsPointer) {
1515 analyzed_types.Add (type, null);
1520 if (type.IsArray || type.IsByRef) {
1521 result = IsClsCompliant (TypeManager.GetElementType (type));
1523 result = AnalyzeTypeCompliance (type);
1525 analyzed_types.Add (type, result ? TRUE : FALSE);
1530 /// Returns IFixedBuffer implementation if field is fixed buffer else null.
1532 public static IFixedBuffer GetFixedBuffer (FieldInfo fi)
1534 FieldBase fb = TypeManager.GetField (fi);
1536 return fb as IFixedBuffer;
1540 object o = fixed_buffer_cache [fi];
1542 if (System.Attribute.GetCustomAttribute (fi, TypeManager.fixed_buffer_attr_type) == null) {
1543 fixed_buffer_cache.Add (fi, FALSE);
1547 IFixedBuffer iff = new FixedFieldExternal (fi);
1548 fixed_buffer_cache.Add (fi, iff);
1555 return (IFixedBuffer)o;
1561 public static void VerifyModulesClsCompliance ()
1563 Module[] modules = TypeManager.Modules;
1564 if (modules == null)
1567 // The first module is generated assembly
1568 for (int i = 1; i < modules.Length; ++i) {
1569 Module module = modules [i];
1570 if (!IsClsCompliant (module)) {
1571 Report.Error (3013, "Added modules must be marked with the CLSCompliant attribute to match the assembly", module.Name);
1578 /// Tests container name for CLS-Compliant name (differing only in case)
1580 public static void VerifyTopLevelNameClsCompliance ()
1582 Hashtable locase_table = new Hashtable ();
1584 // Convert imported type names to lower case and ignore not cls compliant
1585 foreach (DictionaryEntry de in TypeManager.all_imported_types) {
1586 Type t = (Type)de.Value;
1587 if (!AttributeTester.IsClsCompliant (t))
1590 locase_table.Add (((string)de.Key).ToLower (System.Globalization.CultureInfo.InvariantCulture), t);
1593 foreach (DictionaryEntry de in RootContext.Tree.AllDecls) {
1594 if (!(de.Key is MemberName))
1595 throw new InternalErrorException ("");
1596 DeclSpace decl = (DeclSpace) de.Value;
1597 if (!decl.IsClsCompliaceRequired (decl))
1600 string lcase = decl.Name.ToLower (System.Globalization.CultureInfo.InvariantCulture);
1601 if (!locase_table.Contains (lcase)) {
1602 locase_table.Add (lcase, decl);
1606 object conflict = locase_table [lcase];
1607 if (conflict is Type)
1608 Report.SymbolRelatedToPreviousError ((Type)conflict);
1610 Report.SymbolRelatedToPreviousError ((MemberCore)conflict);
1612 Report.Error (3005, decl.Location, "Identifier `{0}' differing only in case is not CLS-compliant", decl.GetSignatureForError ());
1616 static bool IsClsCompliant (ICustomAttributeProvider attribute_provider)
1618 object[] CompliantAttribute = attribute_provider.GetCustomAttributes (TypeManager.cls_compliant_attribute_type, false);
1619 if (CompliantAttribute.Length == 0)
1622 return ((CLSCompliantAttribute)CompliantAttribute[0]).IsCompliant;
1625 static bool AnalyzeTypeCompliance (Type type)
1627 DeclSpace ds = TypeManager.LookupDeclSpace (type);
1629 return ds.IsClsCompliaceRequired (ds.Parent);
1632 object[] CompliantAttribute = type.GetCustomAttributes (TypeManager.cls_compliant_attribute_type, false);
1633 if (CompliantAttribute.Length == 0)
1634 return IsClsCompliant (type.Assembly);
1636 return ((CLSCompliantAttribute)CompliantAttribute[0]).IsCompliant;
1640 /// Returns instance of ObsoleteAttribute when type is obsolete
1642 public static ObsoleteAttribute GetObsoleteAttribute (Type type)
1644 object type_obsolete = analyzed_types_obsolete [type];
1645 if (type_obsolete == FALSE)
1648 if (type_obsolete != null)
1649 return (ObsoleteAttribute)type_obsolete;
1651 ObsoleteAttribute result = null;
1652 if (type.IsByRef || type.IsArray || type.IsPointer) {
1653 result = GetObsoleteAttribute (TypeManager.GetElementType (type));
1655 DeclSpace type_ds = TypeManager.LookupDeclSpace (type);
1657 // Type is external, we can get attribute directly
1658 if (type_ds == null) {
1659 object[] attribute = type.GetCustomAttributes (TypeManager.obsolete_attribute_type, false);
1660 if (attribute.Length == 1)
1661 result = (ObsoleteAttribute)attribute [0];
1663 result = type_ds.GetObsoleteAttribute (type_ds);
1667 analyzed_types_obsolete.Add (type, result == null ? FALSE : result);
1672 /// Returns instance of ObsoleteAttribute when method is obsolete
1674 public static ObsoleteAttribute GetMethodObsoleteAttribute (MethodBase mb)
1676 IMethodData mc = TypeManager.GetMethod (mb);
1678 return mc.GetObsoleteAttribute ();
1680 // compiler generated methods are not registered by AddMethod
1681 if (mb.DeclaringType is TypeBuilder)
1684 PropertyInfo pi = PropertyExpr.AccessorTable [mb] as PropertyInfo;
1686 return GetMemberObsoleteAttribute (pi);
1688 return GetMemberObsoleteAttribute (mb);
1692 /// Returns instance of ObsoleteAttribute when member is obsolete
1694 public static ObsoleteAttribute GetMemberObsoleteAttribute (MemberInfo mi)
1696 object type_obsolete = analyzed_member_obsolete [mi];
1697 if (type_obsolete == FALSE)
1700 if (type_obsolete != null)
1701 return (ObsoleteAttribute)type_obsolete;
1703 ObsoleteAttribute oa = System.Attribute.GetCustomAttribute (mi, TypeManager.obsolete_attribute_type, false) as ObsoleteAttribute;
1704 analyzed_member_obsolete.Add (mi, oa == null ? FALSE : oa);
1709 /// Common method for Obsolete error/warning reporting.
1711 public static void Report_ObsoleteMessage (ObsoleteAttribute oa, string member, Location loc)
1714 Report.Error (619, loc, "`{0}' is obsolete: `{1}'", member, oa.Message);
1718 if (oa.Message == null) {
1719 Report.Warning (612, loc, "`{0}' is obsolete", member);
1722 if (RootContext.WarningLevel >= 2)
1723 Report.Warning (618, loc, "`{0}' is obsolete: `{1}'", member, oa.Message);
1726 public static bool IsConditionalMethodExcluded (MethodBase mb)
1728 object excluded = analyzed_method_excluded [mb];
1729 if (excluded != null)
1730 return excluded == TRUE ? true : false;
1732 ConditionalAttribute[] attrs = mb.GetCustomAttributes (TypeManager.conditional_attribute_type, true) as ConditionalAttribute[];
1733 if (attrs.Length == 0) {
1734 analyzed_method_excluded.Add (mb, FALSE);
1738 foreach (ConditionalAttribute a in attrs) {
1739 if (RootContext.AllDefines.Contains (a.ConditionString)) {
1740 analyzed_method_excluded.Add (mb, FALSE);
1744 analyzed_method_excluded.Add (mb, TRUE);
1749 /// Analyzes class whether it has attribute which has ConditionalAttribute
1750 /// and its condition is not defined.
1752 public static bool IsAttributeExcluded (Type type)
1757 Class class_decl = TypeManager.LookupDeclSpace (type) as Class;
1759 // TODO: add caching
1760 // TODO: merge all Type bases attribute caching to one cache to save memory
1761 if (class_decl == null) {
1762 object[] attributes = type.GetCustomAttributes (TypeManager.conditional_attribute_type, false);
1763 foreach (ConditionalAttribute ca in attributes) {
1764 if (RootContext.AllDefines.Contains (ca.ConditionString))
1767 return attributes.Length > 0;
1770 return class_decl.IsExcluded ();