windows-clang 0.100.0

Generates RDL from C/C++ headers using libclang
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
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
use super::*;

#[derive(Debug)]
pub struct Const {
    pub name: String,
    pub ty: Option<metadata::Type>,
    pub value: metadata::Value,
}

impl Const {
    /// Parse object-like macros whose token body has a fixed constant shape.
    pub fn parse(cursor: Cursor, parser: &mut Parser<'_>) -> Result<Option<Self>, Error> {
        if cursor.is_macro_builtin() {
            return Ok(None);
        }
        // Source-adjacency catches libclang misreports that leak macro parameters as types.
        if cursor.is_macro_function_like()
            || parser.tu.macro_is_function_like_by_source(cursor.extent())
        {
            return Ok(None);
        }

        let name = cursor.name();
        if name.is_empty() || name.starts_with('_') {
            return Ok(None);
        }

        let tokens = parser.tu.tokenize(cursor.extent());
        let body: Vec<_> = tokens.into_iter().skip(1).collect();

        if let Some((ty, value)) = parse_native_negative_cast(&body, parser.ref_map) {
            return Ok(Some(Self {
                name,
                ty: Some(ty),
                value,
            }));
        }

        let Some(value) = parse_body(&body, parser.namespace, parser.ref_map, parser.header_names)
        else {
            return Ok(None);
        };

        Ok(Some(Self {
            name,
            ty: None,
            value,
        }))
    }

    /// Parse file-scope floating-point `const` variables that flat metadata would otherwise lose.
    pub fn parse_var_decl(cursor: &Cursor) -> Option<Self> {
        let name = cursor.name();
        if name.is_empty() || name.starts_with('_') {
            return None;
        }
        let ty = cursor.ty();
        if !ty.is_const() {
            return None;
        }
        let value = match ty.canonical_type().kind() {
            CXType_Float => metadata::Value::F32(cursor.evaluate_double()? as f32),
            CXType_Double | CXType_LongDouble => metadata::Value::F64(cursor.evaluate_double()?),
            _ => return None,
        };
        Some(Self {
            name,
            ty: None,
            value,
        })
    }

    pub fn write(&self, namespace: &str) -> Result<TokenStream, Error> {
        let name = write_ident(&self.name);
        let value_ty = self.value.ty();
        let ty = self.ty.as_ref().unwrap_or(&value_ty);
        let value = write_typed_value(namespace, ty, &self.value);
        let ty = write_type(namespace, ty);
        match &self.value {
            metadata::Value::Utf8(_) => {
                let attr = native_encoding_attr("ansi");
                Ok(quote! {
                    #attr
                    const #name: #ty = #value;
                })
            }
            metadata::Value::Utf16(_) => {
                let attr = native_encoding_attr("utf-16");
                Ok(quote! {
                    #attr
                    const #name: #ty = #value;
                })
            }
            _ => Ok(quote! { const #name: #ty = #value; }),
        }
    }
}

/// Preserve a direct negative literal cast as a fixed signed Constant row on a native field.
///
/// `(ULONG_PTR)-1` is one architecture-neutral source spelling. Keeping `I32(-1)` separate from
/// the `usize` field lets the projection apply the cast for each target instead of materializing
/// separate x86 and 64-bit maximum values.
fn parse_native_negative_cast(
    body: &[(CXTokenKind, String)],
    ref_map: &HashMap<String, String>,
) -> Option<(metadata::Type, metadata::Value)> {
    let mut ty = None;
    let mut literal = None;
    let mut negate = false;

    for (kind, token) in body {
        match *kind {
            CXToken_Punctuation if token == "(" || token == ")" => {}
            CXToken_Punctuation if token == "-" && !negate && literal.is_none() => negate = true,
            CXToken_Identifier if ty.is_none() => {
                if ref_map.contains_key(token) {
                    return None;
                }
                ty = pointer_sized_abi(token);
                ty.as_ref()?;
            }
            CXToken_Literal if literal.is_none() => literal = Some(token.as_str()),
            _ => return None,
        }
    }

    if !negate {
        return None;
    }
    let value = parse_literal(literal?, true)?;
    if !matches!(
        value,
        metadata::Value::I32(_)
            | metadata::Value::U32(_)
            | metadata::Value::I64(_)
            | metadata::Value::U64(_)
    ) {
        return None;
    }
    Some((ty?, value))
}

/// A GUID constant from a forward-declared C++ class with `__declspec(uuid(...))`.
/// MIDL uses this for COM server activation CLSIDs.
#[derive(Debug)]
pub struct GuidConst {
    pub name: String,
    pub uuid: String,
}

impl GuidConst {
    pub fn write(&self) -> Result<TokenStream, Error> {
        let name = write_ident(&self.name);
        let lit_str = uuid_to_u128_literal(&self.uuid);
        let lit = syn::LitInt::new(&lit_str, Span::call_site());
        Ok(quote! { const #name: GUID = #lit; })
    }
}

/// A `PROPERTYKEY`/`DEVPROPKEY` macro constant: GUID in `#[guid]`, `pid` as the value.
#[derive(Debug)]
pub struct PropertyKeyConst {
    pub name: String,
    pub ty: String,
    pub uuid: String,
    pub pid: u32,
}

impl PropertyKeyConst {
    pub fn write(&self) -> Result<TokenStream, Error> {
        let name = write_ident(&self.name);
        let ty = write_ident(&self.ty);
        let guid = syn::LitInt::new(&uuid_to_u128_literal(&self.uuid), Span::call_site());
        let pid = syn::LitInt::new(&format!("{}u32", self.pid), Span::call_site());
        Ok(quote! {
            #[guid(#guid)]
            const #name: #ty = #pid;
        })
    }
}

impl Const {
    /// Evaluate macro expressions by injecting per-name enum probes into a synthetic TU.
    /// Independent probes and `CXTranslationUnit_KeepGoing` let one bad macro fail without
    /// hiding the rest; size/signedness probes recover the C integer type clang otherwise drops.
    pub fn evaluate_macros(
        input: &str,
        names: &[String],
        index: &Index,
        args: &[&str],
    ) -> Result<Vec<Self>, Error> {
        if names.is_empty() {
            return Ok(vec![]);
        }

        // Put the synthetic file beside the header; include by basename so relative includes work.
        let input_basename = Path::new(input)
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or(input);
        let prefix = format!("#include \"{input_basename}\"\n{NARG_PROLOGUE}");

        let synthetic = format!("{input}.__rdl_eval__.cpp");

        Self::evaluate_names(&prefix, &synthetic, names, index, args)
    }

    /// Evaluate macro names from in-memory source by embedding it into the synthetic TU.
    pub fn evaluate_macros_str(
        content: &str,
        names: &[String],
        index: &Index,
        args: &[&str],
    ) -> Result<Vec<Self>, Error> {
        if names.is_empty() {
            return Ok(vec![]);
        }

        // There is no on-disk directory context, so relative includes may not resolve.
        let prefix = format!("{content}\n{NARG_PROLOGUE}");
        const SYNTHETIC: &str = "__rdl_input_text_eval__.cpp";

        Self::evaluate_names(&prefix, SYNTHETIC, names, index, args)
    }

    /// Evaluate names in batches, retrying swallowed probe enums in smaller batches.
    /// Validity comes from recovered enum values, not diagnostics; unbalanced delimiters can
    /// consume following probes, so missing names are requeued until the poison macro is isolated.
    fn evaluate_names(
        prefix: &str,
        synthetic: &str,
        names: &[String],
        index: &Index,
        args: &[&str],
    ) -> Result<Vec<Self>, Error> {
        let eval_args = with_unlimited_errors(args);
        let mut results = vec![];
        // Swallowed probes are split until a singleton poison macro can be dropped.
        let mut queue: Vec<Vec<String>> = vec![names.to_vec()];
        while let Some(batch) = queue.pop() {
            let mut source = String::from(prefix);
            for name in &batch {
                // The name came from libclang, but validate before writing generated C++.
                if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
                    continue;
                }
                source.push_str(&eval_probe(name));
            }
            let tu =
                index.parse_unsaved(synthetic, &source, &eval_args, CXTranslationUnit_KeepGoing)?;
            let (consts, present) = collect_eval_results(&tu);
            results.extend(consts);

            let missing: Vec<String> = batch
                .iter()
                .filter(|n| n.chars().all(|c| c.is_ascii_alphanumeric() || c == '_'))
                .filter(|n| !present.contains(n.as_str()))
                .cloned()
                .collect();
            if !missing.is_empty() && batch.len() > 1 {
                let retry = if missing.len() == batch.len() {
                    &batch
                } else {
                    &missing
                };
                if retry.len() > 1 {
                    let mid = retry.len() / 2;
                    queue.push(retry[mid..].to_vec());
                    queue.push(retry[..mid].to_vec());
                }
            }
        }
        Ok(results)
    }
}

/// Disable clang's error cap so bad probe enums do not abort the TU before later valid macros.
fn with_unlimited_errors<'a>(args: &[&'a str]) -> Vec<&'a str> {
    let mut out = args.to_vec();
    out.push("-ferror-limit=0");
    out
}

