1use crate::kind::{ArrayLen, Qualifiers, TypeKind};
26use crate::types::{TypeId, Types};
27
28pub(crate) fn bare(types: &Types, id: TypeId) -> TypeKind {
30 match types.kind(types.canonical(id)) {
31 TypeKind::Atomic(inner) => types.kind(types.canonical(inner)),
32 other => other,
33 }
34}
35
36#[must_use]
38pub fn is_void(types: &Types, id: TypeId) -> bool {
39 matches!(bare(types, id), TypeKind::Void)
40}
41
42#[must_use]
47pub fn is_integer(types: &Types, id: TypeId) -> bool {
48 matches!(
49 bare(types, id),
50 TypeKind::Bool | TypeKind::Int(_) | TypeKind::BitInt { .. } | TypeKind::Enum(_)
51 )
52}
53
54#[must_use]
56pub fn is_real_floating(types: &Types, id: TypeId) -> bool {
57 matches!(bare(types, id), TypeKind::Float(_))
58}
59
60#[must_use]
62pub fn is_complex(types: &Types, id: TypeId) -> bool {
63 matches!(bare(types, id), TypeKind::Complex(_))
64}
65
66#[must_use]
72pub fn real_part(types: &Types, id: TypeId) -> Option<TypeId> {
73 match bare(types, id) {
74 TypeKind::Complex(part) => Some(part),
75 _ => None,
76 }
77}
78
79#[must_use]
85pub fn is_floating(types: &Types, id: TypeId) -> bool {
86 match bare(types, id) {
87 TypeKind::Float(_) => true,
88 TypeKind::Complex(part) => is_real_floating(types, part),
89 _ => false,
90 }
91}
92
93#[must_use]
99pub fn is_arithmetic(types: &Types, id: TypeId) -> bool {
100 is_integer(types, id) || is_floating(types, id) || is_complex(types, id)
101}
102
103#[must_use]
108pub fn is_real(types: &Types, id: TypeId) -> bool {
109 is_integer(types, id) || is_real_floating(types, id)
110}
111
112#[must_use]
114pub fn is_pointer(types: &Types, id: TypeId) -> bool {
115 matches!(bare(types, id), TypeKind::Pointer(_))
116}
117
118#[must_use]
120pub fn pointee(types: &Types, id: TypeId) -> Option<TypeId> {
121 match bare(types, id) {
122 TypeKind::Pointer(inner) => Some(inner),
123 _ => None,
124 }
125}
126
127#[must_use]
129pub fn is_array(types: &Types, id: TypeId) -> bool {
130 matches!(bare(types, id), TypeKind::Array { .. })
131}
132
133#[must_use]
135pub fn element(types: &Types, id: TypeId) -> Option<TypeId> {
136 match bare(types, id) {
137 TypeKind::Array { elem, .. } | TypeKind::Vector { elem, .. } => Some(elem),
138 _ => None,
139 }
140}
141
142#[must_use]
144pub fn is_function(types: &Types, id: TypeId) -> bool {
145 matches!(bare(types, id), TypeKind::Function(_))
146}
147
148#[must_use]
150pub fn is_record(types: &Types, id: TypeId) -> bool {
151 matches!(bare(types, id), TypeKind::Record(_))
152}
153
154#[must_use]
156pub fn is_vector(types: &Types, id: TypeId) -> bool {
157 matches!(bare(types, id), TypeKind::Vector { .. })
158}
159
160#[must_use]
162pub fn lanes(types: &Types, id: TypeId) -> Option<u32> {
163 match bare(types, id) {
164 TypeKind::Vector { len, .. } => Some(len),
165 _ => None,
166 }
167}
168
169#[must_use]
173pub fn is_atomic(types: &Types, id: TypeId) -> bool {
174 matches!(types.kind(types.canonical(id)), TypeKind::Atomic(_))
175}
176
177#[must_use]
183pub fn is_scalar(types: &Types, id: TypeId) -> bool {
184 is_arithmetic(types, id) || is_pointer(types, id)
185}
186
187#[must_use]
192pub fn is_aggregate(types: &Types, id: TypeId) -> bool {
193 match bare(types, id) {
194 TypeKind::Array { .. } => true,
195 TypeKind::Record(record) => {
196 matches!(types.record_info(record).kind, crate::kind::RecordKind::Struct)
197 }
198 _ => false,
199 }
200}
201
202#[must_use]
207pub fn is_object(types: &Types, id: TypeId) -> bool {
208 !is_function(types, id)
209}
210
211#[must_use]
218pub fn is_complete(types: &Types, id: TypeId) -> bool {
219 match bare(types, id) {
220 TypeKind::Void => false,
221 TypeKind::Array { len: ArrayLen::Unknown, .. } => false,
222 TypeKind::Array { elem, .. } => is_complete(types, elem),
223 TypeKind::Record(record) => types.record_info(record).layout.is_some(),
224 TypeKind::Enum(id) => types.enum_info(id).underlying.is_some(),
225 _ => true,
226 }
227}
228
229#[must_use]
235pub fn is_modifiable(types: &Types, id: TypeId) -> bool {
236 if types.quals(id).has(Qualifiers::CONST) || is_array(types, id) || !is_complete(types, id) {
237 return false;
238 }
239 match bare(types, id) {
240 TypeKind::Record(record) => {
241 types.record_info(record).fields.iter().all(|field| is_modifiable(types, field.ty))
242 }
243 _ => true,
244 }
245}
246
247#[cfg(test)]
248mod tests {
249 use rucc_base::Interner;
250 use rucc_target::{TargetInfo, Triple};
251
252 use super::*;
253 use crate::kind::{ArrayLen, FloatKind, IntKind, RecordKind};
254 use crate::record::{FieldDecl, RecordOptions, layout_record};
255
256 #[test]
257 fn an_enumeration_is_an_integer_type() {
258 let mut types = Types::new();
259 let id = types.declare_enum(None);
260 let int = types.int(IntKind::Int);
261 types.complete_enum(id, int, false);
262 let enumeration = types.enumeration(id);
263
264 assert!(is_integer(&types, enumeration));
266 assert!(is_arithmetic(&types, enumeration));
267 assert!(is_scalar(&types, enumeration));
268 }
269
270 #[test]
271 fn atomic_is_in_whatever_category_it_wraps() {
272 let mut types = Types::new();
273 let int = types.int(IntKind::Int);
274 let atomic = types.atomic(int);
275
276 assert!(is_integer(&types, atomic));
277 assert!(is_scalar(&types, atomic));
278 assert!(is_atomic(&types, atomic));
279 assert!(!is_atomic(&types, int));
280 }
281
282 #[test]
283 fn a_typedef_answers_as_what_it_names() {
284 let mut types = Types::new();
285 let mut names = Interner::new();
286 let int = types.int(IntKind::Int);
287 let name = names.intern("size_t");
288 let alias = types.typedef(name, int);
289
290 assert!(is_integer(&types, alias));
291 assert!(types.is_sugar(alias));
292 }
293
294 #[test]
295 fn a_complex_type_is_arithmetic_and_is_not_real() {
296 let mut types = Types::new();
297 let complex = types.complex_float(FloatKind::Double);
298
299 assert!(is_arithmetic(&types, complex));
300 assert!(is_floating(&types, complex));
301 assert!(!is_real(&types, complex));
303 }
304
305 #[test]
306 fn the_corresponding_real_type_is_the_type_of_both_halves() {
307 let mut types = Types::new();
308 let complex = types.complex_float(FloatKind::Float);
309 let qualified = types.qualified(complex, Qualifiers::CONST);
310
311 assert_eq!(real_part(&types, complex), Some(types.float(FloatKind::Float)));
312 assert_eq!(real_part(&types, qualified), Some(types.float(FloatKind::Float)));
315 assert_eq!(real_part(&types, types.float(FloatKind::Float)), None);
317 assert_eq!(real_part(&types, types.int(IntKind::Int)), None);
318 }
319
320 #[test]
321 fn void_is_an_object_type_and_is_never_complete() {
322 let types = Types::new();
323 let void = types.void();
324
325 assert!(is_object(&types, void));
326 assert!(!is_complete(&types, void));
327 assert!(!is_scalar(&types, void));
328 }
329
330 #[test]
331 fn a_union_is_not_an_aggregate() {
332 let mut types = Types::new();
333 let union = types.declare_record(RecordKind::Union, None);
334 let union = types.record(union);
335 let int = types.int(IntKind::Int);
336 let array = types.array(int, ArrayLen::Fixed(2));
337
338 assert!(!is_aggregate(&types, union));
340 assert!(is_aggregate(&types, array));
341 }
342
343 #[test]
344 fn an_incomplete_record_is_an_object_type_that_cannot_be_made() {
345 let mut types = Types::new();
346 let record = types.declare_record(RecordKind::Struct, None);
347 let id = types.record(record);
348
349 assert!(is_object(&types, id));
350 assert!(!is_complete(&types, id));
351 assert!(!is_modifiable(&types, id));
352 }
353
354 #[test]
355 fn a_const_member_makes_the_whole_structure_unmodifiable() {
356 let mut types = Types::new();
357 let int = types.int(IntKind::Int);
358 let constant = types.qualified(int, Qualifiers::CONST);
359 let record = types.declare_record(RecordKind::Struct, None);
360 let target =
361 TargetInfo::new("x86_64-unknown-linux-gnu".parse::<Triple>().expect("a triple"));
362 let laid_out = layout_record(
363 &types,
364 RecordKind::Struct,
365 &[FieldDecl::new(None, constant)],
366 &RecordOptions::default(),
367 &target,
368 )
369 .expect("a layout");
370 types.complete_record(record, laid_out);
371 let id = types.record(record);
372
373 assert!(!is_modifiable(&types, id));
375 }
376}