Skip to main content

lisette_syntax/program/
package.rs

1use crate::collections::DenseMap;
2use ecow::EcoString;
3use rustc_hash::{FxHashMap, FxHashSet};
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 const GO_IMPORT_PREFIX: &str = "go:";
72
73pub const PRELUDE_PACKAGE_ID: &str = "prelude";
74
75pub const PRELUDE_PREFIX: &str = "prelude.";
76
77pub fn is_go_import(id: &str) -> bool {
78    id.starts_with(GO_IMPORT_PREFIX)
79}
80
81pub fn strip_go_prefix(id: &str) -> Option<&str> {
82    id.strip_prefix(GO_IMPORT_PREFIX)
83}
84
85pub fn is_prelude_symbol(id: &str) -> bool {
86    id.starts_with(PRELUDE_PREFIX)
87}
88
89pub fn strip_prelude_prefix(id: &str) -> Option<&str> {
90    id.strip_prefix(PRELUDE_PREFIX)
91}
92
93pub fn is_internal_package_id(id: &str) -> bool {
94    id == PRELUDE_PACKAGE_ID || id == "**test_prelude" || id == "**nominal" || is_go_import(id)
95}
96
97#[derive(Debug, Clone)]
98pub struct Package {
99    pub id: String,
100    /// file ID -> file. Source and declaration files are classified by [`File::is_d_lis`].
101    pub files: FxHashMap<u32, File>,
102    /// qualified name -> definition
103    pub definitions: DenseMap<Symbol, Definition>,
104    /// Go symbol name -> reason Lisette cannot support it, from `#[go(skipped, "...")]`
105    pub unsupported_go_symbols: FxHashMap<EcoString, String>,
106    /// Set when an import cycle keeps the package out of inference: nothing
107    /// registers its files, so its exports are read from syntax.
108    pub uninferred_exports: Option<UninferredExports>,
109}
110
111#[derive(Debug, Clone)]
112pub enum UninferredExports {
113    /// Read from the syntax of the package's files.
114    Known(FxHashSet<EcoString>),
115    /// A file did not parse, so its declarations cannot be read.
116    Unreadable,
117}
118
119impl UninferredExports {
120    pub fn may_contain(&self, member: &str) -> bool {
121        match self {
122            Self::Known(names) => names.contains(member),
123            Self::Unreadable => true,
124        }
125    }
126}
127
128impl Package {
129    pub fn new(id: &str) -> Package {
130        Package {
131            id: id.to_string(),
132            files: Default::default(),
133            definitions: Default::default(),
134            unsupported_go_symbols: Default::default(),
135            uninferred_exports: None,
136        }
137    }
138
139    pub fn nominal() -> Package {
140        Package::new("**nominal")
141    }
142
143    pub fn is_public(&self, qualified_name: &str) -> bool {
144        if let Some(definition) = self.definitions.get(qualified_name) {
145            return definition.visibility == Visibility::Public;
146        }
147
148        false
149    }
150
151    pub fn get_file(&self, file_id: u32) -> Option<&File> {
152        self.files.get(&file_id)
153    }
154
155    pub fn file_ids(&self) -> impl Iterator<Item = u32> + '_ {
156        self.source_file_entries().map(|(file_id, _)| *file_id)
157    }
158
159    pub fn source_files(&self) -> impl Iterator<Item = &File> {
160        self.files.values().filter(|file| !file.is_d_lis())
161    }
162
163    pub fn source_file_entries(&self) -> impl Iterator<Item = (&u32, &File)> {
164        self.files.iter().filter(|(_, file)| !file.is_d_lis())
165    }
166
167    pub fn typedef_files(&self) -> impl Iterator<Item = &File> {
168        self.files.values().filter(|file| file.is_d_lis())
169    }
170
171    pub fn is_typedef(&self, file_id: u32) -> bool {
172        self.files.get(&file_id).is_some_and(File::is_d_lis)
173    }
174
175    pub fn is_empty_stub(&self) -> bool {
176        self.files.is_empty() && self.definitions.is_empty()
177    }
178}