/// Counts top-level comma-separated macro-expansion results for the shape gate.
/// This rejects GUID initializer lists before the C comma operator can fold them to an integer.
const NARG_PROLOGUE: &str = "\
#define __RDL_NARG(...) __RDL_NARG_(__VA_ARGS__,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0)\n\
#define __RDL_NARG_(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,N,...) N\n";

/// Emits independent enum probes for one macro: value, integer gate, comma-count gate,
/// width, and signedness. `& 0` rejects pointer/string/floating expressions, while
/// `__RDL_NARG` catches post-expansion comma lists such as GUID initializers.
/// Validity is read from recovered enum values, not diagnostics.
fn eval_probe(name: &str) -> String {
    format!(
        "constexpr auto __rdl_eval_{name} = ({name});\n\
         enum {{ __rdl_ok_{name} = (({name}) & 0) + 1 }};\n\
         enum {{ __rdl_nc_{name} = __RDL_NARG({name}) }};\n\
         enum {{ __rdl_sz_{name} = sizeof({name}) }};\n\
         enum {{ __rdl_sg_{name} = ((({name}) * 0 - 1) < 0) ? 1 : 2 }};\n"
    )
}

/// Collect kept eval probes and the names whose gating probes were fully parsed.
/// Missing gates mean a preceding macro swallowed later enum declarations, so the caller
/// retries those names; failed gates are real rejects and are not retried.
fn collect_eval_results(tu: &TranslationUnit) -> (Vec<Const>, HashSet<String>) {
    let mut evals: Vec<(String, u64, i64, Option<metadata::Type>)> = vec![];
    let mut eval_seen: HashSet<String> = HashSet::new();
    let mut ok_seen: HashSet<String> = HashSet::new();
    let mut nc_seen: HashSet<String> = HashSet::new();
    let mut type_ok: HashSet<String> = HashSet::new();
    let mut shape_ok: HashSet<String> = HashSet::new();
    let mut sizes: HashMap<String, i64> = HashMap::new();
    let mut signs: HashMap<String, i64> = HashMap::new();
    for child in tu.cursor().children() {
        if !child.is_from_main_file() {
            continue;
        }
        if child.kind() == CXCursor_VarDecl
            && let Some(original_name) = child.name().strip_prefix("__rdl_eval_")
        {
            eval_seen.insert(original_name.to_string());
            if let Some((unsigned, signed)) = child.evaluate_integer() {
                let ty = child.ty();
                let semantic = pointer_sized_abi(&ty.ty().name());
                evals.push((original_name.to_string(), unsigned, signed, semantic));
            }
            continue;
        }
        if child.kind() != CXCursor_EnumDecl {
            continue;
        }
        for constant in child.children() {
            if constant.kind() != CXCursor_EnumConstantDecl {
                continue;
            }
            let const_name = constant.name();
            if let Some(original_name) = const_name.strip_prefix("__rdl_ok_") {
                ok_seen.insert(original_name.to_string());
                if constant.enum_value() == 1 {
                    type_ok.insert(original_name.to_string());
                }
            } else if let Some(original_name) = const_name.strip_prefix("__rdl_nc_") {
                nc_seen.insert(original_name.to_string());
                if constant.enum_value() == 1 {
                    shape_ok.insert(original_name.to_string());
                }
            } else if let Some(original_name) = const_name.strip_prefix("__rdl_sz_") {
                sizes.insert(original_name.to_string(), constant.enum_value());
            } else if let Some(original_name) = const_name.strip_prefix("__rdl_sg_") {
                signs.insert(original_name.to_string(), constant.enum_value());
            }
        }
    }

    // Missing gating probes mean the candidate was swallowed and must be retried.
    let present: HashSet<String> = eval_seen
        .iter()
        .filter(|n| ok_seen.contains(*n) && nc_seen.contains(*n))
        .cloned()
        .collect();

    let kept = evals
        .into_iter()
        .filter(|(name, _, _, _)| type_ok.contains(name) && shape_ok.contains(name))
        .map(|(name, unsigned, signed, ty)| {
            let value = if let Some(ty) = &ty {
                native_integer_value(unsigned, signed, ty)
            } else {
                eval_integer_value(
                    unsigned,
                    signed,
                    sizes.get(&name).copied(),
                    signs.get(&name).copied(),
                )
            };
            Const { name, ty, value }
        })
        .collect();

    (kept, present)
}

