Skip to main content

lisette_syntax/program/
package.rs

1use ecow::EcoString;
2use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
3use std::borrow::Borrow;
4use std::fmt;
5use std::ops::Deref;
6
7use super::file::File;
8use super::{Definition, Visibility};
9use crate::types::Symbol;
10
11#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
12pub struct PackageId(String);
13
14impl PackageId {
15    pub fn as_str(&self) -> &str {
16        &self.0
17    }
18}
19
20impl From<&str> for PackageId {
21    fn from(value: &str) -> Self {
22        Self(value.to_string())
23    }
24}
25
26impl From<String> for PackageId {
27    fn from(value: String) -> Self {
28        Self(value)
29    }
30}
31
32impl Borrow<str> for PackageId {
33    fn borrow(&self) -> &str {
34        self.as_str()
35    }
36}
37
38impl AsRef<str> for PackageId {
39    fn as_ref(&self) -> &str {
40        self.as_str()
41    }
42}
43
44impl Deref for PackageId {
45    type Target = str;
46
47    fn deref(&self) -> &Self::Target {
48        self.as_str()
49    }
50}
51
52impl fmt::Display for PackageId {
53    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
54        formatter.write_str(self.as_str())
55    }
56}
57
58impl PartialEq<str> for PackageId {
59    fn eq(&self, other: &str) -> bool {
60        self.as_str() == other
61    }
62}
63
64impl PartialEq<&str> for PackageId {
65    fn eq(&self, other: &&str) -> bool {
66        self.as_str() == *other
67    }
68}
69
70pub fn is_internal_package_id(id: &str) -> bool {
71    id == "prelude" || id == "**test_prelude" || id == "**nominal" || id.starts_with("go:")
72}
73
74#[derive(Debug, Clone)]
75pub struct Package {
76    pub id: String,
77    /// file ID -> file. Source and declaration files are classified by [`File::is_d_lis`].
78    pub files: HashMap<u32, File>,
79    /// qualified name -> definition
80    pub definitions: HashMap<Symbol, Definition>,
81    /// Set when an import cycle keeps the package out of inference: nothing
82    /// registers its files, so its exports are read from syntax.
83    pub uninferred_exports: Option<UninferredExports>,
84}
85
86#[derive(Debug, Clone)]
87pub enum UninferredExports {
88    /// Read from the syntax of the package's files.
89    Known(HashSet<EcoString>),
90    /// A file did not parse, so its declarations cannot be read.
91    Unreadable,
92}
93
94impl UninferredExports {
95    pub fn may_contain(&self, member: &str) -> bool {
96        match self {
97            Self::Known(names) => names.contains(member),
98            Self::Unreadable => true,
99        }
100    }
101}
102
103impl Package {
104    pub fn new(id: &str) -> Package {
105        Package {
106            id: id.to_string(),
107            files: Default::default(),
108            definitions: Default::default(),
109            uninferred_exports: None,
110        }
111    }
112
113    pub fn nominal() -> Package {
114        Package::new("**nominal")
115    }
116
117    pub fn is_public(&self, qualified_name: &str) -> bool {
118        if let Some(definition) = self.definitions.get(qualified_name) {
119            return definition.visibility == Visibility::Public;
120        }
121
122        false
123    }
124
125    pub fn get_file(&self, file_id: u32) -> Option<&File> {
126        self.files.get(&file_id)
127    }
128
129    pub fn file_ids(&self) -> impl Iterator<Item = u32> + '_ {
130        self.source_file_entries().map(|(file_id, _)| *file_id)
131    }
132
133    pub fn source_files(&self) -> impl Iterator<Item = &File> {
134        self.files.values().filter(|file| !file.is_d_lis())
135    }
136
137    pub fn source_file_entries(&self) -> impl Iterator<Item = (&u32, &File)> {
138        self.files.iter().filter(|(_, file)| !file.is_d_lis())
139    }
140
141    pub fn typedef_files(&self) -> impl Iterator<Item = &File> {
142        self.files.values().filter(|file| file.is_d_lis())
143    }
144
145    pub fn is_typedef(&self, file_id: u32) -> bool {
146        self.files.get(&file_id).is_some_and(File::is_d_lis)
147    }
148
149    pub fn is_empty_stub(&self) -> bool {
150        self.files.is_empty() && self.definitions.is_empty()
151    }
152}