Пример #1
0
 public static bool HasKnownAttribute(this CustomAttributeHandleCollection customAttributes, MetadataReader metadata, KnownAttribute type)
 {
     foreach (var handle in customAttributes)
     {
         var customAttribute = metadata.GetCustomAttribute(handle);
         if (customAttribute.IsKnownAttribute(metadata, type))
         {
             return(true);
         }
     }
     return(false);
 }
Пример #2
0
 internal static bool IsKnownAttribute(this SRM.CustomAttribute attr, MetadataReader metadata, KnownAttribute attrType)
 {
     return(attr.GetAttributeType(metadata).IsKnownType(metadata, attrType));
 }
Пример #3
0
 internal static bool IsKnownType(this EntityHandle handle, MetadataReader reader, KnownAttribute knownType)
 {
     return(GetFullTypeName(handle, reader) == knownType.GetTypeName());
 }
        /// <summary>
        /// If any of the attributes are known types.
        /// </summary>
        /// <param name="attributes">The attribute to check.</param>
        /// <param name="type">The known type to check.</param>
        /// <returns>If any of the attributes are of the known type.</returns>
        public static bool HasKnownAttribute(this IEnumerable <AttributeWrapper> attributes, KnownAttribute type)
        {
            if (attributes == null)
            {
                throw new ArgumentNullException(nameof(attributes));
            }

            foreach (var customAttribute in attributes)
            {
                if (customAttribute.KnownAttributeType == type)
                {
                    return(true);
                }
            }

            return(false);
        }
        /// <summary>
        /// If there is a attribute wrapper matching the known attribute get that wrapper.
        /// </summary>
        /// <param name="attributes">The collection of attributes to check.</param>
        /// <param name="type">The type of attribute we want to find.</param>
        /// <param name="wrapper">Output value of the attribute if found.</param>
        /// <returns>If the attribute was found or not.</returns>
        public static bool TryGetKnownAttribute(this IEnumerable <AttributeWrapper> attributes, KnownAttribute type, out AttributeWrapper wrapper)
        {
            if (attributes == null)
            {
                throw new ArgumentNullException(nameof(attributes));
            }

            foreach (var customAttribute in attributes)
            {
                if (customAttribute.KnownAttributeType == type)
                {
                    wrapper = customAttribute;
                    return(true);
                }
            }

            wrapper = default !;
Пример #6
0
 /// <summary>
 /// Gets whether the parameter has an attribute of the specified attribute type (or derived attribute types).
 /// </summary>
 /// <param name="parameter">The parameter on which the attributes are declared.</param>
 /// <param name="attributeType">The attribute type to look for.</param>
 public static bool HasAttribute(this IParameter parameter, KnownAttribute attrType)
 {
     return(GetAttribute(parameter, attrType) != null);
 }
Пример #7
0
 /// <summary>
 /// Gets the attribute of the specified attribute type (or derived attribute types).
 /// </summary>
 /// <param name="parameter">The parameter on which the attributes are declared.</param>
 /// <param name="attributeType">The attribute type to look for.</param>
 /// <returns>
 /// Returns the attribute that was found; or <c>null</c> if none was found.
 /// </returns>
 public static IAttribute GetAttribute(this IParameter parameter, KnownAttribute attributeType)
 {
     return(parameter.GetAttributes().FirstOrDefault(a => a.AttributeType.IsKnownType(attributeType)));
 }
Пример #8
0
 /// <summary>
 /// Gets whether the entity has an attribute of the specified attribute type (or derived attribute types).
 /// </summary>
 /// <param name="entity">The entity on which the attributes are declared.</param>
 /// <param name="attributeType">The attribute type to look for.</param>
 /// <param name="inherit">
 /// Specifies whether attributes inherited from base classes and base members
 /// (if the given <paramref name="entity"/> in an <c>override</c>)
 /// should be returned.
 /// </param>
 public static bool HasAttribute(this IEntity entity, KnownAttribute attrType, bool inherit = false)
 {
     return(GetAttribute(entity, attrType, inherit) != null);
 }
Пример #9
0
 /// <summary>
 /// Gets the attribute of the specified attribute type (or derived attribute types).
 /// </summary>
 /// <param name="entity">The entity on which the attributes are declared.</param>
 /// <param name="attributeType">The attribute type to look for.</param>
 /// <param name="inherit">
 /// Specifies whether attributes inherited from base classes and base members
 /// (if the given <paramref name="entity"/> in an <c>override</c>)
 /// should be returned.
 /// </param>
 /// <returns>
 /// Returns the attribute that was found; or <c>null</c> if none was found.
 /// If inherit is true, an from the entity itself will be returned if possible;
 /// and the base entity will only be searched if none exists.
 /// </returns>
 public static IAttribute GetAttribute(this IEntity entity, KnownAttribute attributeType, bool inherit = false)
 {
     return(GetAttributes(entity, inherit).FirstOrDefault(a => a.AttributeType.IsKnownType(attributeType)));
 }
Пример #10
0
        /// <summary>
        /// Gets whether the type is the specified known type.
        /// For generic known types, this returns true any parameterization of the type (and also for the definition itself).
        /// </summary>
        internal static bool IsKnownType(this IType type, KnownAttribute knownType)
        {
            var def = type.GetDefinition();

            return(def != null && def.FullTypeName.IsKnownType(knownType));
        }
Пример #11
0
 internal static bool IsKnownType(this TopLevelTypeName typeName, KnownAttribute knownType)
 {
     return(typeName == knownType.GetTypeName());
 }
Пример #12
0
 /// <summary>
 /// Construct a builtin attribute.
 /// </summary>
 public void Add(KnownAttribute type, ImmutableArray <CustomAttributeTypedArgument <IType> > fixedArguments)
 {
     Add(new DefaultAttribute(module.GetAttributeType(type), fixedArguments,
                              ImmutableArray.Create <CustomAttributeNamedArgument <IType> >()));
 }
Пример #13
0
 /// <summary>
 /// Construct a builtin attribute with a single positional argument of known type.
 /// </summary>
 public void Add(KnownAttribute type, TopLevelTypeName argType, object argValue)
 {
     Add(type, ImmutableArray.Create(new CustomAttributeTypedArgument <IType>(module.Compilation.FindType(argType), argValue)));
 }
Пример #14
0
 /// <summary>
 /// Add a builtin attribute without any arguments.
 /// </summary>
 public void Add(KnownAttribute type)
 {
     // use the assemblies' cache for simple attributes
     Add(module.MakeAttribute(type));
 }
Пример #15
0
 public AttributeBuilder(MetadataModule module, KnownAttribute attributeType)
     : this(module, module.GetAttributeType(attributeType))
 {
 }
Пример #16
0
        IEnumerable <IEnumerable <ISymbol> > HandleBuiltinAttribute(KnownAttribute attribute, AnalyzerScope scope)
        {
            IEnumerable <ISymbol> ScanTypes(DecompilerTypeSystem ts)
            {
                return(ts.MainModule.TypeDefinitions
                       .Where(t => t.HasAttribute(attribute)));
            }

            IEnumerable <ISymbol> ScanMethods(DecompilerTypeSystem ts)
            {
                return(ts.MainModule.TypeDefinitions
                       .SelectMany(t => t.Members.OfType <IMethod>())
                       .Where(m => m.HasAttribute(attribute))
                       .Select(m => m.AccessorOwner ?? m));
            }

            IEnumerable <ISymbol> ScanFields(DecompilerTypeSystem ts)
            {
                return(ts.MainModule.TypeDefinitions
                       .SelectMany(t => t.Fields)
                       .Where(f => f.HasAttribute(attribute)));
            }

            IEnumerable <ISymbol> ScanProperties(DecompilerTypeSystem ts)
            {
                return(ts.MainModule.TypeDefinitions
                       .SelectMany(t => t.Properties)
                       .Where(p => p.HasAttribute(attribute)));
            }

            IEnumerable <ISymbol> ScanParameters(DecompilerTypeSystem ts)
            {
                return(ts.MainModule.TypeDefinitions
                       .SelectMany(t => t.Members.OfType <IMethod>())
                       .Where(m => m.Parameters.Any(p => p.HasAttribute(attribute)))
                       .Select(m => m.AccessorOwner ?? m));
            }

            foreach (Decompiler.Metadata.PEFile module in scope.GetAllModules())
            {
                var ts = new DecompilerTypeSystem(module, module.GetAssemblyResolver());

                switch (attribute)
                {
                case KnownAttribute.Serializable:
                case KnownAttribute.ComImport:
                case KnownAttribute.StructLayout:
                    yield return(ScanTypes(ts));

                    break;

                case KnownAttribute.DllImport:
                case KnownAttribute.PreserveSig:
                case KnownAttribute.MethodImpl:
                    yield return(ScanMethods(ts));

                    break;

                case KnownAttribute.FieldOffset:
                case KnownAttribute.NonSerialized:
                    yield return(ScanFields(ts));

                    break;

                case KnownAttribute.MarshalAs:
                    yield return(ScanFields(ts));

                    yield return(ScanParameters(ts));

                    goto case KnownAttribute.Out;

                case KnownAttribute.Optional:
                case KnownAttribute.In:
                case KnownAttribute.Out:
                    yield return(ScanParameters(ts));

                    break;

                case KnownAttribute.IndexerName:
                    yield return(ScanProperties(ts));

                    break;
                }
            }
        }