windows-bindgen 0.101.0

Code generator for Windows metadata
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
use super::*;
use std::sync::*;
use windows_metadata::reader::{Item, TypeCategory};

fn resolve(name: TypeName, item: Item<'static>, legacy: bool) -> Type {
    match item {
        Item::Type(def) => {
            if def.flags().contains(TypeAttributes::WindowsRuntime) {
                match def.category() {
                    TypeCategory::Class => Type::Class(Class { def }),
                    TypeCategory::Delegate => Type::Delegate(Delegate {
                        def,
                        generics: def.generics(),
                    }),
                    TypeCategory::Enum => Type::Enum(Enum { def }),
                    TypeCategory::Interface => Type::Interface(Interface {
                        def,
                        generics: def.generics(),
                        kind: InterfaceKind::None,
                    }),
                    TypeCategory::Struct => Type::Struct(Struct { def }),
                    TypeCategory::Attribute => unreachable!(),
                }
            } else {
                match def.category() {
                    TypeCategory::Delegate => Type::CppDelegate(CppDelegate { def }),
                    TypeCategory::Enum => Type::CppEnum(CppEnum { def }),
                    TypeCategory::Interface => Type::CppInterface(CppInterface { def }),
                    TypeCategory::Struct => {
                        Type::CppStruct(make_struct(def, name.name(), None, legacy))
                    }
                    _ => unreachable!(),
                }
            }
        }
        Item::Fn(method) => Type::CppFn(CppFn {
            namespace: name.namespace(),
            method,
        }),
        Item::Const(field) => Type::CppConst(CppConst {
            namespace: name.namespace(),
            field,
            arches: field.arches(),
            is_enum_member: false,
        }),
    }
}

/// Legacy metadata names nested structs by their position among all nested types, as the
/// `windows` 0.62 releases did.
fn make_struct(
    def: TypeDef,
    name: &'static str,
    enclosing: Option<TypeName>,
    legacy: bool,
) -> CppStruct {
    let index = def.index();
    let mut ty = CppStruct {
        def,
        name,
        enclosing,
        nested: BTreeMap::new(),
    };
    let mut anonymous = BTreeMap::new();
    for child in index.nested(def) {
        if let Some(position) = child.anonymous_index().unwrap() {
            assert!(
                anonymous.insert(position, child).is_none(),
                "duplicate anonymous field position in `{}`",
                def.name()
            );
        }
    }
    let anonymous: HashMap<_, _> = anonymous
        .into_values()
        .enumerate()
        .map(|(index, child)| (child, index))
        .collect();

    for (position, nested_def) in index.nested(def).enumerate() {
        if nested_def.category() == TypeCategory::Struct {
            let name = if legacy {
                format!("{}_{position}", ty.name)
            } else {
                match anonymous.get(&nested_def) {
                    Some(index) => format!("{}_{index}", ty.name),
                    None => format!("{}_{}", ty.name, nested_def.name()),
                }
            };
            ty.nested.insert(
                nested_def.name(),
                make_struct(
                    nested_def,
                    name.leak(),
                    Some(enclosing.unwrap_or(ty.type_name())),
                    legacy,
                ),
            );
        }
    }
    ty
}

fn has_numerics(index: &windows_metadata::reader::Index) -> bool {
    const TYPES: [(&str, usize); 5] = [
        ("Matrix3x2", 6),
        ("Matrix4x4", 16),
        ("Vector2", 2),
        ("Vector3", 3),
        ("Vector4", 4),
    ];
    let mut found = [false; TYPES.len()];
    for (name, item) in index.namespace_items("Windows.Foundation.Numerics") {
        if let Item::Type(def) = item
            && def.category() == TypeCategory::Struct
            && let Some(index) = TYPES.iter().position(|candidate| candidate.0 == name)
            && def.fields().len() == TYPES[index].1
            && def
                .fields()
                .all(|field| field.ty() == windows_metadata::Type::F32)
        {
            found[index] = true;
            if found.into_iter().all(|found| found) {
                return true;
            }
        }
    }
    false
}

