ridx-syntax 0.3.8

Zero-dependency lossless Rust lexer and syntax tree for Rust Intelligence
Documentation
/*
 * rust-intelligence — Ultra-fast, zero-memory code intelligence for Rust
 * Copyright (c) 2026 Alexander Panasenko
 *
 * Contact: alex@prod.codes
 * Author: https://prod.codes/about/
 * Project: https://github.com/alex09x/rust-intelligence
 * SPDX-License-Identifier: MIT OR Apache-2.0
 */

//! Core `u32`-keyed identifiers and trait boundaries (`SourceDb`, `ItemDb`) decoupling
//! `ridx-syntax::name_res` from binary index storage (`ridx-format`).

use super::db_impls::{ImplTargetKey, IndexedImpl};

/// Dense crate or compilation-unit identifier (`u32::MAX` = absent sentinel).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct CrateId(pub u32);

impl CrateId {
    pub const NONE: Self = Self(u32::MAX);
    /// Synthetic crate slot used for dirty-file local items not yet present in the compiler index.
    pub const LOCAL_OVERLAY: Self = Self(u32::MAX - 1);

    #[inline]
    pub const fn is_some(self) -> bool {
        self.0 != Self::NONE.0
    }
}

impl Default for CrateId {
    #[inline]
    fn default() -> Self {
        Self::NONE
    }
}

/// Dense source-file identifier inside a [`SourceDb`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct FileId(pub u32);

impl FileId {
    pub const NONE: Self = Self(u32::MAX);

    #[inline]
    pub const fn is_some(self) -> bool {
        self.0 != Self::NONE.0
    }
}

impl Default for FileId {
    #[inline]
    fn default() -> Self {
        Self::NONE
    }
}

/// Dense module identifier inside a crate's [`super::mod_tree::ModuleTree`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ModuleId(pub u32);

impl ModuleId {
    pub const ROOT: Self = Self(0);
    pub const NONE: Self = Self(u32::MAX);

    #[inline]
    pub const fn is_some(self) -> bool {
        self.0 != Self::NONE.0
    }
}

impl Default for ModuleId {
    #[inline]
    fn default() -> Self {
        Self::NONE
    }
}

/// Compact 8-byte global definition key (`(CrateId, def_index)`), mapping 1-to-1 to `(shard_idx, def_idx)`
/// in `WorkspaceIndex` or `(CrateId::LOCAL_OVERLAY, local_idx)` for dirty-file definitions.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct GlobalDefId {
    pub krate: CrateId,
    pub index: u32,
}

impl GlobalDefId {
    pub const NONE: Self = Self { krate: CrateId::NONE, index: u32::MAX };

    #[inline]
    pub const fn new(krate: CrateId, index: u32) -> Self {
        Self { krate, index }
    }

    #[inline]
    pub const fn is_some(self) -> bool {
        self.krate.is_some() && self.index != u32::MAX
    }

    #[inline]
    pub const fn is_overlay(self) -> bool {
        self.krate.0 == CrateId::LOCAL_OVERLAY.0 && self.index != u32::MAX
    }
}

impl Default for GlobalDefId {
    #[inline]
    fn default() -> Self {
        Self::NONE
    }
}

/// Rust language edition of a crate.
#[repr(u8)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Edition {
    Edition2015 = 0,
    Edition2018 = 1,
    #[default]
    Edition2021 = 2,
    Edition2024 = 3,
}

impl Edition {
    #[inline]
    pub const fn has_uniform_paths(self) -> bool {
        (self as u8) >= (Self::Edition2018 as u8)
    }
}

/// Visibility class aligned with `ridx-format::ItemVis`, extended with local `super` / `self` forms.
#[repr(u8)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum VisKind {
    Public = 0,
    Crate = 1,
    Restricted = 2,
    #[default]
    Private = 3,
    Super = 4,
}

/// Kind of an indexed or local definition, aligned with `ridx-format::DefKind`.
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ItemKind {
    Mod = 0,
    Struct = 1,
    Enum = 2,
    Union = 3,
    Trait = 4,
    Fn = 5,
    TypeAlias = 6,
    Const = 7,
    Static = 8,
    Variant = 9,
    Field = 10,
    AssocFn = 11,
    AssocTy = 12,
    AssocConst = 13,
    Macro = 14,
    Impl = 15,
    Use = 16,
    ExternCrate = 17,
}