/// Type an evaluated integer from size/signedness probes, with a value-based fallback.
fn eval_integer_value(
    unsigned: u64,
    signed: i64,
    sz: Option<i64>,
    sg: Option<i64>,
) -> metadata::Value {
    match (sz, sg) {
        (Some(4), Some(1)) => metadata::Value::I32(signed as i32),
        (Some(4), Some(2)) => metadata::Value::U32(unsigned as u32),
        (Some(8), Some(1)) => metadata::Value::I64(signed),
        (Some(8), Some(2)) => metadata::Value::U64(unsigned),
        _ => {
            if signed >= 0 {
                if let Ok(v) = u32::try_from(unsigned) {
                    metadata::Value::U32(v)
                } else {
                    metadata::Value::U64(unsigned)
                }
            } else if let Ok(v) = i32::try_from(signed) {
                metadata::Value::I32(v)
            } else {
                metadata::Value::I64(signed)
            }
        }
    }
}

/// Store native-sized fields in the smallest fixed-width Constant type that retains the value.
fn native_integer_value(unsigned: u64, signed: i64, ty: &metadata::Type) -> metadata::Value {
    match ty {
        metadata::Type::USize => u32::try_from(unsigned)
            .map(metadata::Value::U32)
            .unwrap_or(metadata::Value::U64(unsigned)),
        metadata::Type::ISize => i32::try_from(signed)
            .map(metadata::Value::I32)
            .unwrap_or(metadata::Value::I64(signed)),
        _ => unreachable!(),
    }
}

