Skip to content

Commit 58460dc

Browse files
committed
Update vendor suffix identification (but not preservation/prettification) to work with OpenCL
1 parent fc89700 commit 58460dc

1 file changed

Lines changed: 46 additions & 26 deletions

File tree

sources/SilkTouch/SilkTouch/Mods/MixKhronosData.cs

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ public Dictionary<
125125
/// A mapping from struct type to information about the structure type member.
126126
/// </summary>
127127
public Dictionary<string, StructureTypeMember> StructureTypeMembers = [];
128+
129+
/// <summary>
130+
/// Whether the API is determined to be likely in OpenCL's style.
131+
/// </summary>
132+
/// <remarks>
133+
/// OpenCL differs strongly from the rest of the Khronos APIs so this is used to special case some functionality.
134+
/// </remarks>
135+
public bool IsLikelyOpenCL { get; set; } = false;
128136
}
129137

130138
/// <summary>
@@ -454,15 +462,23 @@ public async Task ExecuteAsync(IModContext ctx, CancellationToken ct = default)
454462
}
455463

456464
// Rewrite phase 3
457-
var rewriter3 = new RewriterPhase3(jobData, currentConfig);
458-
foreach (var docId in proj.DocumentIds)
459465
{
460-
var doc =
461-
proj.GetDocument(docId) ?? throw new InvalidOperationException("Document missing");
462-
proj = doc.WithSyntaxRoot(
463-
rewriter3.Visit(await doc.GetSyntaxRootAsync(ct))
464-
?? throw new InvalidOperationException("Visit returned null.")
465-
).Project;
466+
// OpenCL uses lower snake case names
467+
var vendorSuffixComparison = jobData.IsLikelyOpenCL
468+
? StringComparison.OrdinalIgnoreCase
469+
: StringComparison.Ordinal;
470+
471+
var rewriter3 = new RewriterPhase3(jobData, currentConfig, vendorSuffixComparison);
472+
foreach (var docId in proj.DocumentIds)
473+
{
474+
var doc =
475+
proj.GetDocument(docId)
476+
?? throw new InvalidOperationException("Document missing");
477+
proj = doc.WithSyntaxRoot(
478+
rewriter3.Visit(await doc.GetSyntaxRootAsync(ct))
479+
?? throw new InvalidOperationException("Visit returned null.")
480+
).Project;
481+
}
466482
}
467483

