Skip to main content

lisette_syntax/program/
package.rs

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