/// Parse fixed literal/cast macro bodies; nested handle casts fall through to `parse_nested_cast`.
fn parse_body(
    body: &[(CXTokenKind, String)],
    namespace: &str,
    ref_map: &HashMap<String, String>,
    header_names: Option<&HashMap<String, String>>,
) -> Option<metadata::Value> {
    match body {
        [(CXToken_Literal, lit)] => parse_literal(lit, false),
        [(CXToken_Punctuation, minus), (CXToken_Literal, lit)] if minus == "-" => {
            parse_literal(lit, true)
        }
        [
            (CXToken_Punctuation, lp),
            (CXToken_Literal, lit),
            (CXToken_Punctuation, rp),
        ] if lp == "(" && rp == ")" => parse_literal(lit, false),
        [
            (CXToken_Punctuation, lp1),
            (CXToken_Punctuation, lp2),
            (CXToken_Identifier, ty),
            (CXToken_Punctuation, rp1),
            (CXToken_Literal, lit),
            (CXToken_Punctuation, rp2),
        ] if lp1 == "(" && lp2 == "(" && rp1 == ")" && rp2 == ")" => {
            parse_named_cast(namespace, ref_map, header_names, ty, lit, false)
        }
        [
            (CXToken_Punctuation, lp1),
            (CXToken_Punctuation, lp2),
            (CXToken_Identifier, ty),
            (CXToken_Punctuation, rp1),
            (CXToken_Punctuation, minus),
            (CXToken_Literal, lit),
            (CXToken_Punctuation, rp2),
        ] if lp1 == "(" && lp2 == "(" && rp1 == ")" && minus == "-" && rp2 == ")" => {
            parse_named_cast(namespace, ref_map, header_names, ty, lit, true)
        }
        [
            (CXToken_Punctuation, lp),
            (CXToken_Identifier, ty),
            (CXToken_Punctuation, rp),
            (CXToken_Literal, lit),
        ] if lp == "(" && rp == ")" => {
            parse_named_cast(namespace, ref_map, header_names, ty, lit, false)
        }
        [
            (CXToken_Punctuation, lp),
            (CXToken_Identifier, ty),
            (CXToken_Punctuation, rp),
            (CXToken_Punctuation, minus),
            (CXToken_Literal, lit),
        ] if lp == "(" && rp == ")" && minus == "-" => {
            parse_named_cast(namespace, ref_map, header_names, ty, lit, true)
        }
        [
            (CXToken_Punctuation, lp1),
            (CXToken_Identifier, ty),
            (CXToken_Punctuation, rp1),
            (CXToken_Punctuation, lp2),
            (CXToken_Punctuation, not),
            (CXToken_Literal, lit),
            (CXToken_Punctuation, rp2),
        ] if lp1 == "(" && rp1 == ")" && lp2 == "(" && not == "~" && rp2 == ")" => {
            parse_named_complement(namespace, ref_map, header_names, ty, lit)
        }
        [
            (CXToken_Punctuation, lp1),
            (CXToken_Punctuation, lp2),
            (CXToken_Keyword, kw),
            (CXToken_Punctuation, rp1),
            (CXToken_Literal, lit),
            (CXToken_Punctuation, rp2),
        ] if lp1 == "(" && lp2 == "(" && rp1 == ")" && rp2 == ")" => {
            parse_keyword_cast(kw, lit, false)
        }
        [
            (CXToken_Punctuation, lp1),
            (CXToken_Punctuation, lp2),
            (CXToken_Keyword, kw),
            (CXToken_Punctuation, rp1),
            (CXToken_Punctuation, minus),
            (CXToken_Literal, lit),
            (CXToken_Punctuation, rp2),
        ] if lp1 == "(" && lp2 == "(" && rp1 == ")" && minus == "-" && rp2 == ")" => {
            parse_keyword_cast(kw, lit, true)
        }
        [
            (CXToken_Punctuation, lp),
            (CXToken_Keyword, kw),
            (CXToken_Punctuation, rp),
            (CXToken_Literal, lit),
        ] if lp == "(" && rp == ")" => parse_keyword_cast(kw, lit, false),
        [
            (CXToken_Punctuation, lp),
            (CXToken_Keyword, kw),
            (CXToken_Punctuation, rp),
            (CXToken_Punctuation, minus),
            (CXToken_Literal, lit),
        ] if lp == "(" && rp == ")" && minus == "-" => parse_keyword_cast(kw, lit, true),
        // Preserve SDK error-code wrapper casts such as `_HRESULT_TYPEDEF_`.
        [
            (CXToken_Identifier, w),
            (CXToken_Punctuation, lp),
            (CXToken_Literal, lit),
            (CXToken_Punctuation, rp),
        ] if lp == "(" && rp == ")" && cast_wrapper_macro(w).is_some() => parse_named_cast(
            namespace,
            ref_map,
            header_names,
            cast_wrapper_macro(w)?,
            lit,
            false,
        ),
        [
            (CXToken_Identifier, w),
            (CXToken_Punctuation, lp),
            (CXToken_Punctuation, minus),
            (CXToken_Literal, lit),
            (CXToken_Punctuation, rp),
        ] if lp == "(" && rp == ")" && minus == "-" && cast_wrapper_macro(w).is_some() => {
            parse_named_cast(
                namespace,
                ref_map,
                header_names,
                cast_wrapper_macro(w)?,
                lit,
                true,
            )
        }
        // Ordinal resources are pointer-valued macros; parse them here so evaluation does
        // not drop them.
        [
            (CXToken_Identifier, w),
            (CXToken_Punctuation, lp),
            (CXToken_Literal, lit),
            (CXToken_Punctuation, rp),
        ] if lp == "(" && rp == ")" && makeintresource_macro(w).is_some() => {
            let (digits, _suffix) = split_int_suffix(lit);
            let raw: u64 = parse_int_digits(digits)?;
            Some(metadata::Value::EnumValue(
                metadata::TypeName::named(namespace, makeintresource_macro(w)?),
                Box::new(metadata::Value::I32(raw as i32)),
            ))
        }
        // `MAKEINTRESOURCEW(-2)` truncates to `WORD` before widening to a pointer; keep the
        // zero-extended 16-bit ordinal so resource APIs do not treat it as a string pointer.
        [
            (CXToken_Identifier, w),
            (CXToken_Punctuation, lp),
            (CXToken_Punctuation, minus),
            (CXToken_Literal, lit),
            (CXToken_Punctuation, rp),
        ] if lp == "(" && rp == ")" && minus == "-" && makeintresource_macro(w).is_some() => {
            let (digits, _suffix) = split_int_suffix(lit);
            let raw: u64 = parse_int_digits(digits)?;
            Some(metadata::Value::EnumValue(
                metadata::TypeName::named(namespace, makeintresource_macro(w)?),
                Box::new(metadata::Value::I32((raw as u16).wrapping_neg() as i32)),
            ))
        }
        // Inline char-pointer sentinels are not named aliases, so match their token shape here.
        [
            (CXToken_Punctuation, lp1),
            (CXToken_Punctuation, lp2),
            (CXToken_Identifier, ptr_ty),
            (CXToken_Punctuation, star),
            (CXToken_Punctuation, rp1),
            (CXToken_Punctuation, lp3),
            (CXToken_Identifier, inner),
            (CXToken_Punctuation, rp2),
            (CXToken_Punctuation, minus),
            (CXToken_Literal, lit),
            (CXToken_Punctuation, rp3),
        ] if lp1 == "("
            && lp2 == "("
            && star == "*"
            && rp1 == ")"
            && lp3 == "("
            && rp2 == ")"
            && minus == "-"
            && rp3 == ")"
            && char_pointer_target(ptr_ty).is_some() =>
        {
            let (digits, _suffix) = split_int_suffix(lit);
            let raw: u64 = parse_int_digits(digits)?;
            Some(metadata::Value::EnumValue(
                metadata::TypeName::named(namespace, char_pointer_target(ptr_ty)?),
                Box::new(inner_scalar_value(inner, raw, true)),
            ))
        }
        _ => parse_nested_cast(body, namespace, ref_map, header_names),
    }
}

