Exemplo n.º 1
0
        public static bool GetAttributeNamespaceAndName(this MetadataReader metadataReader, CustomAttributeHandle attributeHandle,
            out StringHandle namespaceHandle, out StringHandle nameHandle)
        {
            EntityHandle attributeType, attributeCtor;
            if (!GetAttributeTypeAndConstructor(metadataReader, attributeHandle, out attributeType, out attributeCtor))
            {
                namespaceHandle = default(StringHandle);
                nameHandle = default(StringHandle);
                return false;
            }

            return GetAttributeTypeNamespaceAndName(metadataReader, attributeType, out namespaceHandle, out nameHandle);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Returns the name of the attribute class 
 /// </summary>
 internal static string GetAttributeName(MetadataReader metadataReader, CustomAttributeHandle customAttribute)
 {
     var ctorHandle = metadataReader.GetCustomAttribute(customAttribute).Constructor;
     if (ctorHandle.Kind == HandleKind.MemberReference) // MemberRef
     {
         var container = metadataReader.GetMemberReference((MemberReferenceHandle)ctorHandle).Parent;
         var name = metadataReader.GetTypeReference((TypeReferenceHandle)container).Name;
         return metadataReader.GetString(name);
     }
     else
     {
         Assert.True(false, "not impl");
         return null;
     }
 }
Exemplo n.º 3
0
        public static bool GetAttributeTypeAndConstructor(this MetadataReader metadataReader, CustomAttributeHandle attributeHandle,
            out EntityHandle attributeType, out EntityHandle attributeCtor)
        {
            attributeCtor = metadataReader.GetCustomAttribute(attributeHandle).Constructor;

            if (attributeCtor.Kind == HandleKind.MemberReference)
            {
                attributeType = metadataReader.GetMemberReference((MemberReferenceHandle)attributeCtor).Parent;
                return true;
            }
            else if (attributeCtor.Kind == HandleKind.MethodDefinition)
            {
                attributeType = metadataReader.GetMethodDefinition((MethodDefinitionHandle)attributeCtor).GetDeclaringType();
                return true;
            }
            else
            {
                // invalid metadata
                attributeType = default(EntityHandle);
                return false;
            }
        }
Exemplo n.º 4
0
 internal PEAttributeData(PEModuleSymbol moduleSymbol, CustomAttributeHandle handle)
 {
     _decoder = new MetadataDecoder(moduleSymbol);
     _handle = handle;
 }
 private static bool IsGeneratorAttribute(PEModule peModule, CustomAttributeHandle customAttrHandle)
 {
     return(peModule.IsTargetAttribute(customAttrHandle, s_generatorAttributeNamespace, nameof(GeneratorAttribute), ctor: out _));
 }
Exemplo n.º 6
0
 public Attribute(Context cx, IExtractedEntity @object, CustomAttributeHandle handle) : base(cx)
 {
     attrib       = cx.MdReader.GetCustomAttribute(handle);
     this.handle  = handle;
     this.@object = @object;
 }
 private Mapping<CustomAttributeHandle> MapCustomAttributeImpl(CustomAttributeHandle handle)
 {
     throw new NotImplementedException();
 }
 private static bool IsDiagnosticAnalyzerAttribute(PEModule peModule, CustomAttributeHandle customAttrHandle)
 {
     return(peModule.IsTargetAttribute(customAttrHandle, s_diagnosticAnalyzerAttributeNamespace, nameof(DiagnosticAnalyzerAttribute), ctor: out _));
 }
Exemplo n.º 9
0
 /// <summary>
 /// Get the custom attributes, but filter out any ParamArrayAttributes.
 /// </summary>
 /// <param name="token">The parameter token handle.</param>
 /// <param name="paramArrayAttribute">Set to a ParamArrayAttribute</param>
 /// CustomAttributeHandle if any are found. Nil token otherwise.
 internal ImmutableArray<CSharpAttributeData> GetCustomAttributesForToken(EntityHandle token,
     out CustomAttributeHandle paramArrayAttribute)
 {
     CustomAttributeHandle ignore;
     return GetCustomAttributesForToken(
         token,
         out paramArrayAttribute,
         AttributeDescription.ParamArrayAttribute,
         out ignore,
         default(AttributeDescription));
 }
Exemplo n.º 10
0
        private bool GetAttributeTypeNameRaw(CustomAttributeHandle caHandle, out StringHandle namespaceName, out StringHandle typeName)
        {
            namespaceName = typeName = default(StringHandle);

            EntityHandle typeDefOrRef = GetAttributeTypeRaw(caHandle);
            if (typeDefOrRef.IsNil)
            {
                return false;
            }

            if (typeDefOrRef.Kind == HandleKind.TypeReference)
            {
                TypeReferenceHandle typeRef = (TypeReferenceHandle)typeDefOrRef;
                var resolutionScope = TypeRefTable.GetResolutionScope(typeRef);

                if (!resolutionScope.IsNil && resolutionScope.Kind == HandleKind.TypeReference)
                {
                    // we don't need to handle nested types
                    return false;
                }

                // other resolution scopes don't affect full name

                typeName = TypeRefTable.GetName(typeRef);
                namespaceName = TypeRefTable.GetNamespace(typeRef);
            }
            else if (typeDefOrRef.Kind == HandleKind.TypeDefinition)
            {
                TypeDefinitionHandle typeDef = (TypeDefinitionHandle)typeDefOrRef;

                if (TypeDefTable.GetFlags(typeDef).IsNested())
                {
                    // we don't need to handle nested types
                    return false;
                }

                typeName = TypeDefTable.GetName(typeDef);
                namespaceName = TypeDefTable.GetNamespace(typeDef);
            }
            else
            {
                // invalid metadata
                return false;
            }

            return true;
        }
Exemplo n.º 11
0
        public static bool GetAttributeTypeAndConstructor(this MetadataReader metadataReader, CustomAttributeHandle attributeHandle,
                                                          out Handle attributeType, out Handle attributeCtor)
        {
            CustomAttribute attribute = metadataReader.GetCustomAttribute(attributeHandle);

            attributeCtor = attribute.Constructor;
            attributeType = attribute.GetAttributeTypeHandle(metadataReader);
            return(true);
        }
Exemplo n.º 12
0
        public void IsNil()
        {
            Assert.False(ModuleDefinitionHandle.FromRowId(1).IsNil);
            Assert.False(AssemblyDefinitionHandle.FromRowId(1).IsNil);
            Assert.False(InterfaceImplementationHandle.FromRowId(1).IsNil);
            Assert.False(MethodDefinitionHandle.FromRowId(1).IsNil);
            Assert.False(MethodSpecificationHandle.FromRowId(1).IsNil);
            Assert.False(TypeDefinitionHandle.FromRowId(1).IsNil);
            Assert.False(ExportedTypeHandle.FromRowId(1).IsNil);
            Assert.False(TypeReferenceHandle.FromRowId(1).IsNil);
            Assert.False(TypeSpecificationHandle.FromRowId(1).IsNil);
            Assert.False(MemberReferenceHandle.FromRowId(1).IsNil);
            Assert.False(FieldDefinitionHandle.FromRowId(1).IsNil);
            Assert.False(EventDefinitionHandle.FromRowId(1).IsNil);
            Assert.False(PropertyDefinitionHandle.FromRowId(1).IsNil);
            Assert.False(StandaloneSignatureHandle.FromRowId(1).IsNil);
            Assert.False(MemberReferenceHandle.FromRowId(1).IsNil);
            Assert.False(FieldDefinitionHandle.FromRowId(1).IsNil);
            Assert.False(EventDefinitionHandle.FromRowId(1).IsNil);
            Assert.False(PropertyDefinitionHandle.FromRowId(1).IsNil);
            Assert.False(ParameterHandle.FromRowId(1).IsNil);
            Assert.False(GenericParameterHandle.FromRowId(1).IsNil);
            Assert.False(GenericParameterConstraintHandle.FromRowId(1).IsNil);
            Assert.False(ModuleReferenceHandle.FromRowId(1).IsNil);
            Assert.False(CustomAttributeHandle.FromRowId(1).IsNil);
            Assert.False(DeclarativeSecurityAttributeHandle.FromRowId(1).IsNil);
            Assert.False(ManifestResourceHandle.FromRowId(1).IsNil);
            Assert.False(ConstantHandle.FromRowId(1).IsNil);
            Assert.False(ManifestResourceHandle.FromRowId(1).IsNil);
            Assert.False(AssemblyFileHandle.FromRowId(1).IsNil);
            Assert.False(MethodImplementationHandle.FromRowId(1).IsNil);
            Assert.False(AssemblyReferenceHandle.FromRowId(1).IsNil);

            Assert.False(((EntityHandle)ModuleDefinitionHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)AssemblyDefinitionHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)InterfaceImplementationHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)MethodDefinitionHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)MethodSpecificationHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)TypeDefinitionHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)ExportedTypeHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)TypeReferenceHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)TypeSpecificationHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)MemberReferenceHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)FieldDefinitionHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)EventDefinitionHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)PropertyDefinitionHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)StandaloneSignatureHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)MemberReferenceHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)FieldDefinitionHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)EventDefinitionHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)PropertyDefinitionHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)ParameterHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)GenericParameterHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)GenericParameterConstraintHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)ModuleReferenceHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)CustomAttributeHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)DeclarativeSecurityAttributeHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)ManifestResourceHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)ConstantHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)ManifestResourceHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)AssemblyFileHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)MethodImplementationHandle.FromRowId(1)).IsNil);
            Assert.False(((EntityHandle)AssemblyReferenceHandle.FromRowId(1)).IsNil);

            Assert.False(StringHandle.FromOffset(1).IsNil);
            Assert.False(BlobHandle.FromOffset(1).IsNil);
            Assert.False(UserStringHandle.FromOffset(1).IsNil);
            Assert.False(GuidHandle.FromIndex(1).IsNil);

            Assert.False(((Handle)StringHandle.FromOffset(1)).IsNil);
            Assert.False(((Handle)BlobHandle.FromOffset(1)).IsNil);
            Assert.False(((Handle)UserStringHandle.FromOffset(1)).IsNil);
            Assert.False(((Handle)GuidHandle.FromIndex(1)).IsNil);

            Assert.True(ModuleDefinitionHandle.FromRowId(0).IsNil);
            Assert.True(AssemblyDefinitionHandle.FromRowId(0).IsNil);
            Assert.True(InterfaceImplementationHandle.FromRowId(0).IsNil);
            Assert.True(MethodDefinitionHandle.FromRowId(0).IsNil);
            Assert.True(MethodSpecificationHandle.FromRowId(0).IsNil);
            Assert.True(TypeDefinitionHandle.FromRowId(0).IsNil);
            Assert.True(ExportedTypeHandle.FromRowId(0).IsNil);
            Assert.True(TypeReferenceHandle.FromRowId(0).IsNil);
            Assert.True(TypeSpecificationHandle.FromRowId(0).IsNil);
            Assert.True(MemberReferenceHandle.FromRowId(0).IsNil);
            Assert.True(FieldDefinitionHandle.FromRowId(0).IsNil);
            Assert.True(EventDefinitionHandle.FromRowId(0).IsNil);
            Assert.True(PropertyDefinitionHandle.FromRowId(0).IsNil);
            Assert.True(StandaloneSignatureHandle.FromRowId(0).IsNil);
            Assert.True(MemberReferenceHandle.FromRowId(0).IsNil);
            Assert.True(FieldDefinitionHandle.FromRowId(0).IsNil);
            Assert.True(EventDefinitionHandle.FromRowId(0).IsNil);
            Assert.True(PropertyDefinitionHandle.FromRowId(0).IsNil);
            Assert.True(ParameterHandle.FromRowId(0).IsNil);
            Assert.True(GenericParameterHandle.FromRowId(0).IsNil);
            Assert.True(GenericParameterConstraintHandle.FromRowId(0).IsNil);
            Assert.True(ModuleReferenceHandle.FromRowId(0).IsNil);
            Assert.True(CustomAttributeHandle.FromRowId(0).IsNil);
            Assert.True(DeclarativeSecurityAttributeHandle.FromRowId(0).IsNil);
            Assert.True(ManifestResourceHandle.FromRowId(0).IsNil);
            Assert.True(ConstantHandle.FromRowId(0).IsNil);
            Assert.True(ManifestResourceHandle.FromRowId(0).IsNil);
            Assert.True(AssemblyFileHandle.FromRowId(0).IsNil);
            Assert.True(MethodImplementationHandle.FromRowId(0).IsNil);
            Assert.True(AssemblyReferenceHandle.FromRowId(0).IsNil);

            Assert.True(((EntityHandle)ModuleDefinitionHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)AssemblyDefinitionHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)InterfaceImplementationHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)MethodDefinitionHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)MethodSpecificationHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)TypeDefinitionHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)ExportedTypeHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)TypeReferenceHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)TypeSpecificationHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)MemberReferenceHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)FieldDefinitionHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)EventDefinitionHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)PropertyDefinitionHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)StandaloneSignatureHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)MemberReferenceHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)FieldDefinitionHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)EventDefinitionHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)PropertyDefinitionHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)ParameterHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)GenericParameterHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)GenericParameterConstraintHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)ModuleReferenceHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)CustomAttributeHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)DeclarativeSecurityAttributeHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)ManifestResourceHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)ConstantHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)ManifestResourceHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)AssemblyFileHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)MethodImplementationHandle.FromRowId(0)).IsNil);
            Assert.True(((EntityHandle)AssemblyReferenceHandle.FromRowId(0)).IsNil);

            // heaps:
            Assert.True(StringHandle.FromOffset(0).IsNil);
            Assert.True(BlobHandle.FromOffset(0).IsNil);
            Assert.True(UserStringHandle.FromOffset(0).IsNil);
            Assert.True(GuidHandle.FromIndex(0).IsNil);

            Assert.True(((Handle)StringHandle.FromOffset(0)).IsNil);
            Assert.True(((Handle)BlobHandle.FromOffset(0)).IsNil);
            Assert.True(((Handle)UserStringHandle.FromOffset(0)).IsNil);
            Assert.True(((Handle)GuidHandle.FromIndex(0)).IsNil);

            // virtual:
            Assert.False(AssemblyReferenceHandle.FromVirtualIndex(0).IsNil);
            Assert.False(StringHandle.FromVirtualIndex(0).IsNil);
            Assert.False(BlobHandle.FromVirtualIndex(0, 0).IsNil);

            Assert.False(((Handle)AssemblyReferenceHandle.FromVirtualIndex(0)).IsNil);
            Assert.False(((Handle)StringHandle.FromVirtualIndex(0)).IsNil);
            Assert.False(((Handle)BlobHandle.FromVirtualIndex(0, 0)).IsNil);
        }
