Пример #1
0
        private TType DecodeTypeHandle(ref BlobReader blobReader, byte rawTypeKind, bool allowTypeSpecifications)
        {
            EntityHandle handle = blobReader.ReadTypeHandle();

            if (!handle.IsNil)
            {
                switch (handle.Kind)
                {
                case HandleKind.TypeDefinition:
                    return(_provider.GetTypeFromDefinition(_metadataReaderOpt, (TypeDefinitionHandle)handle, rawTypeKind));

                case HandleKind.TypeReference:
                    return(_provider.GetTypeFromReference(_metadataReaderOpt, (TypeReferenceHandle)handle, rawTypeKind));

                case HandleKind.TypeSpecification:
                    if (!allowTypeSpecifications)
                    {
                        // To prevent cycles, the token following (CLASS | VALUETYPE) must not be a type spec.
                        // https://github.com/dotnet/coreclr/blob/8ff2389204d7c41b17eff0e9536267aea8d6496f/src/md/compiler/mdvalidator.cpp#L6154-L6160
                        throw new BadImageFormatException(SR.NotTypeDefOrRefHandle);
                    }

                    return(_provider.GetTypeFromSpecification(_metadataReaderOpt, _genericContext, (TypeSpecificationHandle)handle, rawTypeKind));

                default:
                    // indicates an error returned from ReadTypeHandle, otherwise unreachable.
                    Debug.Assert(handle.IsNil);     // will fall through to throw in release.
                    break;
                }
            }

            throw new BadImageFormatException(SR.NotTypeDefOrRefOrSpecHandle);
        }
Пример #2
0
        private TType DecodeTypeDefOrRef(ref BlobReader blobReader, SignatureTypeHandleCode code)
        {
            // Force no differentiation of class vs. value type unless the option is enabled.
            // Avoids cost of WinRT projection.
            if ((_options & SignatureDecoderOptions.DifferentiateClassAndValueTypes) == 0)
            {
                code = SignatureTypeHandleCode.Unresolved;
            }

            EntityHandle handle = blobReader.ReadTypeHandle();

            switch (handle.Kind)
            {
            case HandleKind.TypeDefinition:
                var typeDef = (TypeDefinitionHandle)handle;
                return(_provider.GetTypeFromDefinition(_metadataReaderOpt, typeDef, code));

            case HandleKind.TypeReference:
                var typeRef = (TypeReferenceHandle)handle;
                if (code != SignatureTypeHandleCode.Unresolved)
                {
                    ProjectClassOrValueType(typeRef, ref code);
                }
                return(_provider.GetTypeFromReference(_metadataReaderOpt, typeRef, code));

            default:
                // To prevent cycles, the token following (CLASS | VALUETYPE) must not be a type spec
                // https://github.com/dotnet/coreclr/blob/8ff2389204d7c41b17eff0e9536267aea8d6496f/src/md/compiler/mdvalidator.cpp#L6154-L6160
                throw new BadImageFormatException(SR.NotTypeDefOrRefHandle);
            }
        }
Пример #3
0
        private TType DecodeTypeHandle(ref BlobReader blobReader, SignatureTypeHandleCode code, bool alllowTypeSpecifications)
        {
            // Force no differentiation of class vs. value type unless the option is enabled.
            // Avoids cost of WinRT projection.
            if ((_options & SignatureDecoderOptions.DifferentiateClassAndValueTypes) == 0)
            {
                code = SignatureTypeHandleCode.Unresolved;
            }

            EntityHandle handle = blobReader.ReadTypeHandle();

            if (!handle.IsNil)
            {
                switch (handle.Kind)
                {
                case HandleKind.TypeDefinition:
                    var typeDef = (TypeDefinitionHandle)handle;
                    return(_provider.GetTypeFromDefinition(_metadataReaderOpt, typeDef, code));

                case HandleKind.TypeReference:
                    var typeRef = (TypeReferenceHandle)handle;
                    if (code != SignatureTypeHandleCode.Unresolved)
                    {
                        ProjectClassOrValueType(typeRef, ref code);
                    }
                    return(_provider.GetTypeFromReference(_metadataReaderOpt, typeRef, code));

                case HandleKind.TypeSpecification:
                    if (!alllowTypeSpecifications)
                    {
                        // To prevent cycles, the token following (CLASS | VALUETYPE) must not be a type spec.
                        // https://github.com/dotnet/coreclr/blob/8ff2389204d7c41b17eff0e9536267aea8d6496f/src/md/compiler/mdvalidator.cpp#L6154-L6160
                        throw new BadImageFormatException(SR.NotTypeDefOrRefHandle);
                    }

                    if (code != SignatureTypeHandleCode.Unresolved)
                    {
                        // TODO: We need more work here in differentiating case because instantiations can project class
                        // to value type as in IReference<T> -> Nullable<T>. Unblocking Roslyn work where the differentiation
                        // feature is not used. Note that the use-case of custom-mods will not hit this because there is no
                        // CLASS | VALUETYPE before the modifier token and so it always comes in unresolved.
                        code = SignatureTypeHandleCode.Unresolved;     // never lie in the meantime.
                    }

                    var typeSpec = (TypeSpecificationHandle)handle;
                    return(_provider.GetTypeFromSpecification(_metadataReaderOpt, typeSpec, SignatureTypeHandleCode.Unresolved));

                default:
                    // indicates an error returned from ReadTypeHandle, otherwise unreachable.
                    Debug.Assert(handle.IsNil);     // will fall through to throw in release.
                    break;
                }
            }

            throw new BadImageFormatException(SR.NotTypeDefOrRefOrSpecHandle);
        }
Пример #4
0
        /// <summary>
        /// Decodes a type definition, reference or specification to its representation as TType.
        /// </summary>
        /// <param name="handle">A type definition, reference, or specification handle.</param>
        /// <param name="provider">The type provider.</param>
        /// <param name="isValueType">Is the type a class or a value type. Null signifies that the current type signature does not have the prefix</param>
        /// <exception cref="System.BadImageFormatException">The handle does not represent a valid type reference, definition, or specification.</exception>
        public static TType DecodeType <TType>(Handle handle, ISignatureTypeProvider <TType> provider, bool?isValueType)
        {
            switch (handle.Kind)
            {
            case HandleKind.TypeReference:
                return(provider.GetTypeFromReference((TypeReferenceHandle)handle, isValueType));

            case HandleKind.TypeDefinition:
                return(provider.GetTypeFromDefinition((TypeDefinitionHandle)handle, isValueType));

            case HandleKind.TypeSpecification:
                return(DecodeTypeSpecification((TypeSpecificationHandle)handle, provider));

            default:
                throw new BadImageFormatException();
            }
        }
Пример #5
0
 public RoType GetTypeFromReference(MetadataReader reader, TypeReferenceHandle handle, byte rawTypeKind) => _typeProvider.GetTypeFromReference(reader, handle, rawTypeKind);