vulkan_gen 0.3.0

Vulkan XML specification parser and Rust binding generator. Used internally by the vulkane crate, but reusable as a standalone code generator.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
//! Tree-based XML parser for the Vulkan specification
//!
//! Uses `roxmltree` for DOM-based parsing, which correctly handles
//! nested XML elements (unlike the streaming event-bus approach).

use crate::parser::vk_types::*;
use crate::vulkan_spec_parser::VulkanSpecification;

/// Extract the text content of a node, concatenating all descendant text
/// (but not child element text). For mixed-content nodes like:
///   `const <type>VkFoo</type>* <name>pFoo</name>`
/// this returns the full reconstructed text including child element text.
fn full_text(node: roxmltree::Node) -> String {
    let mut result = String::new();
    for child in node.children() {
        if child.is_text() {
            result.push_str(child.text().unwrap_or(""));
        } else if child.is_element() {
            result.push_str(&full_text(child));
        }
    }
    result
}

/// Extract the text content of the first child element with the given tag name
fn child_element_text<'a>(node: roxmltree::Node<'a, 'a>, tag: &str) -> Option<String> {
    node.children()
        .find(|c| c.is_element() && c.tag_name().name() == tag)
        .map(|c| full_text(c))
}

/// Reconstruct the full C declaration from a mixed-content <member> or <param> node.
/// E.g., `const <type>VkFoo</type>* <name>pFoo</name>` → `"const VkFoo* pFoo"`
fn reconstruct_definition(node: roxmltree::Node) -> String {
    let mut parts = Vec::new();
    for child in node.children() {
        if child.is_text() {
            let t = child.text().unwrap_or("");
            if !t.trim().is_empty() {
                parts.push(t.to_string());
            }
        } else if child.is_element() {
            let tag = child.tag_name().name();
            if tag == "type" || tag == "name" || tag == "enum" {
                parts.push(full_text(child));
            }
            // skip <comment> children
        }
    }
    // Join parts and normalize whitespace
    let joined = parts.join(" ");
    // Collapse multiple spaces
    let mut result = String::new();
    let mut prev_space = false;
    for ch in joined.chars() {
        if ch.is_whitespace() {
            if !prev_space && !result.is_empty() {
                result.push(' ');
                prev_space = true;
            }
        } else {
            // Fix "* " → "*" adjacency: remove space before/after * for pointer declarations
            result.push(ch);
            prev_space = false;
        }
    }
    result.trim().to_string()
}

/// Get the raw XML content of a node as a string (for raw_content fields)
fn raw_xml_content(node: roxmltree::Node) -> String {
    full_text(node)
}

fn attr(node: roxmltree::Node, name: &str) -> Option<String> {
    node.attribute(name).map(|s| s.to_string())
}

/// Get a documentation comment from a node, checking both the `comment` attribute
/// and a `<comment>` child element. Returns None if neither is present.
fn comment_or_child(node: roxmltree::Node) -> Option<String> {
    if let Some(c) = node.attribute("comment") {
        return Some(c.to_string());
    }
    node.children()
        .find(|c| c.is_element() && c.tag_name().name() == "comment")
        .and_then(|c| c.text())
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
}

/// Parse the complete Vulkan specification from an XML string using DOM parsing
pub fn parse_vk_xml(xml_content: &str) -> Result<VulkanSpecification, String> {
    let doc =
        roxmltree::Document::parse(xml_content).map_err(|e| format!("XML parse error: {}", e))?;
    let root = doc.root_element(); // <registry>

    let mut spec = VulkanSpecification::default();

    for child in root.children().filter(|n| n.is_element()) {
        // Top-level api-profile filter: any element tagged for a non-
        // desktop Vulkan profile is skipped entirely. This catches
        // <feature api="vulkansc"> blocks (which would otherwise
        // contribute Vulkan SC core enum values to vulkane's enums) and
        // any other top-level element that gets tagged in future
        // vk.xml releases.
        if let Some(api) = attr(child, "api") {
            let included = api
                .split(',')
                .any(|s| matches!(s.trim(), "vulkan" | "vulkanbase"));
            if !included {
                continue;
            }
        }

        match child.tag_name().name() {
            "types" => parse_types_section(child, &mut spec),
            "enums" => parse_enums_block(child, &mut spec),
            "commands" => parse_commands_section(child, &mut spec),
            "extensions" => parse_extensions_section(child, &mut spec),
            "feature" => parse_feature(child, &mut spec),
            "platforms" => parse_platforms_section(child, &mut spec),
            "tags" => parse_tags_section(child, &mut spec),
            _ => {} // comment, spirvextensions, spirvcapabilities, sync, formats, etc.
        }
    }

    // Post-processing: merge extension enum values into base enums
    // (Feature enum values are merged inline during parse_feature)
    merge_extension_enum_values(&mut spec);

    Ok(spec)
}