Exemplo n.º 13
0
 public static BlobReader GetCustomAttributeBlobReader(this MetadataReader reader, CustomAttributeHandle handle)
 {
     BlobReader result = reader.GetBlobReader(reader.GetCustomAttribute(handle).Value);
     if (result.ReadInt16() != 1)
         throw new BadImageFormatException();
     return result;
 }
Exemplo n.º 14
0
        public static bool GetAttributeNamespaceAndName(this MetadataReader metadataReader, CustomAttributeHandle attributeHandle,
                                                        out string namespaceString, out ConstantStringValueHandle nameHandle)
        {
            Handle attributeType, attributeCtor;

            if (!GetAttributeTypeAndConstructor(metadataReader, attributeHandle, out attributeType, out attributeCtor))
            {
                namespaceString = null;
                nameHandle      = default(ConstantStringValueHandle);
                return(false);
            }

            return(GetAttributeTypeNamespaceAndName(metadataReader, attributeType, out namespaceString, out nameHandle));
        }
Exemplo n.º 15
0
 private static CustomAttributeData GetCustomAttributeData(MetadataReader reader, CustomAttributeHandle customAttributeHandle)
 {
     return(new RuntimeNormalCustomAttributeData(reader, customAttributeHandle));
 }
Exemplo n.º 16
0
 internal RuntimeNormalCustomAttributeData(ReflectionDomain reflectionDomain, MetadataReader reader, CustomAttributeHandle customAttributeHandle)
 {
     _reflectionDomain = reflectionDomain;
     _reader           = reader;
     _customAttribute  = customAttributeHandle.GetCustomAttribute(reader);
 }
