IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
GenericParamTable.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2009-2012 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24using System;
25using System.Collections.Generic;
26using System.Diagnostics;
27using System.Reflection.Metadata;
28using System.Reflection.Metadata.Ecma335;
29
31
33{
34
35 sealed class GenericParamTable : SortedTable<GenericParamTable.Record>
36 {
37
38 internal struct Record : IRecord
39 {
40
41 internal short Number;
42 internal short Flags;
43 internal int Owner;
44 internal StringHandle Name;
45
49 internal int UnsortedIndex;
50
51 readonly int IRecord.FilterKey => Owner;
52
53 public readonly int CompareTo(Record other)
54 {
55 if (Owner == other.Owner)
56 return Comparer<short>.Default.Compare(Number, other.Number);
57 else
58 return Comparer<int>.Default.Compare(EncodeOwner(Owner), EncodeOwner(other.Owner));
59 }
60
61 }
62
63 internal const int Index = 0x2A;
64
65 internal override void Read(IKVM.Reflection.Reader.MetadataReader mr)
66 {
67 for (int i = 0; i < records.Length; i++)
68 {
69 records[i].Number = mr.ReadInt16();
70 records[i].Flags = mr.ReadInt16();
71 records[i].Owner = mr.ReadTypeOrMethodDef();
72 records[i].Name = MetadataTokens.StringHandle(mr.ReadStringIndex());
73 }
74 }
75
76 internal override void Write(ModuleBuilder module)
77 {
78 for (int i = 0; i < rowCount; i++)
79 {
80 var h = module.Metadata.AddGenericParameter(
81 MetadataTokens.EntityHandle(records[i].Owner),
82 (System.Reflection.GenericParameterAttributes)records[i].Flags,
83 records[i].Name,
84 records[i].Number);
85
86 Debug.Assert(h == MetadataTokens.GenericParameterHandle(i + 1));
87 }
88 }
89
90 internal void Fixup(ModuleBuilder moduleBuilder)
91 {
92 for (int i = 0; i < rowCount; i++)
93 {
94 moduleBuilder.FixupPseudoToken(ref records[i].Owner);
95 records[i].UnsortedIndex = i;
96 }
97
98 Sort();
99 }
100
101 internal static int EncodeOwner(int token) => (token >> 24) switch
102 {
103 TypeDefTable.Index => (token & 0xFFFFFF) << 1 | 0,
104 MethodDefTable.Index => (token & 0xFFFFFF) << 1 | 1,
105 _ => throw new InvalidOperationException(),
106 };
107
108 internal void PatchAttribute(int token, GenericParameterAttributes genericParameterAttributes)
109 {
110 records[(token & 0xFFFFFF) - 1].Flags = (short)genericParameterAttributes;
111 }
112
113 internal int[] GetIndexFixup()
114 {
115 var array = new int[rowCount];
116 for (int i = 0; i < rowCount; i++)
117 array[records[i].UnsortedIndex] = i;
118
119 return array;
120 }
121
122 internal int FindFirstByOwner(int token)
123 {
124 foreach (int i in Filter(token))
125 return i;
126
127 return -1;
128 }
129
130 }
131
132}
global::java.lang.invoke.LambdaForm.Name Name