summaryrefslogtreecommitdiffstats
path: root/vendor/windows-metadata/src/tables.rs
blob: d7e3ad6086a2c591304747b73f4a81dcd6ab73e0 (plain)
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
use super::*;

macro_rules! tables {
    ($(($name:ident, $table:literal))+) => {
        $(
        #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd)]
        pub struct $name(pub Row);
        impl AsRow for $name {
            const TABLE: usize = $table;
            fn to_row(&self) -> Row {
                self.0
            }
            fn from_row(row: Row) -> Self {
                $name(row)
            }
        }
    )*
    };
}

tables! {
    (AssemblyRef, 15)
    (Attribute, 1)
    (ClassLayout, 16)
    (Constant, 0)
    (Field, 2)
    (GenericParam, 3)
    (ImplMap, 11)
    (InterfaceImpl, 4)
    (MemberRef, 5)
    (MethodDef, 6)
    (Module, 14)
    (ModuleRef, 12)
    (NestedClass, 13)
    (Param, 7)
    (TypeDef, 8)
    (TypeRef, 9)
    (TypeSpec, 10)
}

impl Attribute {
    pub fn parent(&self) -> HasAttribute {
        self.decode(0)
    }

    pub fn ty(&self) -> AttributeType {
        self.decode(1)
    }

    pub fn name(&self) -> &'static str {
        let AttributeType::MemberRef(ctor) = self.ty();
        let MemberRefParent::TypeRef(ty) = ctor.parent();
        ty.name()
    }

    pub fn type_name(&self) -> TypeName {
        let AttributeType::MemberRef(ctor) = self.ty();
        let MemberRefParent::TypeRef(ty) = ctor.parent();
        ty.type_name()
    }

    pub fn args(&self) -> Vec<(&'static str, Value)> {
        let AttributeType::MemberRef(member) = self.ty();
        let mut sig = member.blob(2);
        let mut values = self.blob(2);
        let _prolog = values.read_u16();
        let _this_and_gen_param_count = sig.read_usize();
        let fixed_arg_count = sig.read_usize();
        let _ret_type = sig.read_usize();
        let mut args = Vec::with_capacity(fixed_arg_count);
        let reader = self.reader();

        for _ in 0..fixed_arg_count {
            let arg = match reader.type_from_blob(&mut sig, None, &[]) {
                Type::Bool => Value::Bool(values.read_bool()),
                Type::I8 => Value::I8(values.read_i8()),
                Type::U8 => Value::U8(values.read_u8()),
                Type::I16 => Value::I16(values.read_i16()),
                Type::U16 => Value::U16(values.read_u16()),
                Type::I32 => Value::I32(values.read_i32()),
                Type::U32 => Value::U32(values.read_u32()),
                Type::I64 => Value::I64(values.read_i64()),
                Type::U64 => Value::U64(values.read_u64()),
                Type::String => Value::String(values.read_str().to_string()),
                Type::Type => Value::TypeName(TypeName::parse(values.read_str())),
                Type::TypeDef(def, _) => Value::EnumDef(def, Box::new(values.read_integer(def.underlying_type()))),
                rest => unimplemented!("{rest:?}"),
            };

            args.push(("", arg));
        }

        let named_arg_count = values.read_u16();
        args.reserve(named_arg_count as usize);

        for _ in 0..named_arg_count {
            let _id = values.read_u8();
            let arg_type = values.read_u8();
            let mut name = values.read_str();
            let arg = match arg_type {
                ELEMENT_TYPE_BOOLEAN => Value::Bool(values.read_bool()),
                ELEMENT_TYPE_I2 => Value::I16(values.read_i16()),
                ELEMENT_TYPE_I4 => Value::I32(values.read_i32()),
                ELEMENT_TYPE_U4 => Value::U32(values.read_u32()),
                ELEMENT_TYPE_STRING => Value::String(values.read_str().to_string()),
                0x50 => Value::TypeName(TypeName::parse(values.read_str())),
                0x55 => {
                    let type_name = TypeName::parse(name);
                    let def = reader.get_type_def(type_name.namespace, type_name.name).next().expect("Type not found");
                    name = values.read_str();
                    Value::EnumDef(def, Box::new(values.read_integer(def.underlying_type())))
                }
                rest => unimplemented!("{rest:?}"),
            };
            args.push((name, arg));
        }

        debug_assert_eq!(sig.slice.len(), 0);
        debug_assert_eq!(values.slice.len(), 0);

        args
    }
}

