IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
MethodSemanticsTable.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.Reflection.Metadata.Ecma335;
27
29
31{
32
33 sealed class MethodSemanticsTable : SortedTable<MethodSemanticsTable.Record>
34 {
35
36 internal struct Record : IRecord
37 {
38
39 internal short Semantics;
40 internal int Method;
41 internal int Association;
42
43 readonly int IRecord.FilterKey => Association;
44
45 public readonly int CompareTo(Record other) => Comparer<int>.Default.Compare(EncodeHasSemantics(Association), EncodeHasSemantics(other.Association));
46
47 }
48
49 internal const int Index = 0x18;
50
51 // semantics
52 internal const short Setter = 0x0001;
53 internal const short Getter = 0x0002;
54 internal const short Other = 0x0004;
55 internal const short AddOn = 0x0008;
56 internal const short RemoveOn = 0x0010;
57 internal const short Fire = 0x0020;
58
59 internal override void Read(Reader.MetadataReader mr)
60 {
61 for (int i = 0; i < records.Length; i++)
62 {
63 records[i].Semantics = mr.ReadInt16();
64 records[i].Method = mr.ReadMethodDef();
65 records[i].Association = mr.ReadHasSemantics();
66 }
67 }
68
69 internal override void Write(ModuleBuilder module)
70 {
71 for (int i = 0; i < rowCount; i++)
72 module.Metadata.AddMethodSemantics(
73 MetadataTokens.EntityHandle(records[i].Association),
74 (System.Reflection.MethodSemanticsAttributes)records[i].Semantics,
75 MetadataTokens.MethodDefinitionHandle(records[i].Method));
76 }
77
78 static internal int EncodeHasSemantics(int token) => (token >> 24) switch
79 {
80 EventTable.Index => (token & 0xFFFFFF) << 1 | 0,
81 PropertyTable.Index => (token & 0xFFFFFF) << 1 | 1,
82 _ => throw new InvalidOperationException(),
83 };
84
85 internal void Fixup(ModuleBuilder moduleBuilder)
86 {
87 for (int i = 0; i < rowCount; i++)
88 moduleBuilder.FixupPseudoToken(ref records[i].Method);
89
90 Sort();
91 }
92
93 internal MethodInfo GetMethod(Module module, int token, bool nonPublic, short semantics)
94 {
95 foreach (int i in Filter(token))
96 {
97 if ((records[i].Semantics & semantics) != 0)
98 {
99 var method = module.ResolveMethod((MethodDefTable.Index << 24) + records[i].Method);
100 if (nonPublic || method.IsPublic)
101 return (MethodInfo)method;
102 }
103 }
104
105 return null;
106 }
107
108 internal MethodInfo[] GetMethods(Module module, int token, bool nonPublic, short semantics)
109 {
110 var methods = new List<MethodInfo>();
111 foreach (int i in Filter(token))
112 {
113 if ((records[i].Semantics & semantics) != 0)
114 {
115 var method = (MethodInfo)module.ResolveMethod((MethodDefTable.Index << 24) + records[i].Method);
116 if (nonPublic || method.IsPublic)
117 methods.Add(method);
118 }
119 }
120
121 return methods.ToArray();
122 }
123
124 internal void ComputeFlags(Module module, int token, out bool isPublic, out bool isNonPrivate, out bool isStatic)
125 {
126 isPublic = false;
127 isNonPrivate = false;
128 isStatic = false;
129
130 foreach (int i in Filter(token))
131 {
132 var method = module.ResolveMethod((MethodDefTable.Index << 24) + records[i].Method);
133 isPublic |= method.IsPublic;
134 isNonPrivate |= (method.Attributes & MethodAttributes.MemberAccessMask) > MethodAttributes.Private;
135 isStatic |= method.IsStatic;
136 }
137 }
138
139 }
140
141}
IKVM.Reflection.MethodInfo MethodInfo