/// Compute the numeric value for an extension enum entry using the Vulkan formula:
/// base_value = 1000000000 + (extnumber - 1) * 1000 + offset
/// If dir == "-", negate the result.
fn compute_extension_enum_value(extnumber: &str, offset: &str, dir: Option<&str>) -> String {
    let ext_num: i64 = extnumber.parse().unwrap_or(0);
    let off: i64 = offset.parse().unwrap_or(0);
    let mut value = 1000000000 + (ext_num - 1) * 1000 + off;
    if dir == Some("-") {
        value = -value;
    }
    value.to_string()
}

/// Merge extension enum values (from extensions' require blocks) into base enum definitions
fn merge_extension_enum_values(spec: &mut VulkanSpecification) {
    // Collect all extension enum items that extend a base enum
    let mut additions: Vec<(String, EnumValue)> = Vec::new();

    for ext in &spec.extensions {
        let ext_number = ext.number.as_deref().unwrap_or("0");

        for req_block in &ext.require_blocks {
            for item in &req_block.items {
                if item.item_type != "enum" {
                    continue;
                }
                let extends = match &item.extends {
                    Some(e) => e.clone(),
                    None => continue, // Not extending any enum
                };

                // Compute the value. Note: bitpos values and alias references
                // are stored in the EnumValue struct's separate fields, not in
                // `value`, so we leave `value` as None for those cases.
                let value = if let Some(v) = &item.value {
                    Some(v.clone())
                } else if let Some(offset) = &item.offset {
                    let ext_num = item.extnumber.as_deref().unwrap_or(ext_number);
                    Some(compute_extension_enum_value(
                        ext_num,
                        offset,
                        item.dir.as_deref(),
                    ))
                } else if item.bitpos.is_some() || item.alias.is_some() {
                    None
                } else {
                    continue;
                };

                additions.push((
                    extends,
                    EnumValue {
                        name: item.name.clone(),
                        value,
                        bitpos: item.bitpos.clone(),
                        alias: item.alias.clone(),
                        comment: item.comment.clone(),
                        api: item.api.clone(),
                        deprecated: item.deprecated.clone(),
                        protect: item.protect.clone(),
                        extnumber: item.extnumber.clone(),
                        offset: item.offset.clone(),
                        dir: item.dir.clone(),
                        extends: item.extends.clone(),
                        raw_content: item.raw_content.clone(),
                        is_alias: item.alias.is_some(),
                        source_line: None,
                    },
                ));
            }
        }
    }

    // Apply additions to base enums
    let mut seen_names: std::collections::HashSet<String> = std::collections::HashSet::new();
    // First collect existing enum value names to avoid duplicates
    for e in &spec.enums {
        for v in &e.values {
            seen_names.insert(v.name.clone());
        }
    }

    for (target_enum, value) in additions {
        if seen_names.contains(&value.name) {
            continue;
        }
        seen_names.insert(value.name.clone());

        if let Some(base_enum) = spec.enums.iter_mut().find(|e| e.name == target_enum) {
            base_enum.values.push(value);
        }
    }
}

// Feature enum extensions are handled inline during parse_feature()

// ---------------------------------------------------------------------------
// Types section: <types> contains <type category="..."> elements
// ---------------------------------------------------------------------------