Exemplo n.º 17
0
        private static IEnumerable <string> GetGeneratorSupportedLanguages(PEModule peModule, CustomAttributeHandle customAttrHandle)
        {
            // The GeneratorAttribute has two constructors: one default, and one with a string parameter for the
            // first supported language and an array parameter for additional supported languages.
            BlobReader argsReader = peModule.GetMemoryReaderOrThrow(peModule.GetCustomAttributeValueOrThrow(customAttrHandle));

            if (argsReader.Length == 4)
            {
                // default ctor
                return(ImmutableArray.Create(LanguageNames.CSharp));
            }
            else
            {
                // Parse the argument blob to extract the languages.
                return(ReadLanguagesFromAttribute(ref argsReader));
            }
        }
Exemplo n.º 18
0
 internal EntityHandle GetConstructor(CustomAttributeHandle handle)
 {
     int rowOffset = (handle.RowId - 1) * this.RowSize;
     return CustomAttributeTypeTag.ConvertToHandle(this.Block.PeekTaggedReference(rowOffset + _TypeOffset, _IsCustomAttributeTypeRefSizeSmall));
 }
Exemplo n.º 19
0
 public CustomAttribute GetCustomAttribute(CustomAttributeHandle handle)
 {
     // PERF: This code pattern is JIT friendly and results in very efficient code.
     return new CustomAttribute(this, GetCustomAttributeTreatmentAndRowId(handle));
 }
