emmylua_code_analysis/semantic/cache/
mod.rs1mod cache_options;
2
3pub use cache_options::{CacheOptions, LuaAnalysisPhase};
4use emmylua_parser::LuaSyntaxId;
5use std::{
6 collections::{HashMap, HashSet},
7 sync::Arc,
8};
9
10use crate::{FileId, FlowId, LuaFunctionType, db_index::LuaType, semantic::infer::VarRefId};
11
12#[derive(Debug)]
13pub enum CacheEntry<T> {
14 Ready,
15 Cache(T),
16}
17
18#[derive(Debug)]
19pub struct LuaInferCache {
20 file_id: FileId,
21 config: CacheOptions,
22 pub expr_cache: HashMap<LuaSyntaxId, CacheEntry<LuaType>>,
23 pub call_cache:
24 HashMap<(LuaSyntaxId, Option<usize>, LuaType), CacheEntry<Arc<LuaFunctionType>>>,
25 pub flow_node_cache: HashMap<(VarRefId, FlowId), CacheEntry<LuaType>>,
26 pub index_ref_origin_type_cache: HashMap<VarRefId, CacheEntry<LuaType>>,
27 pub expr_var_ref_id_cache: HashMap<LuaSyntaxId, VarRefId>,
28 pub narrow_by_literal_stop_position_cache: HashSet<LuaSyntaxId>,
29}
30
31impl LuaInferCache {
32 pub fn new(file_id: FileId, config: CacheOptions) -> Self {
33 Self {
34 file_id,
35 config,
36 expr_cache: HashMap::new(),
37 call_cache: HashMap::new(),
38 flow_node_cache: HashMap::new(),
39 index_ref_origin_type_cache: HashMap::new(),
40 expr_var_ref_id_cache: HashMap::new(),
41 narrow_by_literal_stop_position_cache: HashSet::new(),
42 }
43 }
44
45 pub fn get_config(&self) -> &CacheOptions {
46 &self.config
47 }
48
49 pub fn get_file_id(&self) -> FileId {
50 self.file_id
51 }
52
53 pub fn set_phase(&mut self, phase: LuaAnalysisPhase) {
54 self.config.analysis_phase = phase;
55 }
56
57 pub fn clear(&mut self) {
58 self.expr_cache.clear();
59 self.call_cache.clear();
60 self.flow_node_cache.clear();
61 self.index_ref_origin_type_cache.clear();
62 self.expr_var_ref_id_cache.clear();
63 }
64}