fn parse_types_section(types_node: roxmltree::Node, spec: &mut VulkanSpecification) {
    for type_node in types_node
        .children()
        .filter(|n| n.is_element() && n.tag_name().name() == "type")
    {
        // Filter on the `api` attribute the same way commands and
        // struct members do — vk.xml ships some `<type>` definitions
        // for both desktop Vulkan and Vulkan SC (Safety Critical),
        // and the SC variants would otherwise leak into the generated
        // bindings as duplicate or wrong definitions. Skip any type
        // explicitly tagged with a non-desktop profile. Types with no
        // `api` attribute apply universally and are kept.
        if let Some(api) = attr(type_node, "api") {
            let included = api
                .split(',')
                .any(|s| matches!(s.trim(), "vulkan" | "vulkanbase"));
            if !included {
                continue;
            }
        }

        let category = attr(type_node, "category").unwrap_or_default();

        match category.as_str() {
            "struct" | "union" => parse_struct_or_union(type_node, &category, spec),
            "include" => parse_include(type_node, spec),
            "define" => parse_define(type_node, spec),
            _ => parse_general_type(type_node, &category, spec),
        }
    }
}

fn parse_struct_or_union(node: roxmltree::Node, category: &str, spec: &mut VulkanSpecification) {
    let name = attr(node, "name").unwrap_or_default();

    // Check for alias
    if let Some(alias) = attr(node, "alias") {
        spec.structs.push(VulkanStruct {
            name,
            category: category.to_string(),
            comment: comment_or_child(node),
            returnedonly: attr(node, "returnedonly"),
            structextends: attr(node, "structextends"),
            allowduplicate: attr(node, "allowduplicate"),
            deprecated: attr(node, "deprecated"),
            alias: Some(alias),
            api: attr(node, "api"),
            members: Vec::new(),
            raw_content: raw_xml_content(node),
            is_alias: true,
            source_line: None,
        });
        return;
    }

    let mut members = Vec::new();
    for member_node in node
        .children()
        .filter(|n| n.is_element() && n.tag_name().name() == "member")
    {
        // Filter on `api` attribute the same way commands do — vk.xml
        // sometimes lists the same struct member twice for the desktop
        // Vulkan profile vs Vulkan SC. Skip non-desktop entries.
        if let Some(api) = attr(member_node, "api") {
            let included = api
                .split(',')
                .any(|s| matches!(s.trim(), "vulkan" | "vulkanbase"));
            if !included {
                continue;
            }
        }
        let member_name = child_element_text(member_node, "name").unwrap_or_default();
        let type_name = child_element_text(member_node, "type").unwrap_or_default();
        let definition = reconstruct_definition(member_node);

        members.push(StructMember {
            name: member_name,
            type_name,
            optional: attr(member_node, "optional"),
            len: attr(member_node, "len"),
            altlen: attr(member_node, "altlen"),
            noautovalidity: attr(member_node, "noautovalidity"),
            values: attr(member_node, "values"),
            limittype: attr(member_node, "limittype"),
            selector: attr(member_node, "selector"),
            selection: attr(member_node, "selection"),
            externsync: attr(member_node, "externsync"),
            objecttype: attr(member_node, "objecttype"),
            deprecated: attr(member_node, "deprecated"),
            comment: comment_or_child(member_node),
            api: attr(member_node, "api"),
            definition,
            raw_content: raw_xml_content(member_node),
        });
    }

    spec.structs.push(VulkanStruct {
        name,
        category: category.to_string(),
        comment: comment_or_child(node),
        returnedonly: attr(node, "returnedonly"),
        structextends: attr(node, "structextends"),
        allowduplicate: attr(node, "allowduplicate"),
        deprecated: attr(node, "deprecated"),
        alias: None,
        api: attr(node, "api"),
        members,
        raw_content: raw_xml_content(node),
        is_alias: false,
        source_line: None,
    });
}

fn parse_include(node: roxmltree::Node, spec: &mut VulkanSpecification) {
    let name = attr(node, "name").unwrap_or_default();
    if name.is_empty() {
        return;
    }
    spec.includes.push(VulkanInclude {
        filename: name,
        category: "include".to_string(),
        comment: attr(node, "comment"),
        api: attr(node, "api"),
        deprecated: attr(node, "deprecated"),
        raw_content: raw_xml_content(node),
    });
}