impl ClassLayout {
    pub fn packing_size(&self) -> usize {
        self.usize(0)
    }
}

impl Constant {
    pub fn ty(&self) -> Type {
        Type::from_code(self.usize(0)).expect("Constant type not found")
    }

    pub fn value(&self) -> Value {
        let mut blob = self.blob(2);

        match self.ty() {
            Type::I8 => Value::I8(blob.read_i8()),
            Type::U8 => Value::U8(blob.read_u8()),
            Type::I16 => Value::I16(blob.read_i16()),
            Type::U16 => Value::U16(blob.read_u16()),
            Type::I32 => Value::I32(blob.read_i32()),
            Type::U32 => Value::U32(blob.read_u32()),
            Type::I64 => Value::I64(blob.read_i64()),
            Type::U64 => Value::U64(blob.read_u64()),
            Type::F32 => Value::F32(blob.read_f32()),
            Type::F64 => Value::F64(blob.read_f64()),
            Type::String => Value::String(blob.read_string()),
            rest => unimplemented!("{rest:?}"),
        }
    }
}

impl Field {
    pub fn flags(&self) -> FieldAttributes {
        FieldAttributes(self.usize(0) as u16)
    }

    pub fn name(&self) -> &'static str {
        self.str(1)
    }

    pub fn constant(&self) -> Option<Constant> {
        self.equal_range(1, HasConstant::Field(*self).encode()).next()
    }

    // TODO: enclosing craziness is only needed for nested structs - get rid of those in riddle and this goes away.
    pub fn ty(&self, enclosing: Option<TypeDef>) -> Type {
        let mut blob = self.blob(2);
        blob.read_usize();
        blob.read_modifiers();
        let def = self.reader().type_from_blob(&mut blob, enclosing, &[]);

        if self.has_attribute("ConstAttribute") {
            def.to_const_type().to_const_ptr()
        } else {
            def
        }
    }
}

impl GenericParam {
    pub fn number(&self) -> u16 {
        self.usize(0) as u16
    }

    pub fn name(&self) -> &'static str {
        self.str(3)
    }
}

impl ImplMap {
    pub fn flags(&self) -> PInvokeAttributes {
        PInvokeAttributes(self.usize(0))
    }

    pub fn scope(&self) -> ModuleRef {
        ModuleRef(self.row(3))
    }

    pub fn import_name(&self) -> &'static str {
        self.str(2)
    }
}

impl InterfaceImpl {
    pub fn ty(&self, generics: &[Type]) -> Type {
        self.reader().type_from_ref(self.decode(1), None, generics)
    }
}

impl MemberRef {
    pub fn parent(&self) -> MemberRefParent {
        self.decode(0)
    }

    pub fn name(&self) -> &'static str {
        self.str(1)
    }
}

impl MethodDef {
    pub fn impl_flags(&self) -> MethodImplAttributes {
        MethodImplAttributes(self.usize(1) as u16)
    }

    pub fn flags(&self) -> MethodAttributes {
        MethodAttributes(self.usize(2) as u16)
    }

    pub fn name(&self) -> &'static str {
        self.str(3)
    }

    pub fn params(&self) -> RowIterator<Param> {
        self.list(5)
    }

    pub fn impl_map(&self) -> Option<ImplMap> {
        self.equal_range(1, MemberForwarded::MethodDef(*self).encode()).next()
    }

    pub fn module_name(&self) -> String {
        // TODO: riddle should always lower case the module name to avoid allocating here
        let Some(impl_map) = self.impl_map() else {
            return String::new();
        };

        impl_map.scope().name().to_lowercase()
    }

    pub fn signature(&self, generics: &[Type]) -> MethodDefSig {
        let reader = self.reader();
        let mut blob = self.blob(4);
        let call_flags = MethodCallAttributes(blob.read_usize() as u8);
        let params = blob.read_usize();
        let return_type = reader.type_from_blob(&mut blob, None, generics);

        MethodDefSig { call_flags, return_type, params: (0..params).map(|_| reader.type_from_blob(&mut blob, None, generics)).collect() }
    }
}

