1use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
2
3use ecow::EcoString;
4
5use crate::ast::{Annotation, EnumVariant, Generic, Literal, Span, StructFields};
6use crate::types::{FunctionParameter, Type, build_substitution_map, substitute};
7
8#[derive(Debug, Clone)]
9pub struct Definition {
10 pub visibility: Visibility,
11 pub ty: Type,
12 pub name: Option<EcoString>,
13 pub name_span: Option<Span>,
14 pub doc: Option<String>,
15 pub body: DefinitionBody,
16}
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
19#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
20pub enum TypeAttribute {
21 Display,
22 ClosedDomain,
23 AnonStruct,
24 HiddenEmbed,
27 Serialized,
28}
29
30pub type Attributes = HashSet<TypeAttribute>;
31
32#[derive(Debug, Clone)]
33pub enum ValueKind {
34 Runtime,
35 ConstantDeclaration,
36 Constant(Literal),
37}
38
39#[derive(Debug, Clone)]
40pub enum DefinitionBody {
41 TypeAlias {
42 generics: Vec<Generic>,
43 alias: AliasKind,
44 methods: MethodSignatures,
45 attributes: Attributes,
46 },
47 Enum {
48 generics: Vec<Generic>,
49 variants: Vec<EnumVariant>,
50 methods: MethodSignatures,
51 attributes: Attributes,
52 },
53 Struct {
54 generics: Vec<Generic>,
55 fields: StructFields,
56 methods: MethodSignatures,
57 attributes: Attributes,
58 },
59 Interface {
60 definition: Interface,
61 },
62 Value {
63 kind: ValueKind,
64 allowed_lints: Vec<String>,
65 go_hints: Vec<String>,
66 go_name: Option<String>,
67 go_type_param_recipe: Option<String>,
72 },
73}
74
75#[derive(Debug, Clone)]
76pub enum AliasKind {
77 Opaque(Annotation),
78 Transparent {
79 annotation: Annotation,
80 target: Type,
81 },
82}
83
84impl AliasKind {
85 pub fn annotation(&self) -> &Annotation {
86 match self {
87 Self::Opaque(annotation) | Self::Transparent { annotation, .. } => annotation,
88 }
89 }
90}
91
92impl DefinitionBody {
93 pub fn generics(&self) -> Option<&[Generic]> {
94 match self {
95 Self::TypeAlias { generics, .. }
96 | Self::Enum { generics, .. }
97 | Self::Struct { generics, .. } => Some(generics),
98 Self::Interface { definition } => Some(&definition.generics),
99 Self::Value { .. } => None,
100 }
101 }
102
103 pub fn generics_mut(&mut self) -> Option<&mut [Generic]> {
104 match self {
105 Self::TypeAlias { generics, .. }
106 | Self::Enum { generics, .. }
107 | Self::Struct { generics, .. } => Some(generics),
108 Self::Interface { definition } => Some(&mut definition.generics),
109 Self::Value { .. } => None,
110 }
111 }
112}
113
114impl Definition {
115 pub fn is_newtype(&self) -> bool {
120 matches!(
121 &self.body,
122 DefinitionBody::Struct {
123 fields: StructFields::Tuple(fields),
124 generics,
125 ..
126 } if fields.len() == 1 && generics.is_empty()
127 )
128 }
129
130 pub fn is_pointer_backed_newtype<'d, F>(&self, lookup: F) -> bool
131 where
132 F: Fn(&str) -> Option<&'d Definition>,
133 {
134 self.is_newtype()
135 && matches!(
136 &self.body,
137 DefinitionBody::Struct {
138 fields: StructFields::Tuple(fields),
139 ..
140 } if crate::types::peel_alias(&fields[0].ty, lookup).is_ref()
141 )
142 }
143
144 pub fn instantiate_alias_target(&self, params: &[Type]) -> Option<Type> {
145 let DefinitionBody::TypeAlias {
146 generics,
147 alias: AliasKind::Transparent { target, .. },
148 ..
149 } = &self.body
150 else {
151 return None;
152 };
153 Some(substitute(
154 target,
155 &build_substitution_map(generics, params),
156 ))
157 }
158
159 pub fn instantiate_underlying(&self, params: &[Type]) -> Option<Type> {
160 if let Some(target) = self.instantiate_alias_target(params) {
161 return Some(target);
162 }
163 match &self.body {
164 DefinitionBody::Struct {
165 fields: StructFields::Tuple(fields),
166 ..
167 } if self.is_newtype() => Some(fields[0].ty.clone()),
168 _ => None,
169 }
170 }
171
172 pub fn constructor_type(&self) -> Option<Type> {
178 let DefinitionBody::Struct {
179 fields: StructFields::Tuple(fields),
180 generics,
181 ..
182 } = &self.body
183 else {
184 return None;
185 };
186
187 let return_type = self.ty.unwrap_forall().clone();
188 let function = Type::function(
189 fields
190 .iter()
191 .map(|field| FunctionParameter::new(field.ty.clone(), false))
192 .collect(),
193 Default::default(),
194 return_type.into(),
195 );
196
197 Some(if generics.is_empty() {
198 function
199 } else {
200 Type::Forall {
201 vars: generics
202 .iter()
203 .map(|generic| generic.name.clone())
204 .collect(),
205 body: Box::new(function),
206 }
207 })
208 }
209
210 pub fn is_transparent_type_alias(&self) -> bool {
211 matches!(
212 self.body,
213 DefinitionBody::TypeAlias {
214 alias: AliasKind::Transparent { .. },
215 ..
216 }
217 )
218 }
219
220 pub fn allowed_lints(&self) -> &[String] {
221 match &self.body {
222 DefinitionBody::Value { allowed_lints, .. } => allowed_lints,
223 _ => &[],
224 }
225 }
226
227 pub fn go_hints(&self) -> &[String] {
228 match &self.body {
229 DefinitionBody::Value { go_hints, .. } => go_hints,
230 _ => &[],
231 }
232 }
233
234 pub fn go_name(&self) -> Option<&str> {
235 match &self.body {
236 DefinitionBody::Value { go_name, .. } => go_name.as_deref(),
237 _ => None,
238 }
239 }
240
241 pub fn go_type_param_recipe(&self) -> Option<&str> {
242 match &self.body {
243 DefinitionBody::Value {
244 go_type_param_recipe,
245 ..
246 } => go_type_param_recipe.as_deref(),
247 _ => None,
248 }
249 }
250
251 pub fn const_value(&self) -> Option<&Literal> {
252 match &self.body {
253 DefinitionBody::Value {
254 kind: ValueKind::Constant(value),
255 ..
256 } => Some(value),
257 _ => None,
258 }
259 }
260
261 pub fn is_const(&self) -> bool {
262 matches!(
263 self.body,
264 DefinitionBody::Value {
265 kind: ValueKind::ConstantDeclaration | ValueKind::Constant(_),
266 ..
267 }
268 )
269 }
270
271 pub fn methods_mut(&mut self) -> Option<&mut MethodSignatures> {
272 match &mut self.body {
273 DefinitionBody::Struct { methods, .. } => Some(methods),
274 DefinitionBody::TypeAlias { methods, .. } => Some(methods),
275 DefinitionBody::Enum { methods, .. } => Some(methods),
276 _ => None,
277 }
278 }
279
280 fn attributes(&self) -> Option<&Attributes> {
281 match &self.body {
282 DefinitionBody::Struct { attributes, .. }
283 | DefinitionBody::Enum { attributes, .. }
284 | DefinitionBody::TypeAlias { attributes, .. } => Some(attributes),
285 _ => None,
286 }
287 }
288
289 pub fn is_display(&self) -> bool {
290 self.attributes()
291 .is_some_and(|a| a.contains(&TypeAttribute::Display))
292 }
293
294 pub fn is_closed_domain(&self) -> bool {
295 self.attributes()
296 .is_some_and(|a| a.contains(&TypeAttribute::ClosedDomain))
297 }
298
299 pub fn is_serialized(&self) -> bool {
300 self.attributes()
301 .is_some_and(|a| a.contains(&TypeAttribute::Serialized))
302 }
303
304 pub fn is_anon_struct(&self) -> bool {
305 self.attributes()
306 .is_some_and(|a| a.contains(&TypeAttribute::AnonStruct))
307 }
308
309 pub fn has_hidden_embed(&self) -> bool {
310 self.attributes()
311 .is_some_and(|a| a.contains(&TypeAttribute::HiddenEmbed))
312 }
313
314 pub fn is_type_definition(&self) -> bool {
315 matches!(
316 self.body,
317 DefinitionBody::Struct { .. }
318 | DefinitionBody::Enum { .. }
319 | DefinitionBody::TypeAlias { .. }
320 )
321 }
322
323 pub fn is_type_alias(&self) -> bool {
324 matches!(self.body, DefinitionBody::TypeAlias { .. })
325 }
326
327 pub fn is_value(&self, qualified_name: &str) -> bool {
328 matches!(self.body, DefinitionBody::Value { .. })
329 && self.ty.unwrap_forall().get_qualified_id() != Some(qualified_name)
330 }
331}
332
333pub type MethodSignatures = HashMap<EcoString, Type>;
334
335#[derive(Debug, Clone, PartialEq)]
336pub enum Visibility {
337 Public,
338 Private,
339 Local,
340}
341
342impl Visibility {
343 pub fn is_public(&self) -> bool {
344 matches!(self, Visibility::Public)
345 }
346}
347
348#[derive(Debug, Clone, PartialEq)]
349pub struct Interface {
350 pub name: EcoString,
351 pub generics: Vec<Generic>,
352 pub parents: Vec<Type>,
353 pub methods: HashMap<EcoString, Type>,
354}