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;
26 namespace Mono.CSharp {
29 /// Base class for objects that can have Attributes applied to them.
31 public abstract class Attributable {
33 /// Attributes for this type
35 Attributes attributes;
37 public Attributable (Attributes attrs)
39 OptAttributes = attrs;
42 public Attributes OptAttributes
50 if (attributes != null) {
51 attributes.AttachTo (this);
57 /// Use member-specific procedure to apply attribute @a in @cb to the entity being built in @builder
59 public abstract void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb);
62 /// Returns one AttributeTarget for this element.
64 public abstract AttributeTargets AttributeTargets { get; }
66 public abstract IResolveContext ResolveContext { get; }
68 public abstract bool IsClsComplianceRequired ();
71 /// Gets list of valid attribute targets for explicit target declaration.
72 /// The first array item is default target. Don't break this rule.
74 public abstract string[] ValidAttributeTargets { get; }
77 public class Attribute {
78 public readonly string ExplicitTarget;
79 public AttributeTargets Target;
81 // TODO: remove this member
82 public readonly string Name;
83 public readonly Expression LeftExpr;
84 public readonly string Identifier;
86 public readonly ArrayList Arguments;
88 public readonly Location Location;
93 readonly bool nameEscaped;
96 static AttributeUsageAttribute DefaultUsageAttribute = new AttributeUsageAttribute (AttributeTargets.All);
97 static Assembly orig_sec_assembly;
99 // non-null if named args present after Resolve () is called
100 PropertyInfo [] prop_info_arr;
101 FieldInfo [] field_info_arr;
102 object [] field_values_arr;
103 object [] prop_values_arr;
104 object [] pos_values;
106 static PtrHashtable usage_attr_cache = new PtrHashtable ();
108 public Attribute (string target, Expression left_expr, string identifier, ArrayList args, Location loc, bool nameEscaped)
110 LeftExpr = left_expr;
111 Identifier = identifier;
112 Name = LeftExpr == null ? identifier : LeftExpr + "." + identifier;
115 ExplicitTarget = target;
116 this.nameEscaped = nameEscaped;
119 public void AttachTo (Attributable owner)
124 void Error_InvalidNamedArgument (string name)
126 Report.Error (617, Location, "`{0}' is not a valid named attribute argument. Named attribute arguments " +
127 "must be fields which are not readonly, static, const or read-write properties which are " +
128 "public and not static",
132 void Error_InvalidNamedAgrumentType (string name)
134 Report.Error (655, Location, "`{0}' is not a valid named attribute argument because it is not a valid " +
135 "attribute parameter type", name);
138 static void Error_AttributeArgumentNotValid (string extra, Location loc)
140 Report.Error (182, loc,
141 "An attribute argument must be a constant expression, typeof " +
142 "expression or array creation expression" + extra);
145 static void Error_AttributeArgumentNotValid (Location loc)
147 Error_AttributeArgumentNotValid ("", loc);
150 static void Error_TypeParameterInAttribute (Location loc)
153 -202, loc, "Can not use a type parameter in an attribute");
157 /// This is rather hack. We report many emit attribute error with same error to be compatible with
158 /// csc. But because csc has to report them this way because error came from ilasm we needn't.
160 public void Error_AttributeEmitError (string inner)
162 Report.Error (647, Location, "Error during emitting `{0}' attribute. The reason is `{1}'",
163 TypeManager.CSharpName (Type), inner);
166 public void Error_InvalidSecurityParent ()
168 Error_AttributeEmitError ("it is attached to invalid parent");
171 protected virtual FullNamedExpression ResolveAsTypeTerminal (Expression expr, EmitContext ec, bool silent)
173 return expr.ResolveAsTypeTerminal (ec, silent);
176 protected virtual FullNamedExpression ResolveAsTypeStep (Expression expr, EmitContext ec, bool silent)
178 return expr.ResolveAsTypeStep (ec, silent);
181 Type ResolvePossibleAttributeType (string name, bool silent, ref bool is_attr)
183 // It will disapear when IResolveContext will take a place
184 DeclSpace container = owner.ResolveContext.DeclContainer;
185 if (owner is TypeParameter)
186 container = ((TypeParameter)owner).DeclSpace;
187 EmitContext ec = new EmitContext (container, container,
188 Location, null, null, container.ModFlags, false);
191 FullNamedExpression fn;
192 if (LeftExpr == null) {
193 fn = ResolveAsTypeTerminal (new SimpleName (name, Location), ec, silent);
195 fn = ResolveAsTypeStep (LeftExpr, ec, silent);
198 fn = new MemberAccess (fn, name, Location).ResolveAsTypeTerminal (ec, silent);
201 TypeExpr te = fn as TypeExpr;
206 if (TypeManager.IsSubclassOf (t, TypeManager.attribute_type)) {
208 } else if (!silent) {
209 Report.SymbolRelatedToPreviousError (t);
210 Report.Error (616, Location, "`{0}': is not an attribute class", TypeManager.CSharpName (t));
216 /// Tries to resolve the type of the attribute. Flags an error if it can't, and complain is true.
218 void ResolveAttributeType ()
220 bool t1_is_attr = false;
221 Type t1 = ResolvePossibleAttributeType (Identifier, true, ref t1_is_attr);
223 bool t2_is_attr = false;
224 Type t2 = nameEscaped ? null :
225 ResolvePossibleAttributeType (Identifier + "Attribute", true, ref t2_is_attr);
227 if (t1_is_attr && t2_is_attr) {
228 Report.Error (1614, Location, "`{0}' is ambiguous between `{0}' and `{0}Attribute'. " +
229 "Use either `@{0}' or `{0}Attribute'", GetSignatureForError ());
230 resolve_error = true;
244 if (t1 == null && t2 == null)
245 ResolvePossibleAttributeType (Identifier, false, ref t1_is_attr);
247 ResolvePossibleAttributeType (Identifier, false, ref t1_is_attr);
249 ResolvePossibleAttributeType (Identifier + "Attribute", false, ref t2_is_attr);
251 resolve_error = true;
254 public virtual Type ResolveType ()
256 if (Type == null && !resolve_error)
257 ResolveAttributeType ();
261 public string GetSignatureForError ()
263 return LeftExpr == null ? Identifier : LeftExpr.GetSignatureForError () + "." + Identifier;
267 // Given an expression, if the expression is a valid attribute-argument-expression
268 // returns an object that can be used to encode it, or null on failure.
270 public static bool GetAttributeArgumentExpression (Expression e, Location loc, Type arg_type, out object result)
272 Constant constant = e as Constant;
273 if (constant != null) {
274 constant = constant.ToType (arg_type, loc);
275 if (constant == null) {
279 result = constant.GetTypedValue ();
281 } else if (e is TypeOf) {
282 result = ((TypeOf) e).TypeArg;
284 } else if (e is ArrayCreation){
285 result = ((ArrayCreation) e).EncodeAsAttribute ();
288 } else if (e is EmptyCast) {
289 Expression child = ((EmptyCast)e).Child;
290 return GetAttributeArgumentExpression (child, loc, child.Type, out result);
291 } else if (e is As) {
293 return GetAttributeArgumentExpression (as_e.Expr, loc, as_e.ProbeType.Type, out result);
297 Error_AttributeArgumentNotValid (loc);
301 bool IsValidArgumentType (Type t)
304 t = t.GetElementType ();
306 return TypeManager.IsPrimitiveType (t) ||
307 TypeManager.IsEnumType (t) ||
308 t == TypeManager.string_type ||
309 t == TypeManager.object_type ||
310 t == TypeManager.type_type;
313 // Cache for parameter-less attributes
314 static PtrHashtable att_cache = new PtrHashtable ();
316 public CustomAttributeBuilder Resolve ()
321 resolve_error = true;
324 ResolveAttributeType ();
329 if (Type.IsAbstract) {
330 Report.Error (653, Location, "Cannot apply attribute class `{0}' because it is abstract", GetSignatureForError ());
334 ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (Type);
335 if (obsolete_attr != null) {
336 AttributeTester.Report_ObsoleteMessage (obsolete_attr, TypeManager.CSharpName (Type), Location);
339 if (Arguments == null) {
340 object o = att_cache [Type];
342 resolve_error = false;
343 return (CustomAttributeBuilder)o;
347 ConstructorInfo ctor = ResolveArguments ();
349 if (Type is TypeBuilder &&
350 TypeManager.LookupDeclSpace (Type).MemberCache == null)
351 // The attribute type has been DefineType'd, but not Defined. Let's not treat it as an error.
352 // It'll be resolved again when the attached-to entity is emitted.
353 resolve_error = false;
357 CustomAttributeBuilder cb;
360 if (prop_info_arr != null || field_info_arr != null) {
361 cb = new CustomAttributeBuilder (
363 prop_info_arr, prop_values_arr,
364 field_info_arr, field_values_arr);
366 cb = new CustomAttributeBuilder (
369 if (pos_values.Length == 0)
370 att_cache.Add (Type, cb);
374 Error_AttributeArgumentNotValid (Location);
378 resolve_error = false;
382 protected virtual ConstructorInfo ResolveArguments ()
384 // Now we extract the positional and named arguments
386 ArrayList pos_args = null;
387 ArrayList named_args = null;
388 int pos_arg_count = 0;
389 int named_arg_count = 0;
391 if (Arguments != null) {
392 pos_args = (ArrayList) Arguments [0];
393 if (pos_args != null)
394 pos_arg_count = pos_args.Count;
395 if (Arguments.Count > 1) {
396 named_args = (ArrayList) Arguments [1];
397 named_arg_count = named_args.Count;
401 pos_values = new object [pos_arg_count];
404 // First process positional arguments
407 EmitContext ec = new EmitContext (owner.ResolveContext.DeclContainer, owner.ResolveContext.DeclContainer,
408 Location, null, null, owner.ResolveContext.DeclContainer.ModFlags, false);
411 for (i = 0; i < pos_arg_count; i++) {
412 Argument a = (Argument) pos_args [i];
415 if (!a.Resolve (ec, Location))
421 if (!GetAttributeArgumentExpression (e, Location, a.Type, out val))
424 pos_values [i] = val;
426 if (i == 0 && Type == TypeManager.attribute_usage_type && (int)val == 0) {
427 Report.Error (591, Location, "Invalid value for argument to 'System.AttributeUsage' attribute");
433 // Now process named arguments
436 ArrayList field_infos = null;
437 ArrayList prop_infos = null;
438 ArrayList field_values = null;
439 ArrayList prop_values = null;
440 Hashtable seen_names = null;
442 if (named_arg_count > 0) {
443 field_infos = new ArrayList (4);
444 prop_infos = new ArrayList (4);
445 field_values = new ArrayList (4);
446 prop_values = new ArrayList (4);
448 seen_names = new Hashtable(4);
451 for (i = 0; i < named_arg_count; i++) {
452 DictionaryEntry de = (DictionaryEntry) named_args [i];
453 string member_name = (string) de.Key;
454 Argument a = (Argument) de.Value;
457 if (seen_names.Contains(member_name)) {
458 Report.Error(643, Location, "'" + member_name + "' duplicate named attribute argument");
461 seen_names.Add(member_name, 1);
463 if (!a.Resolve (ec, Location))
466 Expression member = Expression.MemberLookup (
467 ec.ContainerType, Type, member_name,
468 MemberTypes.Field | MemberTypes.Property,
469 BindingFlags.Public | BindingFlags.Instance,
472 if (member == null) {
473 member = Expression.MemberLookup (ec.ContainerType, Type, member_name,
474 MemberTypes.Field | MemberTypes.Property, BindingFlags.NonPublic | BindingFlags.Instance,
477 if (member != null) {
478 Expression.ErrorIsInaccesible (Location, member.GetSignatureForError ());
484 Report.Error (117, Location, "`{0}' does not contain a definition for `{1}'",
485 TypeManager.CSharpName (Type), member_name);
489 if (!(member is PropertyExpr || member is FieldExpr)) {
490 Error_InvalidNamedArgument (member_name);
495 if (e is TypeParameterExpr){
496 Error_TypeParameterInAttribute (Location);
500 if (member is PropertyExpr) {
501 PropertyExpr pe = (PropertyExpr) member;
502 PropertyInfo pi = pe.PropertyInfo;
504 if (!pi.CanWrite || !pi.CanRead) {
505 Report.SymbolRelatedToPreviousError (pi);
506 Error_InvalidNamedArgument (member_name);
510 if (!IsValidArgumentType (pi.PropertyType)) {
511 Report.SymbolRelatedToPreviousError (pi);
512 Error_InvalidNamedAgrumentType (member_name);
517 if (!GetAttributeArgumentExpression (e, Location, pi.PropertyType, out value))
520 prop_values.Add (value);
523 } else if (member is FieldExpr) {
524 FieldExpr fe = (FieldExpr) member;
525 FieldInfo fi = fe.FieldInfo;
528 Error_InvalidNamedArgument (member_name);
532 if (!IsValidArgumentType (fi.FieldType)) {
533 Report.SymbolRelatedToPreviousError (fi);
534 Error_InvalidNamedAgrumentType (member_name);
539 if (!GetAttributeArgumentExpression (e, Location, fi.FieldType, out value))
542 field_values.Add (value);
543 field_infos.Add (fi);
547 Expression mg = Expression.MemberLookup (ec.ContainerType,
548 Type, ".ctor", MemberTypes.Constructor,
549 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
553 // FIXME: Punt the issue for now.
554 if (Type is TypeBuilder)
556 throw new InternalErrorException ("Type " + Type + " doesn't have constructors");
559 MethodBase constructor = Invocation.OverloadResolve (
560 ec, (MethodGroupExpr) mg, pos_args, false, Location);
562 if (constructor == null) {
567 // Here we do the checks which should be done by corlib or by runtime.
568 // However Zoltan doesn't like it and every Mono compiler has to do it again.
570 if (Type == TypeManager.guid_attr_type) {
572 new Guid ((string)pos_values [0]);
574 catch (Exception e) {
575 Error_AttributeEmitError (e.Message);
580 if (Type == TypeManager.methodimpl_attr_type &&
581 pos_values.Length == 1 && ((Argument)pos_args [0]).Type == TypeManager.short_type &&
582 !System.Enum.IsDefined (typeof (MethodImplOptions), pos_values [0])) {
583 Error_AttributeEmitError ("Incorrect argument value.");
588 // Now we perform some checks on the positional args as they
589 // cannot be null for a constructor which expects a parameter
593 ParameterData pd = TypeManager.GetParameterData (constructor);
595 int last_real_param = pd.Count;
597 // When the params is not filled we need to put one
598 if (last_real_param > pos_arg_count) {
599 object [] new_pos_values = new object [pos_arg_count + 1];
600 pos_values.CopyTo (new_pos_values, 0);
601 new_pos_values [pos_arg_count] = new object [] {} ;
602 pos_values = new_pos_values;
607 for (int j = 0; j < pos_arg_count; ++j) {
608 Argument a = (Argument) pos_args [j];
610 if (a.Expr is NullLiteral && pd.ParameterType (j) == TypeManager.object_type) {
611 Error_AttributeArgumentNotValid (Location);
615 object value = pos_values [j];
616 if (value != null && a.Type != value.GetType () && TypeManager.IsPrimitiveType (a.Type)) {
618 pos_values [j] = TypeManager.ChangeType (value, a.Type, out fail);
620 // TODO: Can failed ?
621 throw new NotImplementedException ();
625 if (j < last_real_param)
628 if (j == last_real_param) {
629 object [] array = new object [pos_arg_count - last_real_param];
630 array [0] = pos_values [j];
631 pos_values [j] = array;
635 object [] params_array = (object []) pos_values [last_real_param];
636 params_array [j - last_real_param] = pos_values [j];
639 // Adjust the size of the pos_values if it had params
640 if (last_real_param != pos_arg_count) {
641 object [] new_pos_values = new object [last_real_param + 1];
642 Array.Copy (pos_values, new_pos_values, last_real_param + 1);
643 pos_values = new_pos_values;
646 if (named_arg_count > 0) {
647 prop_info_arr = new PropertyInfo [prop_infos.Count];
648 field_info_arr = new FieldInfo [field_infos.Count];
649 field_values_arr = new object [field_values.Count];
650 prop_values_arr = new object [prop_values.Count];
652 field_infos.CopyTo (field_info_arr, 0);
653 field_values.CopyTo (field_values_arr, 0);
655 prop_values.CopyTo (prop_values_arr, 0);
656 prop_infos.CopyTo (prop_info_arr, 0);
659 return (ConstructorInfo) constructor;
663 /// Get a string containing a list of valid targets for the attribute 'attr'
665 public string GetValidTargets ()
667 StringBuilder sb = new StringBuilder ();
668 AttributeTargets targets = GetAttributeUsage ().ValidOn;
670 if ((targets & AttributeTargets.Assembly) != 0)
671 sb.Append ("assembly, ");
673 if ((targets & AttributeTargets.Module) != 0)
674 sb.Append ("module, ");
676 if ((targets & AttributeTargets.Class) != 0)
677 sb.Append ("class, ");
679 if ((targets & AttributeTargets.Struct) != 0)
680 sb.Append ("struct, ");
682 if ((targets & AttributeTargets.Enum) != 0)
683 sb.Append ("enum, ");
685 if ((targets & AttributeTargets.Constructor) != 0)
686 sb.Append ("constructor, ");
688 if ((targets & AttributeTargets.Method) != 0)
689 sb.Append ("method, ");
691 if ((targets & AttributeTargets.Property) != 0)
692 sb.Append ("property, indexer, ");
694 if ((targets & AttributeTargets.Field) != 0)
695 sb.Append ("field, ");
697 if ((targets & AttributeTargets.Event) != 0)
698 sb.Append ("event, ");
700 if ((targets & AttributeTargets.Interface) != 0)
701 sb.Append ("interface, ");
703 if ((targets & AttributeTargets.Parameter) != 0)
704 sb.Append ("parameter, ");
706 if ((targets & AttributeTargets.Delegate) != 0)
707 sb.Append ("delegate, ");
709 if ((targets & AttributeTargets.ReturnValue) != 0)
710 sb.Append ("return, ");
712 if ((targets & AttributeTargets.GenericParameter) != 0)
713 sb.Append ("type parameter, ");
714 return sb.Remove (sb.Length - 2, 2).ToString ();
718 /// Returns AttributeUsage attribute for this type
720 AttributeUsageAttribute GetAttributeUsage ()
722 AttributeUsageAttribute ua = usage_attr_cache [Type] as AttributeUsageAttribute;
726 Class attr_class = TypeManager.LookupClass (Type);
728 if (attr_class == null) {
729 object[] usage_attr = Type.GetCustomAttributes (TypeManager.attribute_usage_type, true);
730 ua = (AttributeUsageAttribute)usage_attr [0];
731 usage_attr_cache.Add (Type, ua);
735 Attribute a = attr_class.OptAttributes == null
737 : attr_class.OptAttributes.Search (TypeManager.attribute_usage_type);
740 ? DefaultUsageAttribute
741 : a.GetAttributeUsageAttribute ();
743 usage_attr_cache.Add (Type, ua);
747 AttributeUsageAttribute GetAttributeUsageAttribute ()
749 if (pos_values == null)
750 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
751 // But because a lot of attribute class code must be rewritten will be better to wait...
755 return DefaultUsageAttribute;
757 AttributeUsageAttribute usage_attribute = new AttributeUsageAttribute ((AttributeTargets)pos_values [0]);
759 object field = GetPropertyValue ("AllowMultiple");
761 usage_attribute.AllowMultiple = (bool)field;
763 field = GetPropertyValue ("Inherited");
765 usage_attribute.Inherited = (bool)field;
767 return usage_attribute;
771 /// Returns custom name of indexer
773 public string GetIndexerAttributeValue ()
775 if (pos_values == null)
776 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
777 // But because a lot of attribute class code must be rewritten will be better to wait...
783 return pos_values [0] as string;
787 /// Returns condition of ConditionalAttribute
789 public string GetConditionalAttributeValue ()
791 if (pos_values == null)
792 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
793 // But because a lot of attribute class code must be rewritten will be better to wait...
799 return (string)pos_values [0];
803 /// Creates the instance of ObsoleteAttribute from this attribute instance
805 public ObsoleteAttribute GetObsoleteAttribute ()
807 if (pos_values == null)
808 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
809 // But because a lot of attribute class code must be rewritten will be better to wait...
815 if (pos_values == null || pos_values.Length == 0)
816 return new ObsoleteAttribute ();
818 if (pos_values.Length == 1)
819 return new ObsoleteAttribute ((string)pos_values [0]);
821 return new ObsoleteAttribute ((string)pos_values [0], (bool)pos_values [1]);
825 /// Returns value of CLSCompliantAttribute contructor parameter but because the method can be called
826 /// before ApplyAttribute. We need to resolve the arguments.
827 /// This situation occurs when class deps is differs from Emit order.
829 public bool GetClsCompliantAttributeValue ()
831 if (pos_values == null)
832 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
833 // But because a lot of attribute class code must be rewritten will be better to wait...
839 return (bool)pos_values [0];
842 public Type GetCoClassAttributeValue ()
844 if (pos_values == null)
850 return (Type)pos_values [0];
853 public bool CheckTarget ()
855 string[] valid_targets = owner.ValidAttributeTargets;
856 if (ExplicitTarget == null || ExplicitTarget == valid_targets [0]) {
857 Target = owner.AttributeTargets;
861 // TODO: we can skip the first item
862 if (((IList) valid_targets).Contains (ExplicitTarget)) {
863 switch (ExplicitTarget) {
864 case "return": Target = AttributeTargets.ReturnValue; return true;
865 case "param": Target = AttributeTargets.Parameter; return true;
866 case "field": Target = AttributeTargets.Field; return true;
867 case "method": Target = AttributeTargets.Method; return true;
868 case "property": Target = AttributeTargets.Property; return true;
870 throw new InternalErrorException ("Unknown explicit target: " + ExplicitTarget);
873 StringBuilder sb = new StringBuilder ();
874 foreach (string s in valid_targets) {
878 sb.Remove (sb.Length - 2, 2);
879 Report.Error (657, Location, "`{0}' is not a valid attribute location for this declaration. " +
880 "Valid attribute locations for this declaration are `{1}'", ExplicitTarget, sb.ToString ());
885 /// Tests permitted SecurityAction for assembly or other types
887 public bool CheckSecurityActionValidity (bool for_assembly)
889 SecurityAction action = GetSecurityActionValue ();
892 case SecurityAction.Demand:
893 case SecurityAction.Assert:
894 case SecurityAction.Deny:
895 case SecurityAction.PermitOnly:
896 case SecurityAction.LinkDemand:
897 case SecurityAction.InheritanceDemand:
902 case SecurityAction.RequestMinimum:
903 case SecurityAction.RequestOptional:
904 case SecurityAction.RequestRefuse:
910 Error_AttributeEmitError ("SecurityAction is out of range");
914 Error_AttributeEmitError (String.Concat ("SecurityAction `", action, "' is not valid for this declaration"));
918 System.Security.Permissions.SecurityAction GetSecurityActionValue ()
920 return (SecurityAction)pos_values [0];
924 /// Creates instance of SecurityAttribute class and add result of CreatePermission method to permission table.
926 /// <returns></returns>
927 public void ExtractSecurityPermissionSet (ListDictionary permissions)
929 Type orig_assembly_type = null;
931 if (TypeManager.LookupDeclSpace (Type) != null) {
932 if (!RootContext.StdLib) {
933 orig_assembly_type = Type.GetType (Type.FullName);
935 string orig_version_path = Environment.GetEnvironmentVariable ("__SECURITY_BOOTSTRAP_DB");
936 if (orig_version_path == null) {
937 Error_AttributeEmitError ("security custom attributes can not be referenced from defining assembly");
941 if (orig_sec_assembly == null) {
942 string file = Path.Combine (orig_version_path, Driver.OutputFile);
943 orig_sec_assembly = Assembly.LoadFile (file);
946 orig_assembly_type = orig_sec_assembly.GetType (Type.FullName, true);
947 if (orig_assembly_type == null) {
948 Report.Warning (-112, 1, Location, "Self-referenced security attribute `{0}' " +
949 "was not found in previous version of assembly");
955 SecurityAttribute sa;
956 // For all non-selfreferencing security attributes we can avoid all hacks
957 if (orig_assembly_type == null) {
958 sa = (SecurityAttribute) Activator.CreateInstance (Type, pos_values);
960 if (prop_info_arr != null) {
961 for (int i = 0; i < prop_info_arr.Length; ++i) {
962 PropertyInfo pi = prop_info_arr [i];
963 pi.SetValue (sa, prop_values_arr [i], null);
967 // HACK: All security attributes have same ctor syntax
968 sa = (SecurityAttribute) Activator.CreateInstance (orig_assembly_type, new object[] { GetSecurityActionValue () } );
970 // All types are from newly created assembly but for invocation with old one we need to convert them
971 if (prop_info_arr != null) {
972 for (int i = 0; i < prop_info_arr.Length; ++i) {
973 PropertyInfo emited_pi = prop_info_arr [i];
974 PropertyInfo pi = orig_assembly_type.GetProperty (emited_pi.Name, emited_pi.PropertyType);
976 object old_instance = pi.PropertyType.IsEnum ?
977 System.Enum.ToObject (pi.PropertyType, prop_values_arr [i]) :
980 pi.SetValue (sa, old_instance, null);
986 perm = sa.CreatePermission ();
987 SecurityAction action = GetSecurityActionValue ();
989 // IS is correct because for corlib we are using an instance from old corlib
990 if (!(perm is System.Security.CodeAccessPermission)) {
992 case SecurityAction.Demand:
993 action = (SecurityAction)13;
995 case SecurityAction.LinkDemand:
996 action = (SecurityAction)14;
998 case SecurityAction.InheritanceDemand:
999 action = (SecurityAction)15;
1004 PermissionSet ps = (PermissionSet)permissions [action];
1006 if (sa is PermissionSetAttribute)
1007 ps = new PermissionSet (sa.Unrestricted ? PermissionState.Unrestricted : PermissionState.None);
1009 ps = new PermissionSet (PermissionState.None);
1011 permissions.Add (action, ps);
1012 } else if (!ps.IsUnrestricted () && (sa is PermissionSetAttribute) && sa.Unrestricted) {
1013 ps = ps.Union (new PermissionSet (PermissionState.Unrestricted));
1014 permissions [action] = ps;
1016 ps.AddPermission (perm);
1019 object GetValue (object value)
1021 if (value is EnumConstant)
1022 return ((EnumConstant) value).GetValue ();
1027 public object GetPropertyValue (string name)
1029 if (prop_info_arr == null)
1032 for (int i = 0; i < prop_info_arr.Length; ++i) {
1033 if (prop_info_arr [i].Name == name)
1034 return prop_values_arr [i];
1040 object GetFieldValue (string name)
1043 if (field_info_arr == null)
1046 foreach (FieldInfo fi in field_info_arr) {
1047 if (fi.Name == name)
1048 return GetValue (field_values_arr [i]);
1055 // Theoretically, we can get rid of this, since FieldBuilder.SetCustomAttribute()
1056 // and ParameterBuilder.SetCustomAttribute() are supposed to handle this attribute.
1057 // However, we can't, since it appears that the .NET 1.1 SRE hangs when given a MarshalAsAttribute.
1059 public UnmanagedMarshal GetMarshal (Attributable attr)
1061 UnmanagedType UnmanagedType;
1062 if (!RootContext.StdLib || pos_values [0].GetType () != typeof (UnmanagedType))
1063 UnmanagedType = (UnmanagedType) System.Enum.ToObject (typeof (UnmanagedType), pos_values [0]);
1065 UnmanagedType = (UnmanagedType) pos_values [0];
1067 object value = GetFieldValue ("SizeParamIndex");
1068 if (value != null && UnmanagedType != UnmanagedType.LPArray) {
1069 Error_AttributeEmitError ("SizeParamIndex field is not valid for the specified unmanaged type");
1073 object o = GetFieldValue ("ArraySubType");
1074 UnmanagedType array_sub_type = o == null ? (UnmanagedType) 0x50 /* NATIVE_MAX */ : (UnmanagedType) o;
1076 switch (UnmanagedType) {
1077 case UnmanagedType.CustomMarshaler: {
1078 MethodInfo define_custom = typeof (UnmanagedMarshal).GetMethod ("DefineCustom",
1079 BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
1080 if (define_custom == null) {
1081 Report.RuntimeMissingSupport (Location, "set marshal info");
1085 object [] args = new object [4];
1086 args [0] = GetFieldValue ("MarshalTypeRef");
1087 args [1] = GetFieldValue ("MarshalCookie");
1088 args [2] = GetFieldValue ("MarshalType");
1089 args [3] = Guid.Empty;
1090 return (UnmanagedMarshal) define_custom.Invoke (null, args);
1092 case UnmanagedType.LPArray: {
1093 object size_const = GetFieldValue ("SizeConst");
1094 object size_param_index = GetFieldValue ("SizeParamIndex");
1096 if ((size_const != null) || (size_param_index != null)) {
1097 MethodInfo define_array = typeof (UnmanagedMarshal).GetMethod ("DefineLPArrayInternal",
1098 BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
1099 if (define_array == null) {
1100 Report.RuntimeMissingSupport (Location, "set marshal info");
1104 object [] args = new object [3];
1105 args [0] = array_sub_type;
1106 args [1] = size_const == null ? -1 : size_const;
1107 args [2] = size_param_index == null ? -1 : size_param_index;
1108 return (UnmanagedMarshal) define_array.Invoke (null, args);
1111 return UnmanagedMarshal.DefineLPArray (array_sub_type);
1113 case UnmanagedType.SafeArray:
1114 return UnmanagedMarshal.DefineSafeArray (array_sub_type);
1116 case UnmanagedType.ByValArray:
1117 FieldMember fm = attr as FieldMember;
1119 Error_AttributeEmitError ("Specified unmanaged type is only valid on fields");
1122 return UnmanagedMarshal.DefineByValArray ((int) GetFieldValue ("SizeConst"));
1124 case UnmanagedType.ByValTStr:
1125 return UnmanagedMarshal.DefineByValTStr ((int) GetFieldValue ("SizeConst"));
1128 return UnmanagedMarshal.DefineUnmanagedMarshal (UnmanagedType);
1132 public CharSet GetCharSetValue ()
1134 return (CharSet)System.Enum.Parse (typeof (CharSet), pos_values [0].ToString ());
1137 public MethodImplOptions GetMethodImplOptions ()
1139 if (pos_values [0].GetType () != typeof (MethodImplOptions))
1140 return (MethodImplOptions)System.Enum.ToObject (typeof (MethodImplOptions), pos_values [0]);
1141 return (MethodImplOptions)pos_values [0];
1144 public LayoutKind GetLayoutKindValue ()
1146 if (!RootContext.StdLib || pos_values [0].GetType () != typeof (LayoutKind))
1147 return (LayoutKind)System.Enum.ToObject (typeof (LayoutKind), pos_values [0]);
1149 return (LayoutKind)pos_values [0];
1153 /// Emit attribute for Attributable symbol
1155 public void Emit (ListDictionary emitted_attr)
1157 CustomAttributeBuilder cb = Resolve ();
1161 AttributeUsageAttribute usage_attr = GetAttributeUsage ();
1162 if ((usage_attr.ValidOn & Target) == 0) {
1163 Report.Error (592, Location, "Attribute `{0}' is not valid on this declaration type. " +
1164 "It is valid on `{1}' declarations only",
1165 GetSignatureForError (), GetValidTargets ());
1170 owner.ApplyAttributeBuilder (this, cb);
1172 catch (Exception e) {
1173 Error_AttributeEmitError (e.Message);
1177 if (!usage_attr.AllowMultiple) {
1178 ArrayList emitted_targets = (ArrayList)emitted_attr [Type];
1179 if (emitted_targets == null) {
1180 emitted_targets = new ArrayList ();
1181 emitted_attr.Add (Type, emitted_targets);
1182 } else if (emitted_targets.Contains (Target)) {
1183 Report.Error (579, Location, "Duplicate `{0}' attribute", GetSignatureForError ());
1186 emitted_targets.Add (Target);
1189 if (!RootContext.VerifyClsCompliance)
1192 // Here we are testing attribute arguments for array usage (error 3016)
1193 if (owner.IsClsComplianceRequired ()) {
1194 if (Arguments == null)
1197 ArrayList pos_args = (ArrayList) Arguments [0];
1198 if (pos_args != null) {
1199 foreach (Argument arg in pos_args) {
1200 // Type is undefined (was error 246)
1201 if (arg.Type == null)
1204 if (arg.Type.IsArray) {
1205 Report.Error (3016, Location, "Arrays as attribute arguments are not CLS-compliant");
1211 if (Arguments.Count < 2)
1214 ArrayList named_args = (ArrayList) Arguments [1];
1215 foreach (DictionaryEntry de in named_args) {
1216 Argument arg = (Argument) de.Value;
1218 // Type is undefined (was error 246)
1219 if (arg.Type == null)
1222 if (arg.Type.IsArray) {
1223 Report.Error (3016, Location, "Arrays as attribute arguments are not CLS-compliant");
1230 public MethodBuilder DefinePInvokeMethod (TypeBuilder builder, string name,
1231 MethodAttributes flags, Type ret_type, Type [] param_types)
1233 if (pos_values == null)
1234 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
1235 // But because a lot of attribute class code must be rewritten will be better to wait...
1241 string dll_name = (string)pos_values [0];
1244 CallingConvention cc = CallingConvention.Winapi;
1245 CharSet charset = CodeGen.Module.DefaultCharSet;
1246 bool preserve_sig = true;
1247 string entry_point = name;
1248 bool best_fit_mapping = false;
1249 bool throw_on_unmappable = false;
1250 bool exact_spelling = false;
1251 bool set_last_error = false;
1253 bool best_fit_mapping_set = false;
1254 bool throw_on_unmappable_set = false;
1255 bool exact_spelling_set = false;
1256 bool set_last_error_set = false;
1258 MethodInfo set_best_fit = null;
1259 MethodInfo set_throw_on = null;
1260 MethodInfo set_exact_spelling = null;
1261 MethodInfo set_set_last_error = null;
1263 if (field_info_arr != null) {
1265 for (int i = 0; i < field_info_arr.Length; i++) {
1266 switch (field_info_arr [i].Name) {
1267 case "BestFitMapping":
1268 best_fit_mapping = (bool) field_values_arr [i];
1269 best_fit_mapping_set = true;
1271 case "CallingConvention":
1272 cc = (CallingConvention) field_values_arr [i];
1275 charset = (CharSet) field_values_arr [i];
1278 entry_point = (string) field_values_arr [i];
1280 case "ExactSpelling":
1281 exact_spelling = (bool) field_values_arr [i];
1282 exact_spelling_set = true;
1285 preserve_sig = (bool) field_values_arr [i];
1287 case "SetLastError":
1288 set_last_error = (bool) field_values_arr [i];
1289 set_last_error_set = true;
1291 case "ThrowOnUnmappableChar":
1292 throw_on_unmappable = (bool) field_values_arr [i];
1293 throw_on_unmappable_set = true;
1296 throw new InternalErrorException (field_info_arr [i].ToString ());
1301 if (throw_on_unmappable_set || best_fit_mapping_set || exact_spelling_set || set_last_error_set) {
1302 set_best_fit = typeof (MethodBuilder).
1303 GetMethod ("set_BestFitMapping", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
1304 set_throw_on = typeof (MethodBuilder).
1305 GetMethod ("set_ThrowOnUnmappableChar", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
1306 set_exact_spelling = typeof (MethodBuilder).
1307 GetMethod ("set_ExactSpelling", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
1308 set_set_last_error = typeof (MethodBuilder).
1309 GetMethod ("set_SetLastError", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
1311 if ((set_best_fit == null) || (set_throw_on == null) ||
1312 (set_exact_spelling == null) || (set_set_last_error == null)) {
1313 Report.Error (-1, Location,
1314 "The ThrowOnUnmappableChar, BestFitMapping, SetLastError, " +
1315 "and ExactSpelling attributes can only be emitted when running on the mono runtime.");
1321 MethodBuilder mb = builder.DefinePInvokeMethod (
1322 name, dll_name, entry_point, flags | MethodAttributes.HideBySig | MethodAttributes.PinvokeImpl,
1323 CallingConventions.Standard, ret_type, param_types, cc, charset);
1326 mb.SetImplementationFlags (MethodImplAttributes.PreserveSig);
1328 if (throw_on_unmappable_set)
1329 set_throw_on.Invoke (mb, 0, null, new object [] { throw_on_unmappable }, null);
1330 if (best_fit_mapping_set)
1331 set_best_fit.Invoke (mb, 0, null, new object [] { best_fit_mapping }, null);
1332 if (exact_spelling_set)
1333 set_exact_spelling.Invoke (mb, 0, null, new object [] { exact_spelling }, null);
1334 if (set_last_error_set)
1335 set_set_last_error.Invoke (mb, 0, null, new object [] { set_last_error }, null);
1339 catch (ArgumentException e) {
1340 Error_AttributeEmitError (e.Message);
1345 private Expression GetValue ()
1347 if ((Arguments == null) || (Arguments.Count < 1))
1349 ArrayList al = (ArrayList) Arguments [0];
1350 if ((al == null) || (al.Count < 1))
1352 Argument arg = (Argument) al [0];
1353 if ((arg == null) || (arg.Expr == null))
1358 public string GetString ()
1360 Expression e = GetValue ();
1361 if (e is StringLiteral)
1362 return (e as StringLiteral).Value;
1366 public bool GetBoolean ()
1368 Expression e = GetValue ();
1369 if (e is BoolLiteral)
1370 return (e as BoolLiteral).Value;
1377 /// For global attributes (assembly, module) we need special handling.
1378 /// Attributes can be located in the several files
1380 public class GlobalAttribute : Attribute
1382 public readonly NamespaceEntry ns;
1384 public GlobalAttribute (NamespaceEntry ns, string target,
1385 Expression left_expr, string identifier, ArrayList args, Location loc, bool nameEscaped):
1386 base (target, left_expr, identifier, args, loc, nameEscaped)
1393 // RootContext.Tree.Types has a single NamespaceEntry which gets overwritten
1394 // each time a new file is parsed. However, we need to use the NamespaceEntry
1395 // in effect where the attribute was used. Since code elsewhere cannot assume
1396 // that the NamespaceEntry is right, just overwrite it.
1398 // Precondition: RootContext.Tree.Types == null
1400 if (RootContext.Tree.Types.NamespaceEntry != null)
1401 throw new InternalErrorException (Location + " non-null NamespaceEntry");
1403 RootContext.Tree.Types.NamespaceEntry = ns;
1408 RootContext.Tree.Types.NamespaceEntry = null;
1411 protected override FullNamedExpression ResolveAsTypeStep (Expression expr, EmitContext ec, bool silent)
1415 return base.ResolveAsTypeStep (expr, ec, silent);
1423 protected override FullNamedExpression ResolveAsTypeTerminal (Expression expr, EmitContext ec, bool silent)
1427 return base.ResolveAsTypeTerminal (expr, ec, silent);
1434 protected override ConstructorInfo ResolveArguments ()
1438 return base.ResolveArguments ();
1446 public class Attributes {
1447 public readonly ArrayList Attrs;
1449 public Attributes (Attribute a)
1451 Attrs = new ArrayList ();
1455 public Attributes (ArrayList attrs)
1460 public void AddAttributes (ArrayList attrs)
1462 Attrs.AddRange (attrs);
1465 public void AttachTo (Attributable attributable)
1467 foreach (Attribute a in Attrs)
1468 a.AttachTo (attributable);
1472 /// Checks whether attribute target is valid for the current element
1474 public bool CheckTargets ()
1476 foreach (Attribute a in Attrs) {
1477 if (!a.CheckTarget ())
1483 public Attribute Search (Type t)
1485 foreach (Attribute a in Attrs) {
1486 if (a.ResolveType () == t)
1493 /// Returns all attributes of type 't'. Use it when attribute is AllowMultiple = true
1495 public Attribute[] SearchMulti (Type t)
1497 ArrayList ar = null;
1499 foreach (Attribute a in Attrs) {
1500 if (a.ResolveType () == t) {
1502 ar = new ArrayList ();
1507 return ar == null ? null : ar.ToArray (typeof (Attribute)) as Attribute[];
1514 ListDictionary ld = new ListDictionary ();
1516 foreach (Attribute a in Attrs)
1520 public bool Contains (Type t)
1522 return Search (t) != null;
1527 /// Helper class for attribute verification routine.
1529 sealed class AttributeTester
1531 static PtrHashtable analyzed_types = new PtrHashtable ();
1532 static PtrHashtable analyzed_types_obsolete = new PtrHashtable ();
1533 static PtrHashtable analyzed_member_obsolete = new PtrHashtable ();
1534 static PtrHashtable analyzed_method_excluded = new PtrHashtable ();
1536 static PtrHashtable fixed_buffer_cache = new PtrHashtable ();
1538 static object TRUE = new object ();
1539 static object FALSE = new object ();
1541 private AttributeTester ()
1545 public enum Result {
1552 /// Returns true if parameters of two compared methods are CLS-Compliant.
1553 /// It tests differing only in ref or out, or in array rank.
1555 public static Result AreOverloadedMethodParamsClsCompliant (Type[] types_a, Type[] types_b)
1557 if (types_a == null || types_b == null)
1560 if (types_a.Length != types_b.Length)
1563 Result result = Result.Ok;
1564 for (int i = 0; i < types_b.Length; ++i) {
1565 Type aType = types_a [i];
1566 Type bType = types_b [i];
1568 if (aType.IsArray && bType.IsArray) {
1569 Type a_el_type = aType.GetElementType ();
1570 Type b_el_type = bType.GetElementType ();
1571 if (aType.GetArrayRank () != bType.GetArrayRank () && a_el_type == b_el_type) {
1572 result = Result.RefOutArrayError;
1576 if (a_el_type.IsArray || b_el_type.IsArray) {
1577 result = Result.ArrayArrayError;
1582 Type aBaseType = aType;
1583 bool is_either_ref_or_out = false;
1585 if (aType.IsByRef || aType.IsPointer) {
1586 aBaseType = aType.GetElementType ();
1587 is_either_ref_or_out = true;
1590 Type bBaseType = bType;
1591 if (bType.IsByRef || bType.IsPointer)
1593 bBaseType = bType.GetElementType ();
1594 is_either_ref_or_out = !is_either_ref_or_out;
1597 if (aBaseType != bBaseType)
1600 if (is_either_ref_or_out)
1601 result = Result.RefOutArrayError;
1607 /// This method tests the CLS compliance of external types. It doesn't test type visibility.
1609 public static bool IsClsCompliant (Type type)
1614 object type_compliance = analyzed_types[type];
1615 if (type_compliance != null)
1616 return type_compliance == TRUE;
1618 if (type.IsPointer) {
1619 analyzed_types.Add (type, null);
1624 if (type.IsArray || type.IsByRef) {
1625 result = IsClsCompliant (TypeManager.GetElementType (type));
1627 result = AnalyzeTypeCompliance (type);
1629 analyzed_types.Add (type, result ? TRUE : FALSE);
1634 /// Returns IFixedBuffer implementation if field is fixed buffer else null.
1636 public static IFixedBuffer GetFixedBuffer (FieldInfo fi)
1638 FieldBase fb = TypeManager.GetField (fi);
1640 return fb as IFixedBuffer;
1643 object o = fixed_buffer_cache [fi];
1645 if (System.Attribute.GetCustomAttribute (fi, TypeManager.fixed_buffer_attr_type) == null) {
1646 fixed_buffer_cache.Add (fi, FALSE);
1650 IFixedBuffer iff = new FixedFieldExternal (fi);
1651 fixed_buffer_cache.Add (fi, iff);
1658 return (IFixedBuffer)o;
1661 public static void VerifyModulesClsCompliance ()
1663 Module[] modules = RootNamespace.Global.Modules;
1664 if (modules == null)
1667 // The first module is generated assembly
1668 for (int i = 1; i < modules.Length; ++i) {
1669 Module module = modules [i];
1670 if (!IsClsCompliant (module)) {
1671 Report.Error (3013, "Added modules must be marked with the CLSCompliant attribute " +
1672 "to match the assembly", module.Name);
1678 public static Type GetImportedIgnoreCaseClsType (string name)
1680 foreach (Assembly a in RootNamespace.Global.Assemblies) {
1681 Type t = a.GetType (name, false, true);
1685 if (IsClsCompliant (t))
1691 static bool IsClsCompliant (ICustomAttributeProvider attribute_provider)
1693 object[] CompliantAttribute = attribute_provider.GetCustomAttributes (TypeManager.cls_compliant_attribute_type, false);
1694 if (CompliantAttribute.Length == 0)
1697 return ((CLSCompliantAttribute)CompliantAttribute[0]).IsCompliant;
1700 static bool AnalyzeTypeCompliance (Type type)
1702 type = TypeManager.DropGenericTypeArguments (type);
1703 DeclSpace ds = TypeManager.LookupDeclSpace (type);
1705 return ds.IsClsComplianceRequired ();
1708 if (type.IsGenericParameter)
1711 object[] CompliantAttribute = type.GetCustomAttributes (TypeManager.cls_compliant_attribute_type, false);
1712 if (CompliantAttribute.Length == 0)
1713 return IsClsCompliant (type.Assembly);
1715 return ((CLSCompliantAttribute)CompliantAttribute[0]).IsCompliant;
1719 /// Returns instance of ObsoleteAttribute when type is obsolete
1721 public static ObsoleteAttribute GetObsoleteAttribute (Type type)
1723 object type_obsolete = analyzed_types_obsolete [type];
1724 if (type_obsolete == FALSE)
1727 if (type_obsolete != null)
1728 return (ObsoleteAttribute)type_obsolete;
1730 ObsoleteAttribute result = null;
1731 if (type.IsByRef || type.IsArray || type.IsPointer) {
1732 result = GetObsoleteAttribute (TypeManager.GetElementType (type));
1733 } else if (type.IsGenericParameter || type.IsGenericType)
1736 DeclSpace type_ds = TypeManager.LookupDeclSpace (type);
1738 // Type is external, we can get attribute directly
1739 if (type_ds == null) {
1740 object[] attribute = type.GetCustomAttributes (TypeManager.obsolete_attribute_type, false);
1741 if (attribute.Length == 1)
1742 result = (ObsoleteAttribute)attribute [0];
1744 // Is null during corlib bootstrap
1745 if (TypeManager.obsolete_attribute_type != null)
1746 result = type_ds.GetObsoleteAttribute ();
1750 // Cannot use .Add because of corlib bootstrap
1751 analyzed_types_obsolete [type] = result == null ? FALSE : result;
1756 /// Returns instance of ObsoleteAttribute when method is obsolete
1758 public static ObsoleteAttribute GetMethodObsoleteAttribute (MethodBase mb)
1760 IMethodData mc = TypeManager.GetMethod (mb);
1762 return mc.GetObsoleteAttribute ();
1764 // compiler generated methods are not registered by AddMethod
1765 if (mb.DeclaringType is TypeBuilder)
1768 if (mb.IsSpecialName) {
1769 PropertyInfo pi = PropertyExpr.AccessorTable [mb] as PropertyInfo;
1771 // FIXME: This is buggy as properties from this assembly are included as well
1773 //return GetMemberObsoleteAttribute (pi);
1777 return GetMemberObsoleteAttribute (mb);
1781 /// Returns instance of ObsoleteAttribute when member is obsolete
1783 public static ObsoleteAttribute GetMemberObsoleteAttribute (MemberInfo mi)
1785 object type_obsolete = analyzed_member_obsolete [mi];
1786 if (type_obsolete == FALSE)
1789 if (type_obsolete != null)
1790 return (ObsoleteAttribute)type_obsolete;
1792 if ((mi.DeclaringType is TypeBuilder) || mi.DeclaringType.IsGenericType)
1795 ObsoleteAttribute oa = System.Attribute.GetCustomAttribute (mi, TypeManager.obsolete_attribute_type, false)
1796 as ObsoleteAttribute;
1797 analyzed_member_obsolete.Add (mi, oa == null ? FALSE : oa);
1802 /// Common method for Obsolete error/warning reporting.
1804 public static void Report_ObsoleteMessage (ObsoleteAttribute oa, string member, Location loc)
1807 Report.Error (619, loc, "`{0}' is obsolete: `{1}'", member, oa.Message);
1811 if (oa.Message == null) {
1812 Report.Warning (612, 1, loc, "`{0}' is obsolete", member);
1815 Report.Warning (618, 2, loc, "`{0}' is obsolete: `{1}'", member, oa.Message);
1818 public static bool IsConditionalMethodExcluded (MethodBase mb)
1820 mb = TypeManager.DropGenericMethodArguments (mb);
1821 if ((mb is MethodBuilder) || (mb is ConstructorBuilder))
1824 object excluded = analyzed_method_excluded [mb];
1825 if (excluded != null)
1826 return excluded == TRUE ? true : false;
1828 ConditionalAttribute[] attrs = mb.GetCustomAttributes (TypeManager.conditional_attribute_type, true)
1829 as ConditionalAttribute[];
1830 if (attrs.Length == 0) {
1831 analyzed_method_excluded.Add (mb, FALSE);
1835 foreach (ConditionalAttribute a in attrs) {
1836 if (RootContext.AllDefines.Contains (a.ConditionString)) {
1837 analyzed_method_excluded.Add (mb, FALSE);
1841 analyzed_method_excluded.Add (mb, TRUE);
1846 /// Analyzes class whether it has attribute which has ConditionalAttribute
1847 /// and its condition is not defined.
1849 public static bool IsAttributeExcluded (Type type)
1854 Class class_decl = TypeManager.LookupDeclSpace (type) as Class;
1856 // TODO: add caching
1857 // TODO: merge all Type bases attribute caching to one cache to save memory
1858 if (class_decl == null) {
1859 object[] attributes = type.GetCustomAttributes (TypeManager.conditional_attribute_type, false);
1860 foreach (ConditionalAttribute ca in attributes) {
1861 if (RootContext.AllDefines.Contains (ca.ConditionString))
1864 return attributes.Length > 0;
1867 return class_decl.IsExcluded ();
1870 public static Type GetCoClassAttribute (Type type)
1872 TypeContainer tc = TypeManager.LookupInterface (type);
1874 object[] o = type.GetCustomAttributes (TypeManager.coclass_attr_type, false);
1875 return ((System.Runtime.InteropServices.CoClassAttribute)o[0]).CoClass;
1878 if (tc.OptAttributes == null)
1881 Attribute a = tc.OptAttributes.Search (TypeManager.coclass_attr_type);
1885 return a.GetCoClassAttributeValue ();