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    /// Go symbol name -> reason Lisette cannot support it, from `#[go(skipped, "...")]`
83    pub unsupported_go_symbols: HashMap<EcoString, String>,
84    /// Set when an import cycle keeps the package out of inference: nothing
85    /// registers its files, so its exports are read from syntax.
86    pub uninferred_exports: Option<UninferredExports>,
87}
88
89#[derive(Debug, Clone)]
90pub enum UninferredExports {
91    /// Read from the syntax of the package's files.
92    Known(HashSet<EcoString>),
93    /// A file did not parse, so its declarations cannot be read.
94    Unreadable,
95}
96
97impl UninferredExports {
98    pub fn may_contain(&self, member: &str) -> bool {
99        match self {
100            Self::Known(names) => names.contains(member),
101            Self::Unreadable => true,
102        }
103    }
104}
105
106impl Package {
107    pub fn new(id: &str) -> Package {
108        Package {
109            id: id.to_string(),
110            files: Default::default(),
111            definitions: Default::default(),
112            unsupported_go_symbols: Default::default(),
113            uninferred_exports: None,
114        }
115    }
116
117    pub fn nominal() -> Package {
118        Package::new("**nominal")
119    }
120
121    pub fn is_public(&self, qualified_name: &str) -> bool {
122        if let Some(definition) = self.definitions.get(qualified_name) {
123            return definition.visibility == Visibility::Public;
124        }
125
126        false
127    }
128
129    pub fn get_file(&self, file_id: u32) -> Option<&File> {
130        self.files.get(&file_id)
131    }
132
133    pub fn file_ids(&self) -> impl Iterator<Item = u32> + '_ {
134        self.source_file_entries().map(|(file_id, _)| *file_id)
135    }
136
137    pub fn source_files(&self) -> impl Iterator<Item = &File> {
138        self.files.values().filter(|file| !file.is_d_lis())
139    }
140
141    pub fn source_file_entries(&self) -> impl Iterator<Item = (&u32, &File)> {
142        self.files.iter().filter(|(_, file)| !file.is_d_lis())
143    }
144
145    pub fn typedef_files(&self) -> impl Iterator<Item = &File> {
146        self.files.values().filter(|file| file.is_d_lis())
147    }
148
149    pub fn is_typedef(&self, file_id: u32) -> bool {
150        self.files.get(&file_id).is_some_and(File::is_d_lis)
151    }
152
153    pub fn is_empty_stub(&self) -> bool {
154        self.files.is_empty() && self.definitions.is_empty()
155    }
156}