Exemplo n.º 20
0
 internal BlobHandle GetValue(CustomAttributeHandle handle)
 {
     int rowOffset = (int)(handle.RowId - 1) * this.RowSize;
     return BlobHandle.FromIndex(this.Block.PeekReference(rowOffset + this.ValueOffset, this.IsBlobHeapRefSizeSmall));
 }
 public static CustomAttributeData ToCustomAttributeData(this CustomAttributeHandle handle, EcmaModule module) => new EcmaCustomAttributeData(handle, module);
Exemplo n.º 22
0
        internal CustomAttributeValueTreatment CalculateCustomAttributeValueTreatment(CustomAttributeHandle handle)
        {
            Debug.Assert(_metadataKind != MetadataKind.Ecma335);

            var parent = CustomAttributeTable.GetParent(handle);

            // Check for Windows.Foundation.Metadata.AttributeUsageAttribute.
            // WinMD rules: 
            //   - The attribute is only applicable on TypeDefs.
            //   - Constructor must be a MemberRef with TypeRef.
            if (!IsWindowsAttributeUsageAttribute(parent, handle))
            {
                return CustomAttributeValueTreatment.None;
            }

            var targetTypeDef = (TypeDefinitionHandle)parent;
            if (StringStream.EqualsRaw(TypeDefTable.GetNamespace(targetTypeDef), "Windows.Foundation.Metadata"))
            {
                if (StringStream.EqualsRaw(TypeDefTable.GetName(targetTypeDef), "VersionAttribute"))
                {
                    return CustomAttributeValueTreatment.AttributeUsageVersionAttribute;
                }

                if (StringStream.EqualsRaw(TypeDefTable.GetName(targetTypeDef), "DeprecatedAttribute"))
                {
                    return CustomAttributeValueTreatment.AttributeUsageDeprecatedAttribute;
                }
            }

            bool allowMultiple = HasAttribute(targetTypeDef, "Windows.Foundation.Metadata", "AllowMultipleAttribute");
            return allowMultiple ? CustomAttributeValueTreatment.AttributeUsageAllowMultiple : CustomAttributeValueTreatment.AttributeUsageAllowSingle;
        }
Exemplo n.º 23
0
        private static bool IsDiagnosticAnalyzerAttribute(PEModule peModule, CustomAttributeHandle customAttrHandle)
        {
            EntityHandle ctor;

            return(peModule.IsTargetAttribute(customAttrHandle, s_diagnosticNamespaceName, nameof(DiagnosticAnalyzerAttribute), out ctor));
        }
 internal EcmaFormatCustomAttributeData(MetadataReader reader, CustomAttributeHandle customAttributeHandle)
 {
     _reader          = reader;
     _customAttribute = reader.GetCustomAttribute(customAttributeHandle);
 }
 public Mapping<CustomAttributeHandle> MapCustomAttribute(CustomAttributeHandle handle)
 {
     return _customAttributes.GetOrAdd(handle, MapCustomAttributeImpl);
 }
Exemplo n.º 26
0
 internal Handle GetConstructor(CustomAttributeHandle handle)
 {
     int rowOffset = (int)(handle.RowId - 1) * this.RowSize;
     return CustomAttributeTypeTag.ConvertToToken(this.Block.PeekTaggedReference(rowOffset + this.TypeOffset, this.IsCustomAttriubuteTypeRefSizeSmall));
 }
Exemplo n.º 27
0
        private NetType GetTypeFromCustomAttributeHandle(CustomAttributeHandle handle)
        {
            var attribute = Reader.GetCustomAttribute(handle);

            return(GetTypeFromEntityHandle(attribute.Constructor));
        }
