1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use super::super::types::{IrEnum, IrStruct};
use super::super::{EnumId, ResolvedType, StructId};
use super::IrModule;
impl IrModule {
/// Prelude-defined `Array<T>` struct id. The four built-in compound
/// types live in `src/prelude.fv` as ordinary generic definitions;
/// these accessors hand back the ids the lowering and IR walkers
/// need to identify built-ins without hardcoding name strings.
#[must_use]
pub fn prelude_array_id(&self) -> Option<StructId> {
self.struct_id("Array")
}
/// Prelude-defined `Dictionary<K, V>` struct id.
#[must_use]
pub fn prelude_dictionary_id(&self) -> Option<StructId> {
self.struct_id("Dictionary")
}
/// Prelude-defined `Range<T>` struct id.
#[must_use]
pub fn prelude_range_id(&self) -> Option<StructId> {
self.struct_id("Range")
}
/// Prelude-defined `Optional<T>` enum id.
#[must_use]
pub fn prelude_optional_id(&self) -> Option<EnumId> {
self.enum_id("Optional")
}
/// True iff `id` points at a prelude-defined built-in struct
/// (`Array`, `Dictionary`, `Range`).
#[must_use]
pub fn is_prelude_struct(&self, id: StructId) -> bool {
Some(id) == self.prelude_array_id()
|| Some(id) == self.prelude_dictionary_id()
|| Some(id) == self.prelude_range_id()
}
/// True iff `id` points at the prelude-defined `Optional<T>` enum.
#[must_use]
pub fn is_prelude_enum(&self, id: EnumId) -> bool {
Some(id) == self.prelude_optional_id()
}
/// Iterate over user-defined structs only, skipping the prelude
/// built-ins (`Array`, `Dictionary`, `Range`). Use this when a test
/// wants the user-authored structs without indexing past the
/// prelude's leading slots.
pub fn user_structs(&self) -> impl Iterator<Item = &IrStruct> {
let array = self.prelude_array_id();
let dict = self.prelude_dictionary_id();
let range = self.prelude_range_id();
self.structs.iter().enumerate().filter_map(move |(i, s)| {
#[expect(
clippy::cast_possible_truncation,
reason = "struct count fits in u32 by construction (add_struct guards the cast)"
)]
let id = StructId(i as u32);
if Some(id) == array || Some(id) == dict || Some(id) == range {
None
} else {
Some(s)
}
})
}
/// Iterate over user-defined enums only, skipping the prelude-built-in
/// `Optional<T>` enum.
pub fn user_enums(&self) -> impl Iterator<Item = &IrEnum> {
let optional = self.prelude_optional_id();
self.enums.iter().enumerate().filter_map(move |(i, e)| {
#[expect(
clippy::cast_possible_truncation,
reason = "enum count fits in u32 by construction (add_enum guards the cast)"
)]
let id = EnumId(i as u32);
if Some(id) == optional {
None
} else {
Some(e)
}
})
}
/// If `ty` is `Array<T>` (the prelude-defined struct), return `T`.
/// Built-in compound types share the `Generic` variant with user-
/// defined generics; these helpers let callers introspect by shape
/// without needing prelude IDs themselves.
#[must_use]
pub fn array_element_ty<'a>(&self, ty: &'a ResolvedType) -> Option<&'a ResolvedType> {
let arr = self.prelude_array_id()?;
if let ResolvedType::Generic {
base: crate::ir::GenericBase::Struct(id),
args,
} = ty
{
if *id == arr && args.len() == 1 {
return args.first();
}
}
None
}
/// If `ty` is `Dictionary<K, V>`, return `(K, V)`.
#[must_use]
pub fn dictionary_kv_ty<'a>(
&self,
ty: &'a ResolvedType,
) -> Option<(&'a ResolvedType, &'a ResolvedType)> {
let did = self.prelude_dictionary_id()?;
if let ResolvedType::Generic {
base: crate::ir::GenericBase::Struct(id),
args,
} = ty
{
if *id == did {
if let [k, v] = args.as_slice() {
return Some((k, v));
}
}
}
None
}
/// If `ty` is `Range<T>`, return `T`.
#[must_use]
pub fn range_element_ty<'a>(&self, ty: &'a ResolvedType) -> Option<&'a ResolvedType> {
let rid = self.prelude_range_id()?;
if let ResolvedType::Generic {
base: crate::ir::GenericBase::Struct(id),
args,
} = ty
{
if *id == rid && args.len() == 1 {
return args.first();
}
}
None
}
/// If `ty` is `Optional<T>`, return `T`.
#[must_use]
pub fn optional_inner_ty<'a>(&self, ty: &'a ResolvedType) -> Option<&'a ResolvedType> {
let opt = self.prelude_optional_id()?;
if let ResolvedType::Generic {
base: crate::ir::GenericBase::Enum(id),
args,
} = ty
{
if *id == opt && args.len() == 1 {
return args.first();
}
}
None
}
}