type CachedTypes = Arc<OnceLock<Arc<[Type]>>>;

pub struct Reader {
    index: &'static windows_metadata::reader::Index,
    namespaces: HashSet<&'static str>,
    enum_members: HashMap<&'static str, HashMap<&'static str, Vec<(Field, i32)>>>,
    cache: RwLock<HashMap<TypeName, CachedTypes>>,
    project_numerics: bool,
    legacy: bool,
}

impl Reader {
    pub fn new(files: Vec<File>) -> Self {
        Self::from_index(windows_metadata::reader::Index::new(files).leak(), true)
    }

    pub fn new_sys(files: Vec<File>) -> Self {
        Self::from_index(windows_metadata::reader::Index::new(files).leak(), false)
    }

    /// Package generation also interprets the Win32 metadata vocabulary
    /// (`Windows.Win32.Foundation.Metadata`) used by the `windows` 0.62 releases, when the inputs
    /// use it.
    pub fn new_package(files: Vec<File>, sys: bool) -> Self {
        let index = windows_metadata::reader::Index::new(files).leak();
        let legacy = index
            .get_item(
                "Windows.Win32.Foundation.Metadata",
                "NativeTypedefAttribute",
            )
            .next()
            .is_some();
        Self::build(index, !sys, legacy)
    }

    pub fn from_index(
        index: &'static windows_metadata::reader::Index,
        project_numerics: bool,
    ) -> Self {
        Self::build(index, project_numerics, false)
    }

    fn build(
        index: &'static windows_metadata::reader::Index,
        project_numerics: bool,
        legacy: bool,
    ) -> Self {
        let project_numerics = project_numerics && has_numerics(index);

        let mut reader = Self {
            index,
            namespaces: HashSet::new(),
            enum_members: HashMap::new(),
            cache: RwLock::new(HashMap::new()),
            project_numerics,
            legacy,
        };

        for namespace in index.namespaces() {
            if index
                .namespace_items(namespace)
                .any(|(name, item)| match item {
                    Item::Type(_) => reader.remap(namespace, name) == Remap::None,
                    _ => reader.item_name(item).is_some(),
                })
            {
                reader.namespaces.insert(namespace);
            }
        }

        for (namespace, name, def) in index.iter() {
            if reader.remap(namespace, name) != Remap::None
                || def.flags().contains(TypeAttributes::WindowsRuntime)
                || def.category() != TypeCategory::Enum
                || def.has_attribute("ScopedEnumAttribute")
            {
                continue;
            }
            let arches = def.arches();
            let members = reader.enum_members.entry(namespace).or_default();
            for field in def.fields() {
                if field.flags().contains(FieldAttributes::Literal) {
                    members
                        .entry(field.name())
                        .or_insert_with(|| Vec::with_capacity(1))
                        .push((
                            field,
                            match field.arches() {
                                0 => arches,
                                arches => arches,
                            },
                        ));
                }
            }
        }

        reader
    }

    /// Whether the inputs use the legacy Win32 metadata vocabulary, projected as in the `windows`
    /// 0.62 releases. See [`Reader::new_package`].
    pub fn legacy(&self) -> bool {
        self.legacy
    }

    /// Maps metadata names to core types or other names; see [`Type::remap`]. Legacy metadata
    /// follows the `windows` 0.62 mapping: the `BOOLEAN` and `CHAR` typedefs are Rust primitives
    /// (`BOOLEAN` is `u8` rather than `bool`, which has a different ABI),
    /// `D2D_VECTOR_3F` is not a numerics type, and the status types stay at their metadata paths
    /// (see [`Reader::legacy_core_type`]).
    pub fn remap(&self, namespace: &str, name: &str) -> Remap {
        if self.legacy {
            match (namespace, name) {
                ("Windows.Win32.Foundation", "BOOLEAN") => return Remap::Type(Type::U8),
                ("Windows.Win32.Foundation", "CHAR") => return Remap::Type(Type::I8),
                ("Windows.Win32.Foundation", "NTSTATUS")
                | ("Windows.Win32.System.Rpc", "RPC_STATUS")
                | ("Windows.Win32.Graphics.Direct2D.Common", "D2D_VECTOR_3F") => {
                    return Remap::None;
                }
                _ => {}
            }
        }
        Type::remap(namespace, name, self.project_numerics)
    }