Exemplo n.º 28
0
        //
        // Lightweight check to see if a custom attribute's is of a well-known type.
        //
        // This check performs without instantating the Type object and bloating memory usage. On the flip side,
        // it doesn't check on whether the type is defined in a paricular assembly. The desktop CLR typically doesn't
        // check this either so this is useful from a compat persective as well.
        //
        public static bool IsCustomAttributeOfType(this CustomAttributeHandle customAttributeHandle,
                                                   MetadataReader reader,
                                                   String ns,
                                                   String name)
        {
            String[]   namespaceParts = ns.Split('.');
            Handle     typeHandle     = customAttributeHandle.GetCustomAttribute(reader).GetAttributeTypeHandle(reader);
            HandleType handleType     = typeHandle.HandleType;

            if (handleType == HandleType.TypeDefinition)
            {
                TypeDefinition typeDefinition = typeHandle.ToTypeDefinitionHandle(reader).GetTypeDefinition(reader);
                if (!typeDefinition.Name.StringEquals(name, reader))
                {
                    return(false);
                }
                NamespaceDefinitionHandle nsHandle = typeDefinition.NamespaceDefinition;
                int idx = namespaceParts.Length;
                while (idx-- != 0)
                {
                    String namespacePart = namespaceParts[idx];
                    NamespaceDefinition namespaceDefinition = nsHandle.GetNamespaceDefinition(reader);
                    if (!namespaceDefinition.Name.StringOrNullEquals(namespacePart, reader))
                    {
                        return(false);
                    }
                    if (!namespaceDefinition.ParentScopeOrNamespace.IsNamespaceDefinitionHandle(reader))
                    {
                        return(false);
                    }
                    nsHandle = namespaceDefinition.ParentScopeOrNamespace.ToNamespaceDefinitionHandle(reader);
                }
                if (!nsHandle.GetNamespaceDefinition(reader).Name.StringOrNullEquals(null, reader))
                {
                    return(false);
                }
                return(true);
            }
            else if (handleType == HandleType.TypeReference)
            {
                TypeReference typeReference = typeHandle.ToTypeReferenceHandle(reader).GetTypeReference(reader);
                if (!typeReference.TypeName.StringEquals(name, reader))
                {
                    return(false);
                }
                if (!typeReference.ParentNamespaceOrType.IsNamespaceReferenceHandle(reader))
                {
                    return(false);
                }
                NamespaceReferenceHandle nsHandle = typeReference.ParentNamespaceOrType.ToNamespaceReferenceHandle(reader);
                int idx = namespaceParts.Length;
                while (idx-- != 0)
                {
                    String             namespacePart      = namespaceParts[idx];
                    NamespaceReference namespaceReference = nsHandle.GetNamespaceReference(reader);
                    if (!namespaceReference.Name.StringOrNullEquals(namespacePart, reader))
                    {
                        return(false);
                    }
                    if (!namespaceReference.ParentScopeOrNamespace.IsNamespaceReferenceHandle(reader))
                    {
                        return(false);
                    }
                    nsHandle = namespaceReference.ParentScopeOrNamespace.ToNamespaceReferenceHandle(reader);
                }
                if (!nsHandle.GetNamespaceReference(reader).Name.StringOrNullEquals(null, reader))
                {
                    return(false);
                }
                return(true);
            }
            else
            {
                throw new NotSupportedException();
            }
        }
Exemplo n.º 29
0
        private static bool IsTargetFrameworkMonikerAttribute(this MetadataReader metadataReader, CustomAttributeHandle handle)
        {
            if (handle.IsNil)
            {
                return(false);
            }

            var customAttribute = metadataReader.GetCustomAttribute(handle);

            if (customAttribute.Constructor.Kind != HandleKind.MemberReference)
            {
                return(false);
            }

            // Managed C++ can build modules which are later linked together into an assembly by the linker.
            // In such cases, there's no place to hang assembly-level attributes at compile-time, so the compiler
            // attaches the attributes to specific typerefs which exists solely for this purpose, and the linker
            // later picks them up from there and attaches them to the assembly. So, if we find assembly-level
            // attributes on a type reference, we can safely ignore them (since they will be
            // duplicated by what the linker propagates to the assembly level).
            if (customAttribute.Parent.Kind == HandleKind.TypeReference)
            {
                return(false);
            }

            var constructorRef = metadataReader.GetMemberReference((MemberReferenceHandle)customAttribute.Constructor);

            if (constructorRef.Parent.Kind != HandleKind.TypeReference)
            {
                return(false);
            }

            var typeRef = metadataReader.GetTypeReference((TypeReferenceHandle)constructorRef.Parent);

            var typeRefName      = metadataReader.GetString(typeRef.Name);
            var typeRefNamespace = metadataReader.GetString(typeRef.Namespace);

            return(string.Equals(typeRefName, "TargetFrameworkAttribute", StringComparison.Ordinal) &&
                   string.Equals(typeRefNamespace, "System.Runtime.Versioning", StringComparison.Ordinal));
        }
Exemplo n.º 30
0
 private static bool IsNil(CustomAttributeHandle x) => x.IsNil;
Exemplo n.º 31
0
 private static CustomAttributeData GetCustomAttributeData(ReflectionDomain reflectionDomain, MetadataReader reader, CustomAttributeHandle customAttributeHandle)
 {
     return(new RuntimeNormalCustomAttributeData(reflectionDomain, reader, customAttributeHandle));
 }
Exemplo n.º 32
0
        private static IEnumerable <string> GetDiagnosticsAnalyzerSupportedLanguages(PEModule peModule, CustomAttributeHandle customAttrHandle)
        {
            // The DiagnosticAnalyzerAttribute has one constructor, which has a string parameter for the
            // first supported language and an array parameter for addition supported languages.
            // Parse the argument blob to extract the languages.
            BlobReader argsReader = peModule.GetMemoryReaderOrThrow(peModule.GetCustomAttributeValueOrThrow(customAttrHandle));

            if (argsReader.Length > 4)
            {
                // Arguments are present--check prologue.
                if (argsReader.ReadByte() == 1 && argsReader.ReadByte() == 0)
                {
                    string firstLanguageName;
                    if (!PEModule.CrackStringInAttributeValue(out firstLanguageName, ref argsReader))
                    {
                        return(SpecializedCollections.EmptyEnumerable <string>());
                    }

                    ImmutableArray <string> additionalLanguageNames;
                    if (PEModule.CrackStringArrayInAttributeValue(out additionalLanguageNames, ref argsReader))
                    {
                        if (additionalLanguageNames.Length == 0)
                        {
                            return(SpecializedCollections.SingletonEnumerable(firstLanguageName));
                        }

                        return(additionalLanguageNames.Insert(0, firstLanguageName));
                    }
                }
            }

            return(SpecializedCollections.EmptyEnumerable <string>());
        }
 internal EcmaCustomAttributeData(CustomAttributeHandle handle, EcmaModule module)
 {
     _handle = handle;
     _module = module;
     _neverAccessThisExceptThroughCustomAttributeProperty = handle.GetCustomAttribute(Reader);
 }
Exemplo n.º 34
0
 private static IEnumerable <string> GetGeneratorsSupportedLanguages(PEModule peModule, CustomAttributeHandle customAttrHandle) => ImmutableArray.Create(LanguageNames.CSharp);
