plotnik_bytecode/type_system/primitives.rs
1//! Primitive (builtin) type definitions.
2//!
3//! These are the fundamental types that exist in every query,
4//! with fixed indices 0, 1, 2 reserved across both analysis and bytecode.
5
6/// Index for the Void type (produces nothing).
7pub const TYPE_VOID: u16 = 0;
8
9/// Index for the Node type (tree-sitter AST node reference).
10pub const TYPE_NODE: u16 = 1;
11
12/// Index for the String type (extracted source text).
13pub const TYPE_STRING: u16 = 2;
14
15/// First index available for user-defined/composite types.
16pub const TYPE_CUSTOM_START: u16 = 3;
17
18/// Primitive type enumeration.
19///
20/// These are the builtin scalar types that don't require
21/// additional metadata in the type table.
22#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
23#[repr(u16)]
24pub enum PrimitiveType {
25 /// Produces nothing, transparent to parent scope.
26 Void = TYPE_VOID,
27 /// A tree-sitter AST node reference.
28 Node = TYPE_NODE,
29 /// Extracted text from a node.
30 String = TYPE_STRING,
31}
32
33impl PrimitiveType {
34 /// Try to convert a type index to a primitive type.
35 #[inline]
36 pub fn from_index(index: u16) -> Option<Self> {
37 match index {
38 TYPE_VOID => Some(Self::Void),
39 TYPE_NODE => Some(Self::Node),
40 TYPE_STRING => Some(Self::String),
41 _ => None,
42 }
43 }
44
45 /// Get the type index for this primitive.
46 #[inline]
47 pub const fn index(self) -> u16 {
48 self as u16
49 }
50
51 /// Check if a type index is a builtin primitive.
52 #[inline]
53 pub fn is_builtin(index: u16) -> bool {
54 index <= TYPE_STRING
55 }
56
57 /// Get the display name for this primitive (for bytecode dumps).
58 pub const fn name(self) -> &'static str {
59 match self {
60 Self::Void => "Void",
61 Self::Node => "Node",
62 Self::String => "String",
63 }
64 }
65}