fn parse_define(node: roxmltree::Node, spec: &mut VulkanSpecification) {
    let name = attr(node, "name")
        .or_else(|| child_element_text(node, "name"))
        .unwrap_or_default();
    if name.is_empty() {
        return;
    }

    let raw = raw_xml_content(node);

    // Determine macro type and extract parameters
    let (macro_type, parameters) = if raw.contains('(') && raw.contains(')') {
        // Function-like macro: extract parameter names
        let params = extract_macro_params(&raw);
        ("function_like".to_string(), params)
    } else {
        ("object_like".to_string(), Vec::new())
    };

    spec.macros.push(VulkanMacro {
        name: name.clone(),
        definition: raw.clone(),
        category: "define".to_string(),
        macro_type,
        comment: attr(node, "comment"),
        deprecated: attr(node, "deprecated"),
        requires: attr(node, "requires"),
        api: attr(node, "api"),
        parameters,
        raw_content: raw.clone(),
        parsed_definition: raw,
        source_line: None,
    });
}

fn extract_macro_params(definition: &str) -> Vec<String> {
    // Find content between first ( and matching )
    if let Some(start) = definition.find('(') {
        if let Some(end) = definition[start..].find(')') {
            let params_str = &definition[start + 1..start + end];
            return params_str
                .split(',')
                .map(|p| p.trim().to_string())
                .filter(|p| !p.is_empty())
                .collect();
        }
    }
    Vec::new()
}

fn type_already_exists(spec: &VulkanSpecification, name: &str) -> bool {
    spec.types.iter().any(|t| t.name == name)
        || spec.structs.iter().any(|s| s.name == name)
        || spec.enums.iter().any(|e| e.name == name)
}

fn parse_general_type(node: roxmltree::Node, category: &str, spec: &mut VulkanSpecification) {
    // Try multiple locations for the type name:
    // 1. name= attribute
    // 2. Direct <name> child element
    // 3. <proto>/<name> (newer funcpointer format)
    let name = attr(node, "name")
        .or_else(|| child_element_text(node, "name"))
        .or_else(|| {
            // Check <proto>/<name> for newer funcpointer format
            node.children()
                .find(|c| c.is_element() && c.tag_name().name() == "proto")
                .and_then(|proto| child_element_text(proto, "name"))
        })
        .unwrap_or_default();
    if name.is_empty() {
        return;
    }

    // Skip duplicates
    if type_already_exists(spec, &name) {
        return;
    }

    // Check for alias
    let is_alias = attr(node, "alias").is_some();

    // Extract type references from child <type> elements
    let type_refs: Vec<String> = node
        .children()
        .filter(|c| c.is_element() && c.tag_name().name() == "type")
        .map(|c| full_text(c))
        .collect();

    // Build definition from full text content
    let definition = {
        let text = raw_xml_content(node);
        if text.trim().is_empty() {
            None
        } else {
            Some(text)
        }
    };

    spec.types.push(VulkanType {
        name,
        category: category.to_string(),
        definition,
        api: attr(node, "api"),
        requires: attr(node, "requires"),
        bitvalues: attr(node, "bitvalues"),
        parent: attr(node, "parent"),
        objtypeenum: attr(node, "objtypeenum"),
        alias: attr(node, "alias"),
        deprecated: attr(node, "deprecated"),
        comment: comment_or_child(node),
        raw_content: raw_xml_content(node),
        type_references: type_refs,
        is_alias,
    });
}

// ---------------------------------------------------------------------------
// Enums: <enums name="..." type="..."> blocks
// ---------------------------------------------------------------------------

