expr_solver/
metadata.rs

1//! Symbol metadata for bytecode validation and linking.
2
3use crate::symbol::Symbol;
4#[cfg(feature = "serialization")]
5use serde::{Deserialize, Serialize};
6use std::borrow::Cow;
7
8/// Metadata about a symbol required by compiled bytecode.
9///
10/// This is used to validate and remap symbol indices when linking
11/// bytecode with a symbol table.
12#[derive(Debug, Clone)]
13#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
14pub struct SymbolMetadata {
15    /// The name of the symbol
16    pub name: Cow<'static, str>,
17    /// The kind and requirements of the symbol
18    pub kind: SymbolKind,
19    /// Local or global?
20    pub local: bool,
21    /// The resolved index in the linked symbol table (None until linked)
22    #[cfg_attr(feature = "serialization", serde(skip))]
23    pub index: Option<usize>,
24}
25
26/// The kind of symbol (constant or function) with its requirements.
27#[derive(Debug, Clone, PartialEq, Eq)]
28#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
29pub enum SymbolKind {
30    /// A constant value
31    Const,
32    /// A function with specified arity
33    Func {
34        /// Minimum number of arguments
35        arity: usize,
36        /// Whether the function accepts additional arguments
37        variadic: bool,
38    },
39}
40
41impl From<&Symbol> for SymbolKind {
42    fn from(symbol: &Symbol) -> Self {
43        match symbol {
44            Symbol::Const { .. } => SymbolKind::Const,
45            Symbol::Func { args, variadic, .. } => SymbolKind::Func {
46                arity: *args,
47                variadic: *variadic,
48            },
49        }
50    }
51}
52
53impl From<&Symbol> for SymbolMetadata {
54    fn from(symbol: &Symbol) -> Self {
55        SymbolMetadata {
56            name: symbol.name().to_string().into(),
57            kind: symbol.into(),
58            local: symbol.is_local(),
59            index: None,
60        }
61    }
62}