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

ReaderGen.cs « Generator « NativeFormat « Metadata « Internal « Common « tools « coreclr « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 212426070491a77f97949963c7778820e332ce72 (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

//
// This class generates most of the implementation of the MetadataReader for the NativeAOT format,
// ensuring that the contract defined by CsPublicGen2 is implemented.The generated file is
// 'NativeFormatReaderGen.cs', and any missing implementation is the supplied in the human-authored
// source counterpart 'NativeFormatReader.cs'.
//

class ReaderGen : CsWriter
{
    public ReaderGen(string fileName)
        : base(fileName)
    {
    }

    public void EmitSource()
    {
        WriteLine("#pragma warning disable 649");
        WriteLine("#pragma warning disable 169");
        WriteLine("#pragma warning disable 282 // There is no defined ordering between fields in multiple declarations of partial class or struct");
        WriteLine("#pragma warning disable CA1066 // IEquatable<T> implementations aren't used");
        WriteLine("#pragma warning disable CA1822");
        WriteLine("#pragma warning disable IDE0059");
        WriteLine();

        WriteLine("using System;");
        WriteLine("using System.Reflection;");
        WriteLine("using System.Collections.Generic;");
        WriteLine("using System.Runtime.CompilerServices;");
        WriteLine("using Internal.NativeFormat;");
        WriteLine();

        OpenScope("namespace Internal.Metadata.NativeFormat");

        foreach (var record in SchemaDef.RecordSchema)
        {
            EmitRecord(record);
            EmitHandle(record);
        }
        
        foreach (var typeName in SchemaDef.TypeNamesWithCollectionTypes)
        {
            EmitCollection(typeName + "HandleCollection", typeName + "Handle");
        }

        foreach (var primitiveType in SchemaDef.PrimitiveTypes)
        {
            EmitCollection(primitiveType.TypeName + "Collection", primitiveType.Name);
        }

        EmitOpaqueHandle();
        EmitCollection("HandleCollection", "Handle");
        EmitMetadataReader();

        CloseScope("Internal.Metadata.NativeFormat");
    }

    private void EmitRecord(RecordDef record)
    {
        WriteTypeAttributesForCoreLib();
        OpenScope($"public partial struct {record.Name}");

        WriteLine("internal MetadataReader _reader;");
        WriteLine($"internal {record.Name}Handle _handle;");

        OpenScope($"public {record.Name}Handle Handle");
        OpenScope("get");
        WriteLine("return _handle;");
        CloseScope();
        CloseScope("Handle");

        foreach (var member in record.Members)
        {
            if ((member.Flags & MemberDefFlags.NotPersisted) != 0)
                continue;

            string memberType = member.GetMemberType();
            string fieldType = member.GetMemberType(MemberTypeKind.ReaderField);

            string fieldName = member.GetMemberFieldName();

            string description = member.GetMemberDescription();
            if (description != null)
                WriteDocComment(description);
            OpenScope($"public {memberType} {member.Name}");
            OpenScope("get");
            if (fieldType != memberType)
                WriteLine($"return ({memberType}){fieldName};");
            else
                WriteLine($"return {fieldName};");
            CloseScope();
            CloseScope(member.Name);

            WriteLineIfNeeded();
            WriteLine($"internal {fieldType} {fieldName};");
        }

        CloseScope(record.Name);
    }

    private void EmitHandle(RecordDef record)
    {
        string handleName = $"{record.Name}Handle";

        WriteTypeAttributesForCoreLib();
        OpenScope($"public partial struct {handleName}");

        OpenScope("public override bool Equals(object obj)");
        WriteLine($"if (obj is {handleName})");
        WriteLine($"    return _value == (({handleName})obj)._value;");
        WriteLine("else if (obj is Handle)");
        WriteLine("    return _value == ((Handle)obj)._value;");
        WriteLine("else");
        WriteLine("    return false;");
        CloseScope("Equals");

        OpenScope($"public bool Equals({handleName} handle)");
        WriteLine("return _value == handle._value;");
        CloseScope("Equals");

        OpenScope("public bool Equals(Handle handle)");
        WriteLine("return _value == handle._value;");
        CloseScope("Equals");

        OpenScope("public override int GetHashCode()");
        WriteLine("return (int)_value;");
        CloseScope("GetHashCode");

        WriteLineIfNeeded();
        WriteLine("internal int _value;");

        OpenScope($"internal {handleName}(Handle handle) : this(handle._value)");
        CloseScope();

        OpenScope($"internal {handleName}(int value)");
        WriteLine("HandleType hType = (HandleType)(value >> 24);");
        WriteLine($"if (!(hType == 0 || hType == HandleType.{record.Name} || hType == HandleType.Null))");
        WriteLine("    throw new ArgumentException();");
        WriteLine($"_value = (value & 0x00FFFFFF) | (((int)HandleType.{record.Name}) << 24);");
        WriteLine("_Validate();");
        CloseScope();

        OpenScope($"public static implicit operator  Handle({handleName} handle)");
        WriteLine("return new Handle(handle._value);");
        CloseScope("Handle");

        OpenScope("internal int Offset");
        OpenScope("get");
        WriteLine("return (this._value & 0x00FFFFFF);");
        CloseScope();
        CloseScope("Offset");

        OpenScope($"public {record.Name} Get{record.Name}(MetadataReader reader)");
        WriteLine($"return reader.Get{record.Name}(this);");
        CloseScope($"Get{record.Name}");

        OpenScope("public bool IsNull(MetadataReader reader)");
        WriteLine("return reader.IsNull(this);");
        CloseScope("IsNull");

        OpenScope("public Handle ToHandle(MetadataReader reader)");
        WriteLine("return reader.ToHandle(this);");
        CloseScope("ToHandle");

        WriteScopeAttribute("[System.Diagnostics.Conditional(\"DEBUG\")]");
        OpenScope("internal void _Validate()");
        WriteLine($"if ((HandleType)((_value & 0xFF000000) >> 24) != HandleType.{record.Name})");
        WriteLine("    throw new ArgumentException();");
        CloseScope("_Validate");

        OpenScope("public override string ToString()");
        WriteLine("return string.Format(\"{0:X8}\", _value);");
        CloseScope("ToString");

        CloseScope(handleName);
    }

    private void EmitCollection(string collectionTypeName, string elementTypeName)
    {
        WriteTypeAttributesForCoreLib();
        OpenScope($"public partial struct {collectionTypeName}");

        WriteLine("private NativeReader _reader;");
        WriteLine("private uint _offset;");

        OpenScope($"internal {collectionTypeName}(NativeReader reader, uint offset)");
        WriteLine("_offset = offset;");
        WriteLine("_reader = reader;");
        CloseScope();

        OpenScope("public int Count");
        OpenScope("get");
        WriteLine("uint count;");
        WriteLine("_reader.DecodeUnsigned(_offset, out count);");
        WriteLine("return (int)count;");
        CloseScope();
        CloseScope("Count");

        OpenScope($"public Enumerator GetEnumerator()");
        WriteLine($"return new Enumerator(_reader, _offset);");
        CloseScope("GetEnumerator");

        WriteTypeAttributesForCoreLib();
        OpenScope($"public struct Enumerator");

        WriteLine("private NativeReader _reader;");
        WriteLine("private uint _offset;");
        WriteLine("private uint _remaining;");
        WriteLine($"private {elementTypeName} _current;");

        OpenScope($"internal Enumerator(NativeReader reader, uint offset)");
        WriteLine("_reader = reader;");
        WriteLine("_offset = reader.DecodeUnsigned(offset, out _remaining);");
        WriteLine($"_current = default({elementTypeName});");
        CloseScope();

        OpenScope($"public {elementTypeName} Current");
        OpenScope("get");
        WriteLine("return _current;");
        CloseScope();
        CloseScope("Current");

        OpenScope("public bool MoveNext()");
        WriteLine("if (_remaining == 0)");
        WriteLine("    return false;");
        WriteLine("_remaining--;");
        WriteLine("_offset = _reader.Read(_offset, out _current);");
        WriteLine("return true;");
        CloseScope("MoveNext");

        OpenScope("public void Dispose()");
        CloseScope("Dispose");

        CloseScope("Enumerator");

        CloseScope(collectionTypeName);
    }

    private void EmitOpaqueHandle()
    {
        WriteTypeAttributesForCoreLib();
        OpenScope("public partial struct Handle");

        foreach (var record in SchemaDef.RecordSchema)
        {
            string handleName = $"{record.Name}Handle";

            OpenScope($"public {handleName} To{handleName}(MetadataReader reader)");
            WriteLine($"return new {handleName}(this);");
            CloseScope($"To{handleName}");
        }

        CloseScope("Handle");
    }

    private void EmitMetadataReader()
    {
        WriteTypeAttributesForCoreLib();
        OpenScope("public partial class MetadataReader");

        foreach (var record in SchemaDef.RecordSchema)
        {
            OpenScope($"public {record.Name} Get{record.Name}({record.Name}Handle handle)");
            if (record.Name == "ConstantStringValue")
            {
                WriteLine("if (IsNull(handle))");
                WriteLine("    return new ConstantStringValue();");
            }
            WriteLine($"{record.Name} record;");
            WriteLine("record._reader = this;");
            WriteLine("record._handle = handle;");
            WriteLine("var offset = (uint)handle.Offset;");
            foreach (var member in record.Members)
            {
                if ((member.Flags & MemberDefFlags.NotPersisted) != 0)
                    continue;
                WriteLine($"offset = _streamReader.Read(offset, out record.{member.GetMemberFieldName()});");
            }
            WriteLine("return record;");
            CloseScope($"Get{record.Name}");
        }

        foreach (var record in SchemaDef.RecordSchema)
        {
            OpenScope($"internal Handle ToHandle({record.Name}Handle handle)");
            WriteLine("return new Handle(handle._value);");
            CloseScope("ToHandle");
        }

        foreach (var record in SchemaDef.RecordSchema)
        {
            string handleName = $"{record.Name}Handle";

            OpenScope($"internal {handleName} To{handleName}(Handle handle)");
            WriteLine($"return new {handleName}(handle._value);");
            CloseScope($"To{handleName}");
        }

        foreach (var record in SchemaDef.RecordSchema)
        {
            OpenScope($"internal bool IsNull({record.Name}Handle handle)");
            WriteLine("return (handle._value & 0x00FFFFFF) == 0;");
            CloseScope("IsNull");
        }

        CloseScope("MetadataReader");
    }
}