/// Build the pseudo-attribute the RDL reader maps to `NativeEncodingAttribute`.
fn native_encoding_attr(encoding: &str) -> TokenStream {
    quote! { #[encoding(#encoding)] }
}

/// Parse a C integer, float, or string literal into a `metadata::Value`.
fn parse_literal(lit: &str, negate: bool) -> Option<metadata::Value> {
    if lit.starts_with("L\"") {
        if negate {
            return None;
        }
        let inner = lit.strip_prefix("L\"")?.strip_suffix('"')?;
        return Some(metadata::Value::Utf16(decode_c_wide_string(inner)?));
    }

    if lit.starts_with('"') {
        if negate {
            return None;
        }
        let inner = lit.strip_prefix('"')?.strip_suffix('"')?;
        return Some(metadata::Value::Utf8(decode_c_narrow_string(inner)?));
    }

    let (digits, suffix) = split_int_suffix(lit);
    let Some(raw) = parse_int_digits(digits) else {
        // Not an integer - try a floating-point literal.
        return parse_float_literal(lit, negate);
    };

    integer_value(raw, int_literal_is_decimal(digits), suffix, negate)
}

/// Classify literal base for C typing: only decimal needs a `U` suffix to become unsigned.
fn int_literal_is_decimal(digits: &str) -> bool {
    if digits.len() >= 2 {
        let prefix = &digits[..2];
        if prefix.eq_ignore_ascii_case("0x") || prefix.eq_ignore_ascii_case("0b") {
            return false;
        }
    }
    !(digits.len() > 1 && digits.starts_with('0'))
}

/// Parse decimal floating literals; hex floats are not represented.
/// A decimal point or exponent is required so hex integers like `0xF` stay integer literals.
fn parse_float_literal(lit: &str, negate: bool) -> Option<metadata::Value> {
    let lower = lit.to_ascii_lowercase();
    if lower.starts_with("0x") {
        return None;
    }

    let (body, is_f32) = if let Some(body) = lower.strip_suffix('f') {
        (body, true)
    } else if let Some(body) = lower.strip_suffix('l') {
        (body, false)
    } else {
        (lower.as_str(), false)
    };

    if !body.contains('.') && !body.contains('e') {
        return None;
    }

    if is_f32 {
        let value = body.parse::<f32>().ok()?;
        Some(metadata::Value::F32(if negate { -value } else { value }))
    } else {
        let value = body.parse::<f64>().ok()?;
        Some(metadata::Value::F64(if negate { -value } else { value }))
    }
}

/// Decode a narrow-string literal as bytes, then keep it only if those bytes are valid UTF-8.
/// Raw byte arrays have no exact `String` representation and are omitted.
fn decode_c_narrow_string(inner: &str) -> Option<String> {
    let mut bytes = Vec::with_capacity(inner.len());
    let mut chars = inner.chars().peekable();
    while let Some(c) = chars.next() {
        if c != '\\' {
            let mut buf = [0u8; 4];
            bytes.extend_from_slice(c.encode_utf8(&mut buf).as_bytes());
            continue;
        }
        match chars.next()? {
            '\\' => bytes.push(b'\\'),
            '"' => bytes.push(b'"'),
            '\'' => bytes.push(b'\''),
            '?' => bytes.push(b'?'),
            'n' => bytes.push(b'\n'),
            'r' => bytes.push(b'\r'),
            't' => bytes.push(b'\t'),
            'a' => bytes.push(0x07),
            'b' => bytes.push(0x08),
            'f' => bytes.push(0x0c),
            'v' => bytes.push(0x0b),
            'x' => {
                let (value, count) = take_radix(&mut chars, 16, usize::MAX);
                if count == 0 {
                    return None;
                }
                bytes.push(value as u8);
            }
            o @ '0'..='7' => {
                let mut value = o.to_digit(8)?;
                let (rest, count) = take_radix(&mut chars, 8, 2);
                value = value * 8u32.pow(count as u32) + rest;
                bytes.push(value as u8);
            }
            other => {
                bytes.push(b'\\');
                let mut buf = [0u8; 4];
                bytes.extend_from_slice(other.encode_utf8(&mut buf).as_bytes());
            }
        }
    }
    String::from_utf8(bytes).ok()
}

/// Decode a wide-string literal, dropping escapes that do not name valid Unicode scalars.
fn decode_c_wide_string(inner: &str) -> Option<String> {
    let mut out = String::with_capacity(inner.len());
    let mut chars = inner.chars().peekable();
    while let Some(c) = chars.next() {
        if c != '\\' {
            out.push(c);
            continue;
        }
        let scalar = match chars.next()? {
            '\\' => '\\',
            '"' => '"',
            '\'' => '\'',
            '?' => '?',
            'n' => '\n',
            'r' => '\r',
            't' => '\t',
            'a' => '\u{07}',
            'b' => '\u{08}',
            'f' => '\u{0c}',
            'v' => '\u{0b}',
            'x' | 'u' | 'U' => {
                let (value, count) = take_radix(&mut chars, 16, usize::MAX);
                if count == 0 {
                    return None;
                }
                char::from_u32(value)?
            }
            o @ '0'..='7' => {
                let mut value = o.to_digit(8)?;
                let (rest, count) = take_radix(&mut chars, 8, 2);
                value = value * 8u32.pow(count as u32) + rest;
                char::from_u32(value)?
            }
            other => {
                out.push('\\');
                other
            }
        };
        out.push(scalar);
    }
    Some(out)
}

/// Consume up to `max` leading digits of `radix`.
fn take_radix(
    chars: &mut std::iter::Peekable<std::str::Chars>,
    radix: u32,
    max: usize,
) -> (u32, usize) {
    let mut value = 0;
    let mut count = 0;
    while count < max {
        match chars.peek().and_then(|c| c.to_digit(radix)) {
            Some(digit) => {
                value = value * radix + digit;
                chars.next();
                count += 1;
            }
            None => break,
        }
    }
    (value, count)
}

/// Select the C11 integer literal type under Windows LLP64 widths.
/// Decimal literals use signed candidates unless `U`-suffixed; non-decimal literals may
/// take unsigned candidates to fit the magnitude.
fn c_integer_constant_type(
    is_decimal: bool,
    has_u: bool,
    has_l: bool,
    has_ll: bool,
    raw: u64,
) -> metadata::Type {
    use metadata::Type::{I32, I64, U32, U64};
    let candidates: &[metadata::Type] = if has_u {
        if has_ll { &[U64] } else { &[U32, U64] }
    } else if has_ll {
        if is_decimal { &[I64] } else { &[I64, U64] }
    } else if has_l || is_decimal {
        if is_decimal {
            &[I32, I64]
        } else {
            &[I32, U32, I64, U64]
        }
    } else {
        &[I32, U32, I64, U64]
    };
    for ty in candidates {
        let ty = ty.clone();
        let fits = match ty {
            I32 => raw <= i32::MAX as u64,
            U32 => raw <= u32::MAX as u64,
            I64 => raw <= i64::MAX as u64,
            _ => true,
        };
        if fits {
            return ty;
        }
    }
    U64
}

/// Parse a C integer literal using C11 typing; unary minus applies within that type.
fn integer_value(
    raw: u64,
    is_decimal: bool,
    suffix: &str,
    negate: bool,
) -> Option<metadata::Value> {
    let suffix = suffix.to_ascii_uppercase();
    let has_u = suffix.contains('U');
    let has_ll = suffix.contains("LL");
    let has_l = suffix.contains('L');
    let ty = c_integer_constant_type(is_decimal, has_u, has_l, has_ll, raw);
    Some(if negate {
        match ty {
            metadata::Type::I32 => metadata::Value::I32((raw as i32).wrapping_neg()),
            metadata::Type::I64 => metadata::Value::I64((raw as i64).wrapping_neg()),
            metadata::Type::U32 => metadata::Value::U32((raw as u32).wrapping_neg()),
            metadata::Type::U64 => metadata::Value::U64(raw.wrapping_neg()),
            _ => return None,
        }
    } else {
        match ty {
            metadata::Type::I32 => metadata::Value::I32(raw as i32),
            metadata::Type::U32 => metadata::Value::U32(raw as u32),
            metadata::Type::I64 => metadata::Value::I64(raw as i64),
            metadata::Type::U64 => metadata::Value::U64(raw),
            _ => return None,
        }
    })
}

/// Parse a builtin-keyword integer cast; explicit casts govern width and signedness.
/// Multi-token keywords fall through to batch evaluation.
fn parse_keyword_cast(kw: &str, lit: &str, negate: bool) -> Option<metadata::Value> {
    let ty = keyword_scalar(kw)?;
    let (digits, _suffix) = split_int_suffix(lit);
    let raw: u64 = parse_int_digits(digits)?;
    scalar_value(&ty, raw, negate)
}

/// Map SDK error-code typedef wrapper macros to their hidden cast type.
/// This preserves `HRESULT`/`DWORD` instead of flattening constants to integers.
fn cast_wrapper_macro(name: &str) -> Option<&'static str> {
    Some(match name {
        "_HRESULT_TYPEDEF_" | "_ASF_HRESULT_TYPEDEF_" => "HRESULT",
        "_NDIS_ERROR_TYPEDEF_" => "DWORD",
        _ => return None,
    })
}

