rune/indexing/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
pub(crate) mod index;
pub(crate) mod index2;
mod indexer;
pub(crate) mod items;
mod scopes;

use crate as rune;
use crate::alloc::prelude::*;
use crate::ast::{self, Span, Spanned};
use crate::compile::meta;
use crate::compile::{ItemId, ItemMeta};
use crate::grammar::NodeAt;
use crate::runtime::Call;

use self::indexer::{ast_to_visibility, validate_call};
pub(crate) use self::indexer::{IndexItem, Indexer};
use self::items::Guard;
pub(crate) use self::items::Items;
use self::scopes::Layer;
pub(crate) use self::scopes::Scopes;

#[derive(Debug, TryClone)]
pub(crate) struct Entry {
    /// The query item this indexed entry belongs to.
    pub(crate) item_meta: ItemMeta,
    /// The entry data.
    pub(crate) indexed: Indexed,
}

impl Entry {
    /// The item that best describes this indexed entry.
    pub(crate) fn item(&self) -> ItemId {
        match &self.indexed {
            Indexed::Import(Import { entry, .. }) => entry.target,
            _ => self.item_meta.item,
        }
    }
}

/// An entry that has been indexed.
#[derive(Debug, TryClone)]
pub(crate) enum Indexed {
    /// An enum.
    Enum,
    /// A struct.
    Struct(Struct),
    /// A variant.
    Variant(Variant),
    /// A function.
    Function(Function),
    /// A constant expression.
    ConstExpr(ConstExpr),
    /// A constant block.
    ConstBlock(ConstBlock),
    /// A constant function.
    ConstFn(ConstFn),
    /// An import.
    Import(Import),
    /// An indexed module.
    Module,
}

/// The ast of a function.
#[derive(Debug, TryClone, Spanned)]
pub(crate) enum FunctionAst {
    /// The bare node being processed.
    Bare(#[rune(span)] NodeAt),
    /// The node being processed.
    Node(#[rune(span)] NodeAt, Option<ast::Ident>),
    /// An empty function body.
    Empty(Box<ast::EmptyBlock>, #[rune(span)] Span),
    /// A regular item function body.
    Item(#[rune(span)] Box<ast::ItemFn>, ast::Ident),
}

#[derive(Debug, TryClone)]
pub(crate) struct Function {
    /// Ast for declaration.
    pub(crate) ast: FunctionAst,
    /// The calling convention of the function.
    pub(crate) call: Call,
    /// If this is an instance function that receives `self`.
    pub(crate) is_instance: bool,
    /// If this is a test function.
    pub(crate) is_test: bool,
    /// If this is a bench function.
    pub(crate) is_bench: bool,
    /// The impl item this function is registered in.
    pub(crate) impl_item: Option<ItemId>,
    /// Spans of the arguments to the function for diagnostics.
    pub(crate) args: Vec<Span>,
}

#[derive(Debug, TryClone, Clone, Copy)]
#[try_clone(copy)]
pub(crate) struct Import {
    /// The import entry.
    pub(crate) entry: meta::Import,
    /// Indicates if the import is a wildcard or not.
    ///
    /// Wildcard imports do not cause unused warnings.
    pub(crate) wildcard: bool,
}

#[derive(Debug, TryClone)]
pub(crate) struct Struct {
    /// The fields of the struct.
    pub(crate) fields: meta::Fields,
}

#[derive(Debug, TryClone)]
pub(crate) struct Variant {
    /// Id of of the enum type.
    pub(crate) enum_id: ItemId,
    /// The index of the variant in its source.
    pub(crate) index: usize,
    /// The fields of the variant.
    pub(crate) fields: meta::Fields,
}

#[derive(Debug, TryClone)]
pub(crate) enum ConstExpr {
    /// An ast-based constant expression.
    Ast(Box<ast::Expr>),
    /// A node constant expression.
    Node(NodeAt),
}

#[derive(Debug, TryClone)]
pub(crate) enum ConstBlock {
    /// An ast block.
    Ast(Box<ast::Block>),
    /// A node block.
    Node(NodeAt),
}

#[derive(Debug, TryClone)]
pub(crate) enum ConstFn {
    /// The node of a constant function.
    Node(NodeAt),
    /// The const fn ast.
    Ast(Box<ast::ItemFn>),
}