Skip to content

Commit 21288a1

Browse files
authored
Managed enums are now exported to class declaration (#99)
***UPDATE_DEPENDENTS***
1 parent fbac357 commit 21288a1

File tree

7 files changed

+103
-10
lines changed

7 files changed

+103
-10
lines changed

source/MetadataProcessor.Core/Extensions/TypeDefinitionExtensions.cs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//
55

66
using Mono.Cecil;
7+
using System;
78

89
namespace nanoFramework.Tools.MetadataProcessor.Core.Extensions
910
{
@@ -31,10 +32,48 @@ public static bool IncludeInStub(this TypeDefinition value)
3132
return false;
3233
}
3334

34-
public static bool IsClassToExclude(this TypeDefinition value)
35+
public static bool IsToExclude(this TypeDefinition value)
3536
{
3637
return nanoTablesContext.ClassNamesToExclude.Contains(value.FullName) ||
3738
nanoTablesContext.ClassNamesToExclude.Contains(value.DeclaringType?.FullName);
3839
}
40+
41+
public static EnumDeclaration ToEnumDeclaration(this TypeDefinition source)
42+
{
43+
// sanity check (to prevent missuse)
44+
if(!source.IsEnum)
45+
{
46+
throw new ArgumentException("Can clone only TypeDefinition that are Enums.");
47+
}
48+
49+
EnumDeclaration myEnum = new EnumDeclaration()
50+
{
51+
EnumName = source.Name
52+
};
53+
54+
foreach (var f in source.Fields)
55+
{
56+
if (f.Name == "value__")
57+
{
58+
// skip value field
59+
continue;
60+
}
61+
else
62+
{
63+
// enum items are named with the enum name followed by the enum item and respective value
64+
// pattern: nnnn_yyyyy
65+
var emunItem = new EnumItem()
66+
{
67+
Name = $"{source.Name}_{f.Name}",
68+
};
69+
70+
emunItem.Value = f.Constant.ToString();
71+
72+
myEnum.Items.Add(emunItem);
73+
}
74+
}
75+
76+
return myEnum;
77+
}
3978
}
4079
}

source/MetadataProcessor.Core/SkeletonGenerator/AssemblyClass.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public class Class
2727
public List<StaticField> StaticFields = new List<StaticField>();
2828
public List<InstanceField> InstanceFields = new List<InstanceField>();
2929
public List<MethodStub> Methods = new List<MethodStub>();
30+
public List<EnumDeclaration> Enums = new List<EnumDeclaration>();
31+
3032
}
3133

3234
public class StaticField
@@ -69,4 +71,18 @@ public class ClassWithStubs
6971
{
7072
public string Name;
7173
}
74+
75+
public class EnumDeclaration
76+
{
77+
public string EnumName;
78+
79+
public List<EnumItem> Items = new List<EnumItem>();
80+
}
81+
82+
public class EnumItem
83+
{
84+
public string Name;
85+
public string Value;
86+
}
87+
7288
}