/// Map a `MAKEINTRESOURCE` macro to the string-pointer type that carries its ordinal.
/// The scrape is ANSI-default, but bare `MAKEINTRESOURCE` is treated as wide to match
/// the reference metadata.
fn makeintresource_macro(name: &str) -> Option<&'static str> {
    Some(match name {
        "MAKEINTRESOURCE" | "MAKEINTRESOURCEW" => "PWSTR",
        "MAKEINTRESOURCEA" => "PSTR",
        _ => return None,
    })
}

/// Map inline char-pointer sentinel casts to canonical string-pointer types.
fn char_pointer_target(name: &str) -> Option<&'static str> {
    Some(match name {
        "OLECHAR" | "WCHAR" | "wchar_t" => "PWSTR",
        "CHAR" | "char" => "PSTR",
        _ => return None,
    })
}

/// Map a single C builtin integer keyword under LLP64; multi-token spellings fall through.
fn keyword_scalar(name: &str) -> Option<metadata::Type> {
    Some(match name {
        "char" => metadata::Type::I8,
        "short" => metadata::Type::I16,
        "int" | "long" => metadata::Type::I32,
        "unsigned" => metadata::Type::U32,
        _ => return None,
    })
}

fn parse_named_cast(
    namespace: &str,
    ref_map: &HashMap<String, String>,
    header_names: Option<&HashMap<String, String>>,
    type_name: &str,
    lit: &str,
    negate: bool,
) -> Option<metadata::Value> {
    let (digits, _suffix) = split_int_suffix(lit);
    let raw: u64 = parse_int_digits(digits)?;

    // Collapsed scalar typedefs have no emitted name; preserved seed scalars/enums stay named.
    if !ref_map.contains_key(type_name)
        && let Some(ty) = fundamental_scalar(type_name)
    {
        return scalar_value(&ty, raw, negate);
    }

    // Pointer-sized casts need target-width evaluation. Returning `None` sends the macro through
    // the typed batch evaluator, which retains both the native field type and the per-arch value.
    if !ref_map.contains_key(type_name) && pointer_sized_abi(type_name).is_some() {
        return None;
    }

    // Void-pointer aliases are collapsed too; keeping the alias name would dangle.
    if !ref_map.contains_key(type_name) && void_pointer_alias(type_name).is_some() {
        return scalar_value(&metadata::Type::USize, raw, negate);
    }

    // String-pointer aliases normalize because redundant `LP*` alias definitions are suppressed.
    let type_name = string_alias_canonical(type_name).unwrap_or(type_name);

    // Token casts have no cursor in per-header mode; `header_names` locates the type partition.
    let ns = header_names
        .and_then(|m| m.get(type_name))
        .or_else(|| ref_map.get(type_name))
        .map_or(namespace, |s| s.as_str());
    let v = if negate {
        (raw as i64).wrapping_neg()
    } else {
        raw as i64
    };
    Some(metadata::Value::EnumValue(
        metadata::TypeName::named(ns, type_name),
        Box::new(metadata::Value::I64(v)),
    ))
}