Exemplo n.º 35
0
 private void CheckIdentityFiled()
 {
     IdentityFiled   = CustomAttributeHandle.IdentityFiledStr <TEntity>(IdentityFiled);
     IsAutoIncrement = typeof(TEntity).GetMember(IdentityFiled).Count() > 0;
 }
        private static bool IsTargetFrameworkMonikerAttribute(this MetadataReader metadataReader, CustomAttributeHandle handle)
        {
            if (handle.IsNil)
            {
                return false;
            }

            var customAttribute = metadataReader.GetCustomAttribute(handle);

            if (customAttribute.Constructor.Kind != HandleKind.MemberReference)
            {
                return false;
            }

            // Managed C++ can build modules which are later linked together into an assembly by the linker.
            // In such cases, there's no place to hang assembly-level attributes at compile-time, so the compiler
            // attaches the attributes to specific typerefs which exists solely for this purpose, and the linker
            // later picks them up from there and attaches them to the assembly. So, if we find assembly-level
            // attributes on a type reference, we can safely ignore them (since they will be
            // duplicated by what the linker propagates to the assembly level).
            if (customAttribute.Parent.Kind == HandleKind.TypeReference)
            {
                return false;
            }

            var constructorRef = metadataReader.GetMemberReference((MemberReferenceHandle)customAttribute.Constructor);

            if (constructorRef.Parent.Kind != HandleKind.TypeReference)
            {
                return false;
            }

            var typeRef = metadataReader.GetTypeReference((TypeReferenceHandle)constructorRef.Parent);

            var typeRefName = metadataReader.GetString(typeRef.Name);
            var typeRefNamespace = metadataReader.GetString(typeRef.Namespace);

            return string.Equals(typeRefName, "TargetFrameworkAttribute", StringComparison.Ordinal)
                && string.Equals(typeRefNamespace, "System.Runtime.Versioning", StringComparison.Ordinal);
        }
Exemplo n.º 37
0
 public static CustomAttribute GetCustomAttribute(this CustomAttributeHandle handle, MetadataReader reader) => reader.GetCustomAttribute(handle);
Exemplo n.º 38
0
 internal EntityHandle GetParent(CustomAttributeHandle handle)
 {
     int rowOffset = (handle.RowId - 1) * this.RowSize;
     return HasCustomAttributeTag.ConvertToHandle(this.Block.PeekTaggedReference(rowOffset + _ParentOffset, _IsHasCustomAttributeRefSizeSmall));
 }
Exemplo n.º 39
0
        private bool DecodeCustomAttribute(CustomAttributeHandle caHandle, InspectorField inspectorField)
        {
            // GetCustomAttribute: https://github.com/dotnet/roslyn/blob/master/src/Compilers/Core/Portable/MetadataReader/MetadataDecoder.cs#L1370

            // Custom Attribute
            var ca = metaReader.GetCustomAttribute(caHandle);

            // MethodDefinitionHandle or MemberReferenceHandle
            if (ca.Constructor.Kind != HandleKind.MemberReference)
            {
                Console.WriteLine("ca.Constructor.Kind != HandleKind.MemberReference");
                return(false);
            }

            // constructor of the custom attr which contains the signature
            var memberRef = metaReader.GetMemberReference((MemberReferenceHandle)ca.Constructor);

            // parent of the constructor is the TypeReference
            var parent = memberRef.Parent;

            if (parent.Kind != HandleKind.TypeReference)
            {
                Console.WriteLine("parent.Kind != HandleKind.TypeReference");
                return(false);
            }

            var parentTypeRef = metaReader.GetTypeReference((TypeReferenceHandle)parent);

            // check whether we have an InspectorAttribute
            if (metaReader.GetString(parentTypeRef.Name) != "InspectorAttribute")
            {
                //Console.WriteLine("parentTypeRef != InspectorAttribute");
                return(false);
            }

            // args
            var argsReader = metaReader.GetBlobReader((BlobHandle)ca.Value);

            uint prolog = argsReader.ReadUInt16();

            if (prolog != 1)
            {
                Console.WriteLine("prolog != 1");
                return(false);
            }

            // sig reader is on constructor
            BlobReader      sigReader = metaReader.GetBlobReader(memberRef.Signature);
            SignatureHeader header    = sigReader.ReadSignatureHeader();

            // Get the type parameter count.
            if (header.IsGeneric && sigReader.ReadCompressedInteger() != 0)
            {
                Console.WriteLine("header.IsGeneric && sigReader.ReadCompressedInteger() != 0");
                return(false);
            }

            // Get the parameter count
            int paramCount = sigReader.ReadCompressedInteger();

            // Get the type return type.
            var returnTypeCode = sigReader.ReadSignatureTypeCode();

            if (returnTypeCode != SignatureTypeCode.Void)
            {
                Console.WriteLine("returnTypeCode != SignatureTypeCode.Void");
                return(false);
            }

            List <SignatureTypeCode> sigTypeCodes = new List <SignatureTypeCode>();

            // position args
            for (int i = 0; i < paramCount; i++)
            {
                SignatureTypeCode paramTypeCode = sigReader.ReadSignatureTypeCode();

                // support string custom attr for now to simplify things
                if (paramTypeCode != SignatureTypeCode.String)
                {
                    return(false);
                }

                string value;

                if (CrackStringInAttributeValue(out value, ref argsReader))
                {
                    inspectorField.CustomAttrPositionalArgs.Add(value);
                }

                sigTypeCodes.Add(paramTypeCode);
            }

            // named args

            short namedParamCount = argsReader.ReadInt16();

            for (short i = 0; i < namedParamCount; i++)
            {
                // Ecma-335 23.3 - A NamedArg is simply a FixedArg preceded by information to identify which field or
                // property it represents. [Note: Recall that the CLI allows fields and properties to have the same name; so
                // we require a means to disambiguate such situations. end note] FIELD is the single byte 0x53. PROPERTY is
                // the single byte 0x54.

                // https://github.com/dotnet/roslyn/blob/master/src/Compilers/Core/Portable/MetadataReader/MetadataDecoder.cs#L1305

                var kind = (CustomAttributeNamedArgumentKind)argsReader.ReadCompressedInteger();

                if (kind != CustomAttributeNamedArgumentKind.Field && kind != CustomAttributeNamedArgumentKind.Property)
                {
                    return(false);
                }

                var typeCode = argsReader.ReadSerializationTypeCode();

                // support string custom attr for now to simplify things
                if (typeCode != SerializationTypeCode.String)
                {
                    return(false);
                }

                string name;

                if (!CrackStringInAttributeValue(out name, ref argsReader))
                {
                    return(false);
                }

                string value;

                if (!CrackStringInAttributeValue(out value, ref argsReader))
                {
                    return(false);
                }

                inspectorField.CustomAttrNamedArgs[name] = value;
            }

            return(true);
        }