fn parse_enums_block(node: roxmltree::Node, spec: &mut VulkanSpecification) {
    let name = attr(node, "name").unwrap_or_default();
    let enum_type = attr(node, "type").unwrap_or_default();

    // "API Constants" block contains individual constants, not an enum
    if name == "API Constants" || enum_type.is_empty() {
        for enum_child in node
            .children()
            .filter(|n| n.is_element() && n.tag_name().name() == "enum")
        {
            let const_name = attr(enum_child, "name").unwrap_or_default();
            let is_alias = attr(enum_child, "alias").is_some();
            spec.constants.push(VulkanConstant {
                name: const_name,
                value: attr(enum_child, "value"),
                alias: attr(enum_child, "alias"),
                comment: attr(enum_child, "comment"),
                api: attr(enum_child, "api"),
                deprecated: attr(enum_child, "deprecated"),
                constant_type: attr(enum_child, "type").unwrap_or_else(|| "enum".to_string()),
                raw_content: raw_xml_content(enum_child),
                is_alias,
                source_line: None,
            });
        }
        return;
    }

    // Regular enum or bitmask
    let mut values = Vec::new();
    for enum_child in node
        .children()
        .filter(|n| n.is_element() && n.tag_name().name() == "enum")
    {
        let is_alias = attr(enum_child, "alias").is_some();
        values.push(EnumValue {
            name: attr(enum_child, "name").unwrap_or_default(),
            value: attr(enum_child, "value"),
            bitpos: attr(enum_child, "bitpos"),
            alias: attr(enum_child, "alias"),
            comment: attr(enum_child, "comment"),
            api: attr(enum_child, "api"),
            deprecated: attr(enum_child, "deprecated"),
            protect: attr(enum_child, "protect"),
            extnumber: None,
            offset: None,
            dir: None,
            extends: None,
            raw_content: raw_xml_content(enum_child),
            is_alias,
            source_line: None,
        });
    }

    spec.enums.push(VulkanEnum {
        name,
        enum_type,
        comment: attr(node, "comment"),
        bitwidth: attr(node, "bitwidth"),
        deprecated: attr(node, "deprecated"),
        api: attr(node, "api"),
        values,
        raw_content: raw_xml_content(node),
        is_alias: false,
        source_line: None,
    });
}

// ---------------------------------------------------------------------------
// Commands: <commands> → <command>
// ---------------------------------------------------------------------------

fn parse_commands_section(commands_node: roxmltree::Node, spec: &mut VulkanSpecification) {
    for cmd_node in commands_node
        .children()
        .filter(|n| n.is_element() && n.tag_name().name() == "command")
    {
        // Filter out commands explicitly tagged for a non-desktop API
        // profile (Vulkan SC). The same filter applies symmetrically to
        // <param> children inside parse_command.
        if let Some(api) = attr(cmd_node, "api") {
            let included = api
                .split(',')
                .any(|s| matches!(s.trim(), "vulkan" | "vulkanbase"));
            if !included {
                continue;
            }
        }

        // Skip duplicate commands (some appear twice for vulkan/vulkansc APIs)
        let name = cmd_node
            .children()
            .find(|n| n.is_element() && n.tag_name().name() == "proto")
            .and_then(|proto| child_element_text(proto, "name"))
            .or_else(|| attr(cmd_node, "name"));
        if let Some(ref n) = name {
            if spec.functions.iter().any(|f| f.name == *n) {
                continue;
            }
        }
        parse_command(cmd_node, spec);
    }
}