source/MetadataProcessor.Core/SkeletonGenerator/SkeletonTemplates.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ internal partial class SkeletonTemplates
4444
{{/if}}
4545
{{#newline}}
4646
47+
{{#each Classes}}
48+
{{#each Enums}}
49+
enum {{EnumName}}{{#newline}}
50+
{
51+
{{#newline}}
52+
{{#each Items}}
53+
{{Name}} = {{Value}},{{#newline}}
54+
{{/each}}
55+
56+
};{{#newline}}
57+
{{#newline}}
58+
{{/each}}
59+
{{/each}}
60+
4761
{{#each Classes}}
4862
struct Library_{{AssemblyName}}_{{Name}}{{#newline}}
4963
{{{#newline}}

source/MetadataProcessor.Core/Tables/nanoTypeDefinitionTable.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
using Mono.Cecil;
88
using Mono.Collections.Generic;
9+
using nanoFramework.Tools.MetadataProcessor.Core;
10+
using nanoFramework.Tools.MetadataProcessor.Core.Extensions;
911
using System;
1012
using System.Collections.Generic;
1113
using System.IO;
@@ -44,6 +46,8 @@ public int GetHashCode(TypeDefinition item)
4446

4547
public List<TypeDefinition> TypeDefinitions { get; private set; }
4648

49+
public List<EnumDeclaration> EnumDeclarations { get; }
50+
4751
/// <summary>
4852
/// Creates new instance of <see cref="nanoTypeDefinitionTable"/> object.
4953
/// </summary>
@@ -58,6 +62,9 @@ public nanoTypeDefinitionTable(
5862
{
5963
TypeDefinitions = items
6064
.Select(t => t).OrderBy(t => t.FullName).ToList();
65+
66+
// need to build enum declarations here, because the fields with the enums will be removed during minimization
67+
EnumDeclarations = TypeDefinitions.Where(t => t.IsEnum).Select(et => et.ToEnumDeclaration()).ToList();
6168
}
6269

6370
/// <summary>

source/MetadataProcessor.Core/nanoAssemblyBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public void Minimize()
104104

105105
foreach (var t in _tablesContext.TypeDefinitionTable.TypeDefinitions)
106106
{
107-
if (!t.IsClassToExclude())
107+
if (!t.IsToExclude())
108108
{
109109
setNew.Add(t.MetadataToken);
110110
}

source/MetadataProcessor.Core/nanoSkeletonGenerator.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
//
55

66
using Mono.Cecil;
7+
using Mono.Cecil.Rocks;
78
using Mustache;
89
using nanoFramework.Tools.MetadataProcessor.Core.Extensions;
910
using System;
1011
using System.Collections.Generic;
1112
using System.IO;
1213
using System.Linq;
14+
using System.Reflection;
1315
using System.Text;
1416

1517
namespace nanoFramework.Tools.MetadataProcessor.Core
@@ -89,7 +91,7 @@ private void GenerateStubs()
8991
foreach (var c in _tablesContext.TypeDefinitionTable.TypeDefinitions)
9092
{
9193
if (c.IncludeInStub() &&
92-
!c.IsClassToExclude())
94+
!c.IsToExclude())
9395
{
9496
var className = NativeMethodsCrc.GetClassName(c);
9597

@@ -354,7 +356,7 @@ private void GenerateAssemblyLookup()
354356
if (c.IncludeInStub())
355357
{
356358
// don't include if it's on the exclude list
357-
if (!c.IsClassToExclude())
359+
if (!c.IsToExclude())
358360
{
359361
var className = NativeMethodsCrc.GetClassName(c);
360362

@@ -378,7 +380,7 @@ private void GenerateAssemblyLookup()
378380
// need to add a NULL entry for it
379381
// unless it's on the exclude list
380382

381-
if (!c.IsClassToExclude())
383+
if (!c.IsToExclude())
382384
{
383385
assemblyLookup.LookupTable.Add(new MethodStub()
384386
{
@@ -396,7 +398,7 @@ private void GenerateAssemblyLookup()
396398
// need to add a NULL entry for each method
397399
// unless it's on the exclude list
398400

399-
if (!c.IsClassToExclude())
401+
if (!c.IsToExclude())
400402
{
401403
foreach (var m in nanoTablesContext.GetOrderedMethods(c.Methods))
402404
{
@@ -436,7 +438,7 @@ private void GenerateAssemblyHeader()
436438
foreach (var c in _tablesContext.TypeDefinitionTable.Items)
437439
{
438440
if (c.IncludeInStub() &&
439-
!c.IsClassToExclude())
441+
!c.IsToExclude())
440442
{
441443
var classData = new Class()
442444
{
@@ -520,10 +522,25 @@ private void GenerateAssemblyHeader()
520522

521523
}
522524

525+
// enums
526+
foreach (var t in c.NestedTypes.Where(nt => nt.IsEnum))
527+
{
528+
if(t.IsToExclude())
529+
{
530+
continue;
531+
}
532+
533+
// get enum from backup
534+
var enumDeclaration = _tablesContext.TypeDefinitionTable.EnumDeclarations.FirstOrDefault(td => td.EnumName == t.Name);
535+
536+
classData.Enums.Add(enumDeclaration);
537+
}
538+
523539
// anything to add to the header?
524-
if( classData.StaticFields.Count > 0 ||
540+
if ( classData.StaticFields.Count > 0 ||
525541
classData.InstanceFields.Count > 0 ||
526-
classData.Methods.Count > 0)
542+
classData.Methods.Count > 0 ||
543+
classData.Enums.Count > 0)
527544
{
528545
assemblyData.Classes.Add(classData);
529546
}

source/version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/AArnott/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
3-
"version": "2.22",
3+
"version": "2.23",
44
"release": {
55
"branchName" : "release-v{version}",
66
"versionIncrement" : "minor",

0 commit comments

Comments
 (0)