impl ItemKind {
    /// Bitmask of Rust namespaces (`NsMask`) occupied by a definition of this kind.
    #[inline]
    pub const fn ns_mask(self, has_value_ctor: bool) -> NsMask {
        match self {
            Self::Mod | Self::Enum | Self::Union | Self::Trait | Self::TypeAlias | Self::AssocTy => NsMask::TYPE,
            Self::Struct | Self::Variant => {
                if has_value_ctor {
                    NsMask(NsMask::TYPE.0 | NsMask::VALUE.0)
                } else {
                    NsMask::TYPE
                }
            }
            Self::Fn | Self::Const | Self::Static | Self::Field | Self::AssocFn | Self::AssocConst => NsMask::VALUE,
            Self::Macro => NsMask::MACRO,
            Self::ExternCrate => NsMask::TYPE,
            Self::Impl | Self::Use => NsMask::EMPTY,
        }
    }
}

/// Bitmask over the three Rust namespaces (`type`, `value`, `macro`).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct NsMask(pub u8);

impl NsMask {
    pub const EMPTY: Self = Self(0);
    pub const TYPE: Self = Self(1 << 0);
    pub const VALUE: Self = Self(1 << 1);
    pub const MACRO: Self = Self(1 << 2);
    pub const ALL: Self = Self(Self::TYPE.0 | Self::VALUE.0 | Self::MACRO.0);

    #[inline]
    pub const fn contains(self, other: Self) -> bool {
        (self.0 & other.0) == other.0
    }

    #[inline]
    pub const fn union(self, other: Self) -> Self {
        Self(self.0 | other.0)
    }
}

/// Indexed definition summary surfaced by [`ItemDb`] for module children and associated items.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct IndexedDef<'a> {
    pub id: GlobalDefId,
    pub name: &'a str,
    pub kind: ItemKind,
    pub vis: VisKind,
    pub has_value_ctor: bool,
}

/// Indexed `use` / re-export row surfaced by [`ItemDb`] (matching `v5` `UseEntry`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct IndexedUse<'a> {
    pub name: &'a str,
    pub type_def: GlobalDefId,
    pub value_def: GlobalDefId,
    pub macro_def: GlobalDefId,
    pub vis: VisKind,
    pub is_glob: bool,
    pub is_reexport: bool,
    pub is_prelude: bool,
    pub is_underscore: bool,
    pub prim_ty_code: u8,
}

/// Read-only virtual filesystem / source provider for workspace `.rs` files.
pub trait SourceDb {
    /// Maps a forward-slash normalized crate-relative or workspace-relative path to its [`FileId`].
    fn resolve_path(&self, normalized_path: &str) -> Option<FileId>;

    /// Forward-slash normalized path of `file`.
    fn file_path(&self, file: FileId) -> &str;

    /// UTF-8 source text of `file` (including any unsaved editor overlay).
    fn file_text(&self, file: FileId) -> &str;
}

/// Compiler-index query boundary (`v5` `.ridx` adapter or in-memory test database).
pub trait ItemDb {
    /// Returns `true` when bare `#[cfg(key)]` is active for `krate` (e.g. `unix`, `test`).
    fn cfg_flag(&self, krate: CrateId, key: &str) -> bool;

    /// Returns `true` when `#[cfg(key = "value")]` is active for `krate` (e.g. `feature = "rt"`).
    fn cfg_key_value(&self, krate: CrateId, key: &str, value: &str) -> bool;

    /// Language edition of `krate`.
    fn crate_edition(&self, _krate: CrateId) -> Edition {
        Edition::Edition2021
    }

    /// Canonical crate name of `krate` (underscores, e.g. `"ridx_syntax"`).
    fn crate_name(&self, _krate: CrateId) -> &str {
        ""
    }

    /// Looks up a compile-time Cargo/rustc environment variable (`OUT_DIR`, `CARGO_MANIFEST_DIR`, ...)
    /// recorded for `krate`.
    fn crate_env(&self, _krate: CrateId, _key: &str) -> Option<&str> {
        None
    }

    /// Resolves an `include!(...)` target relative to `from_file` (or absolute) to `(FileId, source_text)`
    /// when a backing [`SourceDb`] is attached to this [`ItemDb`].
    fn resolve_include_file(&self, _from_file: FileId, _rel_or_abs: &str) -> Option<(FileId, &str)> {
        None
    }

    /// Returns the exact byte length of an `include_bytes!(...)` target relative to `from_file` (or absolute).
    fn include_bytes_len(&self, _from_file: FileId, _rel_or_abs: &str) -> Option<u64> {
        None
    }

    /// Resolves an external dependency crate available to `from_krate` under `extern_name`.
    fn extern_crate(&self, _from_krate: CrateId, _extern_name: &str) -> Option<CrateId> {
        None
    }

