Ejemplo n.º 1
0
        /// <summary>
        /// Emit an arbitrary type specification.
        /// </summary>
        /// <param name="typeSpecHandle">Type specification handle</param>
        /// <param name="namespaceQualified">When set to true, include namespace information</param>
        private string EmitTypeSpecificationName(TypeSpecificationHandle typeSpecHandle, bool namespaceQualified)
        {
            TypeSpecification           typeSpec       = _metadataReader.GetTypeSpecification(typeSpecHandle);
            DisassemblingGenericContext genericContext = new DisassemblingGenericContext(Array.Empty <string>(), Array.Empty <string>());

            return(typeSpec.DecodeSignature <string, DisassemblingGenericContext>(this, genericContext));
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Emit a method reference.
        /// </summary>
        /// <param name="memberRefHandle">Member reference handle</param>
        private string EmitMemberReferenceName(MemberReferenceHandle memberRefHandle, string owningTypeOverride)
        {
            MemberReference             methodRef      = _metadataReader.GetMemberReference(memberRefHandle);
            StringBuilder               builder        = new StringBuilder();
            DisassemblingGenericContext genericContext = new DisassemblingGenericContext(Array.Empty <string>(), Array.Empty <string>());
            MethodSignature <String>    methodSig      = methodRef.DecodeMethodSignature <string, DisassemblingGenericContext>(this, genericContext);

            builder.Append(methodSig.ReturnType);
            builder.Append(" ");
            builder.Append(EmitContainingTypeAndMethodName(methodRef, owningTypeOverride));
            builder.Append(EmitMethodSignature(methodSig));
            return(builder.ToString());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Extracts the method signature from the metadata by rid
        /// </summary>
        public R2RMethod(int index, MetadataReader mdReader, uint rid, int entryPointId, CorElementType[] instanceArgs, uint[] tok, FixupCell[] fixups)
        {
            Index = index;
            Token = _mdtMethodDef | rid;
            Rid   = rid;
            EntryPointRuntimeFunctionId = entryPointId;

            _mdReader        = mdReader;
            RuntimeFunctions = new List <RuntimeFunction>();

            // get the method signature from the MethodDefhandle
            MethodDefinitionHandle methodDefHandle = MetadataTokens.MethodDefinitionHandle((int)rid);

            _methodDef = mdReader.GetMethodDefinition(methodDefHandle);
            Name       = mdReader.GetString(_methodDef.Name);
            BlobReader signatureReader = mdReader.GetBlobReader(_methodDef.Signature);

            TypeDefinitionHandle declaringTypeHandle = _methodDef.GetDeclaringType();

            DeclaringType = MetadataNameFormatter.FormatHandle(mdReader, declaringTypeHandle);

            SignatureHeader signatureHeader = signatureReader.ReadSignatureHeader();

            IsGeneric = signatureHeader.IsGeneric;
            GenericParameterHandleCollection genericParams = _methodDef.GetGenericParameters();

            _genericParamInstanceMap = new Dictionary <string, string>();

            int argCount = signatureReader.ReadCompressedInteger();

            if (IsGeneric)
            {
                argCount = signatureReader.ReadCompressedInteger();
            }

            Fixups = fixups;

            DisassemblingTypeProvider provider = new DisassemblingTypeProvider();

            if (IsGeneric && instanceArgs != null && tok != null)
            {
                InitGenericInstances(genericParams, instanceArgs, tok);
            }

            DisassemblingGenericContext genericContext = new DisassemblingGenericContext(new string[0], _genericParamInstanceMap.Values.ToArray());

            Signature = _methodDef.DecodeSignature(provider, genericContext);

            SignatureString = GetSignature();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Emit a method definition.
        /// </summary>
        /// <param name="methodSpecHandle">Method definition handle</param>
        private string EmitMethodDefinitionName(MethodDefinitionHandle methodDefinitionHandle)
        {
            MethodDefinition            methodDef      = _metadataReader.GetMethodDefinition(methodDefinitionHandle);
            DisassemblingGenericContext genericContext = new DisassemblingGenericContext(Array.Empty <string>(), Array.Empty <string>());
            MethodSignature <string>    methodSig      = methodDef.DecodeSignature <string, DisassemblingGenericContext>(this, genericContext);
            StringBuilder builder = new StringBuilder();

            builder.Append(methodSig.ReturnType);
            builder.Append(" ");
            builder.Append(EmitHandleName(methodDef.GetDeclaringType(), namespaceQualified: true));
            builder.Append(".");
            builder.Append(EmitString(methodDef.Name));
            builder.Append(EmitMethodSignature(methodSig));
            return(builder.ToString());
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Extracts the method signature from the metadata by rid
        /// </summary>
        public R2RMethod(byte[] image, MetadataReader mdReader, uint rid, int entryPointId, GenericElementTypes[] instanceArgs, uint[] tok)
        {
            Token = _mdtMethodDef | rid;
            Rid   = rid;
            EntryPointRuntimeFunctionId = entryPointId;

            _mdReader        = mdReader;
            RuntimeFunctions = new List <RuntimeFunction>();

            // get the method signature from the MethodDefhandle
            MethodDefinitionHandle methodDefHandle = MetadataTokens.MethodDefinitionHandle((int)rid);

            _methodDef = mdReader.GetMethodDefinition(methodDefHandle);
            Name       = mdReader.GetString(_methodDef.Name);
            BlobReader signatureReader = mdReader.GetBlobReader(_methodDef.Signature);

            TypeDefinitionHandle declaringTypeHandle = _methodDef.GetDeclaringType();
            TypeDefinition       declaringTypeDef;

            while (!declaringTypeHandle.IsNil)
            {
                declaringTypeDef    = mdReader.GetTypeDefinition(declaringTypeHandle);
                DeclaringType       = mdReader.GetString(declaringTypeDef.Name) + "." + DeclaringType;
                declaringTypeHandle = declaringTypeDef.GetDeclaringType();
            }

            NamespaceDefinitionHandle namespaceHandle = declaringTypeDef.NamespaceDefinition;

            while (!namespaceHandle.IsNil)
            {
                NamespaceDefinition namespaceDef = mdReader.GetNamespaceDefinition(namespaceHandle);
                DeclaringType   = mdReader.GetString(namespaceDef.Name) + "." + DeclaringType;
                namespaceHandle = namespaceDef.Parent;
            }

            SignatureHeader signatureHeader = signatureReader.ReadSignatureHeader();

            IsGeneric = signatureHeader.IsGeneric;
            GenericParameterHandleCollection genericParams = _methodDef.GetGenericParameters();

            _genericParamInstanceMap = new Dictionary <string, string>();

            int argCount = signatureReader.ReadCompressedInteger();

            if (IsGeneric)
            {
                argCount = signatureReader.ReadCompressedInteger();
            }

            DisassemblingTypeProvider provider = new DisassemblingTypeProvider();

            if (IsGeneric && instanceArgs != null && tok != null)
            {
                InitGenericInstances(genericParams, instanceArgs, tok);
            }

            DisassemblingGenericContext genericContext = new DisassemblingGenericContext(new string[0], _genericParamInstanceMap.Values.ToArray());

            Signature = _methodDef.DecodeSignature(provider, genericContext);

            SignatureString = GetSignature();
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Extracts the method signature from the metadata by rid
        /// </summary>
        public R2RMethod(
            int index,
            R2RReader r2rReader,
            EntityHandle methodHandle,
            int entryPointId,
            string owningType,
            string constrainedType,
            string[] instanceArgs,
            FixupCell[] fixups)
        {
            Index        = index;
            MethodHandle = methodHandle;
            EntryPointRuntimeFunctionId = entryPointId;

            R2RReader        = r2rReader;
            RuntimeFunctions = new List <RuntimeFunction>();

            EntityHandle owningTypeHandle;
            GenericParameterHandleCollection genericParams = default(GenericParameterHandleCollection);

            DisassemblingGenericContext genericContext = new DisassemblingGenericContext(typeParameters: Array.Empty <string>(), methodParameters: instanceArgs);
            DisassemblingTypeProvider   typeProvider   = new DisassemblingTypeProvider();

            // get the method signature from the method handle
            switch (MethodHandle.Kind)
            {
            case HandleKind.MethodDefinition:
            {
                MethodDefinition methodDef = R2RReader.MetadataReader.GetMethodDefinition((MethodDefinitionHandle)MethodHandle);
                Name             = R2RReader.MetadataReader.GetString(methodDef.Name);
                Signature        = methodDef.DecodeSignature <string, DisassemblingGenericContext>(typeProvider, genericContext);
                owningTypeHandle = methodDef.GetDeclaringType();
                genericParams    = methodDef.GetGenericParameters();
            }
            break;

            case HandleKind.MemberReference:
            {
                MemberReference memberRef = R2RReader.MetadataReader.GetMemberReference((MemberReferenceHandle)MethodHandle);
                Name             = R2RReader.MetadataReader.GetString(memberRef.Name);
                Signature        = memberRef.DecodeMethodSignature <string, DisassemblingGenericContext>(typeProvider, genericContext);
                owningTypeHandle = memberRef.Parent;
            }
            break;

            default:
                throw new NotImplementedException();
            }

            if (owningType != null)
            {
                DeclaringType = owningType;
            }
            else
            {
                DeclaringType = MetadataNameFormatter.FormatHandle(R2RReader.MetadataReader, owningTypeHandle);
            }

            Fixups = fixups;

            StringBuilder sb = new StringBuilder();

            sb.Append(Signature.ReturnType);
            sb.Append(" ");
            sb.Append(DeclaringType);
            sb.Append(".");
            sb.Append(Name);

            if (Signature.GenericParameterCount != 0)
            {
                sb.Append("<");
                for (int i = 0; i < Signature.GenericParameterCount; i++)
                {
                    if (i > 0)
                    {
                        sb.Append(", ");
                    }
                    if (instanceArgs != null && instanceArgs.Length > i)
                    {
                        sb.Append(instanceArgs[i]);
                    }
                    else
                    {
                        sb.Append("!");
                        sb.Append(i);
                    }
                }
                sb.Append(">");
            }

            sb.Append("(");
            for (int i = 0; i < Signature.ParameterTypes.Length; i++)
            {
                if (i > 0)
                {
                    sb.Append(", ");
                }
                sb.AppendFormat($"{Signature.ParameterTypes[i]}");
            }
            sb.Append(")");

            SignatureString = sb.ToString();
        }