1111using System . Threading ;
1212using System . Threading . Tasks ;
1313using System . Xml . Linq ;
14+ using ClangSharp ;
1415using Humanizer ;
1516using Microsoft . CodeAnalysis ;
1617using Microsoft . CodeAnalysis . CSharp ;
2324using Silk . NET . SilkTouch . Mods . Transformation ;
2425using Silk . NET . SilkTouch . Naming ;
2526using static Microsoft . CodeAnalysis . CSharp . SyntaxFactory ;
27+ using Type = System . Type ;
2628
2729namespace Silk . NET . SilkTouch . Mods ;
2830
@@ -37,6 +39,7 @@ public partial class MixKhronosData(
3739 IOptionsSnapshot < MixKhronosData . Configuration > cfg
3840)
3941 : IMod ,
42+ IResponseFileMod ,
4043 INameTrimmer ,
4144 IFunctionTransformer ,
4245 IApiMetadataProvider < SymbolConstraints > ,
@@ -133,6 +136,14 @@ public Dictionary<
133136 /// these are easily solvable breakages.
134137 /// </remarks>
135138 public HashSet < string > DeprecatedAliases = [ ] ;
139+
140+ /// <summary>
141+ /// Additional type remappings to pass to ClangSharp.
142+ /// </summary>
143+ /// <remarks>
144+ /// This was added for Vulkan Flags/FlagBits remappings.
145+ /// </remarks>
146+ public Dictionary < string , string > AdditionalTypeRemappings = [ ] ;
136147 }
137148
138149 /// <summary>
@@ -311,6 +322,28 @@ .. xml.Element("registry")
311322
312323 job . TypeMap . TryAdd ( type , baseType ) ;
313324 }
325+
326+ // Add mappings from Flags storage types to FlagBits enums types
327+ // We want Flags as the output, but in order to generate everything correctly,
328+ // we need to first map Flags to FlagBits, then rename FlagBits back to Flags (this is done in the rewriters).
329+ // Without this, ClangSharp will output the Flags types as the integral storage type instead of the enum
330+ foreach ( var typeElement in xml . Elements ( "registry" ) . Elements ( "types" ) . Elements ( "type" ) )
331+ {
332+ var typedef = typeElement . Element ( "type" ) ? . Value ;
333+ if ( typedef != "VkFlags" && typedef != "VkFlags64" )
334+ {
335+ continue ;
336+ }
337+
338+ var mapFrom = typeElement . Element ( "name" ) ? . Value ;
339+ var mapTo = typeElement . Attribute ( "requires" ) ? . Value ;
340+ if ( mapFrom == null || mapTo == null )
341+ {
342+ continue ;
343+ }
344+
345+ job . AdditionalTypeRemappings [ mapFrom ] = mapTo ;
346+ }
314347 }
315348
316349 /// <inheritdoc />
@@ -356,6 +389,57 @@ public async Task ExecuteAsync(IModContext ctx, CancellationToken ct = default)
356389 ctx . SourceProject = proj ;
357390 }
358391
392+ /// <inheritdoc />
393+ public Task < List < ResponseFile > > BeforeScrapeAsync ( string key , List < ResponseFile > rsps )
394+ {
395+ var job = Jobs [ key ] ;
396+
397+ var tmp = Path . GetTempFileName ( ) ;
398+ rsps = rsps . Select ( rsp =>
399+ {
400+ File . WriteAllText ( tmp , rsp . GeneratorConfiguration . HeaderText ) ;
401+ return rsp with
402+ {
403+ GeneratorConfiguration = new PInvokeGeneratorConfiguration (
404+ rsp . GeneratorConfiguration . Language ,
405+ rsp . GeneratorConfiguration . LanguageStandard ,
406+ rsp . GeneratorConfiguration . DefaultNamespace ,
407+ rsp . GeneratorConfiguration . OutputLocation ,
408+ tmp ,
409+ rsp . GeneratorConfiguration . OutputMode ,
410+ rsp . GeneratorConfiguration . ReconstructOptions ( )
411+ )
412+ {
413+ DefaultClass = rsp . GeneratorConfiguration . DefaultClass ,
414+ ExcludedNames = rsp . GeneratorConfiguration . ExcludedNames ,
415+ IncludedNames = rsp . GeneratorConfiguration . IncludedNames ,
416+ LibraryPath = rsp . GeneratorConfiguration . LibraryPath ,
417+ MethodPrefixToStrip = rsp . GeneratorConfiguration . MethodPrefixToStrip ,
418+ NativeTypeNamesToStrip = rsp . GeneratorConfiguration . NativeTypeNamesToStrip ,
419+ RemappedNames = rsp . GeneratorConfiguration . RemappedNames . Concat ( job . AdditionalTypeRemappings ) . ToDictionary ( ) ,
420+ TraversalNames = rsp . GeneratorConfiguration . TraversalNames ,
421+ TestOutputLocation = rsp . GeneratorConfiguration . TestOutputLocation ,
422+ WithAccessSpecifiers = rsp . GeneratorConfiguration . WithAccessSpecifiers ,
423+ WithAttributes = rsp . GeneratorConfiguration . WithAttributes ,
424+ WithCallConvs = rsp . GeneratorConfiguration . WithCallConvs ,
425+ WithClasses = rsp . GeneratorConfiguration . WithClasses ,
426+ WithGuids = rsp . GeneratorConfiguration . WithGuids ,
427+ WithLibraryPaths = rsp . GeneratorConfiguration . WithLibraryPaths ,
428+ WithManualImports = rsp . GeneratorConfiguration . WithManualImports ,
429+ WithNamespaces = rsp . GeneratorConfiguration . WithNamespaces ,
430+ WithSetLastErrors = rsp . GeneratorConfiguration . WithSetLastErrors ,
431+ WithSuppressGCTransitions = rsp . GeneratorConfiguration . WithSuppressGCTransitions ,
432+ WithTransparentStructs = rsp . GeneratorConfiguration . WithTransparentStructs ,
433+ WithTypes = rsp . GeneratorConfiguration . WithTypes ,
434+ WithUsings = rsp . GeneratorConfiguration . WithUsings ,
435+ WithPackings = rsp . GeneratorConfiguration . WithPackings ,
436+ }
437+ } ;
438+ } ) . ToList ( ) ;
439+
440+ return Task . FromResult ( rsps ) ;
441+ }
442+
359443 internal record EnumGroup (
360444 string Name ,
361445 string ? Type ,
@@ -1286,10 +1370,10 @@ vendorSuffix is null
12861370 ! names . ContainsKey ( newOriginal )
12871371 && (
12881372 job . Configuration . UseExtensionVendorTrimmings
1289- == ExtensionVendorTrimmingMode . All
1373+ == MixKhronosData . ExtensionVendorTrimmingMode . All
12901374 || (
12911375 job . Configuration . UseExtensionVendorTrimmings
1292- == ExtensionVendorTrimmingMode . KhronosOnly
1376+ == MixKhronosData . ExtensionVendorTrimmingMode . KhronosOnly
12931377 && vendor is "KHR" or "ARB"
12941378 )
12951379 || (
@@ -1571,7 +1655,7 @@ PointerTypeSyntax ptr
15711655 TypeSyntax ? GetTypeTransformation (
15721656 string symbolName ,
15731657 string paramName ,
1574- JobData job ,
1658+ MixKhronosData . JobData job ,
15751659 TypeSyntax type ,
15761660 int pass ,
15771661 ref bool anyNonTrivialParams
@@ -1720,7 +1804,7 @@ jobKey is null
17201804 /// This rewriter is split into two phases because NamespaceFromSyntaxNode breaks due to
17211805 /// the FieldDeclarationSyntax being modified.
17221806 /// </remarks>
1723- private class RewriterPhase1 ( JobData job , ILogger logger ) : CSharpSyntaxRewriter
1807+ private class RewriterPhase1 ( MixKhronosData . JobData job , ILogger logger ) : CSharpSyntaxRewriter
17241808 {
17251809 /// <summary>
17261810 /// Tracks enum groups that already exist in the project, prior to the generation of missing enums.
@@ -1729,7 +1813,7 @@ private class RewriterPhase1(JobData job, ILogger logger) : CSharpSyntaxRewriter
17291813
17301814 /// <summary>
17311815 /// Tracks enums that exist in the project.
1732- /// Note that this includes any enum, including enums not present in <see cref="JobData.Groups"/>.
1816+ /// Note that this includes any enum, including enums not present in <see cref="MixKhronosData. JobData.Groups"/>.
17331817 /// </summary>
17341818 public HashSet < string > AllKnownEnums { get ; } = [ ] ;
17351819
@@ -1998,7 +2082,6 @@ private class RewriterPhase2(JobData job, RewriterPhase1 phase1) : CSharpSyntaxR
19982082 }
19992083
20002084 return base . VisitFieldDeclaration ( node ) ;
2001-
20022085 }
20032086
20042087 public override SyntaxNode ? VisitPropertyDeclaration ( PropertyDeclarationSyntax node )
@@ -2019,7 +2102,6 @@ private class RewriterPhase2(JobData job, RewriterPhase1 phase1) : CSharpSyntaxR
20192102 }
20202103
20212104 return base . VisitParameter ( node ) ;
2022-
20232105 }
20242106
20252107 /// <summary>
@@ -2053,7 +2135,7 @@ private bool TryGetManagedEnumType(SyntaxList<AttributeListSyntax> attributes, [
20532135 }
20542136
20552137 [ SuppressMessage ( "ReSharper" , "MoveLocalFunctionAfterJumpStatement" ) ]
2056- internal void ReadGroups ( XDocument doc , JobData data , HashSet < string > vendors )
2138+ internal void ReadGroups ( XDocument doc , MixKhronosData . JobData data , HashSet < string > vendors )
20572139 {
20582140 // Designed to be compatible with OpenGL, EGL, WGL, GLX, and OpenCL.
20592141 // This will work for Vulkan as well, but for Vulkan the enums are actually "typedef enum"s in the headers and
0 commit comments