    /// Appends all external dependency crates visible to `from_krate` as `(extern_name, CrateId)`.
    fn extern_crates<'a>(&'a self, _from_krate: CrateId, _out: &mut Vec<(&'a str, CrateId)>) {}

    /// Returns the crate root [`GlobalDefId`] of `krate`, if indexed.
    fn crate_root_def(&self, _krate: CrateId) -> Option<GlobalDefId> {
        None
    }

    /// Returns the prelude module [`GlobalDefId`] configured for `krate`, if any.
    fn prelude_module(&self, _krate: CrateId) -> Option<GlobalDefId> {
        None
    }

    /// Looks up a declarative `macro_rules!` body (`($pat) => { $body }; ...`) for item-level
    /// expansion when `call_path` (e.g. `"cfg_if"` or `"cfg::alloc"`) is invoked in `module_path`.
    fn macro_rules_body(&self, _krate: CrateId, _module_path: &str, _call_path: &str) -> Option<&str> {
        None
    }

    /// Like [`ItemDb::macro_rules_body`], also returning the `$crate` substitution token
    /// (`"crate"` for crate-local macros or the dependency crate alias for external macros).
    fn macro_rules_with_crate(&self, krate: CrateId, module_path: &str, call_path: &str) -> Option<(&str, &str)> {
        self.macro_rules_body(krate, module_path, call_path).map(|b| (b, "crate"))
    }

    /// Looks up the declarative `macro_rules!` `(body, dollar_crate)` for a resolved macro [`GlobalDefId`]
    /// invoked inside `from_krate`.
    fn macro_rules_by_def(&self, _from_krate: CrateId, _def: GlobalDefId) -> Option<(&str, &str)> {
        None
    }

    /// Returns the [`ItemKind`] of an indexed [`GlobalDefId`], if known (`O(1)` in `.ridx`).
    fn def_kind(&self, _def: GlobalDefId) -> Option<ItemKind> {
        None
    }

    /// Returns the defining workspace [`FileId`] of `def` (`FileId::NONE` for external crates).
    fn def_file(&self, _def: GlobalDefId) -> FileId {
        FileId::NONE
    }

    /// Underlying [`ImplTargetKey`] of an indexed `type Alias = Target;` (`None` if not an alias or unknown).
    fn type_alias_target(&self, _alias_def: GlobalDefId) -> Option<ImplTargetKey> {
        None
    }

    /// Appends indexed `impl` blocks matching `target` across `krate` + visible dependency crates.
    fn impls_for_target(&self, _target: ImplTargetKey, _out: &mut Vec<IndexedImpl>) {}

    /// Appends indexed `impl` blocks defined inside `krate`.
    fn crate_impls(&self, _krate: CrateId, _out: &mut Vec<IndexedImpl>) {}

    /// Appends direct child definitions of `module_def` into `out`.
    fn module_defs<'a>(&'a self, _module_def: GlobalDefId, _out: &mut Vec<IndexedDef<'a>>) {}

    /// Appends variants of an indexed `enum_def` into `out` (defaults to `module_defs`).
    fn enum_variants<'a>(&'a self, enum_def: GlobalDefId, out: &mut Vec<IndexedDef<'a>>) {
        self.module_defs(enum_def, out);
    }

    /// Appends `UseEntry` rows belonging to `module_def` (or crate root) into `out`.
    fn module_uses<'a>(&'a self, _module_def: GlobalDefId, _out: &mut Vec<IndexedUse<'a>>) {}

    /// Appends inherent and trait associated items on `owner_def` into `out`.
    fn assoc_items<'a>(&'a self, _owner_def: GlobalDefId, _out: &mut Vec<IndexedDef<'a>>) {}

    /// Appends direct supertrait [`GlobalDefId`]s declared on `trait_def` (`trait Sub: Super`).
    fn trait_supertraits(&self, _trait_def: GlobalDefId, _out: &mut Vec<GlobalDefId>) {}

    /// Resolves a named field on `adt_or_variant_def` (`Struct`, `Union`, or `Variant`), if indexed.
    fn adt_field(&self, _adt_or_variant_def: GlobalDefId, _field_name: &str) -> Option<GlobalDefId> {
        None
    }

    /// Resolves an indexed definition in `(krate, file)` whose identifier byte span is `[start..end)`.
    fn def_at_byte_span(&self, _krate: CrateId, _file: FileId, _start: u32, _end: u32) -> GlobalDefId {
        GlobalDefId::NONE
    }

    /// Looks up a lang item (`u16` discriminant matching `LangItemKind`) across visible shards.
    fn lang_item(&self, _lang_kind: u16) -> Option<GlobalDefId> {
        None
    }
}