Skip to content

Commit ff565c6

Browse files
committed
Refactored the generator.
1 parent 227ba4e commit ff565c6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+538
-223
lines changed

HexaGen.Core/CSharp/CsDelegate.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
namespace HexaGen.Core.CSharp
2+
{
3+
using HexaGen.Core;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text.Json.Serialization;
7+
8+
public class CsDelegate : IHasIdentifier
9+
{
10+
[JsonConstructor]
11+
public CsDelegate(string cppName, string name, CsType returnType, List<CsParameterInfo> parameters, List<string>? attributes = null, string? comment = null)
12+
{
13+
CppName = cppName;
14+
Name = name;
15+
ReturnType = returnType;
16+
Parameters = parameters;
17+
Attributes = attributes ?? [];
18+
Comment = comment;
19+
}
20+
21+
public CsDelegate(string cppName, string name, CsType returnType, List<CsParameterInfo> parameters)
22+
{
23+
CppName = cppName;
24+
Name = name;
25+
ReturnType = returnType;
26+
Parameters = parameters;
27+
Attributes = [];
28+
}
29+
30+
public string Identifier => CppName;
31+
32+
public string CppName { get; set; }
33+
34+
public string Name { get; set; }
35+
36+
public CsType ReturnType { get; set; }
37+
38+
public List<CsParameterInfo> Parameters { get; set; }
39+
40+
public List<string> Attributes { get; set; }
41+
42+
public string? Comment { get; set; }
43+
44+
public CsDelegate Clone()
45+
{
46+
return new(CppName, Name, ReturnType.Clone(), Parameters.Select(x => x.Clone()).ToList(), [.. Attributes], Comment);
47+
}
48+
}
49+
}