/// Parse a named cast of a complemented integer literal such as `(SOCKET)(~0)`.
fn parse_named_complement(
    namespace: &str,
    ref_map: &HashMap<String, String>,
    header_names: Option<&HashMap<String, String>>,
    type_name: &str,
    lit: &str,
) -> Option<metadata::Value> {
    let (digits, suffix) = split_int_suffix(lit);
    let raw = parse_int_digits(digits)?;
    let value = integer_complement_value(raw, int_literal_is_decimal(digits), suffix)?;

    if !ref_map.contains_key(type_name) && pointer_sized_abi(type_name).is_some() {
        return None;
    }

    if !ref_map.contains_key(type_name)
        && let Some(ty) = fundamental_scalar(type_name)
    {
        return cast_integer_value(&ty, &value);
    }

    let ns = header_names
        .and_then(|m| m.get(type_name))
        .or_else(|| ref_map.get(type_name))
        .map_or(namespace, |s| s.as_str());
    Some(metadata::Value::EnumValue(
        metadata::TypeName::named(ns, type_name),
        Box::new(value),
    ))
}

/// Apply integer promotion and complement within the literal's C11 candidate type.
fn integer_complement_value(raw: u64, is_decimal: bool, suffix: &str) -> Option<metadata::Value> {
    let suffix = suffix.to_ascii_uppercase();
    let ty = c_integer_constant_type(
        is_decimal,
        suffix.contains('U'),
        suffix.contains('L'),
        suffix.contains("LL"),
        raw,
    );
    Some(match ty {
        metadata::Type::I32 => metadata::Value::I32(!(raw as i32)),
        metadata::Type::U32 => metadata::Value::U32(!(raw as u32)),
        metadata::Type::I64 => metadata::Value::I64(!(raw as i64)),
        metadata::Type::U64 => metadata::Value::U64(!raw),
        _ => return None,
    })
}

/// Cast an integer value to a collapsed scalar typedef.
fn cast_integer_value(ty: &metadata::Type, value: &metadata::Value) -> Option<metadata::Value> {
    let (signed, unsigned) = match value {
        metadata::Value::I32(value) => (Some(*value as i64), None),
        metadata::Value::I64(value) => (Some(*value), None),
        metadata::Value::U32(value) => (None, Some(*value as u64)),
        metadata::Value::U64(value) => (None, Some(*value)),
        _ => return None,
    };
    let raw = signed.map_or_else(|| unsigned.unwrap(), |value| value as u64);
    scalar_value(ty, raw, false)
}

/// Parse nested casts used by handle/pointer constants such as `INVALID_HANDLE_VALUE`.
/// The innermost scalar cast supplies the bit pattern; reading the outer pointer-sized cast
/// would zero-extend values that the SDK intends to sign-extend.
fn parse_nested_cast(
    body: &[(CXTokenKind, String)],
    namespace: &str,
    ref_map: &HashMap<String, String>,
    header_names: Option<&HashMap<String, String>>,
) -> Option<metadata::Value> {
    let mut casts: Vec<&str> = Vec::new();
    let mut negate = false;
    let mut literal: Option<&str> = None;

    for (i, (kind, tok)) in body.iter().enumerate() {
        match *kind {
            CXToken_Punctuation => match tok.as_str() {
                "(" | ")" => {}
                "-" if literal.is_none() && !negate => negate = true,
                _ => return None,
            },
            CXToken_Identifier => {
                if literal.is_some() {
                    return None;
                }
                // Reject function-like macros so batch evaluation can compute them.
                if !matches!(body.get(i + 1), Some((CXToken_Punctuation, p)) if p == ")") {
                    return None;
                }
                casts.push(tok);
            }
            CXToken_Literal => {
                if literal.is_some() {
                    return None;
                }
                literal = Some(tok);
            }
            _ => return None,
        }
    }

    let lit = literal?;
    if casts.len() < 2 {
        return None;
    }
    let outer = casts[0];
    let inner = casts[casts.len() - 1];
    if fundamental_scalar(outer).is_some() {
        return None;
    }
    // Normalize suppressed string-pointer aliases, as in `parse_named_cast`.
    let outer = string_alias_canonical(outer).unwrap_or(outer);

    // Void-pointer aliases have no emitted name, and arithmetic inner expressions are outside
    // this token parser; drop them rather than dangling on the collapsed alias.
    if void_pointer_alias(outer).is_some() {
        return None;
    }

    let (digits, _suffix) = split_int_suffix(lit);
    let raw: u64 = parse_int_digits(digits)?;
    let inner_value = inner_scalar_value(inner, raw, negate);

    let ns = header_names
        .and_then(|m| m.get(outer))
        .or_else(|| ref_map.get(outer))
        .map_or(namespace, |s| s.as_str());
    Some(metadata::Value::EnumValue(
        metadata::TypeName::named(ns, outer),
        Box::new(inner_value),
    ))
}
/// Read the innermost scalar cast of a nested handle constant.
/// Unknown integer casts use `i64`, the widest safe reinterpretation.
fn inner_scalar_value(inner: &str, raw: u64, negate: bool) -> metadata::Value {
    if let Some(ty) = fundamental_scalar(inner)
        && let Some(value) = scalar_value(&ty, raw, negate)
    {
        return value;
    }
    let v = if negate {
        (raw as i64).wrapping_neg()
    } else {
        raw as i64
    };
    metadata::Value::I64(v)
}

/// Build a fixed-width scalar value, applying negation as wrapping two's-complement.
fn scalar_value(ty: &metadata::Type, raw: u64, negate: bool) -> Option<metadata::Value> {
    let signed = if negate {
        (raw as i64).wrapping_neg()
    } else {
        raw as i64
    };
    Some(match ty {
        metadata::Type::U8 => metadata::Value::U8(signed as u8),
        metadata::Type::U16 => metadata::Value::U16(signed as u16),
        metadata::Type::U32 => metadata::Value::U32(signed as u32),
        metadata::Type::U64 => metadata::Value::U64(signed as u64),
        metadata::Type::I8 => metadata::Value::I8(signed as i8),
        metadata::Type::I16 => metadata::Value::I16(signed as i16),
        metadata::Type::I32 => metadata::Value::I32(signed as i32),
        metadata::Type::I64 => metadata::Value::I64(signed),
        metadata::Type::USize => metadata::Value::USize(signed as u64),
        metadata::Type::ISize => metadata::Value::ISize(signed),
        _ => return None,
    })
}

