Skip to content

Commit d4da1bd

Browse files
authored
Fix GenerateAssemblyHeader (#23)
1 parent d4863e9 commit d4da1bd

File tree

1 file changed

+59
-3
lines changed

1 file changed

+59
-3
lines changed

source/MetadataProcessor.Core/nanoSkeletonGenerator.cs

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// Copyright (c) 2019 The nanoFramework project contributors
33
// See LICENSE file in the project root for full license information.
44
//
@@ -176,7 +176,7 @@ private void GenerateAssemblyHeader()
176176

177177
// static fields
178178
int fieldCount = 0;
179-
foreach (var f in c.Fields.Where(f => f.IsStatic))
179+
foreach (var f in c.Fields.Where(f => f.IsStatic && !f.HasConstant))
180180
{
181181
classData.StaticFields.Add(new StaticField()
182182
{
@@ -185,6 +185,11 @@ private void GenerateAssemblyHeader()
185185
});
186186
}
187187

188+
int firstInstanceFieldId = GetInstanceFieldsOffset(c);
189+
190+
// 0 based index, need to add 1
191+
firstInstanceFieldId++;
192+
188193
// instance fields
189194
fieldCount = 0;
190195
foreach (var f in c.Fields.Where(f => !f.IsStatic))
@@ -206,7 +211,7 @@ private void GenerateAssemblyHeader()
206211
classData.InstanceFields.Add(new InstanceField()
207212
{
208213
Name = f.Name,
209-
ReferenceIndex = fieldRefId + 1
214+
ReferenceIndex = firstInstanceFieldId++
210215
});
211216
}
212217
fieldCount++;
@@ -259,5 +264,56 @@ private bool IsClassToExclude(TypeDefinition td)
259264
{
260265
return _tablesContext.ClassNamesToExclude.Contains(td.FullName);
261266
}
267+
268+
private int GetInstanceFieldsOffset(TypeDefinition c)
269+
{
270+
// check if this type has a base type different from System.Object
271+
if( c.BaseType != null &&
272+
c.BaseType.FullName != "System.Object")
273+
{
274+
// get base parent type fields count
275+
return GetNestedFieldsCount(c.BaseType.Resolve());
276+
}
277+
else
278+
{
279+
return 0;
280+
}
281+
}
282+
283+
private int GetNestedFieldsCount(TypeDefinition c)
284+
{
285+
ushort tt;
286+
287+
int fieldCount = 0;
288+
289+
if (c.BaseType != null &&
290+
c.BaseType.FullName != "System.Object")
291+
{
292+
// get parent type fields count
293+
fieldCount = GetNestedFieldsCount(c.BaseType.Resolve());
294+
295+
// now add the fields count from this type
296+
if (_tablesContext.TypeDefinitionTable.TryGetTypeReferenceId(c, out tt))
297+
{
298+
fieldCount += _tablesContext.TypeDefinitionTable.TypeDefinitions[tt].Fields.Count(f => !f.IsStatic);
299+
}
300+
301+
return fieldCount;
302+
}
303+
else
304+
{
305+
// get the fields count from this type
306+
307+
if (_tablesContext.TypeDefinitionTable.TryGetTypeReferenceId(c, out tt))
308+
{
309+
return _tablesContext.TypeDefinitionTable.TypeDefinitions[tt].Fields.Count(f => !f.IsStatic);
310+
}
311+
else
312+
{
313+
// can't find this type in the table
314+
return 0;
315+
}
316+
}
317+
}
262318
}
263319
}

0 commit comments

Comments
 (0)