Exemplo n.º 40
0
 internal BlobHandle GetValue(CustomAttributeHandle handle)
 {
     int rowOffset = (handle.RowId - 1) * this.RowSize;
     return BlobHandle.FromOffset(this.Block.PeekHeapReference(rowOffset + _ValueOffset, _IsBlobHeapRefSizeSmall));
 }
Exemplo n.º 41
0
 private static CustomAttributeData GetCustomAttributeData(MetadataReader reader, CustomAttributeHandle customAttributeHandle)
 {
     return(new NativeFormatCustomAttributeData(reader, customAttributeHandle));
 }
Exemplo n.º 42
0
        /// <summary>
        /// Returns a possibly ExtensionAttribute filtered roArray of attributes. If
        /// filterExtensionAttributes is set to true, the method will remove all ExtensionAttributes
        /// from the returned array. If it is false, the parameter foundExtension will always be set to
        /// false and can be safely ignored.
        /// 
        /// The paramArrayAttribute parameter is similar to the foundExtension parameter, but instead
        /// of just indicating if the attribute was found, the parameter is set to the attribute handle
        /// for the ParamArrayAttribute if any is found and is null otherwise. This allows NoPia to filter
        /// the attribute out for the symbol but still cache it separately for emit.
        /// </summary>
        internal ImmutableArray<CSharpAttributeData> GetCustomAttributesForToken(EntityHandle token,
            out CustomAttributeHandle filteredOutAttribute1,
            AttributeDescription filterOut1,
            out CustomAttributeHandle filteredOutAttribute2,
            AttributeDescription filterOut2)
        {
            filteredOutAttribute1 = default(CustomAttributeHandle);
            filteredOutAttribute2 = default(CustomAttributeHandle);
            ArrayBuilder<CSharpAttributeData> customAttributesBuilder = null;

            try
            {
                foreach (var customAttributeHandle in _module.GetCustomAttributesOrThrow(token))
                {
                    if (filterOut1.Signatures != null &&
                        Module.GetTargetAttributeSignatureIndex(customAttributeHandle, filterOut1) != -1)
                    {
                        // It is important to capture the last application of the attribute that we run into,
                        // it makes a difference for default and constant values.
                        filteredOutAttribute1 = customAttributeHandle;
                        continue;
                    }

                    if (filterOut2.Signatures != null &&
                        Module.GetTargetAttributeSignatureIndex(customAttributeHandle, filterOut2) != -1)
                    {
                        // It is important to capture the last application of the attribute that we run into,
                        // it makes a difference for default and constant values.
                        filteredOutAttribute2 = customAttributeHandle;
                        continue;
                    }

                    if (customAttributesBuilder == null)
                    {
                        customAttributesBuilder = ArrayBuilder<CSharpAttributeData>.GetInstance();
                    }

                    customAttributesBuilder.Add(new PEAttributeData(this, customAttributeHandle));
                }
            }
            catch (BadImageFormatException)
            { }

            if (customAttributesBuilder != null)
            {
                return customAttributesBuilder.ToImmutableAndFree();
            }

            return ImmutableArray<CSharpAttributeData>.Empty;
        }
Exemplo n.º 43
0
        internal CustomAttributeValueTreatment CalculateCustomAttributeValueTreatment(CustomAttributeHandle handle)
        {
            Debug.Assert(_metadataKind != MetadataKind.Ecma335);

            var parent = CustomAttributeTable.GetParent(handle);

            // Check for Windows.Foundation.Metadata.AttributeUsageAttribute.
            // WinMD rules:
            //   - The attribute is only applicable on TypeDefs.
            //   - Constructor must be a MemberRef with TypeRef.
            if (!IsWindowsAttributeUsageAttribute(parent, handle))
            {
                return(CustomAttributeValueTreatment.None);
            }

            var targetTypeDef = (TypeDefinitionHandle)parent;

            if (StringHeap.EqualsRaw(TypeDefTable.GetNamespace(targetTypeDef), "Windows.Foundation.Metadata"))
            {
                if (StringHeap.EqualsRaw(TypeDefTable.GetName(targetTypeDef), "VersionAttribute"))
                {
                    return(CustomAttributeValueTreatment.AttributeUsageVersionAttribute);
                }

                if (StringHeap.EqualsRaw(TypeDefTable.GetName(targetTypeDef), "DeprecatedAttribute"))
                {
                    return(CustomAttributeValueTreatment.AttributeUsageDeprecatedAttribute);
                }
            }

            bool allowMultiple = HasAttribute(targetTypeDef, "Windows.Foundation.Metadata", "AllowMultipleAttribute");

            return(allowMultiple ? CustomAttributeValueTreatment.AttributeUsageAllowMultiple : CustomAttributeValueTreatment.AttributeUsageAllowSingle);
        }
 internal RuntimeNormalCustomAttributeData(ReflectionDomain reflectionDomain, MetadataReader reader, CustomAttributeHandle customAttributeHandle)
 {
     _reflectionDomain = reflectionDomain;
     _reader = reader;
     _customAttribute = customAttributeHandle.GetCustomAttribute(reader);
 }
