1use std::sync::Arc;
2
3use indexmap::IndexMap;
4use mir_types::Union;
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
12pub enum Visibility {
13 Public,
14 Protected,
15 Private,
16}
17
18impl Visibility {
19 pub fn is_at_least(&self, required: Visibility) -> bool {
20 *self <= required
21 }
22}
23
24impl std::fmt::Display for Visibility {
25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26 match self {
27 Visibility::Public => write!(f, "public"),
28 Visibility::Protected => write!(f, "protected"),
29 Visibility::Private => write!(f, "private"),
30 }
31 }
32}
33
34#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
35pub struct TemplateParam {
36 pub name: Arc<str>,
37 pub bound: Option<Union>,
38 pub defining_entity: Arc<str>,
40 pub variance: mir_types::Variance,
41}
42
43#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
44pub struct FnParam {
45 pub name: Arc<str>,
46 pub ty: Option<Union>,
47 pub default: Option<Union>,
48 pub is_variadic: bool,
49 pub is_byref: bool,
50 pub is_optional: bool,
51}
52
53#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
63pub struct Location {
64 pub file: Arc<str>,
65 pub line: u32,
67 pub line_end: u32,
69 pub col_start: u16,
71 pub col_end: u16,
73}
74
75impl Location {
76 pub fn new(file: Arc<str>, line: u32, line_end: u32, col_start: u16, col_end: u16) -> Self {
77 Self {
78 file,
79 line,
80 line_end,
81 col_start,
82 col_end,
83 }
84 }
85}
86
87#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
92pub enum AssertionKind {
93 Assert,
94 AssertIfTrue,
95 AssertIfFalse,
96}
97
98#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
99pub struct Assertion {
100 pub kind: AssertionKind,
101 pub param: Arc<str>,
102 pub ty: Union,
103}
104
105#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
110pub struct MethodStorage {
111 pub name: Arc<str>,
112 pub fqcn: Arc<str>,
113 pub params: Vec<FnParam>,
114 pub return_type: Option<Union>,
116 pub inferred_return_type: Option<Union>,
118 pub visibility: Visibility,
119 pub is_static: bool,
120 pub is_abstract: bool,
121 pub is_final: bool,
122 pub is_constructor: bool,
123 pub template_params: Vec<TemplateParam>,
124 pub assertions: Vec<Assertion>,
125 pub throws: Vec<Arc<str>>,
126 pub deprecated: Option<Arc<str>>,
127 pub is_internal: bool,
128 pub is_pure: bool,
129 pub location: Option<Location>,
130}
131
132impl MethodStorage {
133 pub fn effective_return_type(&self) -> Option<&Union> {
134 self.return_type
135 .as_ref()
136 .or(self.inferred_return_type.as_ref())
137 }
138}
139
140#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
145pub struct PropertyStorage {
146 pub name: Arc<str>,
147 pub ty: Option<Union>,
148 pub inferred_ty: Option<Union>,
149 pub visibility: Visibility,
150 pub is_static: bool,
151 pub is_readonly: bool,
152 pub default: Option<Union>,
153 pub location: Option<Location>,
154}
155
156#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
161pub struct ConstantStorage {
162 pub name: Arc<str>,
163 pub ty: Union,
164 pub visibility: Option<Visibility>,
165 #[serde(default)]
166 pub is_final: bool,
167 pub location: Option<Location>,
168}
169
170#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
175pub struct ClassStorage {
176 pub fqcn: Arc<str>,
177 pub short_name: Arc<str>,
178 pub parent: Option<Arc<str>>,
179 pub interfaces: Vec<Arc<str>>,
180 pub traits: Vec<Arc<str>>,
181 pub own_methods: IndexMap<Arc<str>, Arc<MethodStorage>>,
182 pub own_properties: IndexMap<Arc<str>, PropertyStorage>,
183 pub own_constants: IndexMap<Arc<str>, ConstantStorage>,
184 #[serde(default)]
185 pub mixins: Vec<Arc<str>>,
186 pub template_params: Vec<TemplateParam>,
187 pub extends_type_args: Vec<Union>,
189 #[serde(default)]
191 pub implements_type_args: Vec<(Arc<str>, Vec<Union>)>,
192 pub is_abstract: bool,
193 pub is_final: bool,
194 pub is_readonly: bool,
195 pub deprecated: Option<Arc<str>>,
196 pub is_internal: bool,
197 pub location: Option<Location>,
198 #[serde(default)]
200 pub type_aliases: std::collections::HashMap<Arc<str>, Union>,
201 #[serde(default)]
203 pub pending_import_types: Vec<(Arc<str>, Arc<str>, Arc<str>)>,
204}
205
206impl ClassStorage {
207 pub fn get_method(&self, name: &str) -> Option<&MethodStorage> {
208 self.own_methods.get(name).map(Arc::as_ref).or_else(|| {
211 self.own_methods
212 .iter()
213 .find(|(k, _)| k.as_ref().eq_ignore_ascii_case(name))
214 .map(|(_, v)| v.as_ref())
215 })
216 }
217
218 pub fn get_property(&self, name: &str) -> Option<&PropertyStorage> {
219 self.own_properties.get(name)
220 }
221}
222
223#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
228pub struct InterfaceStorage {
229 pub fqcn: Arc<str>,
230 pub short_name: Arc<str>,
231 pub extends: Vec<Arc<str>>,
232 pub own_methods: IndexMap<Arc<str>, Arc<MethodStorage>>,
233 pub own_constants: IndexMap<Arc<str>, ConstantStorage>,
234 pub template_params: Vec<TemplateParam>,
235 pub location: Option<Location>,
236}
237
238#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
243pub struct TraitStorage {
244 pub fqcn: Arc<str>,
245 pub short_name: Arc<str>,
246 pub own_methods: IndexMap<Arc<str>, Arc<MethodStorage>>,
247 pub own_properties: IndexMap<Arc<str>, PropertyStorage>,
248 pub own_constants: IndexMap<Arc<str>, ConstantStorage>,
249 pub template_params: Vec<TemplateParam>,
250 pub traits: Vec<Arc<str>>,
252 pub location: Option<Location>,
253 #[serde(default)]
255 pub require_extends: Vec<Arc<str>>,
256 #[serde(default)]
258 pub require_implements: Vec<Arc<str>>,
259}
260
261#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
266pub struct EnumCaseStorage {
267 pub name: Arc<str>,
268 pub value: Option<Union>,
269 pub location: Option<Location>,
270}
271
272#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
273pub struct EnumStorage {
274 pub fqcn: Arc<str>,
275 pub short_name: Arc<str>,
276 pub scalar_type: Option<Union>,
277 pub interfaces: Vec<Arc<str>>,
278 pub cases: IndexMap<Arc<str>, EnumCaseStorage>,
279 pub own_methods: IndexMap<Arc<str>, Arc<MethodStorage>>,
280 pub own_constants: IndexMap<Arc<str>, ConstantStorage>,
281 pub location: Option<Location>,
282}
283
284#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
289pub struct FunctionStorage {
290 pub fqn: Arc<str>,
291 pub short_name: Arc<str>,
292 pub params: Vec<FnParam>,
293 pub return_type: Option<Union>,
294 pub inferred_return_type: Option<Union>,
295 pub template_params: Vec<TemplateParam>,
296 pub assertions: Vec<Assertion>,
297 pub throws: Vec<Arc<str>>,
298 pub deprecated: Option<Arc<str>>,
299 pub is_pure: bool,
300 pub location: Option<Location>,
301}
302
303impl FunctionStorage {
304 pub fn effective_return_type(&self) -> Option<&Union> {
305 self.return_type
306 .as_ref()
307 .or(self.inferred_return_type.as_ref())
308 }
309}
310
311#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
320pub struct StubSlice {
321 pub classes: Vec<ClassStorage>,
322 pub interfaces: Vec<InterfaceStorage>,
323 pub traits: Vec<TraitStorage>,
324 pub enums: Vec<EnumStorage>,
325 pub functions: Vec<FunctionStorage>,
326 #[serde(default)]
327 pub constants: Vec<(Arc<str>, Union)>,
328 #[serde(default)]
331 pub file: Option<Arc<str>>,
332 #[serde(default)]
336 pub global_vars: Vec<(Arc<str>, Union)>,
337 #[serde(default)]
341 pub namespace: Option<Arc<str>>,
342 #[serde(default)]
346 pub imports: std::collections::HashMap<String, String>,
347}