diff --git a/MetadataProcessor.Shared/SkeletonGenerator/SkeletonTemplates.cs b/MetadataProcessor.Shared/SkeletonGenerator/SkeletonTemplates.cs index df4f5a0c..26a50a55 100644 --- a/MetadataProcessor.Shared/SkeletonGenerator/SkeletonTemplates.cs +++ b/MetadataProcessor.Shared/SkeletonGenerator/SkeletonTemplates.cs @@ -67,7 +67,9 @@ struct Library_{{AssemblyName}}_{{Name}}{{#newline}} {{#if StaticFields}}{{#newline}}{{/if}} {{#each InstanceFields}} -{{#if FieldWarning}}{{FieldWarning}}{{/if}} +{{#if FieldWarning}} + {{FieldWarning}}{{#newline}} +{{/if}} static const int FIELD__{{Name}} = {{ReferenceIndex}};{{#newline}} {{/each}} {{#if InstanceFields}}{{#newline}}{{/if}} diff --git a/MetadataProcessor.Shared/nanoSkeletonGenerator.cs b/MetadataProcessor.Shared/nanoSkeletonGenerator.cs index 21d9b0bc..44776d8a 100644 --- a/MetadataProcessor.Shared/nanoSkeletonGenerator.cs +++ b/MetadataProcessor.Shared/nanoSkeletonGenerator.cs @@ -11,6 +11,7 @@ using System.IO; using System.Linq; using System.Text; +using System.Text.RegularExpressions; namespace nanoFramework.Tools.MetadataProcessor.Core { @@ -486,27 +487,27 @@ private void GenerateAssemblyHeader() fieldCount = 0; foreach (var f in c.Fields.Where(f => !f.IsStatic && !f.IsLiteral)) { - // sanity check for field name - // like auto-vars and such - if (f.Name.IndexOfAny(new char[] { '<', '>' }) > 0) + // rename auto-properties backing field to a valid C++ identifier + string fixedFieldName = string.Empty; + string fieldWarning = string.Empty; + + if (Regex.IsMatch(f.Name, @"<\w+>k__BackingField")) { - classData.InstanceFields.Add(new InstanceField() - { - FieldWarning = $"*** Something wrong with field '{f.Name}'. Possibly its backing field is missing (mandatory for nanoFramework).\n" - }); + fixedFieldName = $"{f.Name.Replace("<", "").Replace(">", "_")}"; + fieldWarning = $"// auto-property backing field renamed to '{fixedFieldName}'"; } - else + + if (_tablesContext.FieldsTable.TryGetFieldReferenceId(f, false, out ushort fieldRefId)) { - if (_tablesContext.FieldsTable.TryGetFieldReferenceId(f, false, out ushort fieldRefId)) + classData.InstanceFields.Add(new InstanceField() { - classData.InstanceFields.Add(new InstanceField() - { - Name = f.Name, - ReferenceIndex = firstInstanceFieldId++ - }); - } - fieldCount++; + Name = string.IsNullOrEmpty(fixedFieldName) ? f.Name : fixedFieldName, + ReferenceIndex = firstInstanceFieldId++, + FieldWarning = fieldWarning + }); } + + fieldCount++; } // methods