HexaGen.Core/CSharp/CsFunction.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
namespace HexaGen.Core.CSharp
22
{
3+
using System;
34
using System.Collections.Generic;
5+
using System.Text.Json.Serialization;
46

57
public class CsFunction
68
{
9+
[JsonConstructor]
710
public CsFunction(string name, string? comment, List<CsFunctionOverload> overloads)
811
{
912
Name = name;
@@ -28,5 +31,10 @@ public override string ToString()
2831
{
2932
return Name;
3033
}
34+
35+
public CsFunction Clone()
36+
{
37+
return new(Name, Comment, Overloads.Select(overload => overload.Clone()).ToList());
38+
}
3139
}
3240
}

HexaGen.Core/CSharp/CsFunctionOverload.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using HexaGen.Core.Collections;
44
using System.Collections.Generic;
5+
using System.Text.Json.Serialization;
56

67
public enum CsFunctionKind
78
{
@@ -15,6 +16,7 @@ public enum CsFunctionKind
1516

1617
public class CsFunctionOverload : ICsFunction, ICloneable<CsFunctionOverload>
1718
{
19+
[JsonConstructor]
1820
public CsFunctionOverload(string exportedName, string name, string? comment, Dictionary<string, string> defaultValues, string structName, CsFunctionKind kind, CsType returnType, List<CsParameterInfo> parameters, List<CsFunctionVariation> variations, List<string> modifiers, List<string> attributes)
1921
{
2022
ExportedName = exportedName;
@@ -158,7 +160,7 @@ public bool HasParameter(CsParameterInfo cppParameter)
158160

159161
public CsFunctionVariation CreateVariationWith()
160162
{
161-
return new(ExportedName, Name, StructName, Kind, ReturnType);
163+
return new(null!, ExportedName, Name, StructName, Kind, ReturnType);
162164
}
163165

164166
public CsFunctionOverload Clone()

HexaGen.Core/CSharp/CsFunctionVariation.cs

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
using System.Collections.Generic;
44
using System.Diagnostics.CodeAnalysis;
55
using System.Text;
6+
using System.Text.Json.Serialization;
67

7-
public class CsFunctionVariation : ICsFunction, ICloneable<CsFunctionVariation>
8+
public class CsFunctionVariation : ICsFunction, ICloneable<CsFunctionVariation>, IHasIdentifier
89
{
9-
public CsFunctionVariation(string exportedName, string name, string structName, CsFunctionKind kind, CsType returnType, List<CsParameterInfo> parameters, List<CsGenericParameterInfo> genericParameters, List<string> modifiers, List<string> attributes)
10+
[JsonConstructor]
11+
public CsFunctionVariation(string identifier, string exportedName, string name, string structName, CsFunctionKind kind, CsType returnType, List<CsParameterInfo> parameters, List<CsGenericParameterInfo> genericParameters, List<string> modifiers, List<string> attributes)
1012
{
13+
Identifier = identifier;
1114
ExportedName = exportedName;
1215
Name = name;
1316

@@ -20,8 +23,9 @@ public CsFunctionVariation(string exportedName, string name, string structName,
2023
Attributes = attributes;
2124
}
2225

23-
public CsFunctionVariation(string exportedName, string name, string structName, CsFunctionKind kind, CsType returnType)
26+
public CsFunctionVariation(string identifier, string exportedName, string name, string structName, CsFunctionKind kind, CsType returnType)
2427
{
28+
Identifier = identifier;
2529
ExportedName = exportedName;
2630
Name = name;
2731
StructName = structName;
@@ -33,6 +37,8 @@ public CsFunctionVariation(string exportedName, string name, string structName,
3337
Attributes = new();
3438
}
3539

40+
public string Identifier { get; set; }
41+
3642
public string ExportedName { get; set; }
3743

3844
public string Name { get; set; }
@@ -55,29 +61,79 @@ public CsFunctionVariation(string exportedName, string name, string structName,
5561

5662
#region IDs
5763

58-
public string BuildSignatureIdentifier()
64+
protected virtual string BuildFunctionSignature(CsFunctionVariation variation, bool useAttributes, bool useNames, WriteFunctionFlags flags)
65+
{
66+
int offset = flags == WriteFunctionFlags.None ? 0 : 1;
67+
StringBuilder sb = new();
68+
bool isFirst = true;
69+
70+
if (flags == WriteFunctionFlags.Extension)
71+
{
72+
isFirst = false;
73+
var first = variation.Parameters[0];
74+
if (useNames)
75+
{
76+
sb.Append($"this {first.Type} {first.Name}");
77+
}
78+
else
79+
{
80+
sb.Append($"this {first.Type}");
81+
}
82+
}
83+
84+
for (int i = offset; i < variation.Parameters.Count; i++)
85+
{
86+
var param = variation.Parameters[i];
87+
88+
if (param.DefaultValue != null)
89+
continue;
90+
91+
if (!isFirst)
92+
sb.Append(", ");
93+
94+
if (useAttributes)
95+
{
96+
sb.Append($"{string.Join(" ", param.Attributes)} ");
97+
}
98+
99+
sb.Append($"{param.Type}");
100+
101+
if (useNames)
102+
{
103+
sb.Append($" {param.Name}");
104+
}
105+
106+
isFirst = false;
107+
}
108+
109+
return sb.ToString();
110+
}
111+
112+
public string BuildFunctionHeaderId(WriteFunctionFlags flags)
59113
{
60-
return $"{Name}{(IsGeneric ? $"<{BuildGenericSignature()}>" : string.Empty)}({BuildSignature(false, false)})";
114+
string signature = BuildFunctionSignature(this, false, false, flags);
115+
return Identifier = $"{Name}({signature})";
61116
}
62117

63-
public string BuildConstructorSignatureIdentifier()
118+
public string BuildFunctionHeader(CsType csReturnType, WriteFunctionFlags flags, bool generateMetadata)
64119
{
65-
return $"{StructName}({BuildConstructorSignature(false, false, false)})";
120+
string signature = BuildFunctionSignature(this, generateMetadata, true, flags);
121+
return Identifier = $"{csReturnType.Name} {Name}({signature})";
66122
}
67123

68-
public string BuildExtensionSignatureIdentifier(string type)
124+
public string BuildConstructorSignatureIdentifier()
69125
{
70-
return $"{Name}{(IsGeneric ? $"<{BuildGenericSignature()}>" : string.Empty)}({BuildExtensionSignature(type, null, false, false)})";
126+
return Identifier = $"{StructName}({BuildConstructorSignature(false, false, false)})";
71127
}
72128

73129
public string BuildSignatureIdentifierForCOM()
74130
{
75-
return $"{ReturnType.Name} {Name}{(IsGeneric ? $"<{BuildGenericSignature()}>" : string.Empty)}({BuildSignature(false, false)})";
131+
return Identifier = $"{ReturnType.Name} {Name}{(IsGeneric ? $"<{BuildGenericSignature()}>" : string.Empty)}({BuildSignature(false, false)})";
76132
}
77133

78134
public string BuildExtensionSignatureIdentifierForCOM(string comObject)
79135
{
80-
return $"{ReturnType.Name} {Name}{(IsGeneric ? $"<{BuildGenericSignature()}>" : string.Empty)}({BuildExtensionSignatureForCOM(comObject, false, false)})";
136+
return Identifier = $"{ReturnType.Name} {Name}{(IsGeneric ? $"<{BuildGenericSignature()}>" : string.Empty)}({BuildExtensionSignatureForCOM(comObject, false, false)})";
81137
}
82138

83139
#endregion IDs
@@ -230,12 +286,12 @@ public bool TryGetParameter(string name, [NotNullWhen(true)] out CsParameterInfo
230286

231287
public CsFunctionVariation ShallowClone()
232288
{
233-
return new CsFunctionVariation(ExportedName, Name, StructName, Kind, ReturnType.Clone());
289+
return new CsFunctionVariation(Identifier, ExportedName, Name, StructName, Kind, ReturnType.Clone());
234290
}
235291

236292
public CsFunctionVariation Clone()
237293
{
238-
return new CsFunctionVariation(ExportedName, Name, StructName, Kind, ReturnType.Clone(), Parameters.CloneValues(), GenericParameters.CloneValues(), Modifiers.Clone(), Attributes.Clone());
294+
return new CsFunctionVariation(Identifier, ExportedName, Name, StructName, Kind, ReturnType.Clone(), Parameters.CloneValues(), GenericParameters.CloneValues(), Modifiers.Clone(), Attributes.Clone());
239295
}
240296
}
241297
}

HexaGen.Core/CSharp/CsGenericParameterInfo.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
namespace HexaGen.Core.CSharp
22
{
3+
using System.Text.Json.Serialization;
4+
35
public class CsGenericParameterInfo : ICloneable<CsGenericParameterInfo>
46
{
7+
[JsonConstructor]
58
public CsGenericParameterInfo(string name, string constrain)
69
{
710
Name = name;

HexaGen.Core/CSharp/CsParameterInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public enum ParameterFlags
2323

2424
public class CsParameterInfo : ICloneable<CsParameterInfo>
2525
{
26+
[JsonConstructor]
2627
public CsParameterInfo(string name, CppType cppType, CsType type, List<string> modifiers, List<string> attributes, Direction direction, string? defaultValue, string? fieldName)
2728
{
2829
Name = name;

HexaGen.Core/CSharp/CsType.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
namespace HexaGen.Core.CSharp
22
{
33
using CppAst;
4+
using System.Text.Json.Serialization;
45

56
public class CsType : ICloneable<CsType>
67
{
8+
[JsonConstructor]
79
public CsType(string name, string cleanName, bool isPointer, bool isOut, bool isRef, bool isSpan, bool isString, bool isPrimitive, bool isVoid, bool isBool, bool isArray, bool isEnum, CsStringType stringType, CsPrimitiveType primitiveType)
810
{
911
Name = name;

HexaGen/WriteFunctionFlags.cs renamed to HexaGen.Core/CSharp/WriteFunctionFlags.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace HexaGen
1+
namespace HexaGen.Core.CSharp
22
{
33
public enum WriteFunctionFlags
44
{

HexaGen.Core/HexaGen.Core.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.2</PackageVersion>
10+
<PackageVersion>1.1.3</PackageVersion>
1111
<Description></Description>
1212
<PackageTags></PackageTags>
1313
<Authors>Juna Meinhold</Authors>

HexaGen/Metadata/IHasIdentifier.cs renamed to HexaGen.Core/IHasIdentifier.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace HexaGen.Metadata
1+
namespace HexaGen.Core
22
{
33
public interface IHasIdentifier
44
{

0 commit comments

Comments
 (0)