Beispiel #1
0
        internal EcmaField(EcmaType type, FieldDefinitionHandle handle)
        {
            _type = type;
            _handle = handle;

#if DEBUG
            // Initialize name eagerly in debug builds for convenience
            this.ToString();
#endif
        }
        internal TypeDesc GetTypeDescFromQHandle(QTypeDefinition qTypeDefinition)
        {
#if ECMA_METADATA_SUPPORT
            if (qTypeDefinition.IsNativeFormatMetadataBased)
#endif
            {
                MetadataReader           nativeFormatMetadataReader = qTypeDefinition.NativeFormatReader;
                TypeDefinitionHandle     typeDefinitionHandle       = qTypeDefinition.NativeFormatHandle;
                NativeFormatModuleInfo   module           = ModuleList.Instance.GetModuleInfoForMetadataReader(nativeFormatMetadataReader);
                NativeFormatMetadataUnit metadataUnit     = ResolveMetadataUnit(module);
                NativeFormatType         nativeFormatType = (NativeFormatType)metadataUnit.GetType(typeDefinitionHandle);
                return(nativeFormatType);
            }
#if ECMA_METADATA_SUPPORT
            else if (qTypeDefinition.IsEcmaFormatMetadataBased)
            {
                EcmaModuleInfo  module     = ModuleList.Instance.GetModuleInfoForMetadataReader(qTypeDefinition.EcmaFormatReader);
                Ecma.EcmaModule ecmaModule = ResolveEcmaModule(module);
                Ecma.EcmaType   ecmaType   = (Ecma.EcmaType)ecmaModule.GetType(qTypeDefinition.EcmaFormatHandle);
                return(ecmaType);
            }
#endif
            return(null);
        }
Beispiel #3
0
        // Todo: This is looking up the hierarchy to DefType and ParameterizedType. It should really
        // call a virtual or an outside type to handle those parts
        internal bool RetrieveRuntimeTypeHandleIfPossible()
        {
            TypeDesc type = this;

            if (!type.RuntimeTypeHandle.IsNull())
            {
                return(true);
            }

            TypeBuilderState state = GetTypeBuilderStateIfExist();

            if (state != null && state.AttemptedAndFailedToRetrieveTypeHandle)
            {
                return(false);
            }

            if (type is DefType)
            {
                DefType typeAsDefType = (DefType)type;

                TypeDesc          typeDefinition = typeAsDefType.GetTypeDefinition();
                RuntimeTypeHandle typeDefHandle  = typeDefinition.RuntimeTypeHandle;
                if (typeDefHandle.IsNull())
                {
#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
                    NativeFormat.NativeFormatType mdType = typeDefinition as NativeFormat.NativeFormatType;
                    if (mdType != null)
                    {
                        // Look up the runtime type handle in the module metadata
                        if (TypeLoaderEnvironment.Instance.TryGetNamedTypeForMetadata(new QTypeDefinition(mdType.MetadataReader, mdType.Handle), out typeDefHandle))
                        {
                            typeDefinition.SetRuntimeTypeHandleUnsafe(typeDefHandle);
                        }
                    }
#endif
#if ECMA_METADATA_SUPPORT
                    Ecma.EcmaType ecmaType = typeDefinition as Ecma.EcmaType;
                    if (ecmaType != null)
                    {
                        // Look up the runtime type handle in the module metadata
                        if (TypeLoaderEnvironment.Instance.TryGetNamedTypeForMetadata(new QTypeDefinition(ecmaType.MetadataReader, ecmaType.Handle), out typeDefHandle))
                        {
                            typeDefinition.SetRuntimeTypeHandleUnsafe(typeDefHandle);
                        }
                    }
#endif
                }

                if (!typeDefHandle.IsNull())
                {
                    Instantiation instantiation = typeAsDefType.Instantiation;

                    if ((instantiation.Length > 0) && !typeAsDefType.IsGenericDefinition)
                    {
                        // Generic type. First make sure we have type handles for the arguments, then check
                        // the instantiation.
                        bool argumentsRegistered = true;
                        bool arrayArgumentsFound = false;
                        for (int i = 0; i < instantiation.Length; i++)
                        {
                            if (!instantiation[i].RetrieveRuntimeTypeHandleIfPossible())
                            {
                                argumentsRegistered = false;
                                arrayArgumentsFound = arrayArgumentsFound || (instantiation[i] is ArrayType);
                            }
                        }

                        RuntimeTypeHandle rtth;

                        // If at least one of the arguments is not known to the runtime, we take a slower
                        // path to compare the current type we need a handle for to the list of generic
                        // types statically available, by loading them as DefTypes and doing a DefType comparaison
                        if ((argumentsRegistered && TypeLoaderEnvironment.Instance.TryLookupConstructedGenericTypeForComponents(new TypeLoaderEnvironment.HandleBasedGenericTypeLookup(typeAsDefType), out rtth)) ||
                            (arrayArgumentsFound && TypeLoaderEnvironment.Instance.TryLookupConstructedGenericTypeForComponents(new TypeLoaderEnvironment.DefTypeBasedGenericTypeLookup(typeAsDefType), out rtth)))
                        {
                            typeAsDefType.SetRuntimeTypeHandleUnsafe(rtth);
                            return(true);
                        }
                    }
                    else
                    {
                        // Nongeneric, or generic type def types are just the type handle of the type definition as found above
                        type.SetRuntimeTypeHandleUnsafe(typeDefHandle);
                        return(true);
                    }
                }
            }
            else if (type is ParameterizedType)
            {
                ParameterizedType typeAsParameterType = (ParameterizedType)type;

                if (typeAsParameterType.ParameterType.RetrieveRuntimeTypeHandleIfPossible())
                {
                    RuntimeTypeHandle rtth;
                    if ((type is ArrayType &&
                         (TypeLoaderEnvironment.Instance.TryGetArrayTypeForElementType_LookupOnly(typeAsParameterType.ParameterType.RuntimeTypeHandle, type.IsMdArray, type.IsMdArray ? ((ArrayType)type).Rank : -1, out rtth) ||
                          TypeLoaderEnvironment.Instance.TryGetArrayTypeHandleForNonDynamicArrayTypeFromTemplateTable(type as ArrayType, out rtth)))
                        ||
                        (type is PointerType && TypeSystemContext.PointerTypesCache.TryGetValue(typeAsParameterType.ParameterType.RuntimeTypeHandle, out rtth)))
                    {
                        typeAsParameterType.SetRuntimeTypeHandleUnsafe(rtth);
                        return(true);
                    }
                    else if (type is ByRefType)
                    {
                        // Byref types don't have any associated type handles, so return success at this point
                        // since we were able to resolve the typehandle of the element type
                        return(true);
                    }
                }
            }
            else if (type is SignatureVariable)
            {
                // SignatureVariables do not have RuntimeTypeHandles
            }
            else
            {
                Debug.Assert(false);
            }

            // Make a note on the type build state that we have attempted to retrieve RuntimeTypeHandle but there is not one
            GetOrCreateTypeBuilderState().AttemptedAndFailedToRetrieveTypeHandle = true;

            return(false);
        }
