rune/indexing/
mod.rs

1pub(crate) mod index;
2pub(crate) mod index2;
3mod indexer;
4pub(crate) mod items;
5mod scopes;
6
7use crate as rune;
8use crate::alloc::prelude::*;
9use crate::ast::{self, Span, Spanned};
10use crate::compile::meta;
11use crate::compile::{ItemId, ItemMeta};
12use crate::grammar::NodeAt;
13use crate::runtime::Call;
14
15use self::indexer::{ast_to_visibility, validate_call};
16pub(crate) use self::indexer::{IndexItem, Indexer};
17use self::items::Guard;
18pub(crate) use self::items::Items;
19use self::scopes::Layer;
20pub(crate) use self::scopes::Scopes;
21
22#[derive(Debug, TryClone)]
23pub(crate) struct Entry {
24    /// The query item this indexed entry belongs to.
25    pub(crate) item_meta: ItemMeta,
26    /// The entry data.
27    pub(crate) indexed: Indexed,
28}
29
30impl Entry {
31    /// The item that best describes this indexed entry.
32    pub(crate) fn item(&self) -> ItemId {
33        match &self.indexed {
34            Indexed::Import(Import { entry, .. }) => entry.target,
35            _ => self.item_meta.item,
36        }
37    }
38}
39
40/// An entry that has been indexed.
41#[derive(Debug, TryClone)]
42pub(crate) enum Indexed {
43    /// An enum.
44    Enum,
45    /// A struct.
46    Struct(Struct),
47    /// A variant.
48    Variant(Variant),
49    /// A function.
50    Function(Function),
51    /// A constant expression.
52    ConstExpr(ConstExpr),
53    /// A constant block.
54    ConstBlock(ConstBlock),
55    /// A constant function.
56    ConstFn(ConstFn),
57    /// An import.
58    Import(Import),
59    /// An indexed module.
60    Module,
61}
62
63/// The ast of a function.
64#[derive(Debug, TryClone, Spanned)]
65pub(crate) enum FunctionAst {
66    /// The bare node being processed.
67    Bare(#[rune(span)] NodeAt),
68    /// The node being processed.
69    Node(#[rune(span)] NodeAt, Option<ast::Ident>),
70    /// An empty function body.
71    Empty(Box<ast::EmptyBlock>, #[rune(span)] Span),
72    /// A regular item function body.
73    Item(#[rune(span)] Box<ast::ItemFn>, ast::Ident),
74}
75
76#[derive(Debug, TryClone)]
77pub(crate) struct Function {
78    /// Ast for declaration.
79    pub(crate) ast: FunctionAst,
80    /// The calling convention of the function.
81    pub(crate) call: Call,
82    /// If this is an instance function that receives `self`.
83    pub(crate) is_instance: bool,
84    /// If this is a test function.
85    pub(crate) is_test: bool,
86    /// If this is a bench function.
87    pub(crate) is_bench: bool,
88    /// The impl item this function is registered in.
89    pub(crate) impl_item: Option<ItemId>,
90    /// Spans of the arguments to the function for diagnostics.
91    pub(crate) args: Vec<Span>,
92}
93
94#[derive(Debug, TryClone, Clone, Copy)]
95#[try_clone(copy)]
96pub(crate) struct Import {
97    /// The import entry.
98    pub(crate) entry: meta::Import,
99    /// Indicates if the import is a wildcard or not.
100    ///
101    /// Wildcard imports do not cause unused warnings.
102    pub(crate) wildcard: bool,
103}
104
105#[derive(Debug, TryClone)]
106pub(crate) struct Struct {
107    /// The fields of the struct.
108    pub(crate) fields: meta::Fields,
109}
110
111#[derive(Debug, TryClone)]
112pub(crate) struct Variant {
113    /// Id of of the enum type.
114    pub(crate) enum_id: ItemId,
115    /// The fields of the variant.
116    pub(crate) fields: meta::Fields,
117}
118
119#[derive(Debug, TryClone)]
120pub(crate) enum ConstExpr {
121    /// An ast-based constant expression.
122    Ast(Box<ast::Expr>),
123    /// A node constant expression.
124    Node(NodeAt),
125}
126
127#[derive(Debug, TryClone)]
128pub(crate) enum ConstBlock {
129    /// An ast block.
130    Ast(Box<ast::Block>),
131    /// A node block.
132    Node(NodeAt),
133}
134
135#[derive(Debug, TryClone)]
136pub(crate) enum ConstFn {
137    /// The node of a constant function.
138    Node(NodeAt),
139    /// The const fn ast.
140    Ast(Box<ast::ItemFn>),
141}