    /// Returns the core type that a legacy metadata status definition re-exports, so the
    /// `windows` 0.62 paths name the same type as `windows-core`.
    pub fn legacy_core_type(&self, def: TypeDef) -> Option<Type> {
        if !self.legacy {
            return None;
        }
        match (def.namespace(), def.name()) {
            ("Windows.Win32.Foundation", "NTSTATUS") => Some(Type::NTSTATUS),
            ("Windows.Win32.System.Rpc", "RPC_STATUS") => Some(Type::RPC_STATUS),
            _ => None,
        }
    }

    fn item_name(&self, item: Item<'static>) -> Option<&'static str> {
        match item {
            Item::Type(def) => {
                let name = windows_metadata::trim_tick(def.name());
                if self.remap(def.namespace(), name) != Remap::None {
                    return None;
                }
                let category = def.category();
                if category == TypeCategory::Attribute
                    || (!def.flags().contains(TypeAttributes::WindowsRuntime)
                        && category == TypeCategory::Class)
                    || (def.flags().contains(TypeAttributes::WindowsRuntime)
                        && category == TypeCategory::Struct
                        && def.has_attribute("ApiContractAttribute"))
                {
                    return None;
                }
                Some(name)
            }
            Item::Fn(method) => {
                if let Some(map) = method.impl_map() {
                    // Legacy packages also omit functions imported by ordinal.
                    if map.import_scope().name() == "FORCEINLINE"
                        || (self.legacy && map.import_name().starts_with('#'))
                    {
                        return None;
                    }
                }
                Some(method.name())
            }
            Item::Const(field) => Some(field.name()),
        }
    }

    pub fn namespaces(&self) -> impl Iterator<Item = &'static str> + '_ {
        self.namespaces.iter().copied()
    }

    pub fn contains_namespace(&self, namespace: &str) -> bool {
        self.namespaces.contains(namespace)
    }

    pub fn find_name(&self, namespace: &str, name: &str) -> Option<TypeName> {
        let namespace = *self.namespaces.get(namespace)?;
        if let Some(name) = self
            .index
            .get_item(namespace, name)
            .find_map(|item| self.item_name(item))
        {
            return Some(TypeName(namespace, name));
        }
        self.enum_members
            .get(namespace)?
            .get_key_value(name)
            .map(|(name, _)| TypeName(namespace, name))
    }

    pub fn contains_name(&self, namespace: &str, name: &str) -> bool {
        self.find_name(namespace, name).is_some()
    }

    pub fn type_names(&self, namespace: &str) -> impl Iterator<Item = TypeName> {
        let mut names = HashSet::new();
        if let Some(namespace) = self.namespaces.get(namespace) {
            for (_, item) in self.index.namespace_items(namespace) {
                if let Some(name) = self.item_name(item) {
                    names.insert(TypeName(namespace, name));
                }
            }
            if let Some(members) = self.enum_members.get(namespace) {
                names.extend(members.keys().map(|name| TypeName(namespace, name)));
            }
        }
        names.into_iter()
    }

    pub fn types(&self, name: TypeName) -> Arc<[Type]> {
        let cached = self.cache.read().unwrap().get(&name).cloned();
        let cached =
            cached.unwrap_or_else(|| self.cache.write().unwrap().entry(name).or_default().clone());
        // Decode outside the map lock so failures do not poison unrelated lookups.
        cached
            .get_or_init(|| {
                let mut types = Vec::with_capacity(1);
                types.extend(
                    self.index
                        .get_item(name.namespace(), name.name())
                        .filter(|item| self.item_name(*item).is_some())
                        .map(|item| resolve(name, item, self.legacy)),
                );
                if let Some(members) = self
                    .enum_members
                    .get(name.namespace())
                    .and_then(|members| members.get(name.name()))
                {
                    types.extend(members.iter().map(|(field, arches)| {
                        Type::CppConst(CppConst {
                            namespace: name.namespace(),
                            field: *field,
                            arches: *arches,
                            is_enum_member: true,
                        })
                    }));
                }
                // Explicit constants shadow unscoped enum members only on overlapping targets.
                let explicit_arches = types.iter().fold(0, |arches, ty| {
                    if let Type::CppConst(constant) = ty
                        && !constant.is_enum_member
                    {
                        arches | arch_mask(constant.arches)
                    } else {
                        arches
                    }
                });
                if explicit_arches != 0 {
                    types.retain_mut(|ty| {
                        if let Type::CppConst(constant) = ty
                            && constant.is_enum_member
                        {
                            constant.arches = arch_mask(constant.arches) & !explicit_arches;
                            return constant.arches != 0;
                        }
                        true
                    });
                }
                types.into()
            })
            .clone()
    }

    #[track_caller]
    pub fn unwrap_full_name(&self, namespace: &str, name: &str) -> Type {
        if let Some(ty) = self.with_full_name(namespace, name).next() {
            ty
        } else {
            panic!("type not found: {namespace}.{name}")
        }
    }

    #[track_caller]
    pub fn unwrap_type_name(&self, namespace: &str, name: &str) -> Type {
        if let Some(ty) = self
            .with_full_name(namespace, name)
            .find(|ty| !matches!(ty, Type::CppConst(_) | Type::CppFn(_)))
        {
            ty
        } else {
            panic!("type not found: {namespace}.{name}")
        }
    }

    /// Gets all types matching the given namespace and name.
    /// Trims any generic arity suffix (e.g. "`1") so callers may pass raw metadata names.
    pub fn with_full_name<'a>(
        &'a self,
        namespace: &str,
        name: &str,
    ) -> impl Iterator<Item = Type> + use<'a> {
        let (name, nested) = name.split_once('/').map_or((name, None), |(root, nested)| {
            (root, Some(nested.to_string()))
        });
        let name = windows_metadata::trim_tick(name);
        let types = self
            .find_name(namespace, name)
            .map(|name| self.types(name))
            .unwrap_or_default();
        (0..types.len()).filter_map(move |index| {
            let mut ty = types[index].clone();
            if let Some(nested) = &nested {
                for name in nested.split('/') {
                    let Type::CppStruct(outer) = ty else {
                        return None;
                    };
                    ty = outer.nested_type(name)?;
                }
            }
            Some(ty)
        })
    }
}

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

    fn constants() -> &'static Reader {
        static READER: OnceLock<Reader> = OnceLock::new();
        READER.get_or_init(|| {
            let mut file = writer::File::new("reader_resolution");
            let base = file.TypeRef("System", "Object");
            file.TypeDef(
                "Test",
                "Apis",
                writer::TypeDefOrRef::TypeRef(base),
                TypeAttributes::Public,
            );
            let attribute =
                file.TypeRef("Windows.Win32.Metadata", "SupportedArchitectureAttribute");
            let constructor = file.MemberRef(
                ".ctor",
                &windows_metadata::Signature {
                    flags: MethodCallAttributes::HASTHIS,
                    types: vec![windows_metadata::Type::I32],
                    ..Default::default()
                },
                writer::MemberRefParent::TypeRef(attribute),
            );
            for (name, value) in [
                ("Valid", Value::I32(2)),
                ("Concurrent", Value::I32(2)),
                ("Malformed", Value::U16(2)),
            ] {
                let field = file.Field(
                    name,
                    &windows_metadata::Type::I32,
                    FieldAttributes::Public | FieldAttributes::Static | FieldAttributes::Literal,
                );
                file.Constant(writer::HasConstant::Field(field), &Value::I32(0));
                // The malformed value is shorter than the constructor's i32 argument.
                file.Attribute(
                    writer::HasAttribute::Field(field),
                    writer::AttributeType::MemberRef(constructor),
                    &[(String::new(), value)],
                );
            }
            Reader::new(vec![File::new(file.into_stream()).unwrap()])
        })
    }

    #[test]
    fn resolution_is_cached_and_failures_do_not_poison_the_reader() {
        let reader = constants();
        assert!(reader.contains_name("Test", "Malformed"));
        for _ in 0..2 {
            assert!(
                std::panic::catch_unwind(|| reader.unwrap_full_name("Test", "Malformed")).is_err()
            );
            assert_eq!(reader.unwrap_full_name("Test", "Valid").arches(), 2);
        }
        assert!(Arc::ptr_eq(
            &reader.types(TypeName("Test", "Valid")),
            &reader.types(TypeName("Test", "Valid")),
        ));
    }

    #[test]
    fn concurrent_first_lookup_shares_the_resolution() {
        let reader = constants();
        let name = TypeName("Test", "Concurrent");
        assert!(!reader.cache.read().unwrap().contains_key(&name));
        let barrier = Barrier::new(4);
        let results = std::thread::scope(|scope| {
            let threads: Vec<_> = (0..4)
                .map(|_| {
                    scope.spawn(|| {
                        barrier.wait();
                        reader.types(name)
                    })
                })
                .collect();
            threads
                .into_iter()
                .map(|thread| thread.join().unwrap())
                .collect::<Vec<_>>()
        });
        for result in &results {
            assert!(Arc::ptr_eq(&results[0], result));
            assert_eq!(result[0].arches(), 2);
        }
    }

    #[test]
    fn malformed_nested_record_fails_when_reached_as_a_dependency() {
        static READER: OnceLock<Reader> = OnceLock::new();
        let reader = READER.get_or_init(|| {
            let mut file = writer::File::new("nested_dependency");
            let base = writer::TypeDefOrRef::TypeRef(file.TypeRef("System", "ValueType"));
            let flags = TypeAttributes::Public | TypeAttributes::SequentialLayout;
            file.TypeDef("Test", "Valid", base, flags);
            file.Field(
                "value",
                &windows_metadata::Type::I32,
                FieldAttributes::Public,
            );
            file.TypeDef("Test", "Consumer", base, flags);
            file.Field(
                "value",
                &windows_metadata::Type::value_named("Test", "Broken"),
                FieldAttributes::Public,
            );
            let broken = file.TypeDef("Test", "Broken", base, flags);
            file.Field(
                "value",
                &windows_metadata::Type::I32,
                FieldAttributes::Public,
            );
            let attribute = file.TypeRef("Windows.Win32.Metadata", "NativeAnonymousAttribute");
            let constructor = file.MemberRef(
                ".ctor",
                &windows_metadata::Signature {
                    flags: MethodCallAttributes::HASTHIS,
                    types: vec![windows_metadata::Type::I32],
                    ..Default::default()
                },
                writer::MemberRefParent::TypeRef(attribute),
            );
            for name in ["First", "Second"] {
                let child = file.TypeDef(
                    "",
                    name,
                    base,
                    TypeAttributes::NestedPublic | TypeAttributes::SequentialLayout,
                );
                file.NestedClass(child, broken);
                file.Attribute(
                    writer::HasAttribute::TypeDef(child),
                    writer::AttributeType::MemberRef(constructor),
                    &[(String::new(), Value::I32(0))],
                );
            }
            Reader::new(vec![File::new(file.into_stream()).unwrap()])
        });
        reader
            .unwrap_type_name("Test", "Valid")
            .dependencies(reader);
        let consumer = reader.unwrap_type_name("Test", "Consumer");
        let error = std::panic::catch_unwind(|| consumer.dependencies(reader)).unwrap_err();
        assert_eq!(
            error.downcast_ref::<String>().unwrap(),
            "duplicate anonymous field position in `Broken`"
        );
        reader
            .unwrap_type_name("Test", "Valid")
            .dependencies(reader);
    }
}