468484
// Rewrite phase 4
@@ -1981,7 +1997,11 @@ private bool TryGetManagedEnumType(
19811997
/// <summary>
19821998
/// This rewriter identifies and extracts vendor extension suffixes into [NameSuffix] attributes.
19831999
/// </summary>
1984-
private class RewriterPhase3(JobData job, Configuration config) : CSharpSyntaxRewriter
2000+
private class RewriterPhase3(
2001+
JobData job,
2002+
Configuration config,
2003+
StringComparison vendorSuffixComparison
2004+
) : CSharpSyntaxRewriter
19852005
{
19862006
private SyntaxList<AttributeListSyntax> ProcessAndGetNewAttributes(
19872007
SyntaxList<AttributeListSyntax> attributeLists,
@@ -2010,12 +2030,12 @@ private SyntaxList<AttributeListSyntax> ProcessAndGetNewAttributes(
20102030
// Try to identify vendor suffixes
20112031
foreach (var vendor in job.Vendors)
20122032
{
2013-
if (trimmedName.EndsWith(vendor))
2033+
if (trimmedName.EndsWith(vendor, vendorSuffixComparison))
20142034
{
20152035
attributeLists = attributeLists.AddNameAffix(
20162036
NameAffixType.Suffix,
20172037
"KhronosVendor",
2018-
vendor
2038+
trimmedName[^vendor.Length..]
20192039
);
20202040
trimmedName = trimmedName[..^vendor.Length];
20212041

@@ -2106,7 +2126,9 @@ public override SyntaxNode VisitEnumDeclaration(EnumDeclarationSyntax node)
21062126

21072127
var groupInfo = job.Groups.GetValueOrDefault(managedTypeName);
21082128

2109-
var typeVendor = job.Vendors.FirstOrDefault(nativeTypeName.EndsWith);
2129+
var typeVendor = job.Vendors.FirstOrDefault(s =>
2130+
nativeTypeName.EndsWith(s, vendorSuffixComparison)
2131+
);
21102132
var hasTypeSuffix = typeVendor != null;
21112133
var vendorAffixType = "KhronosVendor";
21122134

@@ -2127,7 +2149,9 @@ public override SyntaxNode VisitEnumDeclaration(EnumDeclarationSyntax node)
21272149
var exclusiveVendor = groupInfo?.ExclusiveVendor ?? typeVendor;
21282150
if (
21292151
exclusiveVendor == null
2130-
|| !node.Members.All(member => member.Identifier.Text.EndsWith(exclusiveVendor))
2152+
|| !node.Members.All(member =>
2153+
member.Identifier.Text.EndsWith(exclusiveVendor, vendorSuffixComparison)
2154+
)
21312155
)
21322156
{
21332157
// Not all enum members share the exclusive vendor
@@ -2167,7 +2191,7 @@ public override SyntaxNode VisitEnumDeclaration(EnumDeclarationSyntax node)
21672191
node.AttributeLists.AddNameAffix(
21682192
NameAffixType.Suffix,
21692193
vendorAffixType,
2170-
typeVendor,
2194+
nativeTypeName[^typeVendor.Length..],
21712195
true
21722196
)
21732197
);
@@ -2177,12 +2201,7 @@ public override SyntaxNode VisitEnumDeclaration(EnumDeclarationSyntax node)
21772201
var containsUnsuffixedMembers = node.Members.Any(member =>
21782202
{
21792203
var memberName = member.AttributeLists.GetNativeNameOrDefault(member.Identifier);
2180-
if (job.Vendors.FirstOrDefault(memberName.EndsWith) == null)
2181-
{
2182-
return true;
2183-
}
2184-
2185-
return false;
2204+
return !job.Vendors.Any(s => memberName.EndsWith(s, vendorSuffixComparison));
21862205
});
21872206

21882207
// We should not identify member suffixes for trimming if the enum type already contains unsuffixed members
@@ -2211,15 +2230,15 @@ .. node.Members.Select(member =>
22112230
if (
22122231
member
22132232
.AttributeLists.GetNativeNameOrDefault(member.Identifier)
2214-
.EndsWith(typeVendor)
2233+
.EndsWith(typeVendor, vendorSuffixComparison)
22152234
)
22162235
{
22172236
// Identify for trimming
22182237
return member.WithAttributeLists(
22192238
member.AttributeLists.AddNameAffix(
22202239
NameAffixType.Suffix,
22212240
"KhronosImpliedVendor",
2222-
typeVendor,
2241+
member.Identifier.Text[^typeVendor.Length..],
22232242
true
22242243
)
22252244
);
@@ -2369,7 +2388,7 @@ internal void ReadGroups(XDocument doc, JobData data, HashSet<string> vendors)
23692388
// this information will mostly be used to enhance the enums scraped from the headers (eg: native name and bitmask information).
23702389
var anyNamespaced =
23712390
doc.Element("registry")?.Elements("enums").Attributes("namespace").Any() ?? false;
2372-
var likelyOpenCL = false; // OpenCL specific
2391+
var isLikelyOpenCL = false; // OpenCL specific
23732392
var topLevelIntentionalExclusions = new HashSet<string>(); // OpenCL specific
23742393

23752394
// Parse enum groups
@@ -2457,7 +2476,7 @@ static bool IsUngroupable(string groupName) =>
24572476
// https://github.com/dotnet/Silk.NET/blob/d8919600/src/Core/Silk.NET.BuildTools/Converters/Readers/OpenCLReader.cs#L855-L870
24582477
if (!anyNamespaced && groupName is not null && !topLevelIntentionalExclusion)
24592478
{
2460-
FixupGroupNameForOpenCL(ref groupName, ref likelyOpenCL, ref isBitmask);
2479+
FixupGroupNameForOpenCL(ref groupName, ref isLikelyOpenCL, ref isBitmask);
24612480
}
24622481

24632482
// Initialize the group before enum members are parsed below
@@ -2713,7 +2732,8 @@ void AddData(XElement? element, string applicableSymbol)
27132732
}
27142733

27152734
// The relative sanity of the other specs stops here.
2716-
if (!likelyOpenCL)
2735+
data.IsLikelyOpenCL = isLikelyOpenCL;
2736+
if (!isLikelyOpenCL)
27172737
{
27182738
return;
27192739
}
@@ -2890,7 +2910,7 @@ is var splitList
28902910
// Just in case.
28912911
var tempVar = false;
28922912
var groupStr = group;
2893-
FixupGroupNameForOpenCL(ref groupStr, ref likelyOpenCL, ref tempVar);
2913+
FixupGroupNameForOpenCL(ref groupStr, ref isLikelyOpenCL, ref tempVar);
28942914

28952915
// Update the group info if it doesn't exist.
28962916
if (data.Groups.TryGetValue(groupStr, out var groupInfo))

0 commit comments

Comments
 (0)