fn parse_command(node: roxmltree::Node, spec: &mut VulkanSpecification) {
    // Alias commands: <command name="vkFoo" alias="vkBar"/>
    if let Some(alias) = attr(node, "alias") {
        let name = attr(node, "name").unwrap_or_default();
        spec.functions.push(VulkanCommand {
            name,
            return_type: String::new(),
            comment: attr(node, "comment"),
            successcodes: attr(node, "successcodes"),
            errorcodes: attr(node, "errorcodes"),
            alias: Some(alias),
            api: attr(node, "api"),
            deprecated: attr(node, "deprecated"),
            cmdbufferlevel: attr(node, "cmdbufferlevel"),
            pipeline: attr(node, "pipeline"),
            queues: attr(node, "queues"),
            renderpass: attr(node, "renderpass"),
            videocoding: attr(node, "videocoding"),
            parameters: Vec::new(),
            raw_content: raw_xml_content(node),
            is_alias: true,
            source_line: None,
        });
        return;
    }

    // Full command definition: <command><proto>...</proto><param>...</param>...</command>
    let proto = node
        .children()
        .find(|n| n.is_element() && n.tag_name().name() == "proto");

    let (name, return_type) = if let Some(proto_node) = proto {
        let cmd_name = child_element_text(proto_node, "name").unwrap_or_default();
        let ret_type = child_element_text(proto_node, "type").unwrap_or_default();
        (cmd_name, ret_type)
    } else {
        (String::new(), String::new())
    };

    let mut parameters = Vec::new();
    for param_node in node
        .children()
        .filter(|n| n.is_element() && n.tag_name().name() == "param")
    {
        // Filter on `api` attribute: vk.xml sometimes lists the same
        // parameter twice for the desktop Vulkan profile vs Vulkan SC
        // (Safety Critical). vulkane targets desktop Vulkan, so skip
        // entries explicitly tagged for a non-desktop profile. Entries
        // with no `api` attribute apply to all profiles and are kept.
        if let Some(api) = attr(param_node, "api") {
            // The attribute is a comma-separated list. Keep the param
            // only if it lists "vulkan" or "vulkanbase".
            let included = api
                .split(',')
                .any(|s| matches!(s.trim(), "vulkan" | "vulkanbase"));
            if !included {
                continue;
            }
        }
        let param_name = child_element_text(param_node, "name").unwrap_or_default();
        let type_name = child_element_text(param_node, "type").unwrap_or_default();
        let definition = reconstruct_definition(param_node);

        parameters.push(CommandParam {
            name: param_name,
            type_name,
            optional: attr(param_node, "optional"),
            len: attr(param_node, "len"),
            altlen: attr(param_node, "altlen"),
            externsync: attr(param_node, "externsync"),
            noautovalidity: attr(param_node, "noautovalidity"),
            objecttype: attr(param_node, "objecttype"),
            stride: attr(param_node, "stride"),
            validstructs: attr(param_node, "validstructs"),
            api: attr(param_node, "api"),
            deprecated: attr(param_node, "deprecated"),
            comment: attr(param_node, "comment"),
            definition,
            raw_content: raw_xml_content(param_node),
            source_line: None,
        });
    }

    spec.functions.push(VulkanCommand {
        name,
        return_type,
        comment: attr(node, "comment"),
        successcodes: attr(node, "successcodes"),
        errorcodes: attr(node, "errorcodes"),
        alias: None,
        api: attr(node, "api"),
        deprecated: attr(node, "deprecated"),
        cmdbufferlevel: attr(node, "cmdbufferlevel"),
        pipeline: attr(node, "pipeline"),
        queues: attr(node, "queues"),
        renderpass: attr(node, "renderpass"),
        videocoding: attr(node, "videocoding"),
        parameters,
        raw_content: raw_xml_content(node),
        is_alias: false,
        source_line: None,
    });
}

// ---------------------------------------------------------------------------
// Extensions: <extensions> → <extension>
// ---------------------------------------------------------------------------

fn parse_extensions_section(extensions_node: roxmltree::Node, spec: &mut VulkanSpecification) {
    for ext_node in extensions_node
        .children()
        .filter(|n| n.is_element() && n.tag_name().name() == "extension")
    {
        parse_extension(ext_node, spec);
    }
}

fn parse_extension(node: roxmltree::Node, spec: &mut VulkanSpecification) {
    let name = attr(node, "name").unwrap_or_default();
    let number = attr(node, "number");

    let mut require_blocks = Vec::new();
    let mut remove_blocks = Vec::new();

    for child in node.children().filter(|n| n.is_element()) {
        match child.tag_name().name() {
            "require" => {
                require_blocks.push(parse_require_block(child, &number));
            }
            "remove" => {
                remove_blocks.push(parse_remove_block(child));
            }
            _ => {}
        }
    }

    spec.extensions.push(VulkanExtension {
        name,
        number,
        extension_type: attr(node, "type"),
        requires: attr(node, "requires"),
        requires_core: attr(node, "requiresCore"),
        author: attr(node, "author"),
        contact: attr(node, "contact"),
        supported: attr(node, "supported"),
        ratified: attr(node, "ratified"),
        deprecated: attr(node, "deprecated"),
        obsoletedby: attr(node, "obsoletedby"),
        promotedto: attr(node, "promotedto"),
        provisional: attr(node, "provisional"),
        specialuse: attr(node, "specialuse"),
        platform: attr(node, "platform"),
        comment: attr(node, "comment"),
        api: attr(node, "api"),
        sortorder: attr(node, "sortorder"),
        require_blocks,
        remove_blocks,
        raw_content: raw_xml_content(node),
        source_line: None,
    });
}

