Пример #1
0
        private Object ResolveQualifiedField(QualifiedFieldHandle handle)
        {
            QualifiedField   qualifiedField = _metadataReader.GetQualifiedField(handle);
            NativeFormatType enclosingType  = (NativeFormatType)GetType(qualifiedField.EnclosingType);

            return(GetField(qualifiedField.Field, enclosingType));
        }
Пример #2
0
        private Object ResolveQualifiedMethod(QualifiedMethodHandle handle)
        {
            QualifiedMethod  qualifiedMethod = _metadataReader.GetQualifiedMethod(handle);
            NativeFormatType enclosingType   = (NativeFormatType)GetType(qualifiedMethod.EnclosingType);

            return(GetMethod(qualifiedMethod.Method, enclosingType));
        }
Пример #3
0
        public MethodDesc GetMethod(Handle handle, NativeFormatType type)
        {
            MethodDesc method = GetObject(handle, type) as MethodDesc;

            if (method == null)
            {
                throw new BadImageFormatException("Method expected");
            }
            return(method);
        }
Пример #4
0
        public FieldDesc GetField(Handle handle, NativeFormatType type)
        {
            FieldDesc field = GetObject(handle, type) as FieldDesc;

            if (field == null)
            {
                throw new BadImageFormatException("Field expected");
            }
            return(field);
        }
Пример #5
0
        private TypeDesc[] _genericParameters; // TODO: Optional field?

        internal NativeFormatMethod(NativeFormatType type, MethodHandle handle)
        {
            _type = type;
            _handle = handle;

#if DEBUG
            // Initialize name eagerly in debug builds for convenience
            this.ToString();
#endif
        }
Пример #6
0
        private TypeDesc[] _genericParameters; // TODO: Optional field?

        internal NativeFormatMethod(NativeFormatType type, MethodHandle handle)
        {
            _type   = type;
            _handle = handle;

#if DEBUG
            // Initialize name eagerly in debug builds for convenience
            this.ToString();
#endif
        }
Пример #7
0
        internal NativeFormatField(NativeFormatType type, FieldHandle handle)
        {
            _type   = type;
            _handle = handle;

#if DEBUG
            // Initialize name eagerly in debug builds for convenience
            InitializeName();
#endif
        }
Пример #8
0
        public Object GetObject(Handle handle, NativeFormatType type)
        {
            IHandleObject obj = _resolvedTokens.GetOrCreateValue(new NativeFormatObjectKey(handle, type));

            if (obj is NativeFormatObjectLookupWrapper)
            {
                return(((NativeFormatObjectLookupWrapper)obj).Object);
            }
            else
            {
                return(obj);
            }
        }
Пример #9
0
 public NativeFormatObjectKey(Handle handle, NativeFormatType container)
 {
     _handle    = handle;
     _container = container;
 }
Пример #10
0
 public NativeFormatObjectLookupWrapper(Handle handle, object obj, NativeFormatType container)
 {
     _obj       = obj;
     _handle    = handle;
     _container = container;
 }
Пример #11
0
            protected override IHandleObject CreateValueFromKey(NativeFormatObjectKey key)
            {
                Handle           handle    = key.Handle;
                NativeFormatType container = key.Container;

                object item;

                switch (handle.HandleType)
                {
                case HandleType.TypeDefinition:
                    item = new NativeFormatType(_metadataUnit, handle.ToTypeDefinitionHandle(_metadataReader));
                    break;

                case HandleType.Method:
                    item = new NativeFormatMethod(container, handle.ToMethodHandle(_metadataReader));
                    break;

                case HandleType.Field:
                    item = new NativeFormatField(container, handle.ToFieldHandle(_metadataReader));
                    break;

                case HandleType.TypeReference:
                    item = _metadataUnit.ResolveTypeReference(handle.ToTypeReferenceHandle(_metadataReader));
                    break;

                case HandleType.MemberReference:
                    item = _metadataUnit.ResolveMemberReference(handle.ToMemberReferenceHandle(_metadataReader));
                    break;

                case HandleType.QualifiedMethod:
                    item = _metadataUnit.ResolveQualifiedMethod(handle.ToQualifiedMethodHandle(_metadataReader));
                    break;

                case HandleType.QualifiedField:
                    item = _metadataUnit.ResolveQualifiedField(handle.ToQualifiedFieldHandle(_metadataReader));
                    break;

                case HandleType.ScopeReference:
                    item = _metadataUnit.ResolveAssemblyReference(handle.ToScopeReferenceHandle(_metadataReader));
                    break;

                case HandleType.ScopeDefinition:
                {
                    ScopeDefinition scope = handle.ToScopeDefinitionHandle(_metadataReader).GetScopeDefinition(_metadataReader);
                    item = _metadataUnit.GetModuleFromAssemblyName(scope.Name.GetConstantStringValue(_metadataReader).Value);
                }
                break;

                case HandleType.TypeSpecification:
                case HandleType.TypeInstantiationSignature:
                case HandleType.SZArraySignature:
                case HandleType.ArraySignature:
                case HandleType.PointerSignature:
                case HandleType.ByReferenceSignature:
                case HandleType.TypeVariableSignature:
                case HandleType.MethodTypeVariableSignature:
                {
                    NativeFormatSignatureParser parser = new NativeFormatSignatureParser(_metadataUnit, handle, _metadataReader);

                    item = parser.ParseTypeSignature();
                }
                break;

                case HandleType.MethodInstantiation:
                    item = _metadataUnit.ResolveMethodInstantiation(handle.ToMethodInstantiationHandle(_metadataReader));
                    break;

                // TODO: Resolve other tokens
                default:
                    throw new BadImageFormatException("Unknown metadata token type: " + handle.HandleType);
                }

                switch (handle.HandleType)
                {
                case HandleType.TypeDefinition:
                case HandleType.Field:
                case HandleType.Method:
                    // type/method/field definitions directly correspond to their target item.
                    return((IHandleObject)item);

                default:
                    // Everything else is some form of reference which cannot be self-describing
                    return(new NativeFormatObjectLookupWrapper(handle, item, container));
                }
            }
Пример #12
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);
        }