/// Split a C integer literal after the last digit or `x`/`X` prefix marker.
fn split_int_suffix(lit: &str) -> (&str, &str) {
    let suffix_start = lit
        .rfind(|c: char| c.is_ascii_hexdigit() || c == 'x' || c == 'X')
        .map_or(lit.len(), |i| i + 1);
    (&lit[..suffix_start], &lit[suffix_start..])
}

/// Parse C integer digits, with invalid octal falling back to decimal.
fn parse_int_digits(digits: &str) -> Option<u64> {
    if let Some(hex) = digits
        .strip_prefix("0x")
        .or_else(|| digits.strip_prefix("0X"))
    {
        u64::from_str_radix(hex, 16).ok()
    } else if let Some(bin) = digits
        .strip_prefix("0b")
        .or_else(|| digits.strip_prefix("0B"))
    {
        u64::from_str_radix(bin, 2).ok()
    } else if digits.len() > 1 && digits.starts_with('0') {
        u64::from_str_radix(&digits[1..], 8)
            .or_else(|_| digits.parse::<u64>())
            .ok()
    } else {
        digits.parse::<u64>().ok()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn narrow_decodes_standard_escapes() {
        assert_eq!(decode_c_narrow_string("!<arch>\\n").unwrap(), "!<arch>\n");
        assert_eq!(
            decode_c_narrow_string("Software\\\\Microsoft\\\\OID").unwrap(),
            "Software\\Microsoft\\OID"
        );
        assert_eq!(decode_c_narrow_string("M\\0\\0\\0").unwrap(), "M\0\0\0");
        assert_eq!(decode_c_narrow_string("a\\tb").unwrap(), "a\tb");
    }

    #[test]
    fn narrow_octal_uses_digit_count_not_value() {
        // The trailing octal digits are positional, not the numeric value 1.
        assert_eq!(decode_c_narrow_string("\\101").unwrap(), "A");
    }

    #[test]
    fn narrow_ascii_hex_bytes_are_kept() {
        assert_eq!(
            decode_c_narrow_string("\\x20\\x30\\x10").unwrap(),
            " 0\u{10}"
        );
    }

    #[test]
    fn narrow_non_utf8_byte_array_is_omitted() {
        // Raw GUID byte spellings are not UTF-8.
        assert_eq!(decode_c_narrow_string("\\xaa\\x31\\x28"), None);
    }

    #[test]
    fn wide_decodes_standard_escapes() {
        assert_eq!(decode_c_wide_string("line\\n").unwrap(), "line\n");
        assert_eq!(decode_c_wide_string("path\\\\here").unwrap(), "path\\here");
        assert_eq!(decode_c_wide_string("\\x41\\x42").unwrap(), "AB");
    }

    #[test]
    fn int_digits_respect_radix_prefix() {
        assert_eq!(parse_int_digits("0x1F"), Some(31));
        assert_eq!(parse_int_digits("0b1010"), Some(10));
        assert_eq!(parse_int_digits("010"), Some(8));
        assert_eq!(parse_int_digits("0"), Some(0));
        assert_eq!(parse_int_digits("42"), Some(42));
        // Invalid octal falls back to decimal.
        assert_eq!(parse_int_digits("08"), Some(8));
    }

    #[test]
    fn integer_literals_take_c11_types() {
        use metadata::Value::{I32, I64, U32, U64};
        let v = |raw, decimal, suffix| integer_value(raw, decimal, suffix, false).unwrap();

        // Bare decimal widens to signed i64 rather than u32.
        assert_eq!(v(42, true, ""), I32(42));
        assert_eq!(v(2147483648, true, ""), I64(2147483648));
        // Hex may become unsigned after signed overflow at the same width.
        assert_eq!(v(31, false, ""), I32(31));
        assert_eq!(v(2147483648, false, ""), U32(2147483648));
        assert_eq!(v(4294967295, false, ""), U32(4294967295));
        assert_eq!(v(100, true, "U"), U32(100));
        assert_eq!(v(4000000000, true, "U"), U32(4000000000));
        assert_eq!(v(4294967296, true, "LL"), I64(4294967296));
        assert_eq!(
            v(18446744073709551615, true, "ULL"),
            U64(18446744073709551615)
        );
        assert_eq!(integer_value(5, true, "", true).unwrap(), I32(-5));
    }

    #[test]
    fn eval_values_use_probe_types() {
        use metadata::Value::{I32, I64, U32, U64};
        assert_eq!(eval_integer_value(1, 1, Some(4), Some(1)), I32(1));
        assert_eq!(eval_integer_value(1, 1, Some(4), Some(2)), U32(1));
        assert_eq!(eval_integer_value(1, 1, Some(8), Some(1)), I64(1));
        assert_eq!(eval_integer_value(1, 1, Some(8), Some(2)), U64(1));
        assert_eq!(
            eval_integer_value(u64::MAX, -1, Some(8), Some(2)),
            U64(u64::MAX)
        );
        assert_eq!(eval_integer_value(100, 100, None, None), U32(100));
        assert_eq!(eval_integer_value(u64::MAX, -1, None, None), I32(-1));
        assert_eq!(eval_integer_value(1, 1, Some(0), Some(0)), U32(1));
    }

    #[test]
    fn native_values_use_fixed_width_storage() {
        use metadata::Value::{I32, I64, U32, U64};
        assert_eq!(
            native_integer_value(24, 24, &metadata::Type::USize),
            U32(24)
        );
        assert_eq!(
            native_integer_value(u64::MAX, -1, &metadata::Type::USize),
            U64(u64::MAX)
        );
        assert_eq!(
            native_integer_value((-24i64) as u64, -24, &metadata::Type::ISize),
            I32(-24)
        );
        assert_eq!(
            native_integer_value(i64::MAX as u64, i64::MAX, &metadata::Type::ISize),
            I64(i64::MAX)
        );
    }
}