Skip to content

Commit e38b40d

Browse files
authored
Now processes auto generated backing fields (#172)
1 parent 12c1fa8 commit e38b40d

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
@@ -11,6 +11,7 @@
1111
using System.IO;
1212
using System.Linq;
1313
using System.Text;
14+
using System.Text.RegularExpressions;
1415

1516
namespace nanoFramework.Tools.MetadataProcessor.Core
1617
{
@@ -486,27 +487,27 @@ private void GenerateAssemblyHeader()
486487
fieldCount = 0;
487488
foreach (var f in c.Fields.Where(f => !f.IsStatic && !f.IsLiteral))
488489
{
489-
// sanity check for field name
490-
// like auto-vars and such
491-
if (f.Name.IndexOfAny(new char[] { '<', '>' }) > 0)
490+
// rename auto-properties backing field to a valid C++ identifier
491+
string fixedFieldName = string.Empty;
492+
string fieldWarning = string.Empty;
493+
494+
if (Regex.IsMatch(f.Name, @"<\w+>k__BackingField"))
492495
{
493-
classData.InstanceFields.Add(new InstanceField()
494-
{
495-
FieldWarning = $"*** Something wrong with field '{f.Name}'. Possibly its backing field is missing (mandatory for nanoFramework).\n"
496-
});
496+
fixedFieldName = $"{f.Name.Replace("<", "").Replace(">", "_")}";
497+
fieldWarning = $"// auto-property backing field renamed to '{fixedFieldName}'";
497498
}
498-
else
499+
500+
if (_tablesContext.FieldsTable.TryGetFieldReferenceId(f, false, out ushort fieldRefId))
499501
{
500-
if (_tablesContext.FieldsTable.TryGetFieldReferenceId(f, false, out ushort fieldRefId))
502+
classData.InstanceFields.Add(new InstanceField()
501503
{
502-
classData.InstanceFields.Add(new InstanceField()
503-
{
504-
Name = f.Name,
505-
ReferenceIndex = firstInstanceFieldId++
506-
});
507-
}
508-
fieldCount++;
504+
Name = string.IsNullOrEmpty(fixedFieldName) ? f.Name : fixedFieldName,
505+
ReferenceIndex = firstInstanceFieldId++,
506+
FieldWarning = fieldWarning
507+
});
509508
}
509+
510+
fieldCount++;
510511
}
511512

512513
// methods

0 commit comments

Comments
 (0)