Example #1
0
        internal static string SolveMethodName(MetadataReader metadataReader, int token, CilTypeProvider provider)
        {
            string genericParameters = string.Empty;

            if (IsMethodSpecification(token))
            {
                var methodHandle = MetadataTokens.MethodSpecificationHandle(token);
                var methodSpec   = metadataReader.GetMethodSpecification(methodHandle);
                token             = MetadataTokens.GetToken(methodSpec.Method);
                genericParameters = GetGenericParametersSignature(methodSpec, provider);
            }
            if (IsMemberReference(token))
            {
                return(GetMemberRef(metadataReader, token, provider, genericParameters));
            }
            var handle     = MetadataTokens.MethodDefinitionHandle(token);
            var definition = metadataReader.GetMethodDefinition(handle);
            var parent     = definition.GetDeclaringType();
            MethodSignature <CilType> signature = SignatureDecoder.DecodeMethodSignature(definition.Signature, provider);
            var returnType = GetMethodReturnType(signature);
            var parameters = provider.GetParameterList(signature);
            var parentType = SignatureDecoder.DecodeType(parent, provider, null);

            return(string.Format("{0} {1}::{2}{3}{4}", returnType, parentType.ToString(false), GetString(metadataReader, definition.Name), genericParameters, parameters));
        }
Example #2
0
        /// <summary>
        /// Emit a method specification.
        /// </summary>
        /// <param name="methodSpecHandle">Method specification handle</param>
        private string EmitMethodSpecificationName(MethodSpecificationHandle methodSpecHandle)
        {
            MethodSpecification         methodSpec     = _metadataReader.GetMethodSpecification(methodSpecHandle);
            DisassemblingGenericContext genericContext = new DisassemblingGenericContext(Array.Empty <string>(), Array.Empty <string>());

            return(EmitHandleName(methodSpec.Method, namespaceQualified: true) + methodSpec.DecodeSignature <string, DisassemblingGenericContext>(this, genericContext));
        }
Example #3
0
        private Object ResolveMethodSpecification(MethodSpecificationHandle handle)
        {
            MethodSpecification methodSpecification = _metadataReader.GetMethodSpecification(handle);

            object resolvedMethod = GetObject(methodSpecification.Method, NotFoundBehavior.ReturnResolutionFailure);

            if (resolvedMethod is ResolutionFailure)
            {
                return(resolvedMethod);
            }

            MethodDesc methodDef = resolvedMethod as MethodDesc;

            if (methodDef == null)
            {
                ThrowHelper.ThrowBadImageFormatException($"method expected for handle {handle.ToString()}");
            }

            BlobReader          signatureReader = _metadataReader.GetBlobReader(methodSpecification.Signature);
            EcmaSignatureParser parser          = new EcmaSignatureParser(this, signatureReader, NotFoundBehavior.ReturnResolutionFailure);

            TypeDesc[] instantiation = parser.ParseMethodSpecSignature();

            if (instantiation == null)
            {
                return(parser.ResolutionFailure);
            }

            return(Context.GetInstantiatedMethod(methodDef, new Instantiation(instantiation)));
        }
Example #4
0
        public static bool IsPossibleReferenceTo(EntityHandle member, PEFile module, IMethod analyzedMethod)
        {
            if (member.IsNil)
            {
                return(false);
            }
            MetadataReader metadata = module.Metadata;

            switch (member.Kind)
            {
            case HandleKind.MethodDefinition:
                return(member == analyzedMethod.MetadataToken &&
                       module == analyzedMethod.ParentModule.PEFile);

            case HandleKind.MemberReference:
                var mr = metadata.GetMemberReference((MemberReferenceHandle)member);
                if (mr.GetKind() != MemberReferenceKind.Method)
                {
                    return(false);
                }
                return(metadata.StringComparer.Equals(mr.Name, analyzedMethod.Name));

            case HandleKind.MethodSpecification:
                var ms = metadata.GetMethodSpecification((MethodSpecificationHandle)member);
                return(IsPossibleReferenceTo(ms.Method, module, analyzedMethod));

            default:
                return(false);
            }
        }
