2 // attribute.cs: Attribute Handler
4 // Author: Ravi Pratap (ravi@ximian.com)
5 // Marek Safar (marek.safar@seznam.cz)
7 // Dual licensed under the terms of the MIT X11 or GNU GPL
9 // Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
10 // Copyright 2003-2008 Novell, Inc.
14 using System.Diagnostics;
15 using System.Collections.Generic;
16 using System.Reflection;
17 using System.Reflection.Emit;
18 using System.Runtime.InteropServices;
19 using System.Runtime.CompilerServices;
20 using System.Security;
21 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 // Holds all attributes attached to this element
34 protected Attributes attributes;
36 public void AddAttributes (Attributes attrs, IMemberContext context)
41 if (attributes == null)
44 throw new NotImplementedException ();
46 attributes.AttachTo (this, context);
49 public Attributes OptAttributes {
59 /// Use member-specific procedure to apply attribute @a in @cb to the entity being built in @builder
61 public abstract void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa);
64 /// Returns one AttributeTarget for this element.
66 public abstract AttributeTargets AttributeTargets { 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 : Expression
79 public readonly string ExplicitTarget;
80 public AttributeTargets Target;
81 readonly ATypeNameExpression expression;
83 Arguments PosArguments;
84 Arguments NamedArguments;
87 readonly bool nameEscaped;
90 // An attribute can be attached to multiple targets (e.g. multiple fields)
92 protected Attributable[] targets;
95 // A member context for the attribute, it's much easier to hold it here
96 // than trying to pull it during resolve
98 IMemberContext context;
100 static readonly AttributeUsageAttribute DefaultUsageAttribute = new AttributeUsageAttribute (AttributeTargets.All);
101 static Assembly orig_sec_assembly;
102 public static readonly object[] EmptyObject = new object [0];
104 // non-null if named args present after Resolve () is called
105 PropertyInfo [] prop_info_arr;
106 FieldInfo [] field_info_arr;
107 object [] field_values_arr;
108 object [] prop_values_arr;
109 object [] pos_values;
111 static Dictionary<Type, AttributeUsageAttribute> usage_attr_cache;
112 // Cache for parameter-less attributes
113 static Dictionary<Type, CustomAttributeBuilder> att_cache;
115 public Attribute (string target, ATypeNameExpression expr, Arguments[] args, Location loc, bool nameEscaped)
117 this.expression = expr;
119 PosArguments = args [0];
120 NamedArguments = args [1];
123 ExplicitTarget = target;
124 this.nameEscaped = nameEscaped;
127 public Attribute Clone ()
129 Attribute a = new Attribute (ExplicitTarget, expression, null, loc, nameEscaped);
130 a.PosArguments = PosArguments;
131 a.NamedArguments = NamedArguments;
140 public static void Reset ()
142 usage_attr_cache = new Dictionary<Type, AttributeUsageAttribute> (ReferenceEquality<Type>.Default);
143 att_cache = new Dictionary<Type, CustomAttributeBuilder> (ReferenceEquality<Type>.Default);
147 // When the same attribute is attached to multiple fiels
148 // we use @target field as a list of targets. The attribute
149 // has to be resolved only once but emitted for each target.
151 public virtual void AttachTo (Attributable target, IMemberContext context)
153 if (this.targets == null) {
154 this.targets = new Attributable[] { target };
155 this.context = context;
159 // Resize target array
160 Attributable[] new_array = new Attributable [this.targets.Length + 1];
161 targets.CopyTo (new_array, 0);
162 new_array [targets.Length] = target;
163 this.targets = new_array;
165 // No need to update context, different targets cannot have
166 // different contexts, it's enough to remove same attributes
167 // from secondary members.
169 target.OptAttributes = null;
172 static void Error_InvalidNamedArgument (ResolveContext rc, NamedArgument name)
174 rc.Report.Error (617, name.Location, "`{0}' is not a valid named attribute argument. Named attribute arguments " +
175 "must be fields which are not readonly, static, const or read-write properties which are " +
176 "public and not static",
180 static void Error_InvalidNamedArgumentType (ResolveContext rc, NamedArgument name)
182 rc.Report.Error (655, name.Location,
183 "`{0}' is not a valid named attribute argument because it is not a valid attribute parameter type",
187 public static void Error_AttributeArgumentNotValid (ResolveContext rc, Location loc)
189 rc.Report.Error (182, loc,
190 "An attribute argument must be a constant expression, typeof " +
191 "expression or array creation expression");
194 public void Error_MissingGuidAttribute ()
196 Report.Error (596, Location, "The Guid attribute must be specified with the ComImport attribute");
199 public void Error_MisusedExtensionAttribute ()
201 Report.Error (1112, Location, "Do not use `{0}' directly. Use parameter modifier `this' instead", GetSignatureForError ());
204 public void Error_MisusedDynamicAttribute ()
206 Report.Error (1970, loc, "Do not use `{0}' directly. Use `dynamic' keyword instead", GetSignatureForError ());
210 /// This is rather hack. We report many emit attribute error with same error to be compatible with
211 /// csc. But because csc has to report them this way because error came from ilasm we needn't.
213 public void Error_AttributeEmitError (string inner)
215 Report.Error (647, Location, "Error during emitting `{0}' attribute. The reason is `{1}'",
216 TypeManager.CSharpName (Type), inner);
219 public void Error_InvalidSecurityParent ()
221 Error_AttributeEmitError ("it is attached to invalid parent");
230 protected virtual TypeExpr ResolveAsTypeTerminal (Expression expr, IMemberContext ec)
232 return expr.ResolveAsTypeTerminal (ec, false);
235 Type ResolvePossibleAttributeType (ATypeNameExpression expr, ref bool is_attr)
237 TypeExpr te = ResolveAsTypeTerminal (expr, context);
242 if (TypeManager.IsSubclassOf (t, TypeManager.attribute_type)) {
245 Report.SymbolRelatedToPreviousError (t);
246 Report.Error (616, Location, "`{0}': is not an attribute class", TypeManager.CSharpName (t));
252 /// Tries to resolve the type of the attribute. Flags an error if it can't, and complain is true.
254 void ResolveAttributeType ()
256 SessionReportPrinter resolve_printer = new SessionReportPrinter ();
257 ReportPrinter prev_recorder = context.Compiler.Report.SetPrinter (resolve_printer);
259 bool t1_is_attr = false;
260 bool t2_is_attr = false;
262 ATypeNameExpression expanded = null;
265 t1 = ResolvePossibleAttributeType (expression, ref t1_is_attr);
270 expanded = (ATypeNameExpression) expression.Clone (null);
271 expanded.Name += "Attribute";
273 t2 = ResolvePossibleAttributeType (expanded, ref t2_is_attr);
276 resolve_printer.EndSession ();
278 context.Compiler.Report.SetPrinter (prev_recorder);
281 if (t1_is_attr && t2_is_attr) {
282 Report.Error (1614, Location, "`{0}' is ambiguous between `{1}' and `{2}'. Use either `@{0}' or `{0}Attribute'",
283 GetSignatureForError (), expression.GetSignatureForError (), expanded.GetSignatureForError ());
284 resolve_error = true;
298 resolve_printer.Merge (prev_recorder);
299 resolve_error = true;
302 public virtual Type ResolveType ()
304 if (Type == null && !resolve_error)
305 ResolveAttributeType ();
309 public override string GetSignatureForError ()
312 return TypeManager.CSharpName (Type);
314 return expression.GetSignatureForError ();
317 public bool HasSecurityAttribute {
319 PredefinedAttribute pa = PredefinedAttributes.Get.Security;
320 return pa.IsDefined && TypeManager.IsSubclassOf (type, pa.Type);
324 public bool IsValidSecurityAttribute ()
326 return HasSecurityAttribute && IsSecurityActionValid (false);
329 static bool IsValidArgumentType (Type t)
332 t = TypeManager.GetElementType (t);
334 return t == TypeManager.string_type ||
335 TypeManager.IsPrimitiveType (t) ||
336 TypeManager.IsEnumType (t) ||
337 t == TypeManager.object_type ||
338 t == TypeManager.type_type;
341 // TODO: Don't use this ambiguous value
343 get { return expression.Name; }
346 void ApplyModuleCharSet (ResolveContext rc)
348 if (Type != PredefinedAttributes.Get.DllImport)
351 if (!RootContext.ToplevelTypes.HasDefaultCharSet)
354 const string CharSetEnumMember = "CharSet";
355 if (NamedArguments == null) {
356 NamedArguments = new Arguments (1);
358 foreach (NamedArgument a in NamedArguments) {
359 if (a.Name == CharSetEnumMember)
364 NamedArguments.Add (new NamedArgument (CharSetEnumMember, loc,
365 Constant.CreateConstant (rc, typeof (CharSet), RootContext.ToplevelTypes.DefaultCharSet, Location)));
368 public Report Report {
369 get { return context.Compiler.Report; }
372 public CustomAttributeBuilder Resolve ()
377 resolve_error = true;
380 ResolveAttributeType ();
385 if (Type.IsAbstract) {
386 Report.Error (653, Location, "Cannot apply attribute class `{0}' because it is abstract", GetSignatureForError ());
390 ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (Type);
391 if (obsolete_attr != null) {
392 AttributeTester.Report_ObsoleteMessage (obsolete_attr, TypeManager.CSharpName (Type), Location, Report);
395 CustomAttributeBuilder cb;
396 if (PosArguments == null && NamedArguments == null) {
397 if (att_cache.TryGetValue (Type, out cb)) {
398 resolve_error = false;
403 ResolveContext rc = new ResolveContext (context, ResolveContext.Options.ConstantScope);
404 ConstructorInfo ctor = ResolveConstructor (rc);
406 if (Type is TypeBuilder &&
407 TypeManager.LookupDeclSpace (Type).MemberCache == null)
408 // The attribute type has been DefineType'd, but not Defined. Let's not treat it as an error.
409 // It'll be resolved again when the attached-to entity is emitted.
410 resolve_error = false;
414 ApplyModuleCharSet (rc);
417 // SRE does not allow private ctor but we want to report all source code errors
421 if (NamedArguments == null) {
422 cb = new CustomAttributeBuilder (ctor, pos_values);
424 if (pos_values.Length == 0)
425 att_cache.Add (Type, cb);
427 resolve_error = false;
431 if (!ResolveNamedArguments (rc)) {
435 cb = new CustomAttributeBuilder (ctor, pos_values,
436 prop_info_arr, prop_values_arr,
437 field_info_arr, field_values_arr);
439 resolve_error = false;
443 Error_AttributeArgumentNotValid (rc, Location);
448 protected virtual ConstructorInfo ResolveConstructor (ResolveContext ec)
450 if (PosArguments != null) {
452 PosArguments.Resolve (ec, out dynamic);
454 Error_AttributeArgumentNotValid (ec, loc);
459 MethodGroupExpr mg = MemberLookupFinal (ec, ec.CurrentType,
460 Type, ConstructorInfo.ConstructorName, MemberTypes.Constructor,
461 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
462 Location) as MethodGroupExpr;
465 throw new NotImplementedException ();
467 mg = mg.OverloadResolve (ec, ref PosArguments, false, Location);
471 ConstructorInfo constructor = (ConstructorInfo)mg;
472 if (PosArguments == null) {
473 pos_values = EmptyObject;
477 AParametersCollection pd = TypeManager.GetParameterData (constructor);
479 if (!PosArguments.GetAttributableValue (ec, out pos_values))
482 // Here we do the checks which should be done by corlib or by runtime.
483 // However Zoltan doesn't like it and every Mono compiler has to do it again.
484 PredefinedAttributes pa = PredefinedAttributes.Get;
485 if (Type == pa.Guid) {
487 new Guid ((string)pos_values [0]);
489 catch (Exception e) {
490 Error_AttributeEmitError (e.Message);
495 if (Type == pa.AttributeUsage && (int)pos_values [0] == 0) {
496 ec.Report.Error (591, Location, "Invalid value for argument to `System.AttributeUsage' attribute");
500 if (Type == pa.IndexerName || Type == pa.Conditional) {
501 string v = pos_values [0] as string;
502 if (!Tokenizer.IsValidIdentifier (v) || Tokenizer.IsKeyword (v)) {
503 ec.Report.Error (633, PosArguments [0].Expr.Location,
504 "The argument to the `{0}' attribute must be a valid identifier", GetSignatureForError ());
509 if (Type == pa.MethodImpl && pos_values.Length == 1 &&
510 pd.Types [0] == TypeManager.short_type &&
511 !System.Enum.IsDefined (typeof (MethodImplOptions), pos_values [0].ToString ())) {
512 Error_AttributeEmitError ("Incorrect argument value.");
519 protected virtual bool ResolveNamedArguments (ResolveContext ec)
521 int named_arg_count = NamedArguments.Count;
523 var field_infos = new List<FieldInfo> (named_arg_count);
524 var prop_infos = new List<PropertyInfo> (named_arg_count);
525 var field_values = new List<object> (named_arg_count);
526 var prop_values = new List<object> (named_arg_count);
528 var seen_names = new List<string> (named_arg_count);
530 foreach (NamedArgument a in NamedArguments) {
531 string name = a.Name;
532 if (seen_names.Contains (name)) {
533 ec.Report.Error (643, a.Location, "Duplicate named attribute `{0}' argument", name);
537 seen_names.Add (name);
541 Expression member = Expression.MemberLookup (ec.Compiler,
542 ec.CurrentType, Type, name,
544 BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static,
547 if (member == null) {
548 member = Expression.MemberLookup (ec.Compiler, ec.CurrentType, Type, name,
549 MemberTypes.All, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static,
552 if (member != null) {
553 ec.Report.SymbolRelatedToPreviousError (member.Type);
554 Expression.ErrorIsInaccesible (Location, member.GetSignatureForError (), ec.Report);
560 Expression.Error_TypeDoesNotContainDefinition (ec, Location, Type, name);
564 if (!(member is PropertyExpr || member is FieldExpr)) {
565 Error_InvalidNamedArgument (ec, a);
569 ObsoleteAttribute obsolete_attr;
571 if (member is PropertyExpr) {
572 PropertyInfo pi = ((PropertyExpr) member).PropertyInfo;
574 if (!pi.CanWrite || !pi.CanRead || pi.GetGetMethod ().IsStatic) {
575 ec.Report.SymbolRelatedToPreviousError (pi);
576 Error_InvalidNamedArgument (ec, a);
580 if (!IsValidArgumentType (member.Type)) {
581 ec.Report.SymbolRelatedToPreviousError (pi);
582 Error_InvalidNamedArgumentType (ec, a);
587 if (!a.Expr.GetAttributableValue (ec, member.Type, out value))
590 PropertyBase pb = TypeManager.GetProperty (pi);
592 obsolete_attr = pb.GetObsoleteAttribute ();
594 obsolete_attr = AttributeTester.GetMemberObsoleteAttribute (pi);
596 prop_values.Add (value);
600 var fi = ((FieldExpr) member).Spec;
602 if (fi.IsReadOnly || fi.IsStatic) {
603 Error_InvalidNamedArgument (ec, a);
607 if (!IsValidArgumentType (member.Type)) {
608 ec.Report.SymbolRelatedToPreviousError (fi.MetaInfo);
609 Error_InvalidNamedArgumentType (ec, a);
614 if (!a.Expr.GetAttributableValue (ec, member.Type, out value))
617 FieldBase fb = TypeManager.GetField (fi.MetaInfo);
619 obsolete_attr = fb.GetObsoleteAttribute ();
621 obsolete_attr = AttributeTester.GetMemberObsoleteAttribute (fi.MetaInfo);
623 field_values.Add (value);
624 field_infos.Add (fi.MetaInfo);
627 if (obsolete_attr != null && !context.IsObsolete)
628 AttributeTester.Report_ObsoleteMessage (obsolete_attr, member.GetSignatureForError (), member.Location, Report);
631 prop_info_arr = new PropertyInfo [prop_infos.Count];
632 field_info_arr = new FieldInfo [field_infos.Count];
633 field_values_arr = new object [field_values.Count];
634 prop_values_arr = new object [prop_values.Count];
636 field_infos.CopyTo (field_info_arr, 0);
637 field_values.CopyTo (field_values_arr, 0);
639 prop_values.CopyTo (prop_values_arr, 0);
640 prop_infos.CopyTo (prop_info_arr, 0);
646 /// Get a string containing a list of valid targets for the attribute 'attr'
648 public string GetValidTargets ()
650 StringBuilder sb = new StringBuilder ();
651 AttributeTargets targets = GetAttributeUsage (Type).ValidOn;
653 if ((targets & AttributeTargets.Assembly) != 0)
654 sb.Append ("assembly, ");
656 if ((targets & AttributeTargets.Module) != 0)
657 sb.Append ("module, ");
659 if ((targets & AttributeTargets.Class) != 0)
660 sb.Append ("class, ");
662 if ((targets & AttributeTargets.Struct) != 0)
663 sb.Append ("struct, ");
665 if ((targets & AttributeTargets.Enum) != 0)
666 sb.Append ("enum, ");
668 if ((targets & AttributeTargets.Constructor) != 0)
669 sb.Append ("constructor, ");
671 if ((targets & AttributeTargets.Method) != 0)
672 sb.Append ("method, ");
674 if ((targets & AttributeTargets.Property) != 0)
675 sb.Append ("property, indexer, ");
677 if ((targets & AttributeTargets.Field) != 0)
678 sb.Append ("field, ");
680 if ((targets & AttributeTargets.Event) != 0)
681 sb.Append ("event, ");
683 if ((targets & AttributeTargets.Interface) != 0)
684 sb.Append ("interface, ");
686 if ((targets & AttributeTargets.Parameter) != 0)
687 sb.Append ("parameter, ");
689 if ((targets & AttributeTargets.Delegate) != 0)
690 sb.Append ("delegate, ");
692 if ((targets & AttributeTargets.ReturnValue) != 0)
693 sb.Append ("return, ");
695 if ((targets & AttributeTargets.GenericParameter) != 0)
696 sb.Append ("type parameter, ");
698 return sb.Remove (sb.Length - 2, 2).ToString ();
702 /// Returns AttributeUsage attribute based on types hierarchy
704 static AttributeUsageAttribute GetAttributeUsage (Type type)
706 AttributeUsageAttribute ua;
707 if (usage_attr_cache.TryGetValue (type, out ua))
710 Class attr_class = TypeManager.LookupClass (type);
711 PredefinedAttribute pa = PredefinedAttributes.Get.AttributeUsage;
713 if (attr_class == null) {
715 return new AttributeUsageAttribute (0);
717 object[] usage_attr = type.GetCustomAttributes (pa.Type, true);
718 ua = (AttributeUsageAttribute)usage_attr [0];
719 usage_attr_cache.Add (type, ua);
724 if (attr_class.OptAttributes != null)
725 a = attr_class.OptAttributes.Search (pa);
728 if (attr_class.TypeBuilder.BaseType != TypeManager.attribute_type)
729 ua = GetAttributeUsage (attr_class.TypeBuilder.BaseType);
731 ua = DefaultUsageAttribute;
733 ua = a.GetAttributeUsageAttribute ();
736 usage_attr_cache.Add (type, ua);
740 AttributeUsageAttribute GetAttributeUsageAttribute ()
742 if (pos_values == null)
743 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
744 // But because a lot of attribute class code must be rewritten will be better to wait...
748 return DefaultUsageAttribute;
750 AttributeUsageAttribute usage_attribute = new AttributeUsageAttribute ((AttributeTargets)pos_values [0]);
752 object field = GetPropertyValue ("AllowMultiple");
754 usage_attribute.AllowMultiple = (bool)field;
756 field = GetPropertyValue ("Inherited");
758 usage_attribute.Inherited = (bool)field;
760 return usage_attribute;
764 /// Returns custom name of indexer
766 public string GetIndexerAttributeValue ()
768 if (pos_values == null)
769 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
770 // But because a lot of attribute class code must be rewritten will be better to wait...
776 return pos_values [0] as string;
780 /// Returns condition of ConditionalAttribute
782 public string GetConditionalAttributeValue ()
784 if (pos_values == null)
785 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
786 // But because a lot of attribute class code must be rewritten will be better to wait...
792 return (string)pos_values [0];
796 /// Creates the instance of ObsoleteAttribute from this attribute instance
798 public ObsoleteAttribute GetObsoleteAttribute ()
800 if (pos_values == null)
801 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
802 // But because a lot of attribute class code must be rewritten will be better to wait...
808 if (pos_values == null || pos_values.Length == 0)
809 return new ObsoleteAttribute ();
811 if (pos_values.Length == 1)
812 return new ObsoleteAttribute ((string)pos_values [0]);
814 return new ObsoleteAttribute ((string)pos_values [0], (bool)pos_values [1]);
818 /// Returns value of CLSCompliantAttribute contructor parameter but because the method can be called
819 /// before ApplyAttribute. We need to resolve the arguments.
820 /// This situation occurs when class deps is differs from Emit order.
822 public bool GetClsCompliantAttributeValue ()
824 if (pos_values == null)
825 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
826 // But because a lot of attribute class code must be rewritten will be better to wait...
832 return (bool)pos_values [0];
835 public Type GetCoClassAttributeValue ()
837 if (pos_values == null)
843 return (Type)pos_values [0];
846 public bool CheckTarget ()
848 string[] valid_targets = Owner.ValidAttributeTargets;
849 if (ExplicitTarget == null || ExplicitTarget == valid_targets [0]) {
850 Target = Owner.AttributeTargets;
854 // TODO: we can skip the first item
855 if (Array.Exists (valid_targets, i => i == ExplicitTarget)) {
856 switch (ExplicitTarget) {
857 case "return": Target = AttributeTargets.ReturnValue; return true;
858 case "param": Target = AttributeTargets.Parameter; return true;
859 case "field": Target = AttributeTargets.Field; return true;
860 case "method": Target = AttributeTargets.Method; return true;
861 case "property": Target = AttributeTargets.Property; return true;
863 throw new InternalErrorException ("Unknown explicit target: " + ExplicitTarget);
866 StringBuilder sb = new StringBuilder ();
867 foreach (string s in valid_targets) {
871 sb.Remove (sb.Length - 2, 2);
872 Report.Error (657, Location, "`{0}' is not a valid attribute location for this declaration. " +
873 "Valid attribute locations for this declaration are `{1}'", ExplicitTarget, sb.ToString ());
878 /// Tests permitted SecurityAction for assembly or other types
880 protected virtual bool IsSecurityActionValid (bool for_assembly)
882 SecurityAction action = GetSecurityActionValue ();
885 case SecurityAction.Demand:
886 case SecurityAction.Assert:
887 case SecurityAction.Deny:
888 case SecurityAction.PermitOnly:
889 case SecurityAction.LinkDemand:
890 case SecurityAction.InheritanceDemand:
895 case SecurityAction.RequestMinimum:
896 case SecurityAction.RequestOptional:
897 case SecurityAction.RequestRefuse:
903 Error_AttributeEmitError ("SecurityAction is out of range");
907 Error_AttributeEmitError (String.Concat ("SecurityAction `", action, "' is not valid for this declaration"));
911 System.Security.Permissions.SecurityAction GetSecurityActionValue ()
913 return (SecurityAction)pos_values [0];
917 /// Creates instance of SecurityAttribute class and add result of CreatePermission method to permission table.
919 /// <returns></returns>
920 public void ExtractSecurityPermissionSet (Dictionary<SecurityAction, PermissionSet> permissions)
922 Type orig_assembly_type = null;
924 if (TypeManager.LookupDeclSpace (Type) != null) {
925 if (!RootContext.StdLib) {
926 orig_assembly_type = Type.GetType (Type.FullName);
928 string orig_version_path = Environment.GetEnvironmentVariable ("__SECURITY_BOOTSTRAP_DB");
929 if (orig_version_path == null) {
930 Error_AttributeEmitError ("security custom attributes can not be referenced from defining assembly");
934 if (orig_sec_assembly == null) {
935 string file = Path.Combine (orig_version_path, Driver.OutputFile);
936 orig_sec_assembly = Assembly.LoadFile (file);
939 orig_assembly_type = orig_sec_assembly.GetType (Type.FullName, true);
940 if (orig_assembly_type == null) {
941 Report.Warning (-112, 1, Location, "Self-referenced security attribute `{0}' " +
942 "was not found in previous version of assembly");
948 SecurityAttribute sa;
949 // For all non-selfreferencing security attributes we can avoid all hacks
950 if (orig_assembly_type == null) {
951 sa = (SecurityAttribute) Activator.CreateInstance (Type, pos_values);
953 if (prop_info_arr != null) {
954 for (int i = 0; i < prop_info_arr.Length; ++i) {
955 PropertyInfo pi = prop_info_arr [i];
956 pi.SetValue (sa, prop_values_arr [i], null);
960 // HACK: All security attributes have same ctor syntax
961 sa = (SecurityAttribute) Activator.CreateInstance (orig_assembly_type, new object[] { GetSecurityActionValue () } );
963 // All types are from newly created assembly but for invocation with old one we need to convert them
964 if (prop_info_arr != null) {
965 for (int i = 0; i < prop_info_arr.Length; ++i) {
966 PropertyInfo emited_pi = prop_info_arr [i];
967 // FIXME: We are missing return type filter
968 // TODO: pi can be null
969 PropertyInfo pi = orig_assembly_type.GetProperty (emited_pi.Name);
971 object old_instance = TypeManager.IsEnumType (pi.PropertyType) ?
972 System.Enum.ToObject (pi.PropertyType, prop_values_arr [i]) :
975 pi.SetValue (sa, old_instance, null);
981 perm = sa.CreatePermission ();
982 SecurityAction action = GetSecurityActionValue ();
984 // IS is correct because for corlib we are using an instance from old corlib
985 if (!(perm is System.Security.CodeAccessPermission)) {
987 case SecurityAction.Demand:
988 action = (SecurityAction)13;
990 case SecurityAction.LinkDemand:
991 action = (SecurityAction)14;
993 case SecurityAction.InheritanceDemand:
994 action = (SecurityAction)15;
1000 if (!permissions.TryGetValue (action, out ps)) {
1001 if (sa is PermissionSetAttribute)
1002 ps = new PermissionSet (sa.Unrestricted ? PermissionState.Unrestricted : PermissionState.None);
1004 ps = new PermissionSet (PermissionState.None);
1006 permissions.Add (action, ps);
1007 } else if (!ps.IsUnrestricted () && (sa is PermissionSetAttribute) && sa.Unrestricted) {
1008 ps = ps.Union (new PermissionSet (PermissionState.Unrestricted));
1009 permissions [action] = ps;
1011 ps.AddPermission (perm);
1014 public object GetPropertyValue (string name)
1016 if (prop_info_arr == null)
1019 for (int i = 0; i < prop_info_arr.Length; ++i) {
1020 if (prop_info_arr [i].Name == name)
1021 return prop_values_arr [i];
1028 // Theoretically, we can get rid of this, since FieldBuilder.SetCustomAttribute()
1029 // and ParameterBuilder.SetCustomAttribute() are supposed to handle this attribute.
1030 // However, we can't, since it appears that the .NET 1.1 SRE hangs when given a MarshalAsAttribute.
1033 public UnmanagedMarshal GetMarshal (Attributable attr)
1035 UnmanagedType UnmanagedType;
1036 if (!RootContext.StdLib || pos_values [0].GetType () != typeof (UnmanagedType))
1037 UnmanagedType = (UnmanagedType) System.Enum.ToObject (typeof (UnmanagedType), pos_values [0]);
1039 UnmanagedType = (UnmanagedType) pos_values [0];
1041 object value = GetFieldValue ("SizeParamIndex");
1042 if (value != null && UnmanagedType != UnmanagedType.LPArray) {
1043 Error_AttributeEmitError ("SizeParamIndex field is not valid for the specified unmanaged type");
1047 object o = GetFieldValue ("ArraySubType");
1048 UnmanagedType array_sub_type = o == null ? (UnmanagedType) 0x50 /* NATIVE_MAX */ : (UnmanagedType) o;
1050 switch (UnmanagedType) {
1051 case UnmanagedType.CustomMarshaler: {
1052 MethodInfo define_custom = typeof (UnmanagedMarshal).GetMethod ("DefineCustom",
1053 BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
1054 if (define_custom == null) {
1055 Report.RuntimeMissingSupport (Location, "set marshal info");
1059 object [] args = new object [4];
1060 args [0] = GetFieldValue ("MarshalTypeRef");
1061 args [1] = GetFieldValue ("MarshalCookie");
1062 args [2] = GetFieldValue ("MarshalType");
1063 args [3] = Guid.Empty;
1064 return (UnmanagedMarshal) define_custom.Invoke (null, args);
1066 case UnmanagedType.LPArray: {
1067 object size_const = GetFieldValue ("SizeConst");
1068 object size_param_index = GetFieldValue ("SizeParamIndex");
1070 if ((size_const != null) || (size_param_index != null)) {
1071 MethodInfo define_array = typeof (UnmanagedMarshal).GetMethod ("DefineLPArrayInternal",
1072 BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
1073 if (define_array == null) {
1074 Report.RuntimeMissingSupport (Location, "set marshal info");
1078 object [] args = new object [3];
1079 args [0] = array_sub_type;
1080 args [1] = size_const == null ? -1 : size_const;
1081 args [2] = size_param_index == null ? -1 : size_param_index;
1082 return (UnmanagedMarshal) define_array.Invoke (null, args);
1085 return UnmanagedMarshal.DefineLPArray (array_sub_type);
1087 case UnmanagedType.SafeArray:
1088 return UnmanagedMarshal.DefineSafeArray (array_sub_type);
1090 case UnmanagedType.ByValArray:
1091 FieldBase fm = attr as FieldBase;
1093 Error_AttributeEmitError ("Specified unmanaged type is only valid on fields");
1096 return UnmanagedMarshal.DefineByValArray ((int) GetFieldValue ("SizeConst"));
1098 case UnmanagedType.ByValTStr:
1099 return UnmanagedMarshal.DefineByValTStr ((int) GetFieldValue ("SizeConst"));
1102 return UnmanagedMarshal.DefineUnmanagedMarshal (UnmanagedType);
1106 object GetFieldValue (string name)
1109 if (field_info_arr == null)
1112 foreach (FieldInfo fi in field_info_arr) {
1113 if (fi.Name == name)
1114 return GetValue (field_values_arr [i]);
1120 static object GetValue (object value)
1122 if (value is EnumConstant)
1123 return ((EnumConstant) value).GetValue ();
1130 public CharSet GetCharSetValue ()
1132 return (CharSet)System.Enum.Parse (typeof (CharSet), pos_values [0].ToString ());
1135 public bool HasField (string fieldName)
1137 if (field_info_arr == null)
1140 foreach (FieldInfo fi in field_info_arr) {
1141 if (fi.Name == fieldName)
1148 public bool IsInternalMethodImplAttribute {
1150 if (Type != PredefinedAttributes.Get.MethodImpl)
1153 MethodImplOptions options;
1154 if (pos_values[0].GetType () != typeof (MethodImplOptions))
1155 options = (MethodImplOptions)System.Enum.ToObject (typeof (MethodImplOptions), pos_values[0]);
1157 options = (MethodImplOptions)pos_values[0];
1159 return (options & MethodImplOptions.InternalCall) != 0;
1163 public LayoutKind GetLayoutKindValue ()
1165 if (!RootContext.StdLib || pos_values [0].GetType () != typeof (LayoutKind))
1166 return (LayoutKind)System.Enum.ToObject (typeof (LayoutKind), pos_values [0]);
1168 return (LayoutKind)pos_values [0];
1171 public object GetParameterDefaultValue ()
1173 return pos_values [0];
1176 public override bool Equals (object obj)
1178 Attribute a = obj as Attribute;
1182 return Type == a.Type && Target == a.Target;
1185 public override int GetHashCode ()
1187 return type.GetHashCode () ^ Target.GetHashCode ();
1191 /// Emit attribute for Attributable symbol
1193 public void Emit (Dictionary<Attribute, List<Attribute>> allEmitted)
1195 CustomAttributeBuilder cb = Resolve ();
1199 AttributeUsageAttribute usage_attr = GetAttributeUsage (Type);
1200 if ((usage_attr.ValidOn & Target) == 0) {
1201 Report.Error (592, Location, "The attribute `{0}' is not valid on this declaration type. " +
1202 "It is valid on `{1}' declarations only",
1203 GetSignatureForError (), GetValidTargets ());
1207 var predefined = PredefinedAttributes.Get;
1210 foreach (Attributable target in targets)
1211 target.ApplyAttributeBuilder (this, cb, predefined);
1212 } catch (Exception e) {
1213 Error_AttributeEmitError (e.Message);
1217 if (!usage_attr.AllowMultiple && allEmitted != null) {
1218 if (allEmitted.ContainsKey (this)) {
1219 var a = allEmitted [this];
1221 a = new List<Attribute> (2);
1222 allEmitted [this] = a;
1226 allEmitted.Add (this, null);
1230 if (!RootContext.VerifyClsCompliance)
1233 // Here we are testing attribute arguments for array usage (error 3016)
1234 if (Owner.IsClsComplianceRequired ()) {
1235 if (PosArguments != null)
1236 PosArguments.CheckArrayAsAttribute (context.Compiler);
1238 if (NamedArguments == null)
1241 NamedArguments.CheckArrayAsAttribute (context.Compiler);
1245 private Expression GetValue ()
1247 if (PosArguments == null || PosArguments.Count < 1)
1250 return PosArguments [0].Expr;
1253 public string GetString ()
1255 Expression e = GetValue ();
1256 if (e is StringConstant)
1257 return ((StringConstant)e).Value;
1261 public bool GetBoolean ()
1263 Expression e = GetValue ();
1264 if (e is BoolConstant)
1265 return ((BoolConstant)e).Value;
1269 public Type GetArgumentType ()
1271 TypeOf e = GetValue () as TypeOf;
1274 return e.TypeArgument;
1277 public override Expression CreateExpressionTree (ResolveContext ec)
1279 throw new NotSupportedException ("ET");
1282 protected override Expression DoResolve (ResolveContext ec)
1284 throw new NotImplementedException ();
1287 public override void Emit (EmitContext ec)
1289 throw new NotImplementedException ();
1295 /// For global attributes (assembly, module) we need special handling.
1296 /// Attributes can be located in the several files
1298 public class GlobalAttribute : Attribute
1300 public readonly NamespaceEntry ns;
1302 public GlobalAttribute (NamespaceEntry ns, string target, ATypeNameExpression expression,
1303 Arguments[] args, Location loc, bool nameEscaped):
1304 base (target, expression, args, loc, nameEscaped)
1309 public override void AttachTo (Attributable target, IMemberContext context)
1311 if (ExplicitTarget == "assembly") {
1312 base.AttachTo (CodeGen.Assembly, context);
1316 if (ExplicitTarget == "module") {
1317 base.AttachTo (RootContext.ToplevelTypes, context);
1321 throw new NotImplementedException ("Unknown global explicit target " + ExplicitTarget);
1326 // RootContext.ToplevelTypes has a single NamespaceEntry which gets overwritten
1327 // each time a new file is parsed. However, we need to use the NamespaceEntry
1328 // in effect where the attribute was used. Since code elsewhere cannot assume
1329 // that the NamespaceEntry is right, just overwrite it.
1331 // Precondition: RootContext.ToplevelTypes == null
1333 if (RootContext.ToplevelTypes.NamespaceEntry != null)
1334 throw new InternalErrorException (Location + " non-null NamespaceEntry");
1336 RootContext.ToplevelTypes.NamespaceEntry = ns;
1339 protected override bool IsSecurityActionValid (bool for_assembly)
1341 return base.IsSecurityActionValid (true);
1346 RootContext.ToplevelTypes.NamespaceEntry = null;
1349 protected override TypeExpr ResolveAsTypeTerminal (Expression expr, IMemberContext ec)
1353 return base.ResolveAsTypeTerminal (expr, ec);
1360 protected override ConstructorInfo ResolveConstructor (ResolveContext ec)
1364 return base.ResolveConstructor (ec);
1371 protected override bool ResolveNamedArguments (ResolveContext ec)
1375 return base.ResolveNamedArguments (ec);
1383 public class Attributes {
1384 public readonly List<Attribute> Attrs;
1386 public Attributes (Attribute a)
1388 Attrs = new List<Attribute> ();
1392 public Attributes (List<Attribute> attrs)
1397 public void AddAttributes (List<Attribute> attrs)
1399 Attrs.AddRange (attrs);
1402 public void AttachTo (Attributable attributable, IMemberContext context)
1404 foreach (Attribute a in Attrs)
1405 a.AttachTo (attributable, context);
1408 public Attributes Clone ()
1410 var al = new List<Attribute> (Attrs.Count);
1411 foreach (Attribute a in Attrs)
1412 al.Add (a.Clone ());
1414 return new Attributes (al);
1418 /// Checks whether attribute target is valid for the current element
1420 public bool CheckTargets ()
1422 foreach (Attribute a in Attrs) {
1423 if (!a.CheckTarget ())
1429 public Attribute Search (PredefinedAttribute t)
1431 foreach (Attribute a in Attrs) {
1432 if (a.ResolveType () == t)
1439 /// Returns all attributes of type 't'. Use it when attribute is AllowMultiple = true
1441 public Attribute[] SearchMulti (PredefinedAttribute t)
1443 List<Attribute> ar = null;
1445 foreach (Attribute a in Attrs) {
1446 if (a.ResolveType () == t) {
1448 ar = new List<Attribute> (Attrs.Count);
1453 return ar == null ? null : ar.ToArray ();
1460 Dictionary<Attribute, List<Attribute>> ld = Attrs.Count > 1 ? new Dictionary<Attribute, List<Attribute>> () : null;
1462 foreach (Attribute a in Attrs)
1465 if (ld == null || ld.Count == 0)
1468 foreach (var d in ld) {
1469 if (d.Value == null)
1472 Attribute a = d.Key;
1474 foreach (Attribute collision in d.Value)
1475 a.Report.SymbolRelatedToPreviousError (collision.Location, "");
1477 a.Report.Error (579, a.Location, "The attribute `{0}' cannot be applied multiple times",
1478 a.GetSignatureForError ());
1482 public bool Contains (PredefinedAttribute t)
1484 return Search (t) != null;
1489 /// Helper class for attribute verification routine.
1491 sealed class AttributeTester
1493 static Dictionary<Type, bool> analyzed_types;
1494 static Dictionary<Type, ObsoleteAttribute> analyzed_types_obsolete;
1495 static Dictionary<MemberInfo, ObsoleteAttribute> analyzed_member_obsolete;
1496 static Dictionary<MethodBase, bool> analyzed_method_excluded;
1497 // static Dictionary<FieldInfo, IFixedBuffer> fixed_buffer_cache;
1499 static AttributeTester ()
1504 private AttributeTester ()
1508 public static void Reset ()
1510 analyzed_types = new Dictionary<Type, bool> (ReferenceEquality<Type>.Default);
1511 analyzed_types_obsolete = new Dictionary<Type, ObsoleteAttribute> (ReferenceEquality<Type>.Default);
1512 analyzed_member_obsolete = new Dictionary<MemberInfo, ObsoleteAttribute> (ReferenceEquality<MemberInfo>.Default);
1513 analyzed_method_excluded = new Dictionary<MethodBase, bool> (ReferenceEquality<MethodBase>.Default);
1514 // fixed_buffer_cache = new Dictionary<FieldInfo, IFixedBuffer> (ReferenceEquality<FieldInfo>.Default);
1517 public enum Result {
1524 /// Returns true if parameters of two compared methods are CLS-Compliant.
1525 /// It tests differing only in ref or out, or in array rank.
1527 public static Result AreOverloadedMethodParamsClsCompliant (AParametersCollection pa, AParametersCollection pb)
1529 Type [] types_a = pa.Types;
1530 Type [] types_b = pb.Types;
1531 if (types_a == null || types_b == null)
1534 if (types_a.Length != types_b.Length)
1537 Result result = Result.Ok;
1538 for (int i = 0; i < types_b.Length; ++i) {
1539 Type aType = types_a [i];
1540 Type bType = types_b [i];
1542 if (aType.IsArray && bType.IsArray) {
1543 Type a_el_type = TypeManager.GetElementType (aType);
1544 Type b_el_type = TypeManager.GetElementType (bType);
1545 if (aType.GetArrayRank () != bType.GetArrayRank () && a_el_type == b_el_type) {
1546 result = Result.RefOutArrayError;
1550 if (a_el_type.IsArray || b_el_type.IsArray) {
1551 result = Result.ArrayArrayError;
1559 const Parameter.Modifier out_ref_mod = (Parameter.Modifier.OUTMASK | Parameter.Modifier.REFMASK);
1560 if ((pa.FixedParameters[i].ModFlags & out_ref_mod) != (pb.FixedParameters[i].ModFlags & out_ref_mod))
1561 result = Result.RefOutArrayError;
1567 /// This method tests the CLS compliance of external types. It doesn't test type visibility.
1569 public static bool IsClsCompliant (Type type)
1575 if (analyzed_types.TryGetValue (type, out result))
1578 if (type.IsPointer) {
1579 analyzed_types.Add (type, false);
1584 result = IsClsCompliant (TypeManager.GetElementType (type));
1585 } else if (TypeManager.IsNullableType (type)) {
1586 result = IsClsCompliant (TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (type) [0]));
1588 result = AnalyzeTypeCompliance (type);
1590 analyzed_types.Add (type, result);
1594 public static void VerifyModulesClsCompliance (CompilerContext ctx)
1596 Module[] modules = GlobalRootNamespace.Instance.Modules;
1597 if (modules == null)
1600 // The first module is generated assembly
1601 for (int i = 1; i < modules.Length; ++i) {
1602 Module module = modules [i];
1603 if (!GetClsCompliantAttributeValue (module, null)) {
1604 ctx.Report.Error (3013, "Added modules must be marked with the CLSCompliant attribute " +
1605 "to match the assembly", module.Name);
1611 public static Type GetImportedIgnoreCaseClsType (string name)
1613 foreach (Assembly a in GlobalRootNamespace.Instance.Assemblies) {
1614 Type t = a.GetType (name, false, true);
1618 if (IsClsCompliant (t))
1624 static bool GetClsCompliantAttributeValue (ICustomAttributeProvider attribute_provider, Assembly a)
1626 PredefinedAttribute pa = PredefinedAttributes.Get.CLSCompliant;
1630 object[] cls_attr = attribute_provider.GetCustomAttributes (pa.Type, false);
1631 if (cls_attr.Length == 0) {
1635 return GetClsCompliantAttributeValue (a, null);
1638 return ((CLSCompliantAttribute)cls_attr [0]).IsCompliant;
1641 static bool AnalyzeTypeCompliance (Type type)
1643 type = TypeManager.DropGenericTypeArguments (type);
1644 DeclSpace ds = TypeManager.LookupDeclSpace (type);
1646 return ds.IsClsComplianceRequired ();
1649 if (TypeManager.IsGenericParameter (type))
1652 return GetClsCompliantAttributeValue (type, type.Assembly);
1656 /// Returns instance of ObsoleteAttribute when type is obsolete
1658 public static ObsoleteAttribute GetObsoleteAttribute (Type type)
1660 ObsoleteAttribute result;
1661 if (analyzed_types_obsolete.TryGetValue (type, out result))
1664 if (TypeManager.HasElementType (type)) {
1665 result = GetObsoleteAttribute (TypeManager.GetElementType (type));
1666 } else if (TypeManager.IsGenericParameter (type))
1667 result = null; // TODO: throw new NotSupportedException ()
1668 else if (TypeManager.IsGenericType (type) && !TypeManager.IsGenericTypeDefinition (type)) {
1669 return GetObsoleteAttribute (TypeManager.DropGenericTypeArguments (type));
1671 DeclSpace type_ds = TypeManager.LookupDeclSpace (type);
1673 // Type is external, we can get attribute directly
1674 if (type_ds == null) {
1675 PredefinedAttribute pa = PredefinedAttributes.Get.Obsolete;
1677 object[] attribute = type.GetCustomAttributes (pa.Type, false);
1678 if (attribute.Length == 1)
1679 result = (ObsoleteAttribute) attribute[0];
1682 result = type_ds.GetObsoleteAttribute ();
1686 // Cannot use .Add because of corlib bootstrap
1687 analyzed_types_obsolete [type] = result;
1692 /// Returns instance of ObsoleteAttribute when method is obsolete
1694 public static ObsoleteAttribute GetMethodObsoleteAttribute (MethodBase mb)
1696 IMethodData mc = TypeManager.GetMethod (mb);
1698 return mc.GetObsoleteAttribute ();
1700 // compiler generated methods are not registered by AddMethod
1701 if (mb.DeclaringType is TypeBuilder)
1704 MemberInfo mi = TypeManager.GetPropertyFromAccessor (mb);
1706 return GetMemberObsoleteAttribute (mi);
1708 mi = TypeManager.GetEventFromAccessor (mb);
1710 return GetMemberObsoleteAttribute (mi);
1712 return GetMemberObsoleteAttribute (mb);
1716 /// Returns instance of ObsoleteAttribute when member is obsolete
1718 public static ObsoleteAttribute GetMemberObsoleteAttribute (MemberInfo mi)
1720 ObsoleteAttribute oa;
1721 if (analyzed_member_obsolete.TryGetValue (mi, out oa))
1724 if ((mi.DeclaringType is TypeBuilder) || TypeManager.IsGenericType (mi.DeclaringType))
1727 PredefinedAttribute pa = PredefinedAttributes.Get.Obsolete;
1731 oa = System.Attribute.GetCustomAttribute (mi, pa.Type, false) as ObsoleteAttribute;
1732 analyzed_member_obsolete.Add (mi, oa);
1737 /// Common method for Obsolete error/warning reporting.
1739 public static void Report_ObsoleteMessage (ObsoleteAttribute oa, string member, Location loc, Report Report)
1742 Report.Error (619, loc, "`{0}' is obsolete: `{1}'", member, oa.Message);
1746 if (oa.Message == null || oa.Message.Length == 0) {
1747 Report.Warning (612, 1, loc, "`{0}' is obsolete", member);
1750 Report.Warning (618, 2, loc, "`{0}' is obsolete: `{1}'", member, oa.Message);
1753 public static bool IsConditionalMethodExcluded (MethodBase mb, Location loc)
1756 if (analyzed_method_excluded.TryGetValue (mb, out excluded))
1759 PredefinedAttribute pa = PredefinedAttributes.Get.Conditional;
1763 ConditionalAttribute[] attrs = mb.GetCustomAttributes (pa.Type, true)
1764 as ConditionalAttribute[];
1765 if (attrs.Length == 0) {
1766 analyzed_method_excluded.Add (mb, false);
1770 foreach (ConditionalAttribute a in attrs) {
1771 if (loc.CompilationUnit.IsConditionalDefined (a.ConditionString)) {
1772 analyzed_method_excluded.Add (mb, false);
1777 analyzed_method_excluded.Add (mb, true);
1782 /// Analyzes class whether it has attribute which has ConditionalAttribute
1783 /// and its condition is not defined.
1785 public static bool IsAttributeExcluded (Type type, Location loc)
1790 Class class_decl = TypeManager.LookupDeclSpace (type) as Class;
1792 // TODO: add caching
1793 // TODO: merge all Type bases attribute caching to one cache to save memory
1794 PredefinedAttribute pa = PredefinedAttributes.Get.Conditional;
1795 if (class_decl == null && pa.IsDefined) {
1796 object[] attributes = type.GetCustomAttributes (pa.Type, false);
1797 foreach (ConditionalAttribute ca in attributes) {
1798 if (loc.CompilationUnit.IsConditionalDefined (ca.ConditionString))
1801 return attributes.Length > 0;
1804 return class_decl.IsExcluded ();
1807 public static Type GetCoClassAttribute (Type type)
1809 TypeContainer tc = TypeManager.LookupInterface (type);
1810 PredefinedAttribute pa = PredefinedAttributes.Get.CoClass;
1815 object[] o = type.GetCustomAttributes (pa.Type, false);
1818 return ((System.Runtime.InteropServices.CoClassAttribute)o[0]).CoClass;
1821 if (tc.OptAttributes == null)
1824 Attribute a = tc.OptAttributes.Search (pa);
1828 return a.GetCoClassAttributeValue ();
1832 public class PredefinedAttributes
1835 public readonly PredefinedAttribute ParamArray;
1836 public readonly PredefinedAttribute Out;
1839 public readonly PredefinedAttribute Obsolete;
1840 public readonly PredefinedAttribute DllImport;
1841 public readonly PredefinedAttribute MethodImpl;
1842 public readonly PredefinedAttribute MarshalAs;
1843 public readonly PredefinedAttribute In;
1844 public readonly PredefinedAttribute IndexerName;
1845 public readonly PredefinedAttribute Conditional;
1846 public readonly PredefinedAttribute CLSCompliant;
1847 public readonly PredefinedAttribute Security;
1848 public readonly PredefinedAttribute Required;
1849 public readonly PredefinedAttribute Guid;
1850 public readonly PredefinedAttribute AssemblyCulture;
1851 public readonly PredefinedAttribute AssemblyVersion;
1852 public readonly PredefinedAttribute ComImport;
1853 public readonly PredefinedAttribute CoClass;
1854 public readonly PredefinedAttribute AttributeUsage;
1855 public readonly PredefinedAttribute DefaultParameterValue;
1856 public readonly PredefinedAttribute OptionalParameter;
1859 public readonly PredefinedAttribute DefaultCharset;
1860 public readonly PredefinedAttribute TypeForwarder;
1861 public readonly PredefinedAttribute FixedBuffer;
1862 public readonly PredefinedAttribute CompilerGenerated;
1863 public readonly PredefinedAttribute InternalsVisibleTo;
1864 public readonly PredefinedAttribute RuntimeCompatibility;
1865 public readonly PredefinedAttribute DebuggerHidden;
1866 public readonly PredefinedAttribute UnsafeValueType;
1869 public readonly PredefinedAttribute Extension;
1872 public readonly PredefinedAttribute Dynamic;
1873 public readonly PredefinedAttribute DynamicTransform; // DynamicAttribute with transform arguments
1876 // Optional types which are used as types and for member lookup
1878 public readonly PredefinedAttribute DefaultMember;
1879 public readonly PredefinedAttribute DecimalConstant;
1880 public readonly PredefinedAttribute StructLayout;
1881 public readonly PredefinedAttribute FieldOffset;
1883 public static PredefinedAttributes Get = new PredefinedAttributes ();
1885 private PredefinedAttributes ()
1887 ParamArray = new PredefinedAttribute ("System", "ParamArrayAttribute");
1888 Out = new PredefinedAttribute ("System.Runtime.InteropServices", "OutAttribute");
1890 Obsolete = new PredefinedAttribute ("System", "ObsoleteAttribute");
1891 DllImport = new PredefinedAttribute ("System.Runtime.InteropServices", "DllImportAttribute");
1892 MethodImpl = new PredefinedAttribute ("System.Runtime.CompilerServices", "MethodImplAttribute");
1893 MarshalAs = new PredefinedAttribute ("System.Runtime.InteropServices", "MarshalAsAttribute");
1894 In = new PredefinedAttribute ("System.Runtime.InteropServices", "InAttribute");
1895 IndexerName = new PredefinedAttribute ("System.Runtime.CompilerServices", "IndexerNameAttribute");
1896 Conditional = new PredefinedAttribute ("System.Diagnostics", "ConditionalAttribute");
1897 CLSCompliant = new PredefinedAttribute ("System", "CLSCompliantAttribute");
1898 Security = new PredefinedAttribute ("System.Security.Permissions", "SecurityAttribute");
1899 Required = new PredefinedAttribute ("System.Runtime.CompilerServices", "RequiredAttributeAttribute");
1900 Guid = new PredefinedAttribute ("System.Runtime.InteropServices", "GuidAttribute");
1901 AssemblyCulture = new PredefinedAttribute ("System.Reflection", "AssemblyCultureAttribute");
1902 AssemblyVersion = new PredefinedAttribute ("System.Reflection", "AssemblyVersionAttribute");
1903 ComImport = new PredefinedAttribute ("System.Runtime.InteropServices", "ComImportAttribute");
1904 CoClass = new PredefinedAttribute ("System.Runtime.InteropServices", "CoClassAttribute");
1905 AttributeUsage = new PredefinedAttribute ("System", "AttributeUsageAttribute");
1906 DefaultParameterValue = new PredefinedAttribute ("System.Runtime.InteropServices", "DefaultParameterValueAttribute");
1907 OptionalParameter = new PredefinedAttribute ("System.Runtime.InteropServices", "OptionalAttribute");
1909 DefaultCharset = new PredefinedAttribute ("System.Runtime.InteropServices", "DefaultCharSetAttribute");
1910 TypeForwarder = new PredefinedAttribute ("System.Runtime.CompilerServices", "TypeForwardedToAttribute");
1911 FixedBuffer = new PredefinedAttribute ("System.Runtime.CompilerServices", "FixedBufferAttribute");
1912 CompilerGenerated = new PredefinedAttribute ("System.Runtime.CompilerServices", "CompilerGeneratedAttribute");
1913 InternalsVisibleTo = new PredefinedAttribute ("System.Runtime.CompilerServices", "InternalsVisibleToAttribute");
1914 RuntimeCompatibility = new PredefinedAttribute ("System.Runtime.CompilerServices", "RuntimeCompatibilityAttribute");
1915 DebuggerHidden = new PredefinedAttribute ("System.Diagnostics", "DebuggerHiddenAttribute");
1916 UnsafeValueType = new PredefinedAttribute ("System.Runtime.CompilerServices", "UnsafeValueTypeAttribute");
1918 Extension = new PredefinedAttribute ("System.Runtime.CompilerServices", "ExtensionAttribute");
1920 Dynamic = new PredefinedAttribute ("System.Runtime.CompilerServices", "DynamicAttribute");
1921 DynamicTransform = new PredefinedAttribute ("System.Runtime.CompilerServices", "DynamicAttribute");
1923 DefaultMember = new PredefinedAttribute ("System.Reflection", "DefaultMemberAttribute");
1924 DecimalConstant = new PredefinedAttribute ("System.Runtime.CompilerServices", "DecimalConstantAttribute");
1925 StructLayout = new PredefinedAttribute ("System.Runtime.InteropServices", "StructLayoutAttribute");
1926 FieldOffset = new PredefinedAttribute ("System.Runtime.InteropServices", "FieldOffsetAttribute");
1929 public void Initialize (CompilerContext ctx)
1931 foreach (FieldInfo fi in GetType ().GetFields (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) {
1932 ((PredefinedAttribute) fi.GetValue (this)).Initialize (ctx, true);
1936 public static void Reset ()
1938 Get = new PredefinedAttributes ();
1942 public class PredefinedAttribute
1945 CustomAttributeBuilder cab;
1946 ConstructorInfo ctor;
1947 readonly string ns, name;
1948 CompilerContext compiler;
1950 static readonly Type NotFound = typeof (PredefinedAttribute);
1952 public PredefinedAttribute (string ns, string name)
1958 public static bool operator == (Type type, PredefinedAttribute pa)
1960 return type == pa.type;
1963 public static bool operator != (Type type, PredefinedAttribute pa)
1965 return type != pa.type;
1968 public ConstructorInfo Constructor {
1969 get { return ctor; }
1972 public override int GetHashCode ()
1974 return base.GetHashCode ();
1977 public string GetSignatureForError ()
1979 return ns + "." + name;
1982 public override bool Equals (object obj)
1984 throw new NotSupportedException ();
1987 public void EmitAttribute (ConstructorBuilder builder)
1989 if (ResolveBuilder ())
1990 builder.SetCustomAttribute (cab);
1993 public void EmitAttribute (MethodBuilder builder)
1995 if (ResolveBuilder ())
1996 builder.SetCustomAttribute (cab);
1999 public void EmitAttribute (PropertyBuilder builder)
2001 if (ResolveBuilder ())
2002 builder.SetCustomAttribute (cab);
2005 public void EmitAttribute (FieldBuilder builder)
2007 if (ResolveBuilder ())
2008 builder.SetCustomAttribute (cab);
2011 public void EmitAttribute (TypeBuilder builder)
2013 if (ResolveBuilder ())
2014 builder.SetCustomAttribute (cab);
2017 public void EmitAttribute (AssemblyBuilder builder)
2019 if (ResolveBuilder ())
2020 builder.SetCustomAttribute (cab);
2023 public void EmitAttribute (ParameterBuilder builder)
2025 if (ResolveBuilder ())
2026 builder.SetCustomAttribute (cab);
2029 public bool IsDefined {
2030 get { return type != null && type != NotFound; }
2033 public void Initialize (CompilerContext ctx, bool canFail)
2035 this.compiler = ctx;
2039 public bool Resolve (bool canFail)
2048 type = TypeManager.CoreLookupType (compiler, ns, name, Kind.Class, !canFail);
2057 bool ResolveBuilder ()
2063 // Handle all parameter-less attributes as optional
2065 if (!Resolve (true))
2068 ConstructorInfo ci = TypeManager.GetPredefinedConstructor (type, Location.Null, Type.EmptyTypes);
2072 cab = new CustomAttributeBuilder (ci, new object[0]);
2076 public bool ResolveConstructor (Location loc, params Type[] argType)
2079 throw new InternalErrorException ("Predefined ctor redefined");
2081 if (!Resolve (false))
2084 ctor = TypeManager.GetPredefinedConstructor (type, loc, argType);
2085 return ctor != null;
2089 get { return type; }