Beispiel #4
0
            protected override IEntityHandleObject CreateValueFromKey(EntityHandle handle)
            {
                object item;

                switch (handle.Kind)
                {
                case HandleKind.TypeDefinition:
                    item = new EcmaType(_module, (TypeDefinitionHandle)handle);
                    break;

                case HandleKind.MethodDefinition:
                {
                    MethodDefinitionHandle methodDefinitionHandle = (MethodDefinitionHandle)handle;
                    TypeDefinitionHandle   typeDefinitionHandle   = _module._metadataReader.GetMethodDefinition(methodDefinitionHandle).GetDeclaringType();
                    EcmaType type = (EcmaType)_module.GetObject(typeDefinitionHandle);
                    item = new EcmaMethod(type, methodDefinitionHandle);
                }
                break;

                case HandleKind.FieldDefinition:
                {
                    FieldDefinitionHandle fieldDefinitionHandle = (FieldDefinitionHandle)handle;
                    TypeDefinitionHandle  typeDefinitionHandle  = _module._metadataReader.GetFieldDefinition(fieldDefinitionHandle).GetDeclaringType();
                    EcmaType type = (EcmaType)_module.GetObject(typeDefinitionHandle);
                    item = new EcmaField(type, fieldDefinitionHandle);
                }
                break;

                case HandleKind.TypeReference:
                    item = _module.ResolveTypeReference((TypeReferenceHandle)handle);
                    break;

                case HandleKind.MemberReference:
                    item = _module.ResolveMemberReference((MemberReferenceHandle)handle);
                    break;

                case HandleKind.AssemblyReference:
                    item = _module.ResolveAssemblyReference((AssemblyReferenceHandle)handle);
                    break;

                case HandleKind.TypeSpecification:
                    item = _module.ResolveTypeSpecification((TypeSpecificationHandle)handle);
                    break;

                case HandleKind.MethodSpecification:
                    item = _module.ResolveMethodSpecification((MethodSpecificationHandle)handle);
                    break;

                case HandleKind.ExportedType:
                    item = _module.ResolveExportedType((ExportedTypeHandle)handle);
                    break;

                case HandleKind.StandaloneSignature:
                    item = _module.ResolveStandaloneSignature((StandaloneSignatureHandle)handle);
                    break;

                case HandleKind.ModuleDefinition:
                    // ECMA-335 Partition 2 II.22.38 1d: This should not occur in a CLI ("compressed metadata") module,
                    // but resolves to "current module".
                    item = _module;
                    break;

                case HandleKind.ModuleReference:
                    item = _module.ResolveModuleReference((ModuleReferenceHandle)handle);
                    break;

                default:
                    throw new BadImageFormatException("Unknown metadata token type: " + handle.Kind);
                }

                switch (handle.Kind)
                {
                case HandleKind.TypeDefinition:
                case HandleKind.MethodDefinition:
                case HandleKind.FieldDefinition:
                    // type/method/field definitions directly correspond to their target item.
                    return((IEntityHandleObject)item);

                default:
                    // Everything else is some form of reference which cannot be self-describing
                    return(new EcmaObjectLookupWrapper(handle, item));
                }
            }