Example #5
0
 public MethodSpecEntry(PEFile module, MethodSpecificationHandle handle)
 {
     this.metadataOffset = module.Reader.PEHeaders.MetadataStartOffset;
     this.module         = module;
     this.metadata       = module.Metadata;
     this.handle         = handle;
     this.methodSpec     = metadata.GetMethodSpecification(handle);
 }
Example #6
0
        private Object ResolveMethodSpecification(MethodSpecificationHandle handle)
        {
            MethodSpecification methodSpecification = _metadataReader.GetMethodSpecification(handle);

            MethodDesc methodDef = GetMethod(methodSpecification.Method);

            BlobReader          signatureReader = _metadataReader.GetBlobReader(methodSpecification.Signature);
            EcmaSignatureParser parser          = new EcmaSignatureParser(this, signatureReader);

            TypeDesc[] instantiation = parser.ParseMethodSpecSignature();
            return(Context.GetInstantiatedMethod(methodDef, new Instantiation(instantiation)));
        }
Example #7
0
        protected override MethodImplRecord[] ComputeVirtualMethodImplsForType()
        {
            ArrayBuilder <MethodImplRecord> records = new ArrayBuilder <MethodImplRecord>();

            MetadataReader metadataReader = _module.MetadataReader;

            foreach (var methodImplHandle in _typeDefinition.GetMethodImplementations())
            {
                MethodImplementation methodImpl = metadataReader.GetMethodImplementation(methodImplHandle);

                EntityHandle methodDeclCheckHandle = methodImpl.MethodDeclaration;
                HandleKind   methodDeclHandleKind  = methodDeclCheckHandle.Kind;

                // We want to check that the type is not an interface matches before actually getting the MethodDesc.
                // For MethodSpecifications we need to dereference that handle to the underlying member reference to
                // look at the owning type.
                if (methodDeclHandleKind == HandleKind.MethodSpecification)
                {
                    methodDeclCheckHandle = metadataReader.GetMethodSpecification((MethodSpecificationHandle)methodDeclCheckHandle).Method;
                    methodDeclHandleKind  = methodDeclCheckHandle.Kind;
                }

                MetadataType owningType = null;
                switch (methodDeclHandleKind)
                {
                case HandleKind.MethodDefinition:
                    owningType = ((MethodDesc)_module.GetObject(methodDeclCheckHandle)).OwningType as MetadataType;
                    break;

                case HandleKind.MemberReference:
                    EntityHandle owningTypeHandle = metadataReader.GetMemberReference((MemberReferenceHandle)methodDeclCheckHandle).Parent;
                    owningType = _module.GetObject(owningTypeHandle) as MetadataType;
                    break;

                default:
                    Debug.Fail("unexpected methodDeclHandleKind");
                    break;
                }

                if (!owningType.IsInterface)
                {
                    MethodImplRecord newRecord = new MethodImplRecord(
                        (MethodDesc)_module.GetObject(methodImpl.MethodDeclaration),
                        (MethodDesc)_module.GetObject(methodImpl.MethodBody));
                    records.Add(newRecord);
                }
            }

            return(records.ToArray());
        }
Example #8
0
        IMethod ResolveMethodSpecification(MethodSpecificationHandle methodSpecHandle, GenericContext context, bool expandVarArgs)
        {
            var methodSpec     = metadata.GetMethodSpecification(methodSpecHandle);
            var methodTypeArgs = methodSpec.DecodeSignature(TypeProvider, context)
                                 .SelectReadOnlyArray(IntroduceTupleTypes);
            IMethod method;

            if (methodSpec.Method.Kind == HandleKind.MethodDefinition)
            {
                // generic instance of a methoddef (=generic method in non-generic class in current assembly)
                method = ResolveMethodDefinition((MethodDefinitionHandle)methodSpec.Method, expandVarArgs);
                method = method.Specialize(new TypeParameterSubstitution(null, methodTypeArgs));
            }
            else
            {
                method = ResolveMethodReference((MemberReferenceHandle)methodSpec.Method, context, methodTypeArgs, expandVarArgs);
            }
            return(method);
        }
        private void WriteMethodSpec()
        {
            AddHeader(
                "Method",
                "Signature"
                );

            for (int i = 1, count = reader.GetTableRowCount(TableIndex.MethodSpec); i <= count; i++)
            {
                var entry = reader.GetMethodSpecification(MetadataTokens.MethodSpecificationHandle(i));

                AddRow(
                    Token(entry.Method),
                    Literal(entry.Signature)
                    );
            }

            WriteRows("MethodSpec (0x2b):");
        }
