Skip to content

Commit 67cc400

Browse files
committed
Added multiple enhancements.
1 parent adb0344 commit 67cc400

File tree

7 files changed

+113
-5
lines changed

7 files changed

+113
-5
lines changed

HexaGen/CsCodeGenerator.Delegates.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using CppAst;
44
using HexaGen.Core;
55
using System.IO;
6-
using System.Security.Cryptography;
76

87
public partial class CsCodeGenerator
98
{

HexaGen/CsCodeGenerator.Enum.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,19 @@ protected virtual void GenerateEnums(CppCompilation compilation, string outputPa
103103
}
104104
WriteEnumFile(compilation, folder, filePath, csEnum);
105105
}
106+
107+
for (int i = 0; i < config.CustomEnums.Count; i++)
108+
{
109+
var csEnum = config.CustomEnums[i];
110+
WriteEnumFile(compilation, folder, filePath, csEnum);
111+
}
106112
}
107113
else
108114
{
109115
using var writer = new CsSplitCodeWriter(filePath, config.Namespace, SetupEnumUsings(), config.HeaderInjector, 1);
110116
GenContext context = new(compilation, filePath, writer);
111117

112-
List<CsEnumMetadata> enums = new();
118+
List<CsEnumMetadata> enums = [.. config.CustomEnums];
113119

114120
for (int i = 0; i < compilation.Enums.Count; i++)
115121
{

HexaGen/CsCodeGenerator.Functions.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,17 @@ protected virtual void GenerateFunctions(CppCompilation compilation, string outp
187187
{
188188
returnType = config.GetBoolType();
189189
}
190+
191+
string attributes = $"{cppFunction.CallingConvention.GetCallingConventionDelegate()}";
192+
190193
string delegateType;
191194
if (cppFunction.Parameters.Count == 0)
192195
{
193-
delegateType = $"delegate* unmanaged[{cppFunction.CallingConvention.GetCallingConventionDelegate()}]<{returnType}>";
196+
delegateType = $"delegate* unmanaged[{attributes}]<{returnType}>";
194197
}
195198
else
196199
{
197-
delegateType = $"delegate* unmanaged[{cppFunction.CallingConvention.GetCallingConventionDelegate()}]<{config.GetNamelessParameterSignature(cppFunction.Parameters, false, true)}, {returnType}>";
200+
delegateType = $"delegate* unmanaged[{attributes}]<{config.GetNamelessParameterSignature(cppFunction.Parameters, false, true)}, {returnType}>";
198201
}
199202

200203
writer.WriteLine("#if NET5_0_OR_GREATER");
@@ -281,6 +284,9 @@ protected virtual void GenerateFunctions(CppCompilation compilation, string outp
281284
{
282285
writerfuncTable.WriteLine("internal static FunctionTable funcTable;");
283286
writerfuncTable.WriteLine();
287+
writerfuncTable.WriteLine("/// <summary>");
288+
writerfuncTable.WriteLine("/// Initializes the function table, automatically called. Do not call manually, only after <see cref=\"FreeApi\"/>.");
289+
writerfuncTable.WriteLine("/// </summary>");
284290
using (writerfuncTable.PushBlock("public static void InitApi()"))
285291
{
286292
writerfuncTable.WriteLine($"funcTable = new FunctionTable(LibraryLoader.LoadLibrary({config.GetLibraryNameFunctionName}, {config.GetLibraryExtensionFunctionName ?? "null"}), {count});");

HexaGen/CsCodeGeneratorConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,8 @@ public static CsCodeGeneratorConfig Load(string file)
680680
/// </summary>
681681
public List<string> AdditionalArguments { get; set; } = new();
682682

683+
public readonly List<CsEnumMetadata> CustomEnums = [];
684+
683685
public void Save(string path)
684686
{
685687
File.WriteAllText(path, JsonSerializer.Serialize(this, new JsonSerializerOptions()

HexaGen/GenerationStep.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace HexaGen
2+
{
3+
using CppAst;
4+
using HexaGen.Metadata;
5+
6+
public abstract class GenerationStep
7+
{
8+
public abstract string Name { get; }
9+
10+
public abstract void Execute(CppCompilation compilation, string outputPath, CsCodeGeneratorConfig config, CsCodeGeneratorMetadata metadata);
11+
}
12+
}

HexaGen/HexaGen.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
88

99
<AssemblyVersion>1.1.1</AssemblyVersion>
10-
<PackageVersion>1.1.7</PackageVersion>
10+
<PackageVersion>1.1.8</PackageVersion>
1111
<Description></Description>
1212
<PackageTags></PackageTags>
1313
<Authors>Juna Meinhold</Authors>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using CppAst;
2+
using HexaGen.Metadata;
3+
4+
namespace HexaGen.Patching
5+
{
6+
public class ConstantsToEnumPatch : PrePatch
7+
{
8+
private readonly string macroPrefix;
9+
private readonly string csEnumName;
10+
private readonly string baseType;
11+
private readonly HashSet<string> ignored;
12+
private readonly HashSet<string> extra;
13+
14+
public ConstantsToEnumPatch(string macroPrefix, string csEnumName, string baseType, HashSet<string>? ignored = null, HashSet<string>? extra = null)
15+
{
16+
this.macroPrefix = macroPrefix;
17+
this.csEnumName = csEnumName;
18+
this.baseType = baseType;
19+
this.ignored = ignored ?? [];
20+
this.extra = extra ?? [];
21+
}
22+
23+
protected override void PatchCompilation(CsCodeGeneratorConfig settings, CppCompilation compilation)
24+
{
25+
List<CppMacro> keyEnums = [];
26+
HashSet<string> itemNames = [];
27+
foreach (var macro in compilation.Macros)
28+
{
29+
if (ignored.Contains(macro.Name)) continue;
30+
if (macro.Name.StartsWith(macroPrefix) || extra.Contains(macro.Name))
31+
{
32+
keyEnums.Add(macro);
33+
itemNames.Add(macro.Name);
34+
settings.IgnoredConstants.Add(macro.Name);
35+
}
36+
}
37+
38+
CsEnumMetadata metadata = new(macroPrefix, csEnumName, [], null)
39+
{
40+
BaseType = baseType
41+
};
42+
var prefix = settings.GetEnumNamePrefixEx(macroPrefix);
43+
foreach (var macro in keyEnums)
44+
{
45+
var itemName = settings.GetEnumName(macro.Name, prefix);
46+
47+
string csValue = macro.Value;
48+
if (csValue.IsNumeric(NumberParseOptions.All))
49+
{
50+
// nothing to do.
51+
}
52+
else if (csValue.IsConstantExpression())
53+
{
54+
continue;
55+
}
56+
else if (csValue.IsString())
57+
{
58+
continue;
59+
}
60+
else if (itemNames.Contains(csValue))
61+
{
62+
csValue = settings.GetEnumName(csValue, prefix);
63+
}
64+
else
65+
{
66+
foreach (var item in itemNames)
67+
{
68+
var index = csValue.IndexOf(item);
69+
if (index != -1)
70+
{
71+
csValue = csValue.Remove(index, item.Length);
72+
csValue = csValue.Insert(index, settings.GetEnumName(item, prefix));
73+
}
74+
}
75+
}
76+
77+
CsEnumItemMetadata itemMeta = new(macro.Name, macro.Value, itemName, csValue, [], null);
78+
metadata.Items.Add(itemMeta);
79+
}
80+
settings.CustomEnums.Add(metadata);
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)