fn parse_require_block(node: roxmltree::Node, ext_number: &Option<String>) -> ExtensionRequire {
    let mut items = Vec::new();

    for child in node.children().filter(|n| n.is_element()) {
        // Per-item api filter inside <require> blocks. Skip items
        // explicitly tagged for non-desktop Vulkan profiles.
        if let Some(api) = attr(child, "api") {
            let included = api
                .split(',')
                .any(|s| matches!(s.trim(), "vulkan" | "vulkanbase"));
            if !included {
                continue;
            }
        }

        let tag = child.tag_name().name();
        let item_type = match tag {
            "command" => "command",
            "type" => "type",
            "enum" => "enum",
            "comment" => continue,
            _ => continue,
        };

        let name = attr(child, "name").unwrap_or_default();
        if name.is_empty() {
            continue;
        }

        // For enum extensions, use the extension's number if not specified on the item
        let extnumber = attr(child, "extnumber").or_else(|| ext_number.clone());

        items.push(RequireItem {
            item_type: item_type.to_string(),
            name,
            comment: attr(child, "comment"),
            api: attr(child, "api"),
            deprecated: attr(child, "deprecated"),
            value: attr(child, "value"),
            bitpos: attr(child, "bitpos"),
            offset: attr(child, "offset"),
            dir: attr(child, "dir"),
            extends: attr(child, "extends"),
            extnumber,
            alias: attr(child, "alias"),
            protect: attr(child, "protect"),
            raw_content: raw_xml_content(child),
        });
    }

    ExtensionRequire {
        api: attr(node, "api"),
        profile: attr(node, "profile"),
        extension: attr(node, "extension"),
        feature: attr(node, "feature"),
        comment: attr(node, "comment"),
        depends: attr(node, "depends"),
        items,
        raw_content: raw_xml_content(node),
    }
}

fn parse_remove_block(node: roxmltree::Node) -> ExtensionRemove {
    let mut items = Vec::new();

    for child in node.children().filter(|n| n.is_element()) {
        let tag = child.tag_name().name();
        let item_type = match tag {
            "command" => "command",
            "type" => "type",
            "enum" => "enum",
            _ => continue,
        };

        items.push(RemoveItem {
            item_type: item_type.to_string(),
            name: attr(child, "name").unwrap_or_default(),
            comment: attr(child, "comment"),
            api: attr(child, "api"),
            raw_content: raw_xml_content(child),
        });
    }

    ExtensionRemove {
        api: attr(node, "api"),
        profile: attr(node, "profile"),
        comment: attr(node, "comment"),
        items,
        raw_content: raw_xml_content(node),
    }
}

// ---------------------------------------------------------------------------
// Features: <feature api="..." name="..." number="...">
// ---------------------------------------------------------------------------