impl ModuleRef {
    pub fn name(&self) -> &'static str {
        self.str(0)
    }
}

impl NestedClass {
    pub fn inner(&self) -> TypeDef {
        TypeDef(self.row(0))
    }

    pub fn outer(&self) -> TypeDef {
        TypeDef(self.row(1))
    }
}

impl Param {
    pub fn flags(&self) -> ParamAttributes {
        ParamAttributes(self.usize(0) as u16)
    }

    pub fn sequence(&self) -> u16 {
        self.usize(1) as u16
    }

    pub fn name(&self) -> &'static str {
        self.str(2)
    }
}

impl TypeDef {
    pub fn flags(&self) -> TypeAttributes {
        TypeAttributes(self.usize(0) as u32)
    }

    pub fn name(&self) -> &'static str {
        trim_tick(self.str(1))
    }

    pub fn namespace(&self) -> &'static str {
        self.str(2)
    }

    pub fn type_name(&self) -> TypeName {
        TypeName::new(self.namespace(), self.name())
    }

    pub fn extends(&self) -> Option<TypeName> {
        let extends = self.usize(3);

        if extends == 0 {
            return None;
        }

        Some(TypeDefOrRef::decode(self.file(), extends).type_name())
    }

    pub fn methods(&self) -> RowIterator<MethodDef> {
        self.list(5)
    }

    pub fn fields(&self) -> RowIterator<Field> {
        self.list(4)
    }

    pub fn generics(&self) -> RowIterator<GenericParam> {
        self.equal_range(2, TypeOrMethodDef::TypeDef(*self).encode())
    }

    pub fn interface_impls(&self) -> RowIterator<InterfaceImpl> {
        self.equal_range(0, self.index() + 1)
    }

    pub fn enclosing_type(&self) -> Option<TypeDef> {
        self.equal_range::<NestedClass>(0, self.index() + 1).next().map(|row| TypeDef(row.row(1)))
    }

    pub fn class_layout(&self) -> Option<ClassLayout> {
        self.equal_range(2, self.index() + 1).next()
    }

    pub fn underlying_type(&self) -> Type {
        let field = self.fields().next().expect("Field not found");
        if let Some(constant) = field.constant() {
            constant.ty()
        } else {
            field.ty(Some(*self))
        }
    }

    pub fn kind(&self) -> TypeKind {
        match self.extends() {
            None => TypeKind::Interface,
            Some(TypeName::Enum) => TypeKind::Enum,
            Some(TypeName::Delegate) => TypeKind::Delegate,
            Some(TypeName::Struct) => TypeKind::Struct,
            Some(_) => TypeKind::Class,
        }
    }

    pub fn size(&self) -> usize {
        match self.kind() {
            TypeKind::Struct => {
                if self.flags().contains(TypeAttributes::ExplicitLayout) {
                    self.fields().map(|field| field.ty(Some(*self)).size()).max().unwrap_or(1)
                } else {
                    let mut sum = 0;
                    for field in self.fields() {
                        let ty = field.ty(Some(*self));
                        let size = ty.size();
                        let align = ty.align();
                        sum = (sum + (align - 1)) & !(align - 1);
                        sum += size;
                    }
                    sum
                }
            }
            TypeKind::Enum => self.underlying_type().size(),
            _ => 4,
        }
    }

    pub fn align(&self) -> usize {
        match self.kind() {
            TypeKind::Struct => self.fields().map(|field| field.ty(Some(*self)).align()).max().unwrap_or(1),
            TypeKind::Enum => self.underlying_type().align(),
            _ => 4,
        }
    }
}

impl TypeRef {
    pub fn name(&self) -> &'static str {
        trim_tick(self.str(1))
    }

    pub fn namespace(&self) -> &'static str {
        self.str(2)
    }

    pub fn type_name(&self) -> TypeName {
        TypeName::new(self.namespace(), self.name())
    }

    pub fn resolution_scope(&self) -> ResolutionScope {
        self.decode(0)
    }
}

fn trim_tick(name: &str) -> &str {
    if name.as_bytes().iter().rev().nth(1) == Some(&b'`') {
        &name[..name.len() - 2]
    } else {
        name
    }
}