IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
CustomAttributeTable.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 sealed class CustomAttributeTable : SortedTable<CustomAttributeTable.Record>
35 {
36
37 internal struct Record : IRecord
38 {
39
40 internal int Parent;
41 internal int Constructor;
42 internal BlobHandle Value;
43
44 readonly int IRecord.FilterKey => Parent;
45
46 public readonly int CompareTo(Record other) => Comparer<int>.Default.Compare(EncodeHasCustomAttribute(Parent), EncodeHasCustomAttribute(other.Parent));
47
48 }
49
50 internal const int Index = 0x0C;
51
52
53 internal override void Read(IKVM.Reflection.Reader.MetadataReader mr)
54 {
55 for (int i = 0; i < records.Length; i++)
56 {
57 records[i].Parent = mr.ReadHasCustomAttribute();
58 records[i].Constructor = mr.ReadCustomAttributeType();
59 records[i].Value = MetadataTokens.BlobHandle(mr.ReadBlobIndex());
60 }
61 }
62
63 internal override void Write(ModuleBuilder module)
64 {
65 for (int i = 0; i < rowCount; i++)
66 {
67 var h = module.Metadata.AddCustomAttribute(
68 MetadataTokens.EntityHandle(records[i].Parent),
69 MetadataTokens.EntityHandle(records[i].Constructor),
70 records[i].Value);
71
72 Debug.Assert(h == MetadataTokens.CustomAttributeHandle(i + 1));
73 }
74 }
75
76 internal void Fixup(ModuleBuilder moduleBuilder)
77 {
78 var genericParamFixup = moduleBuilder.GenericParamTable.GetIndexFixup();
79
80 for (int i = 0; i < rowCount; i++)
81 {
82 moduleBuilder.FixupPseudoToken(ref records[i].Constructor);
83 moduleBuilder.FixupPseudoToken(ref records[i].Parent);
84 if (MetadataTokens.EntityHandle(records[i].Parent).Kind == HandleKind.GenericParameter)
85 records[i].Parent = (GenericParamTable.Index << 24) + genericParamFixup[(records[i].Parent & 0xFFFFFF) - 1] + 1;
86
87 // TODO if we ever add support for custom attributes on DeclSecurity or GenericParamConstraint
88 // we need to fix them up here (because they are sorted tables, like GenericParam)
89 }
90
91 Sort();
92 }
93
94 internal static int EncodeHasCustomAttribute(int token) => (token >> 24) switch
95 {
96 MethodDefTable.Index => (token & 0xFFFFFF) << 5 | 0,
97 FieldTable.Index => (token & 0xFFFFFF) << 5 | 1,
98 TypeRefTable.Index => (token & 0xFFFFFF) << 5 | 2,
99 TypeDefTable.Index => (token & 0xFFFFFF) << 5 | 3,
100 ParamTable.Index => (token & 0xFFFFFF) << 5 | 4,
101 InterfaceImplTable.Index => (token & 0xFFFFFF) << 5 | 5,
102 MemberRefTable.Index => (token & 0xFFFFFF) << 5 | 6,
103 ModuleTable.Index => (token & 0xFFFFFF) << 5 | 7,
104 // LAMESPEC spec calls this Permission table
105 DeclSecurityTable.Index => throw new NotImplementedException(), //return (token & 0xFFFFFF) << 5 | 8;
106 PropertyTable.Index => (token & 0xFFFFFF) << 5 | 9,
107 EventTable.Index => (token & 0xFFFFFF) << 5 | 10,
108 StandAloneSigTable.Index => (token & 0xFFFFFF) << 5 | 11,
109 ModuleRefTable.Index => (token & 0xFFFFFF) << 5 | 12,
110 TypeSpecTable.Index => (token & 0xFFFFFF) << 5 | 13,
111 AssemblyTable.Index => (token & 0xFFFFFF) << 5 | 14,
112 AssemblyRefTable.Index => (token & 0xFFFFFF) << 5 | 15,
113 FileTable.Index => (token & 0xFFFFFF) << 5 | 16,
114 ExportedTypeTable.Index => (token & 0xFFFFFF) << 5 | 17,
115 ManifestResourceTable.Index => (token & 0xFFFFFF) << 5 | 18,
116 GenericParamTable.Index => (token & 0xFFFFFF) << 5 | 19,
117 GenericParamConstraintTable.Index => throw new NotImplementedException(), //return (token & 0xFFFFFF) << 5 | 20;
118 MethodSpecTable.Index => (token & 0xFFFFFF) << 5 | 21,
119 _ => throw new InvalidOperationException(),
120 };
121
122 }
123
124}