Skip to content

Commit 300253d

Browse files
committed
Now processes auto generated backing fields (#172)
(cherry picked from commit e38b40d)
1 parent 5196c25 commit 300253d

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

MetadataProcessor.Shared/SkeletonGenerator/SkeletonTemplates.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ struct Library_{{AssemblyName}}_{{Name}}{{#newline}}
6767
{{#if StaticFields}}{{#newline}}{{/if}}
6868
6969
{{#each InstanceFields}}
70-
{{#if FieldWarning}}{{FieldWarning}}{{/if}}
70+
{{#if FieldWarning}}
71+
{{FieldWarning}}{{#newline}}
72+
{{/if}}
7173
static const int FIELD__{{Name}} = {{ReferenceIndex}};{{#newline}}
7274
{{/each}}
7375
{{#if InstanceFields}}{{#newline}}{{/if}}

MetadataProcessor.Shared/nanoSkeletonGenerator.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Linq;
1313
using System.Reflection;
1414
using System.Text;
15+
using System.Text.RegularExpressions;
1516

1617
namespace nanoFramework.Tools.MetadataProcessor.Core
1718
{
@@ -465,27 +466,27 @@ private void GenerateAssemblyHeader()
465466
fieldCount = 0;
466467
foreach (var f in c.Fields.Where(f => !f.IsStatic && !f.IsLiteral))
467468
{
468-
// sanity check for field name
469-
// like auto-vars and such
470-
if (f.Name.IndexOfAny(new char[] { '<', '>' }) > 0)
469+
// rename auto-properties backing field to a valid C++ identifier
470+
string fixedFieldName = string.Empty;
471+
string fieldWarning = string.Empty;
472+
473+
if (Regex.IsMatch(f.Name, @"<\w+>k__BackingField"))
471474
{
472-
classData.InstanceFields.Add(new InstanceField()
473-
{
474-
FieldWarning = $"*** Something wrong with field '{f.Name}'. Possibly its backing field is missing (mandatory for nanoFramework).\n"
475-
});
475+
fixedFieldName = $"{f.Name.Replace("<", "").Replace(">", "_")}";
476+
fieldWarning = $"// auto-property backing field renamed to '{fixedFieldName}'";
476477
}
477-
else
478+
479+
if (_tablesContext.FieldsTable.TryGetFieldReferenceId(f, false, out ushort fieldRefId))
478480
{
479-
if (_tablesContext.FieldsTable.TryGetFieldReferenceId(f, false, out ushort fieldRefId))
481+
classData.InstanceFields.Add(new InstanceField()
480482
{
481-
classData.InstanceFields.Add(new InstanceField()
482-
{
483-
Name = f.Name,
484-
ReferenceIndex = firstInstanceFieldId++
485-
});
486-
}
487-
fieldCount++;
483+
Name = string.IsNullOrEmpty(fixedFieldName) ? f.Name : fixedFieldName,
484+
ReferenceIndex = firstInstanceFieldId++,
485+
FieldWarning = fieldWarning
486+
});
488487
}
488+
489+
fieldCount++;
489490
}
490491

491492
// methods

0 commit comments

Comments
 (0)