fn parse_feature(node: roxmltree::Node, spec: &mut VulkanSpecification) {
    let mut require_blocks = Vec::new();

    // Collect enum extension items from feature require blocks.
    // These extend base enums (e.g., VkStructureType) with values
    // for core API versions (Vulkan 1.1, 1.2, 1.3, 1.4).
    let mut enum_extensions: Vec<(String, EnumValue)> = Vec::new();

    for child in node
        .children()
        .filter(|n| n.is_element() && n.tag_name().name() == "require")
    {
        let mut items = Vec::new();
        for item_node in child.children().filter(|n| n.is_element()) {
            let tag = item_node.tag_name().name();
            let item_type = match tag {
                "command" => "command",
                "type" => "type",
                "enum" => {
                    // Check if this enum item extends a base enum
                    if let Some(extends) = attr(item_node, "extends") {
                        let value = if let Some(v) = attr(item_node, "value") {
                            Some(v)
                        } else if let Some(offset) = attr(item_node, "offset") {
                            let ext_num =
                                attr(item_node, "extnumber").unwrap_or_else(|| "0".to_string());
                            Some(compute_extension_enum_value(
                                &ext_num,
                                &offset,
                                attr(item_node, "dir").as_deref(),
                            ))
                        } else {
                            None
                        };

                        enum_extensions.push((
                            extends,
                            EnumValue {
                                name: attr(item_node, "name").unwrap_or_default(),
                                value,
                                bitpos: attr(item_node, "bitpos"),
                                alias: attr(item_node, "alias"),
                                comment: attr(item_node, "comment"),
                                api: attr(item_node, "api"),
                                deprecated: attr(item_node, "deprecated"),
                                protect: attr(item_node, "protect"),
                                extnumber: attr(item_node, "extnumber"),
                                offset: attr(item_node, "offset"),
                                dir: attr(item_node, "dir"),
                                extends: attr(item_node, "extends"),
                                raw_content: raw_xml_content(item_node),
                                is_alias: attr(item_node, "alias").is_some(),
                                source_line: None,
                            },
                        ));
                        continue; // Don't add to feature items
                    }
                    "enum"
                }
                "comment" => continue,
                _ => continue,
            };

            items.push(FeatureItem {
                item_type: item_type.to_string(),
                name: attr(item_node, "name").unwrap_or_default(),
                comment: attr(item_node, "comment"),
                api: attr(item_node, "api"),
                deprecated: attr(item_node, "deprecated"),
                raw_content: raw_xml_content(item_node),
            });
        }

        require_blocks.push(FeatureRequire {
            api: attr(child, "api"),
            profile: attr(child, "profile"),
            comment: attr(child, "comment"),
            items,
            raw_content: raw_xml_content(child),
        });
    }

    spec.features.push(VulkanFeature {
        api: attr(node, "api").unwrap_or_default(),
        name: attr(node, "name").unwrap_or_default(),
        number: attr(node, "number").unwrap_or_default(),
        comment: attr(node, "comment"),
        deprecated: attr(node, "deprecated"),
        require_blocks,
        raw_content: raw_xml_content(node),
    });

    // Merge feature enum extensions into base enums
    let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
    for e in &spec.enums {
        for v in &e.values {
            seen.insert(v.name.clone());
        }
    }
    for (target, value) in enum_extensions {
        if seen.contains(&value.name) {
            continue;
        }
        seen.insert(value.name.clone());
        if let Some(base_enum) = spec.enums.iter_mut().find(|e| e.name == target) {
            base_enum.values.push(value);
        }
    }
}

// ---------------------------------------------------------------------------
// Platforms: <platforms> → <platform>
// ---------------------------------------------------------------------------

fn parse_platforms_section(node: roxmltree::Node, spec: &mut VulkanSpecification) {
    for platform in node
        .children()
        .filter(|n| n.is_element() && n.tag_name().name() == "platform")
    {
        spec.platforms.push(VulkanPlatform {
            name: attr(platform, "name").unwrap_or_default(),
            protect: attr(platform, "protect").unwrap_or_default(),
            comment: attr(platform, "comment"),
            api: attr(platform, "api"),
            deprecated: attr(platform, "deprecated"),
            raw_content: raw_xml_content(platform),
        });
    }
}

// ---------------------------------------------------------------------------
// Tags: <tags> → <tag>
// ---------------------------------------------------------------------------

fn parse_tags_section(node: roxmltree::Node, spec: &mut VulkanSpecification) {
    for tag in node
        .children()
        .filter(|n| n.is_element() && n.tag_name().name() == "tag")
    {
        spec.tags.push(VulkanTag {
            name: attr(tag, "name").unwrap_or_default(),
            author: attr(tag, "author").unwrap_or_default(),
            contact: attr(tag, "contact"),
            comment: attr(tag, "comment"),
            api: attr(tag, "api"),
            deprecated: attr(tag, "deprecated"),
            raw_content: raw_xml_content(tag),
            source_line: None,
        });
    }
}