Welcome to mirror list, hosted at ThFree Co, Russian Federation.

EcmaSignatureTranslator.cs « Ecma « TypeSystem « Common « tools « coreclr « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4ef47c3fe457a0055c04d705453e857e2df1b575 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;

namespace Internal.TypeSystem.Ecma
{
    public struct EcmaSignatureTranslator
    {
        private BlobReader _input;
        private BlobBuilder _output;

        private Func<int, int> _getAlternateStreamToken;

        public EcmaSignatureTranslator(BlobReader input, BlobBuilder output, Func<int, int> getAlternateStreamToken)
        {
            _input = input;
            _output = output;
            _getAlternateStreamToken = getAlternateStreamToken;
        }

        // Various parsing functions for processing through the locals of a function and translating them to the
        // alternate form with new tokens.
        private int ParseCompressedInt()
        {
            int value = _input.ReadCompressedInteger();
            _output.WriteCompressedInteger(value);
            return value;
        }

        private int ParseCompressedSignedInt()
        {
            int value = _input.ReadCompressedSignedInteger();
            _output.WriteCompressedSignedInteger(value);
            return value;
        }

        private byte ParseByte()
        {
            byte value = _input.ReadByte();
            _output.WriteByte(value);
            return value;
        }

        private byte PeekByte()
        {
            byte value = _input.ReadByte();
            _input.Offset--;
            return value;
        }

        public void ParseMemberRefSignature()
        {
            byte sigHeader = PeekByte();
            if (sigHeader == (byte)SignatureKind.Field)
                ParseFieldSignature();
            else
                ParseMethodSignature();
        }

        public void ParseFieldSignature()
        {
            byte sigHeader = ParseByte();
            if (sigHeader != (byte)SignatureKind.Field)
                throw new BadImageFormatException();
            ParseType();
        }

        public void ParseMethodSignature()
        {
            byte sigHeader = ParseByte();
            if (((int)sigHeader & (int)SignatureAttributes.Generic) == (int)SignatureAttributes.Generic)
            {
                // Parse arity
                ParseCompressedInt();
            }
            int argCount = ParseCompressedInt();
            for (int i = 0; i <= argCount; i++)
                ParseType();
        }

        public void ParseLocalsSignature()
        {
            byte sigHeader = ParseByte();
            if ((SignatureKind)sigHeader != SignatureKind.LocalVariables)
                throw new BadImageFormatException();

            int localsCount = ParseCompressedInt();
            for (int i = 0; i < localsCount; i++)
            {
                ParseType();
            }
        }

        public void ParseMethodSpecSignature()
        {
            byte sigHeader = ParseByte();
            if ((SignatureKind)sigHeader != SignatureKind.MethodSpecification)
            {
                throw new BadImageFormatException();
            }

            int argCount = ParseCompressedInt();
            for (int i = 0; i < argCount; i++)
            {
                ParseType();
            }
        }

        private void ParseTypeHandle()
        {
            int token = MetadataTokens.GetToken(_input.ReadTypeHandle());
            int newToken = _getAlternateStreamToken(token);
            int newEncodedHandle = CodedIndex.TypeDefOrRefOrSpec(MetadataTokens.EntityHandle(newToken));
            _output.WriteCompressedInteger(newEncodedHandle);
        }

        public void ParseType()
        {
            SignatureTypeCode typeCode;
            for (; ; )
            {
                int sigcodeRaw = ParseCompressedInt();
                const int ELEMENT_TYPE_CLASS = 0x12;
                const int ELEMENT_TYPE_VALUETYPE = 0x11;
                if (sigcodeRaw == ELEMENT_TYPE_CLASS || sigcodeRaw == ELEMENT_TYPE_VALUETYPE)
                    typeCode = SignatureTypeCode.TypeHandle;
                else
                    typeCode = (SignatureTypeCode)sigcodeRaw;

                switch (typeCode)
                {
                    case SignatureTypeCode.RequiredModifier:
                    case SignatureTypeCode.OptionalModifier:
                        ParseTypeHandle();
                        continue;
                    case SignatureTypeCode.Pinned:
                    case SignatureTypeCode.Sentinel:
                        continue;
                }
                break;
            }

            switch (typeCode)
            {
                case SignatureTypeCode.Void:
                case SignatureTypeCode.Boolean:
                case SignatureTypeCode.SByte:
                case SignatureTypeCode.Byte:
                case SignatureTypeCode.Int16:
                case SignatureTypeCode.UInt16:
                case SignatureTypeCode.Int32:
                case SignatureTypeCode.UInt32:
                case SignatureTypeCode.Int64:
                case SignatureTypeCode.UInt64:
                case SignatureTypeCode.Single:
                case SignatureTypeCode.Double:
                case SignatureTypeCode.Char:
                case SignatureTypeCode.String:
                case SignatureTypeCode.IntPtr:
                case SignatureTypeCode.UIntPtr:
                case SignatureTypeCode.Object:
                case SignatureTypeCode.TypedReference:
                    break;

                case SignatureTypeCode.TypeHandle:
                    ParseTypeHandle();
                    break;
                case SignatureTypeCode.SZArray:
                case SignatureTypeCode.ByReference:
                case SignatureTypeCode.Pointer:
                    {
                        ParseType();
                        break;
                    }
                case SignatureTypeCode.Array:
                    {
                        ParseType();
                        /*var rank = */ParseCompressedInt();

                        var boundsCount = ParseCompressedInt();
                        for (int i = 0; i < boundsCount; i++)
                            ParseCompressedInt();
                        var lowerBoundsCount = ParseCompressedInt();
                        for (int j = 0; j < lowerBoundsCount; j++)
                            ParseCompressedSignedInt();
                        break;
                    }
                case SignatureTypeCode.GenericTypeParameter:
                case SignatureTypeCode.GenericMethodParameter:
                    ParseCompressedInt();
                    break;
                case SignatureTypeCode.GenericTypeInstance:
                    {
                        ParseType();
                        int instanceLength = ParseCompressedInt();
                        for (int i = 0; i < instanceLength; i++)
                        {
                            ParseType();
                        }
                        break;
                    }

                case SignatureTypeCode.FunctionPointer:
                    ParseMethodSignature();
                    break;
                default:
                    ThrowHelper.ThrowBadImageFormatException();
                    return;
            }
        }
    }
}