swamp_script_semantic/
inst_cache.rs1use seq_fmt::comma;
2use seq_map::{SeqMap, SeqMapError};
3use swamp_script_types::Type;
4
5#[derive(Debug, Clone)]
7pub struct InstantiationCache {
8 pub cache: SeqMap<String, Type>,
9}
10
11impl InstantiationCache {
12 pub fn new() -> Self {
13 Self {
14 cache: SeqMap::default(),
15 }
16 }
17
18 pub fn complete_name(path: &[String], base_name: &str, argument_types: &[Type]) -> String {
19 format!(
20 "{}::{}<{}>",
21 path.join("::"),
22 base_name,
23 comma(argument_types)
24 )
25 }
26 pub fn add(
27 &mut self,
28 path: &[String],
29 name: &str,
30 ty: Type,
31 argument_type: &[Type],
32 ) -> Result<(), SeqMapError> {
33 if let Type::Blueprint(_) = ty {
34 panic!("can not add blueprint to cache");
35 }
36 let converted_name = Self::complete_name(path, name, argument_type);
37 self.cache.insert(converted_name, ty)
38 }
39
40 pub fn is_empty(&self) -> bool {
41 self.cache.is_empty()
42 }
43
44 pub fn get(&self, path: &[String], base_name: &str, argument_type: &[Type]) -> Option<&Type> {
45 let name = Self::complete_name(path, base_name, argument_type);
46
47 self.cache.get(&name)
48 }
49}