Exemplo n.º 45
0
 internal ImmutableArray <CSharpAttributeData> GetCustomAttributesForToken(EntityHandle token,
                                                                           out CustomAttributeHandle filteredOutAttribute1,
                                                                           AttributeDescription filterOut1)
 {
     return(GetCustomAttributesForToken(token, out filteredOutAttribute1, filterOut1, out _, default, out _, default, out _, default));
Exemplo n.º 46
0
        private bool IsWindowsAttributeUsageAttribute(EntityHandle targetType, CustomAttributeHandle attributeHandle)
        {
            // Check for Windows.Foundation.Metadata.AttributeUsageAttribute.
            // WinMD rules: 
            //   - The attribute is only applicable on TypeDefs.
            //   - Constructor must be a MemberRef with TypeRef.

            if (targetType.Kind != HandleKind.TypeDefinition)
            {
                return false;
            }

            var attributeCtor = CustomAttributeTable.GetConstructor(attributeHandle);
            if (attributeCtor.Kind != HandleKind.MemberReference)
            {
                return false;
            }

            var attributeType = MemberRefTable.GetClass((MemberReferenceHandle)attributeCtor);
            if (attributeType.Kind != HandleKind.TypeReference)
            {
                return false;
            }

            var attributeTypeRef = (TypeReferenceHandle)attributeType;
            return StringStream.EqualsRaw(TypeRefTable.GetName(attributeTypeRef), "AttributeUsageAttribute") &&
                   StringStream.EqualsRaw(TypeRefTable.GetNamespace(attributeTypeRef), "Windows.Foundation.Metadata");
        }
 internal NativeFormatCustomAttributeData(MetadataReader reader, CustomAttributeHandle customAttributeHandle)
 {
     _reader          = reader;
     _customAttribute = customAttributeHandle.GetCustomAttribute(reader);
 }
Exemplo n.º 48
0
        /// <summary>
        /// Returns the type definition or reference handle of the attribute type.
        /// </summary>
        /// <returns><see cref="TypeDefinitionHandle"/> or <see cref="TypeReferenceHandle"/> or nil token if the metadata is invalid and the type can't be determined.</returns>
        private EntityHandle GetAttributeTypeRaw(CustomAttributeHandle handle)
        {
            var ctor = CustomAttributeTable.GetConstructor(handle);

            if (ctor.Kind == HandleKind.MethodDefinition)
            {
                return GetDeclaringType((MethodDefinitionHandle)ctor);
            }

            if (ctor.Kind == HandleKind.MemberReference)
            {
                // In general the parent can be MethodDef, ModuleRef, TypeDef, TypeRef, or TypeSpec.
                // For attributes only TypeDef and TypeRef are applicable.
                EntityHandle typeDefOrRef = MemberRefTable.GetClass((MemberReferenceHandle)ctor);
                HandleKind handleType = typeDefOrRef.Kind;

                if (handleType == HandleKind.TypeReference || handleType == HandleKind.TypeDefinition)
                {
                    return typeDefOrRef;
                }
            }

            return default(EntityHandle);
        }
Exemplo n.º 49
0
        public static bool GetAttributeNamespaceAndName(this MetadataReader metadataReader, CustomAttributeHandle attributeHandle,
                                                        out StringHandle namespaceHandle, out StringHandle nameHandle)
        {
            EntityHandle attributeType, attributeCtor;

            if (!GetAttributeTypeAndConstructor(metadataReader, attributeHandle, out attributeType, out attributeCtor))
            {
                namespaceHandle = default(StringHandle);
                nameHandle      = default(StringHandle);
                return(false);
            }

            return(GetAttributeTypeNamespaceAndName(metadataReader, attributeType, out namespaceHandle, out nameHandle));
        }
Exemplo n.º 50
0
        public static bool GetAttributeTypeAndConstructor(this MetadataReader metadataReader, CustomAttributeHandle attributeHandle,
                                                          out EntityHandle attributeType, out EntityHandle attributeCtor)
        {
            attributeCtor = metadataReader.GetCustomAttribute(attributeHandle).Constructor;

            if (attributeCtor.Kind == HandleKind.MemberReference)
            {
                attributeType = metadataReader.GetMemberReference((MemberReferenceHandle)attributeCtor).Parent;
                return(true);
            }
            else if (attributeCtor.Kind == HandleKind.MethodDefinition)
            {
                attributeType = metadataReader.GetMethodDefinition((MethodDefinitionHandle)attributeCtor).GetDeclaringType();
                return(true);
            }
            else
            {
                // invalid metadata
                attributeType = default(EntityHandle);
                return(false);
            }
        }
Exemplo n.º 51
0
 public static CustomAttributeData GetCustomAttributeData(MetadataReader reader, CustomAttributeHandle customAttributeHandle)
 {
     return(new EcmaFormatCustomAttributeData(reader, customAttributeHandle));
 }
Exemplo n.º 52
0
        private uint GetCustomAttributeTreatmentAndRowId(CustomAttributeHandle handle)
        {
            // PERF: This code pattern is JIT friendly and results in very efficient code.
            if (this.metadataKind == MetadataKind.Ecma335)
            {
                return handle.RowId;
            }

            return TreatmentAndRowId((byte)CustomAttributeTreatment.WinMD, handle.RowId);
        }
Exemplo n.º 53
0
 internal Handle GetParent(CustomAttributeHandle handle)
 {
     int rowOffset = (int)(handle.RowId - 1) * this.RowSize;
     return HasCustomAttributeTag.ConvertToToken(this.Block.PeekTaggedReference(rowOffset + this.ParentOffset, this.IsHasCustomAttributeRefSizeSmall));
 }