Skip to main content

xur_bytecode/
items.rs

1//! Finding the syntax behind definitions.
2
3use std::collections::HashMap;
4
5use xur_ast::visit::{self, Visitor};
6use xur_ast::{ConstItem, Fn, Item, ItemKind, NodeId, Variant, VariantData};
7use xur_resolve::{DefId, ModuleIdx, Program, Resolutions};
8
9/// What lowering needs to know about a definition.
10#[derive(Debug, Clone, Copy)]
11pub enum ItemRef {
12    Fn {
13        item: &'static Fn,
14        module: ModuleIdx,
15    },
16    Const {
17        item: &'static ConstItem,
18        module: ModuleIdx,
19    },
20    /// A struct or enum variant, with the shape of its constructor.
21    Ctor(&'static VariantData),
22}
23
24/// Every function, constant and constructor of the loaded modules, by
25/// definition. Updated as modules are imported.
26#[derive(Debug, Default)]
27pub struct ItemIndex {
28    items: HashMap<DefId, ItemRef>,
29    /// How many of the program's modules have been indexed.
30    indexed: usize,
31}
32
33impl ItemIndex {
34    pub fn new(program: &Program) -> Self {
35        let mut index = Self::default();
36        index.update(program);
37        index
38    }
39
40    /// Indexes the modules loaded since the last update.
41    pub fn update(&mut self, program: &Program) {
42        let modules = &program.krate.modules;
43        for (i, module) in modules.iter().enumerate().skip(self.indexed) {
44            let mut indexer = Indexer {
45                resolutions: program.resolutions(),
46                module: ModuleIdx(i),
47                items: &mut self.items,
48            };
49            indexer.visit_module(module.ast);
50        }
51        self.indexed = modules.len();
52    }
53
54    pub fn get(&self, def: DefId) -> Option<ItemRef> {
55        self.items.get(&def).copied()
56    }
57
58    /// Every indexed definition.
59    pub fn iter(&self) -> impl Iterator<Item = (DefId, ItemRef)> + '_ {
60        self.items.iter().map(|(&def, &item)| (def, item))
61    }
62}
63
64struct Indexer<'a> {
65    resolutions: &'a Resolutions,
66    module: ModuleIdx,
67    items: &'a mut HashMap<DefId, ItemRef>,
68}
69
70impl Indexer<'_> {
71    fn add(&mut self, node: NodeId, item: ItemRef) {
72        if let Some(&def) = self.resolutions.node_defs.get(&node) {
73            self.items.insert(def, item);
74        }
75    }
76}
77
78/// Modules' ASTs live for the rest of the process, so the index can keep
79/// references into them.
80impl Visitor<'static> for Indexer<'_> {
81    fn visit_item(&mut self, item: &'static Item) {
82        let module = self.module;
83        match &item.kind {
84            ItemKind::Fn(func) => self.add(item.id, ItemRef::Fn { item: func, module }),
85            ItemKind::Const(konst) => self.add(
86                item.id,
87                ItemRef::Const {
88                    item: konst,
89                    module,
90                },
91            ),
92            ItemKind::Struct(_, _, data) => self.add(item.id, ItemRef::Ctor(data)),
93            ItemKind::Enum(..) | ItemKind::Impl(_) | ItemKind::Use(_) => {}
94        }
95        visit::walk_item(self, item);
96    }
97
98    fn visit_variant(&mut self, variant: &'static Variant) {
99        self.add(variant.id, ItemRef::Ctor(&variant.data));
100        visit::walk_variant(self, variant);
101    }
102}