Skip to content

Commit 5a784a3

Browse files
authored
Several fixes and rework (#30)
1 parent 6636db7 commit 5a784a3

File tree

6 files changed

+93
-91
lines changed

6 files changed

+93
-91
lines changed

source/MetadataProcessor.Core/Tables/nanoMethodDefinitionTable.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ protected override void WriteSingleItem(
7676
_context.SignaturesTable.WriteDataType(item.ReturnType, writer, false, false);
7777
if (item.ReturnType is TypeSpecification)
7878
{
79-
_context.TypeSpecificationsTable
80-
.GetOrCreateTypeSpecificationId(item.ReturnType);
79+
// developer note
80+
// This check is wrong. A TypeSpecification is showing when the return type it's an array which is OK.
81+
// Requires further investigation to evaluate what's the correct condition required to add an entry to the Type Specifications Table
82+
83+
//_context.TypeSpecificationsTable
84+
// .GetOrCreateTypeSpecificationId(item.ReturnType);
8185
}
8286

8387
writer.WriteByte(parametersCount);

source/MetadataProcessor.Core/Tables/nanoSignaturesTable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ private ushort GetOrCreateSignatureIdImpl(
519519
}
520520

521521
var fullSignatures = GetFullSignaturesArray();
522-
for (var i = 0; i < fullSignatures.Length - signature.Length; ++i)
522+
for (var i = 0; i <= fullSignatures.Length - signature.Length; ++i)
523523
{
524524
if (signature.SequenceEqual(fullSignatures.Skip(i).Take(signature.Length)))
525525
{

source/MetadataProcessor.Core/Tables/nanoStringTable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public nanoStringTable(
6666
/// <param name="value">String value for obtaining identifier.</param>
6767
/// <param name="useConstantsTable">
6868
/// If <c>true</c> hard-coded string constants table will be used (should be <c>false</c>
69-
/// for byte code writer because onlyloader use this pre-defined string table optimization).
69+
/// for byte code writer because only loader use this pre-defined string table optimization).
7070
/// </param>
7171
/// <returns>Existing identifier if string already in table or new one.</returns>
7272
public ushort GetOrCreateStringId(

source/MetadataProcessor.Core/Tables/nanoTablesContext.cs

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,48 +17,31 @@ public sealed class nanoTablesContext
1717
new HashSet<string>(StringComparer.Ordinal)
1818
{
1919
// Assembly-level attributes
20-
"System.Reflection.AssemblyCultureAttribute",
21-
"System.Reflection.AssemblyVersionAttribute",
22-
"System.Reflection.AssemblyFileVersionAttribute",
23-
"System.Reflection.AssemblyTrademarkAttribute",
24-
"System.Reflection.AssemblyTitleAttribute",
25-
"System.Reflection.AssemblyProductAttribute",
26-
"System.Reflection.AssemblyKeyNameAttribute",
27-
"System.Reflection.AssemblyKeyFileAttribute",
28-
"System.Reflection.AssemblyInformationalVersionAttribute",
29-
"System.Reflection.AssemblyFlagsAttribute",
30-
"System.Reflection.AssemblyDescriptionAttribute",
31-
"System.Reflection.AssemblyDelaySignAttribute",
32-
"System.Reflection.AssemblyDefaultAliasAttribute",
33-
"System.Reflection.AssemblyCopyrightAttribute",
34-
"System.Reflection.AssemblyConfigurationAttribute",
35-
"System.Reflection.AssemblyCompanyAttribute",
3620
"System.Runtime.InteropServices.ComVisibleAttribute",
3721
"System.Runtime.InteropServices.GuidAttribute",
3822

3923
// Compiler-specific attributes
40-
"System.ParamArrayAttribute",
41-
"System.SerializableAttribute",
42-
"System.NonSerializedAttribute",
43-
"System.Runtime.InteropServices.StructLayoutAttribute",
44-
"System.Runtime.InteropServices.LayoutKind",
45-
"System.Runtime.InteropServices.OutAttribute",
46-
"System.Runtime.CompilerServices.ExtensionAttribute",
47-
"System.Runtime.CompilerServices.MethodImplAttribute",
48-
"System.Runtime.CompilerServices.InternalsVisibleToAttribute",
49-
"System.Runtime.CompilerServices.IndexerNameAttribute",
50-
"System.Runtime.CompilerServices.MethodImplOptions",
51-
"System.Reflection.FieldNoReflectionAttribute",
52-
"System.Reflection.DefaultMemberAttribute",
24+
//"System.ParamArrayAttribute",
25+
//"System.SerializableAttribute",
26+
//"System.NonSerializedAttribute",
27+
//"System.Runtime.InteropServices.StructLayoutAttribute",
28+
//"System.Runtime.InteropServices.LayoutKind",
29+
//"System.Runtime.InteropServices.OutAttribute",
30+
//"System.Runtime.CompilerServices.ExtensionAttribute",
31+
//"System.Runtime.CompilerServices.MethodImplAttribute",
32+
//"System.Runtime.CompilerServices.InternalsVisibleToAttribute",
33+
//"System.Runtime.CompilerServices.IndexerNameAttribute",
34+
//"System.Runtime.CompilerServices.MethodImplOptions",
35+
//"System.Reflection.FieldNoReflectionAttribute",
5336

5437
// Debugger-specific attributes
5538
"System.Diagnostics.DebuggableAttribute",
39+
//"System.Diagnostics.DebuggerBrowsableAttribute",
40+
//"System.Diagnostics.DebuggerBrowsableState",
41+
"System.Diagnostics.DebuggerHiddenAttribute",
5642
"System.Diagnostics.DebuggerNonUserCodeAttribute",
5743
"System.Diagnostics.DebuggerStepThroughAttribute",
5844
"System.Diagnostics.DebuggerDisplayAttribute",
59-
"System.Diagnostics.DebuggerBrowsableAttribute",
60-
"System.Diagnostics.DebuggerBrowsableState",
61-
"System.Diagnostics.DebuggerHiddenAttribute",
6245

6346
// Compile-time attributes
6447
"System.AttributeUsageAttribute",
@@ -73,7 +56,7 @@ public sealed class nanoTablesContext
7356
// Not supported attributes
7457
"System.MTAThreadAttribute",
7558
"System.STAThreadAttribute",
76-
"System.Reflection.DefaultMemberAttribute",
59+
//"System.Reflection.DefaultMemberAttribute",
7760
};
7861

7962
public nanoTablesContext(
@@ -87,11 +70,6 @@ public nanoTablesContext(
8770

8871
ClassNamesToExclude = classNamesToExclude;
8972

90-
foreach (var item in assemblyDefinition.CustomAttributes)
91-
{
92-
_ignoringAttributes.Add(item.AttributeType.FullName);
93-
}
94-
9573
// check CustomAttributes against list of classes to exclude
9674
foreach (var item in assemblyDefinition.CustomAttributes)
9775
{
@@ -103,6 +81,15 @@ public nanoTablesContext(
10381
}
10482
}
10583

84+
// check ignoring attributes against ClassNamesToExclude
85+
foreach(var className in ClassNamesToExclude)
86+
{
87+
if(!_ignoringAttributes.Contains(className))
88+
{
89+
_ignoringAttributes.Add(className);
90+
}
91+
}
92+
10693
var mainModule = AssemblyDefinition.MainModule;
10794

10895
// External references

source/MetadataProcessor.Core/Tables/nanoTypeDefinitionTable.cs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public nanoTypeDefinitionTable(
5757
: base(items, new TypeDefinitionEqualityComparer(), context)
5858
{
5959
TypeDefinitions = items
60-
.Select(t => t).ToList();
60+
.Select(t => t).OrderBy(t => t.FullName).ToList();
6161
}
6262

6363
/// <summary>
@@ -151,8 +151,8 @@ private void WriteClassFields(
151151
nanoBinaryWriter writer)
152152
{
153153
var firstStaticFieldId = _context.FieldsTable.MaxFieldId;
154-
var staticFieldsNumber = 0;
155-
foreach (var field in fieldsList.Where(item => item.IsStatic))
154+
var staticFieldsCount = 0;
155+
foreach (var field in fieldsList.Where(item => item.IsStatic && !item.IsLiteral))
156156
{
157157
ushort fieldReferenceId;
158158
_context.FieldsTable.TryGetFieldReferenceId(field, true, out fieldReferenceId);
@@ -161,33 +161,33 @@ private void WriteClassFields(
161161
_context.SignaturesTable.GetOrCreateSignatureId(field);
162162
_context.StringTable.GetOrCreateStringId(field.Name);
163163

164-
++staticFieldsNumber;
164+
++staticFieldsCount;
165165
}
166166

167-
var firstInstanseFieldId = _context.FieldsTable.MaxFieldId;
168-
var instanceFieldsNumber = 0;
169-
foreach (var field in fieldsList.Where(item => !item.IsStatic))
167+
var firstInstanceFieldId = _context.FieldsTable.MaxFieldId;
168+
var instanceFieldsCount = 0;
169+
foreach (var field in fieldsList.Where(item => !item.IsStatic && !item.IsLiteral))
170170
{
171171
ushort fieldReferenceId;
172172
_context.FieldsTable.TryGetFieldReferenceId(field, true, out fieldReferenceId);
173-
firstInstanseFieldId = Math.Min(firstInstanseFieldId, fieldReferenceId);
173+
firstInstanceFieldId = Math.Min(firstInstanceFieldId, fieldReferenceId);
174174

175175
_context.SignaturesTable.GetOrCreateSignatureId(field);
176176
_context.StringTable.GetOrCreateStringId(field.Name);
177177

178-
++instanceFieldsNumber;
178+
++instanceFieldsCount;
179179
}
180180

181-
if (firstStaticFieldId > firstInstanseFieldId)
181+
if (firstStaticFieldId > firstInstanceFieldId)
182182
{
183-
firstStaticFieldId = firstInstanseFieldId;
183+
firstStaticFieldId = firstInstanceFieldId;
184184
}
185185

186186
writer.WriteUInt16(firstStaticFieldId);
187-
writer.WriteUInt16(firstInstanseFieldId);
187+
writer.WriteUInt16(firstInstanceFieldId);
188188

189-
writer.WriteByte((byte) staticFieldsNumber);
190-
writer.WriteByte((byte) instanceFieldsNumber);
189+
writer.WriteByte((byte) staticFieldsCount);
190+
writer.WriteByte((byte) instanceFieldsCount);
191191
}
192192

193193
private void WriteMethodBodies(
@@ -196,31 +196,31 @@ private void WriteMethodBodies(
196196
nanoBinaryWriter writer)
197197
{
198198
ushort firstMethodId = 0xFFFF;
199-
var virtualMethodsNumber = 0;
199+
var virtualMethodsCount = 0;
200200
foreach (var method in methods.Where(item => item.IsVirtual))
201201
{
202202
firstMethodId = Math.Min(firstMethodId, _context.ByteCodeTable.GetMethodId(method));
203203
CreateMethodSignatures(method);
204-
++virtualMethodsNumber;
204+
++virtualMethodsCount;
205205
}
206206

207-
var instanceMethodsNumber = 0;
207+
var instanceMethodsCount = 0;
208208
foreach (var method in methods.Where(item => !(item.IsVirtual || item.IsStatic)))
209209
{
210210
firstMethodId = Math.Min(firstMethodId, _context.ByteCodeTable.GetMethodId(method));
211211
CreateMethodSignatures(method);
212-
++instanceMethodsNumber;
212+
++instanceMethodsCount;
213213
}
214214

215-
var staticMethodsNumber = 0;
215+
var staticMethodsCount = 0;
216216
foreach (var method in methods.Where(item => item.IsStatic))
217217
{
218218
firstMethodId = Math.Min(firstMethodId, _context.ByteCodeTable.GetMethodId(method));
219219
CreateMethodSignatures(method);
220-
++staticMethodsNumber;
220+
++staticMethodsCount;
221221
}
222222

223-
if (virtualMethodsNumber + instanceMethodsNumber + staticMethodsNumber == 0)
223+
if (virtualMethodsCount + instanceMethodsCount + staticMethodsCount == 0)
224224
{
225225
firstMethodId = _context.ByteCodeTable.NextMethodId;
226226
}
@@ -229,9 +229,9 @@ private void WriteMethodBodies(
229229

230230
writer.WriteUInt16(firstMethodId);
231231

232-
writer.WriteByte((byte)virtualMethodsNumber);
233-
writer.WriteByte((byte)instanceMethodsNumber);
234-
writer.WriteByte((byte)staticMethodsNumber);
232+
writer.WriteByte((byte)virtualMethodsCount);
233+
writer.WriteByte((byte)instanceMethodsCount);
234+
writer.WriteByte((byte)staticMethodsCount);
235235
}
236236

237237
private void CreateMethodSignatures(
@@ -344,11 +344,19 @@ internal static nanoTypeDefinitionFlags GetFlags(
344344
}
345345

346346
var baseType = definition.BaseType;
347-
if (baseType != null && baseType.FullName == "System.MulticastDelegate")
347+
if (baseType != null &&
348+
baseType.FullName == "System.MulticastDelegate")
348349
{
349350
flags |= nanoTypeDefinitionFlags.TD_MulticastDelegate;
350351
}
351352

353+
if (baseType != null &&
354+
baseType.FullName == "System.Delegate" &&
355+
definition.FullName == "System.MulticastDelegate")
356+
{
357+
flags |= nanoTypeDefinitionFlags.TD_Delegate;
358+
}
359+
352360
return flags;
353361
}
354362
}

source/MetadataProcessor.Core/Utility/nanoTypeDefinitionFlags.cs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,55 +17,58 @@ internal enum nanoTypeDefinitionFlags : ushort
1717
TD_Scope_None = 0x0000,
1818

1919
// Class is public scope.
20-
TD_Scope_Public = 0x0001,
20+
TD_Scope_Public = 0x0001,
2121
// Class is nested with public visibility.
22-
TD_Scope_NestedPublic = 0x0002,
22+
TD_Scope_NestedPublic = 0x0002,
2323
// Class is nested with private visibility.
24-
TD_Scope_NestedPrivate = 0x0003,
24+
TD_Scope_NestedPrivate = 0x0003,
2525
// Class is nested with family visibility.
26-
TD_Scope_NestedFamily = 0x0004,
26+
TD_Scope_NestedFamily = 0x0004,
2727
// Class is nested with assembly visibility.
28-
TD_Scope_NestedAssembly = 0x0005,
28+
TD_Scope_NestedAssembly = 0x0005,
2929
// Class is nested with family and assembly visibility.
30-
TD_Scope_NestedFamANDAssem = 0x0006,
30+
TD_Scope_NestedFamANDAssem = 0x0006,
3131
// Class is nested with family or assembly visibility.
32-
TD_Scope_NestedFamORAssem = 0x0007,
32+
TD_Scope_NestedFamORAssem = 0x0007,
3333

3434
/// <summary>
3535
/// Mask for scope flags
3636
/// </summary>
3737
TD_Scope =
38-
(TD_Scope_Public |
38+
TD_Scope_Public |
3939
TD_Scope_NestedPublic |
4040
TD_Scope_NestedPrivate |
4141
TD_Scope_NestedFamily |
4242
TD_Scope_NestedAssembly |
4343
TD_Scope_NestedFamANDAssem |
44-
TD_Scope_NestedFamORAssem ),
44+
TD_Scope_NestedFamORAssem,
4545

46-
TD_Serializable = 0x0008,
46+
TD_Serializable = 0x0008,
4747

48-
TD_Semantics_ValueType = 0x0010,
49-
TD_Semantics_Interface = 0x0020,
50-
TD_Semantics_Enum = 0x0030,
48+
TD_Semantics_ValueType = 0x0010,
49+
TD_Semantics_Interface = 0x0020,
50+
TD_Semantics_Enum = 0x0030,
5151

5252
/// <summary>
5353
/// Mask for semantics flags
5454
/// </summary>
55-
TD_Semantics = (TD_Semantics_ValueType | TD_Semantics_Interface | TD_Semantics_Enum),
55+
TD_Semantics =
56+
TD_Semantics_ValueType |
57+
TD_Semantics_Interface |
58+
TD_Semantics_Enum,
5659

57-
TD_Abstract = 0x0040,
58-
TD_Sealed = 0x0080,
60+
TD_Abstract = 0x0040,
61+
TD_Sealed = 0x0080,
5962

60-
TD_SpecialName = 0x0100,
61-
TD_Delegate = 0x0200,
62-
TD_MulticastDelegate = 0x0400,
63+
TD_SpecialName = 0x0100,
64+
TD_Delegate = 0x0200,
65+
TD_MulticastDelegate = 0x0400,
6366

64-
TD_Patched = 0x0800,
67+
TD_Patched = 0x0800,
6568

66-
TD_BeforeFieldInit = 0x1000,
67-
TD_HasSecurity = 0x2000,
68-
TD_HasFinalizer = 0x4000,
69-
TD_HasAttributes = 0x8000,
69+
TD_BeforeFieldInit = 0x1000,
70+
TD_HasSecurity = 0x2000,
71+
TD_HasFinalizer = 0x4000,
72+
TD_HasAttributes = 0x8000,
7073
}
7174
}

0 commit comments

Comments
 (0)