Example #10
0
        // Virtual function related functionality
        public override MethodImplRecord[] FindMethodsImplWithMatchingDeclName(string declName)
        {
            MetadataReader metadataReader = _module.MetadataReader;
            var            stringComparer = metadataReader.StringComparer;
            ArrayBuilder <MethodImplRecord> foundRecords = new ArrayBuilder <MethodImplRecord>();

            foreach (var methodImplHandle in _typeDefinition.GetMethodImplementations())
            {
                MethodImplementation methodImpl = metadataReader.GetMethodImplementation(methodImplHandle);

                EntityHandle methodDeclCheckHandle = methodImpl.MethodDeclaration;
                HandleKind   methodDeclHandleKind  = methodDeclCheckHandle.Kind;

                // We want to check that the method name matches before actually getting the MethodDesc. For MethodSpecifications
                // we need to dereference that handle to the underlying member reference to look at name matching.
                if (methodDeclHandleKind == HandleKind.MethodSpecification)
                {
                    methodDeclCheckHandle = metadataReader.GetMethodSpecification((MethodSpecificationHandle)methodDeclCheckHandle).Method;
                    methodDeclHandleKind  = methodDeclCheckHandle.Kind;
                }

                bool foundRecord = false;

                switch (methodDeclHandleKind)
                {
                case HandleKind.MethodDefinition:
                    if (stringComparer.Equals(metadataReader.GetMethodDefinition((MethodDefinitionHandle)methodDeclCheckHandle).Name, declName))
                    {
                        foundRecord = true;
                    }
                    break;

                case HandleKind.MemberReference:
                    if (stringComparer.Equals(metadataReader.GetMemberReference((MemberReferenceHandle)methodDeclCheckHandle).Name, declName))
                    {
                        foundRecord = true;
                    }
                    break;

                default:
                    Debug.Fail("unexpected methodDeclHandleKind");
                    break;
                }

                if (foundRecord)
                {
                    MethodImplRecord newRecord = new MethodImplRecord(
                        (MethodDesc)_module.GetObject(methodImpl.MethodDeclaration),
                        (MethodDesc)_module.GetObject(methodImpl.MethodBody));

                    foundRecords.Add(newRecord);
                }
            }

            if (foundRecords.Count != 0)
            {
                return(foundRecords.ToArray());
            }

            return(null);
        }
Example #11
0
 public static MethodSpecification GetMethodSpecification(this MethodSpecificationHandle handle, MetadataReader reader) => reader.GetMethodSpecification(handle);
Example #12
0
 internal static string SolveMethodName(MetadataReader metadataReader, int token, CilTypeProvider provider)
 {
     string genericParameters = string.Empty;
     if (IsMethodSpecification(token))
     {
         var methodHandle = MetadataTokens.MethodSpecificationHandle(token);
         var methodSpec = metadataReader.GetMethodSpecification(methodHandle);
         token = MetadataTokens.GetToken(methodSpec.Method);
         genericParameters = GetGenericParametersSignature(methodSpec, provider);
     }
     if (IsMemberReference(token))
     {
         return GetMemberRef(metadataReader, token, provider, genericParameters);
     }
     var handle = MetadataTokens.MethodDefinitionHandle(token);
     var definition = metadataReader.GetMethodDefinition(handle);
     var parent = definition.GetDeclaringType();
     MethodSignature<CilType> signature = SignatureDecoder.DecodeMethodSignature(definition.Signature, provider);
     var returnType = GetMethodReturnType(signature);
     var parameters = provider.GetParameterList(signature);
     var parentType = SignatureDecoder.DecodeType(parent, provider, null);
     return string.Format("{0} {1}::{2}{3}{4}",returnType, parentType.ToString(false), GetString(metadataReader, definition.Name), genericParameters, parameters);
 }