Skip to content

Commit 6636db7

Browse files
authored
Rework method definition parsing (#29)
1 parent 929a818 commit 6636db7

File tree

1 file changed

+75
-41
lines changed

1 file changed

+75
-41
lines changed

source/MetadataProcessor.Core/Tables/nanoMethodDefinitionTable.cs

Lines changed: 75 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using Mono.Cecil;
44

@@ -85,50 +85,68 @@ protected override void WriteSingleItem(
8585
writer.WriteByte(CodeWriter.CalculateStackSize(item.Body));
8686

8787
var methodSignature = _context.SignaturesTable.GetOrCreateSignatureId(item);
88-
writer.WriteUInt16(item.HasBody ?
89-
_context.SignaturesTable.GetOrCreateSignatureId(item.Body.Variables) :
90-
(item.IsAbstract || item.IsRuntime ? (ushort)0x0000 : (ushort)0xFFFF));
88+
89+
// locals signature
90+
if(item.HasBody)
91+
{
92+
writer.WriteUInt16(_context.SignaturesTable.GetOrCreateSignatureId(item.Body.Variables));
93+
}
94+
else
95+
{
96+
if( item.IsAbstract ||
97+
item.IsRuntime ||
98+
item.IsInternalCall)
99+
{
100+
writer.WriteUInt16(0x0000);
101+
}
102+
else
103+
{
104+
writer.WriteUInt16(0xFFFF);
105+
}
106+
}
107+
91108
writer.WriteUInt16(methodSignature);
92109
}
93110

94111
private uint GetFlags(
95112
MethodDefinition method)
96113
{
97-
const uint MD_Scope_Private = 0x00000001; // Accessible only by the parent type.
98-
const uint MD_Scope_FamANDAssem = 0x00000002; // Accessible by sub-types only in this Assembly.
99-
const uint MD_Scope_Assem = 0x00000003; // Accessibly by anyone in the Assembly.
100-
const uint MD_Scope_Family = 0x00000004; // Accessible only by type and sub-types.
101-
const uint MD_Scope_FamORAssem = 0x00000005; // Accessibly by sub-types anywhere, plus anyone in assembly.
102-
const uint MD_Scope_Public = 0x00000006; // Accessibly by anyone who has visibility to this scope.
103-
104-
const uint MD_Static = 0x00000010; // Defined on type, else per instance.
105-
const uint MD_Final = 0x00000020; // Method may not be overridden.
106-
const uint MD_Virtual = 0x00000040; // Method virtual.
107-
const uint MD_HideBySig = 0x00000080; // Method hides by name+sig, else just by name.
108-
109-
const uint MD_ReuseSlot = 0x00000000; // The default.
110-
const uint MD_NewSlot = 0x00000100; // Method always gets a new slot in the vtable.
111-
const uint MD_Abstract = 0x00000200; // Method does not provide an implementation.
112-
const uint MD_SpecialName = 0x00000400; // Method is special. Name describes how.
113-
const uint MD_NativeProfiled = 0x00000800;
114-
115-
const uint MD_Constructor = 0x00001000;
116-
const uint MD_StaticConstructor = 0x00002000;
117-
const uint MD_Finalizer = 0x00004000;
118-
119-
const uint MD_DelegateConstructor = 0x00010000;
120-
const uint MD_DelegateInvoke = 0x00020000;
121-
const uint MD_DelegateBeginInvoke = 0x00040000;
122-
const uint MD_DelegateEndInvoke = 0x00080000;
123-
124-
const uint MD_Synchronized = 0x01000000;
125-
const uint MD_GloballySynchronized = 0x02000000;
126-
const uint MD_Patched = 0x04000000;
127-
const uint MD_EntryPoint = 0x08000000;
128-
const uint MD_RequireSecObject = 0x10000000; // Method calls another method containing security code.
129-
const uint MD_HasSecurity = 0x20000000; // Method has security associate with it.
130-
const uint MD_HasExceptionHandlers = 0x40000000;
131-
const uint MD_HasAttributes = 0x80000000;
114+
const uint MD_Scope_Private = 0x00000001; // Accessible only by the parent type.
115+
const uint MD_Scope_FamANDAssem = 0x00000002; // Accessible by sub-types only in this Assembly.
116+
const uint MD_Scope_Assem = 0x00000003; // Accessibly by anyone in the Assembly.
117+
const uint MD_Scope_Family = 0x00000004; // Accessible only by type and sub-types.
118+
const uint MD_Scope_FamORAssem = 0x00000005; // Accessibly by sub-types anywhere, plus anyone in assembly.
119+
const uint MD_Scope_Public = 0x00000006; // Accessibly by anyone who has visibility to this scope.
120+
121+
const uint MD_Static = 0x00000010; // Defined on type, else per instance.
122+
const uint MD_Final = 0x00000020; // Method may not be overridden.
123+
const uint MD_Virtual = 0x00000040; // Method virtual.
124+
const uint MD_HideBySig = 0x00000080; // Method hides by name+sig, else just by name.
125+
126+
const uint MD_ReuseSlot = 0x00000000; // The default.
127+
const uint MD_NewSlot = 0x00000100; // Method always gets a new slot in the vtable.
128+
const uint MD_Abstract = 0x00000200; // Method does not provide an implementation.
129+
const uint MD_SpecialName = 0x00000400; // Method is special. Name describes how.
130+
const uint MD_NativeProfiled = 0x00000800;
131+
132+
const uint MD_Constructor = 0x00001000;
133+
const uint MD_StaticConstructor = 0x00002000;
134+
const uint MD_Finalizer = 0x00004000;
135+
136+
const uint MD_DelegateConstructor = 0x00010000;
137+
const uint MD_DelegateInvoke = 0x00020000;
138+
const uint MD_DelegateBeginInvoke = 0x00040000;
139+
const uint MD_DelegateEndInvoke = 0x00080000;
140+
141+
const uint MD_Synchronized = 0x01000000;
142+
const uint MD_GloballySynchronized = 0x02000000;
143+
const uint MD_Patched = 0x04000000;
144+
const uint MD_EntryPoint = 0x08000000;
145+
146+
const uint MD_RequireSecObject = 0x10000000; // Method calls another method containing security code.
147+
const uint MD_HasSecurity = 0x20000000; // Method has security associate with it.
148+
const uint MD_HasExceptionHandlers = 0x40000000;
149+
const uint MD_HasAttributes = 0x80000000;
132150

133151
uint flag = 0;
134152
if (method.IsPrivate)
@@ -189,9 +207,11 @@ private uint GetFlags(
189207
{
190208
flag |= MD_SpecialName;
191209
}
210+
192211
if (method.IsNative)
193212
{
194-
flag |= MD_NativeProfiled; // ???
213+
// can't find anything relevant to do with this...
214+
// flag |= MD_NativeProfiled; // ???
195215
}
196216

197217
if (method.IsConstructor)
@@ -205,7 +225,9 @@ private uint GetFlags(
205225
}
206226
if (method.HasCustomAttributes)
207227
{
208-
flag |= MD_HasAttributes; // ???
228+
// TODO
229+
// parse special attributes: NativeProfiler, GloballySynchronized
230+
//flag |= MD_HasAttributes; // ???
209231
}
210232

211233
if (method == method.Module.EntryPoint)
@@ -238,6 +260,18 @@ private uint GetFlags(
238260
flag |= MD_DelegateEndInvoke;
239261
}
240262
}
263+
264+
var methodName = method.Name;
265+
if (methodName == "Finalize" &&
266+
method.ReturnType.FullName == "System.Void" &&
267+
!method.HasParameters)
268+
{
269+
flag |= MD_Finalizer;
270+
271+
// TODO
272+
// missing setting tdDst->flags |= CLR_RECORD_TYPEDEF::TD_HasFinalizer;
273+
}
274+
241275
return flag;
242276
}
243277
}

0 commit comments

Comments
 (0)