swamp_analyzer/
shared.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/swamp
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5
6use source_map_cache::{FileId, SourceMap};
7use swamp_modules::prelude::ModuleRef;
8use swamp_modules::{
9    prelude::Modules,
10    symtbl::{DefinitionTable, SymbolTableRef},
11};
12use swamp_semantic::ProgramState;
13
14pub struct SharedState<'a> {
15    pub state: &'a mut ProgramState,
16    pub lookup_table: DefinitionTable,
17    pub definition_table: DefinitionTable,
18    pub modules: &'a Modules,
19    pub source_map: &'a SourceMap,
20    pub file_id: FileId,
21    pub core_symbol_table: SymbolTableRef,
22    pub allow_unsafe: bool,
23}
24
25impl<'a> SharedState<'a> {
26    #[must_use]
27    pub fn get_definition_table(&'a self, path: &[String]) -> Option<&'a DefinitionTable> {
28        if path.is_empty() {
29            return Some(&self.lookup_table);
30        }
31        self.get_module(path).map(|module| &module.definition_table)
32    }
33
34    #[must_use]
35    pub fn get_module(&'a self, path: &[String]) -> Option<&'a ModuleRef> {
36        let resolved_path = path.to_vec();
37
38        if path.len() == 1
39            && let Some(module_ref) = self.lookup_table.get_module_link(&path[0])
40        {
41            return Some(module_ref);
42        }
43
44        if let Some(x) = self.modules.get(&resolved_path) {
45            return Some(x);
46        }
47
48        None
49    }
50}