plotnik_bytecode/bytecode/
sections.rs

1//! Binary format section primitives.
2
3use super::ids::StringId;
4
5/// Range into an array: [ptr..ptr+len).
6///
7/// Dual-use depending on context:
8/// - For `TypeDef` wrappers (Optional, Array*): `ptr` is inner `TypeId`, `len` is 0.
9/// - For `TypeDef` composites (Struct, Enum): `ptr` is index into TypeMember array, `len` is count.
10#[derive(Clone, Copy, Debug, Default)]
11#[repr(C)]
12pub struct Slice {
13    pub ptr: u16,
14    pub len: u16,
15}
16
17impl Slice {
18    #[inline]
19    pub fn range(self) -> std::ops::Range<usize> {
20        let start = self.ptr as usize;
21        start..start + self.len as usize
22    }
23}
24
25/// Maps tree-sitter NodeTypeId to its string name.
26#[derive(Clone, Copy, Debug)]
27#[repr(C)]
28pub struct NodeSymbol {
29    /// Tree-sitter node type ID
30    pub id: u16,
31    /// StringId for the node kind name
32    pub name: StringId,
33}
34
35impl NodeSymbol {
36    /// Create a new node symbol.
37    pub fn new(id: u16, name: StringId) -> Self {
38        Self { id, name }
39    }
40}
41
42/// Maps tree-sitter NodeFieldId to its string name.
43#[derive(Clone, Copy, Debug)]
44#[repr(C)]
45pub struct FieldSymbol {
46    /// Tree-sitter field ID
47    pub id: u16,
48    /// StringId for the field name
49    pub name: StringId,
50}
51
52impl FieldSymbol {
53    /// Create a new field symbol.
54    pub fn new(id: u16, name: StringId) -> Self {
55        Self { id, name }
56    }
57}
58
59/// A node type ID that counts as trivia (whitespace, comments).
60#[derive(Clone, Copy, Debug)]
61#[repr(C)]
62pub struct TriviaEntry {
63    pub node_type: u16,
64}
65
66impl TriviaEntry {
67    /// Create a new trivia entry.
68    pub fn new(node_type: u16) -> Self {
69        Self { node_type }
70    }
71}