1#![doc(html_root_url = "https://docs.rs/rucc-types/0.10.73")]
82
83mod classify;
84mod compat;
85mod convert;
86mod granule;
87mod kind;
88mod layout;
89mod print;
90mod record;
91mod types;
92
93pub use crate::classify::{
94 element, is_aggregate, is_arithmetic, is_array, is_atomic, is_complete, is_complex,
95 is_floating, is_function, is_integer, is_modifiable, is_object, is_pointer, is_real,
96 is_real_floating, is_record, is_scalar, is_vector, is_void, lanes, pointee, pointee_as_written,
97 real_part,
98};
99pub use crate::compat::{adjust_parameter, compatible, composite};
100pub use crate::convert::{
101 mask_of, promote, promote_bit_field, usual_arithmetic, vectors_convertible,
102};
103pub use crate::granule::{GRANULE, Keying, Tally, measure, measure_all, report as granule_report};
104pub use crate::kind::{
105 ArrayLen, EnumId, FloatKind, FunctionId, FunctionType, IntKind, Qualifiers, RecordId,
106 RecordKind, Type, TypeKind, VlaId,
107};
108pub use crate::layout::{
109 IntegerInfo, Layout, LayoutError, align, float_format, float_width, int_width, integer_info,
110 layout,
111};
112pub use crate::print::{declare, spell};
113pub use crate::record::{
114 Extent, Field, FieldDecl, RecordError, RecordLayout, RecordOptions, VariableLayout,
115 layout_record,
116};
117pub use crate::types::{EnumInfo, RecordInfo, TypeId, Types};
118
119pub const MILESTONE: &str = "M2";
121
122#[cfg(test)]
123mod tests {
124 use std::num::NonZeroU32;
125
126 use rucc_base::{Interner, Symbol};
127 use rucc_target::{TargetInfo, Triple};
128
129 use super::*;
130
131 fn target(triple: &str) -> TargetInfo {
132 TargetInfo::new(triple.parse::<Triple>().expect("a triple the compiler supports"))
133 }
134
135 fn linux() -> TargetInfo {
136 target("x86_64-unknown-linux-gnu")
137 }
138
139 fn windows() -> TargetInfo {
143 target("x86_64-pc-windows-msvc")
144 }
145
146 fn aapcs() -> TargetInfo {
150 target("aarch64-unknown-linux-gnu")
151 }
152
153 fn lay_out(types: &Types, kind: RecordKind, fields: &[FieldDecl]) -> RecordLayout {
155 lay_out_on(&linux(), types, kind, fields)
156 }
157
158 fn lay_out_on(
160 target: &TargetInfo,
161 types: &Types,
162 kind: RecordKind,
163 fields: &[FieldDecl],
164 ) -> RecordLayout {
165 layout_record(types, kind, fields, &RecordOptions::default(), target)
166 .expect("a record every member of which has a layout")
167 }
168
169 fn offsets(laid_out: &RecordLayout) -> Vec<u128> {
172 laid_out.fields.iter().map(Field::bit_offset).collect()
173 }
174
175 fn record(types: &mut Types, kind: RecordKind, fields: &[FieldDecl]) -> TypeId {
177 let id = types.declare_record(kind, None);
178 let laid_out = lay_out(types, kind, fields);
179 types.complete_record(id, laid_out);
180 types.record(id)
181 }
182
183 fn member(ty: TypeId) -> FieldDecl {
185 FieldDecl::new(None, ty)
186 }
187
188 fn bits(interner: &mut Interner, name: &str, ty: TypeId, width: u32) -> FieldDecl {
191 FieldDecl::bit_field(Some(interner.intern(name)), ty, width)
192 }
193
194 fn unnamed_bits(ty: TypeId, width: u32) -> FieldDecl {
196 FieldDecl::bit_field(None, ty, width)
197 }
198
199 #[test]
200 fn milestone_is_recorded() {
201 assert!(MILESTONE.starts_with('M'));
202 }
203
204 #[test]
205 fn an_integer_type_answers_with_the_width_of_its_value_and_not_of_its_object() {
206 let mut interner = Interner::new();
207 let mut types = Types::new();
208 let target = linux();
209
210 let boolean = types.boolean();
213 let bits = types.bit_int(true, 37);
214 let short = types.int(IntKind::Short);
216 let alias = types.typedef(interner.intern("word"), short);
217 let unsigned_char = types.int(IntKind::UChar);
218 let atomic = types.atomic(unsigned_char);
219
220 let shape = |ty| integer_info(&types, ty, &target).expect("an integer type");
221 assert_eq!(shape(boolean), IntegerInfo::new(false, 1));
222 assert_eq!(shape(bits), IntegerInfo::new(true, 37));
223 assert_eq!(shape(types.int(IntKind::Int)), IntegerInfo::new(true, 32));
224 assert_eq!(shape(types.int(IntKind::ULong)), IntegerInfo::new(false, 64));
225 assert_eq!(shape(alias), IntegerInfo::new(true, 16));
226 assert_eq!(shape(atomic), IntegerInfo::new(false, 8));
227
228 assert_eq!(integer_info(&types, types.float(FloatKind::Double), &target), None);
229 }
230
231 #[test]
232 fn an_enumeration_answers_with_the_type_the_enumerators_are_kept_in() {
233 let mut interner = Interner::new();
234 let mut types = Types::new();
235 let target = linux();
236
237 let colour = types.declare_enum(Some(interner.intern("colour")));
240 let ty = types.enumeration(colour);
241 assert_eq!(integer_info(&types, ty, &target), None);
242
243 let underlying = types.int(IntKind::ULong);
244 types.complete_enum(colour, underlying, true);
245 assert_eq!(integer_info(&types, ty, &target), Some(IntegerInfo::new(false, 64)));
246 }
247
248 #[test]
249 fn a_value_stored_in_an_integer_type_keeps_the_bits_the_type_has_room_for() {
250 let char_type = IntegerInfo::new(true, 8);
251 assert_eq!(char_type.wrap(300), 44);
252 assert!(!char_type.holds(300));
253 assert!(char_type.holds(-128));
254
255 assert_eq!(IntegerInfo::new(false, 32).wrap(-1), 4_294_967_295);
256 assert_eq!(IntegerInfo::new(false, 8).wrap(-1), 255);
257
258 assert!(IntegerInfo::new(false, 128).holds(i128::MIN));
261 assert!(IntegerInfo::new(true, 128).holds(i128::MIN));
262 assert_eq!(IntegerInfo::new(true, 128).wrap(i128::MAX), i128::MAX);
263 }
264
265 #[test]
266 fn a_long_double_has_a_format_the_size_does_not_give_away() {
267 let target = linux();
268 assert_eq!(float_width(FloatKind::LongDouble, &target), 128);
271 assert_eq!(
272 float_format(FloatKind::LongDouble, &target),
273 rucc_base::float::Format::X87Extended
274 );
275 assert_eq!(float_format(FloatKind::Float, &target), rucc_base::float::Format::Single);
276 }
277
278 #[test]
279 fn an_interchange_type_names_a_format_and_an_extended_one_names_the_target() {
280 use rucc_base::float::Format;
281
282 for target in [&linux(), &target("aarch64-apple-darwin")] {
285 assert_eq!(float_format(FloatKind::Float16, target), Format::Half);
286 assert_eq!(float_format(FloatKind::Float32, target), Format::Single);
287 assert_eq!(float_format(FloatKind::Float64, target), Format::Double);
288 assert_eq!(float_format(FloatKind::Float128, target), Format::Quad);
289 assert_eq!(float_width(FloatKind::Float16, target), 16);
290 assert_eq!(float_width(FloatKind::Float32, target), 32);
291 assert_eq!(float_width(FloatKind::Float64, target), 64);
292 assert_eq!(float_width(FloatKind::Float128, target), 128);
293 assert_eq!(float_format(FloatKind::Float32x, target), Format::Double);
295 }
296
297 let x86 = linux();
301 assert_eq!(float_format(FloatKind::Float64x, &x86), Format::X87Extended);
302 assert_eq!(float_format(FloatKind::LongDouble, &x86), Format::X87Extended);
303 let mac = target("aarch64-apple-darwin");
304 assert_eq!(float_format(FloatKind::Float64x, &mac), Format::Quad);
305 assert_eq!(float_format(FloatKind::LongDouble, &mac), Format::Double);
306 assert_eq!(float_width(FloatKind::Float64x, &x86), 128);
309 assert_eq!(float_width(FloatKind::Float64x, &mac), 128);
310 }
311
312 #[test]
313 fn every_floating_type_is_as_wide_as_the_format_it_is_stored_in() {
314 let types = Types::new();
315 let sizes = |target: &TargetInfo| -> Vec<(u64, u64)> {
316 FloatKind::ALL
317 .iter()
318 .map(|&kind| {
319 let found = layout(&types, types.float(kind), target).expect("a complete type");
320 (found.size, found.align)
321 })
322 .collect()
323 };
324 assert_eq!(
328 sizes(&linux()),
329 [(2, 2), (4, 4), (4, 4), (8, 8), (8, 8), (8, 8), (16, 16), (16, 16), (16, 16)]
330 );
331 assert_eq!(
332 sizes(&target("aarch64-apple-darwin")),
333 [(2, 2), (4, 4), (4, 4), (8, 8), (8, 8), (8, 8), (8, 8), (16, 16), (16, 16)]
334 );
335 }
336
337 #[test]
338 fn every_floating_type_has_a_slot_of_its_own_and_a_name_of_its_own() {
339 let types = Types::new();
342 let mut seen = Vec::new();
343 for kind in FloatKind::ALL {
344 seen.push(types.float(kind));
345 }
346 let mut sorted = seen.clone();
347 sorted.sort_unstable();
348 sorted.dedup();
349 assert_eq!(sorted.len(), seen.len(), "two floating types share an id");
350
351 let names: Vec<&str> = FloatKind::ALL.iter().map(|kind| kind.as_str()).collect();
352 assert_eq!(
353 names,
354 [
355 "_Float16",
356 "float",
357 "_Float32",
358 "double",
359 "_Float32x",
360 "_Float64",
361 "long double",
362 "_Float64x",
363 "_Float128",
364 ]
365 );
366 }
367
368 #[test]
369 fn the_same_type_asked_for_twice_is_the_same_id() {
370 let mut types = Types::new();
371 let a = types.pointer(types.int(IntKind::Int));
372 let b = types.pointer(types.int(IntKind::Int));
373 assert_eq!(a, b, "interning is what makes type identity an integer comparison");
374 let c = types.pointer(types.int(IntKind::Long));
375 assert_ne!(a, c);
376 }
377
378 #[test]
379 fn a_qualifier_makes_a_different_type_with_the_same_shape() {
380 let mut types = Types::new();
381 let int = types.int(IntKind::Int);
382 let konst = types.qualified(int, Qualifiers::CONST);
383 assert_ne!(int, konst);
384 assert_eq!(types.kind(konst), types.kind(int));
385 assert!(types.quals(konst).has(Qualifiers::CONST));
386 assert_eq!(types.unqualified(konst), int);
387 }
388
389 #[test]
390 fn qualifiers_accumulate_and_do_not_depend_on_the_order_they_were_written() {
391 let mut types = Types::new();
392 let int = types.int(IntKind::Int);
393 let a = types.qualified(int, Qualifiers::CONST);
394 let a = types.qualified(a, Qualifiers::VOLATILE);
395 let b = types.qualified(int, Qualifiers::VOLATILE);
396 let b = types.qualified(b, Qualifiers::CONST);
397 assert_eq!(a, b, "`const volatile int` and `volatile const int` are one type");
398 }
399
400 #[test]
401 fn qualifying_an_array_qualifies_its_element() {
402 let mut types = Types::new();
405 let int = types.int(IntKind::Int);
406 let array = types.array(int, ArrayLen::Fixed(4));
407 let konst = types.qualified(array, Qualifiers::CONST);
408 assert!(types.quals(konst).is_none(), "the array itself is unqualified");
409 let TypeKind::Array { elem, len } = types.kind(konst) else {
410 panic!("still an array");
411 };
412 assert_eq!(len, ArrayLen::Fixed(4));
413 assert!(types.quals(elem).has(Qualifiers::CONST));
414 }
415
416 #[test]
417 fn a_typedef_is_a_different_type_that_means_the_same_thing() {
418 let mut interner = Interner::new();
419 let mut types = Types::new();
420 let int = types.int(IntKind::Int);
421 let name = types.typedef(interner.intern("int32_t"), int);
422 assert_ne!(name, int, "the sugar survives, so a diagnostic can print it");
423 assert_eq!(types.canonical(name), int, "and no rule ever sees it");
424 assert!(types.is_sugar(name));
425 assert!(!types.is_sugar(int));
426 }
427
428 #[test]
429 fn sugar_below_the_outermost_node_is_resolved_too() {
430 let mut interner = Interner::new();
433 let mut types = Types::new();
434 let int = types.int(IntKind::Int);
435 let name = types.typedef(interner.intern("int32_t"), int);
436 let sugar_pointer = types.pointer(name);
437 let plain_pointer = types.pointer(int);
438 assert_ne!(sugar_pointer, plain_pointer);
439 assert_eq!(types.canonical(sugar_pointer), plain_pointer);
440
441 let sugar_array = types.array(name, ArrayLen::Fixed(3));
442 let plain_array = types.array(int, ArrayLen::Fixed(3));
443 assert_eq!(types.canonical(sugar_array), plain_array);
444 }
445
446 #[test]
447 fn a_typedef_of_a_typedef_canonicalises_all_the_way_down() {
448 let mut interner = Interner::new();
449 let mut types = Types::new();
450 let int = types.int(IntKind::Int);
451 let mut current = int;
452 for i in 0..8 {
453 current = types.typedef(interner.intern(&format!("t{i}")), current);
454 }
455 assert_eq!(types.canonical(current), int);
456 }
457
458 #[test]
459 fn a_typedef_that_asked_for_an_alignment_says_what_it_is_and_not_what_it_is_at_least() {
460 let mut interner = Interner::new();
465 let mut types = Types::new();
466 let target = linux();
467 let int = types.int(IntKind::Int);
468 let low = types.aligned_typedef(interner.intern("L"), int, NonZeroU32::new(2).unwrap());
469 let high = types.aligned_typedef(interner.intern("H"), int, NonZeroU32::new(16).unwrap());
470
471 assert_eq!(layout(&types, low, &target), Ok(Layout::new(4, 2)));
472 assert_eq!(layout(&types, high, &target), Ok(Layout::new(4, 16)));
473 assert_eq!(layout(&types, int, &target), Ok(Layout::new(4, 4)));
476
477 assert_ne!(low, high);
480
481 let outer = types.aligned_typedef(interner.intern("M"), low, NonZeroU32::new(8).unwrap());
484 assert_eq!(types.align_override(outer), NonZeroU32::new(8));
485 let plain = types.typedef(interner.intern("N"), low);
486 assert_eq!(types.align_override(plain), NonZeroU32::new(2));
487 assert_eq!(types.align_override(int), None);
489 }
490
491 #[test]
492 fn a_qualified_typedef_keeps_the_name_and_canonicalises_to_the_qualified_type() {
493 let mut interner = Interner::new();
494 let mut types = Types::new();
495 let int = types.int(IntKind::Int);
496 let name = types.typedef(interner.intern("int32_t"), int);
497 let konst = types.qualified(name, Qualifiers::CONST);
498 assert!(matches!(types.kind(konst), TypeKind::Typedef { .. }), "still prints as int32_t");
499 let want = types.qualified(int, Qualifiers::CONST);
500 assert_eq!(types.canonical(konst), want);
501 }
502
503 #[test]
504 fn a_typedef_of_an_array_pushes_a_qualifier_to_the_element_when_it_canonicalises() {
505 let mut interner = Interner::new();
508 let mut types = Types::new();
509 let int = types.int(IntKind::Int);
510 let array = types.array(int, ArrayLen::Fixed(4));
511 let name = types.typedef(interner.intern("A"), array);
512 let konst = types.qualified(name, Qualifiers::CONST);
513 let konst_int = types.qualified(int, Qualifiers::CONST);
514 let want = types.array(konst_int, ArrayLen::Fixed(4));
515 assert_eq!(types.canonical(konst), want);
516 }
517
518 #[test]
519 fn a_function_type_is_deduplicated_by_its_signature() {
520 let mut types = Types::new();
521 let int = types.int(IntKind::Int);
522 let long = types.int(IntKind::Long);
523 let make = |types: &mut Types, params: Vec<TypeId>, variadic| {
524 types.function(FunctionType { ret: int, params, variadic, prototyped: true })
525 };
526 let a = make(&mut types, vec![int, long], false);
527 let b = make(&mut types, vec![int, long], false);
528 assert_eq!(a, b);
529 assert_ne!(a, make(&mut types, vec![int, long], true), "`...` is part of the type");
530 assert_ne!(a, make(&mut types, vec![long, int], false));
531 }
532
533 #[test]
534 fn a_function_type_written_with_a_typedef_canonicalises_through_its_signature() {
535 let mut interner = Interner::new();
536 let mut types = Types::new();
537 let int = types.int(IntKind::Int);
538 let name = types.typedef(interner.intern("int32_t"), int);
539 let sugar = types.function(FunctionType {
540 ret: name,
541 params: vec![name],
542 variadic: false,
543 prototyped: true,
544 });
545 let plain = types.function(FunctionType {
546 ret: int,
547 params: vec![int],
548 variadic: false,
549 prototyped: true,
550 });
551 assert_ne!(sugar, plain);
552 assert_eq!(types.canonical(sugar), plain);
553 }
554
555 #[test]
556 fn a_record_is_its_declaration_and_not_its_members() {
557 let mut interner = Interner::new();
561 let mut types = Types::new();
562 let tag = interner.intern("point");
563 let first = types.declare_record(RecordKind::Struct, Some(tag));
564 let second = types.declare_record(RecordKind::Struct, Some(tag));
565 assert_ne!(types.record(first), types.record(second));
566 assert_eq!(types.record(first), types.record(first));
567 }
568
569 #[test]
570 fn a_record_has_no_layout_until_it_has_been_completed() {
571 let mut types = Types::new();
572 let id = types.declare_record(RecordKind::Struct, None);
573 let ty = types.record(id);
574 assert_eq!(layout(&types, ty, &linux()), Err(LayoutError::Incomplete));
575 let long_long = types.int(IntKind::LongLong);
576 let laid_out = lay_out(&types, RecordKind::Struct, &[member(long_long); 2]);
577 types.complete_record(id, laid_out);
578 assert_eq!(layout(&types, ty, &linux()).unwrap(), Layout::new(16, 8));
579 }
580
581 #[test]
582 fn an_enum_takes_the_layout_of_its_underlying_type() {
583 let mut types = Types::new();
584 let id = types.declare_enum(None);
585 let ty = types.enumeration(id);
586 assert_eq!(layout(&types, ty, &linux()), Err(LayoutError::Incomplete));
587 let int = types.int(IntKind::Int);
588 types.complete_enum(id, int, false);
589 assert_eq!(layout(&types, ty, &linux()).unwrap(), Layout::new(4, 4));
590 }
591
592 #[test]
593 fn the_scalar_widths_come_from_the_target() {
594 let mut types = Types::new();
595 let linux = linux();
596 let windows = target("x86_64-pc-windows-msvc");
597 let darwin = target("aarch64-apple-darwin");
598
599 let long = types.int(IntKind::Long);
600 assert_eq!(layout(&types, long, &linux).unwrap(), Layout::new(8, 8));
601 assert_eq!(layout(&types, long, &windows).unwrap(), Layout::new(4, 4), "LLP64");
602
603 let ldouble = types.float(FloatKind::LongDouble);
604 assert_eq!(layout(&types, ldouble, &linux).unwrap(), Layout::new(16, 16));
605 assert_eq!(layout(&types, ldouble, &darwin).unwrap(), Layout::new(8, 8));
606
607 let pointer = types.pointer(types.void());
608 assert_eq!(layout(&types, pointer, &linux).unwrap(), Layout::new(8, 8));
609
610 let boolean = types.boolean();
611 assert_eq!(layout(&types, boolean, &linux).unwrap(), Layout::new(1, 1));
612 }
613
614 #[test]
615 fn a_complex_type_is_two_of_its_component_with_the_components_alignment() {
616 let mut types = Types::new();
619 let linux = linux();
620 let cfloat = types.complex_float(FloatKind::Float);
621 assert_eq!(layout(&types, cfloat, &linux).unwrap(), Layout::new(8, 4));
622 let cdouble = types.complex_float(FloatKind::Double);
623 assert_eq!(layout(&types, cdouble, &linux).unwrap(), Layout::new(16, 8));
624 let cldouble = types.complex_float(FloatKind::LongDouble);
625 assert_eq!(layout(&types, cldouble, &linux).unwrap(), Layout::new(32, 16));
626 let darwin = target("aarch64-apple-darwin");
627 assert_eq!(layout(&types, cldouble, &darwin).unwrap(), Layout::new(16, 8));
628 }
629
630 #[test]
631 fn an_atomic_type_can_be_more_aligned_than_the_type_it_wraps() {
632 let mut types = Types::new();
635 let linux = linux();
636 let long_long = types.int(IntKind::LongLong);
637 let plain = record(&mut types, RecordKind::Struct, &[member(long_long); 2]);
638 let atomic = types.atomic(plain);
639 assert_eq!(layout(&types, plain, &linux).unwrap(), Layout::new(16, 8));
640 assert_eq!(layout(&types, atomic, &linux).unwrap(), Layout::new(16, 16));
641
642 let odd = record(&mut types, RecordKind::Struct, &[member(long_long); 3]);
644 let atomic_odd = types.atomic(odd);
645 assert_eq!(layout(&types, atomic_odd, &linux).unwrap(), Layout::new(24, 8));
646
647 let int = types.int(IntKind::Int);
648 let atomic_int = types.atomic(int);
649 assert_eq!(layout(&types, atomic_int, &linux).unwrap(), Layout::new(4, 4));
650 }
651
652 #[test]
653 fn a_bit_int_is_laid_out_like_a_standard_integer_until_it_outgrows_one() {
654 let mut types = Types::new();
657 let linux = linux();
658 let darwin = target("aarch64-apple-darwin");
659 let cases = [(7, 1, 1), (8, 1, 1), (9, 2, 2), (17, 4, 4), (33, 8, 8), (64, 8, 8)];
660 for (width, size, align) in cases {
661 let ty = types.bit_int(true, width);
662 assert_eq!(layout(&types, ty, &linux).unwrap(), Layout::new(size, align), "{width}");
663 assert_eq!(layout(&types, ty, &darwin).unwrap(), Layout::new(size, align), "{width}");
664 }
665 for width in [65, 96, 128] {
666 let ty = types.bit_int(false, width);
667 assert_eq!(layout(&types, ty, &linux).unwrap(), Layout::new(16, 8), "{width}");
668 assert_eq!(layout(&types, ty, &darwin).unwrap(), Layout::new(16, 16), "{width}");
669 }
670 let wide = types.bit_int(true, 129);
671 assert_eq!(layout(&types, wide, &linux).unwrap(), Layout::new(24, 8));
672 assert_eq!(layout(&types, wide, &darwin).unwrap(), Layout::new(32, 16));
673 }
674
675 #[test]
676 fn an_array_is_its_element_repeated_and_keeps_its_elements_alignment() {
677 let mut types = Types::new();
678 let linux = linux();
679 let int = types.int(IntKind::Int);
680 let ty = types.array(int, ArrayLen::Fixed(10));
681 assert_eq!(layout(&types, ty, &linux).unwrap(), Layout::new(40, 4));
682 let nested = types.array(ty, ArrayLen::Fixed(3));
683 assert_eq!(layout(&types, nested, &linux).unwrap(), Layout::new(120, 4));
684 }
685
686 #[test]
687 fn an_array_without_a_size_is_incomplete_and_an_impossible_one_says_so() {
688 let mut types = Types::new();
689 let linux = linux();
690 let int = types.int(IntKind::Int);
691 for len in [ArrayLen::Unknown, ArrayLen::Star] {
692 let ty = types.array(int, len);
693 assert_eq!(layout(&types, ty, &linux), Err(LayoutError::Incomplete));
694 }
695 let measured = types.array(int, ArrayLen::Variable(VlaId(0)));
700 assert_eq!(layout(&types, measured, &linux), Err(LayoutError::Variable));
701 assert_eq!(align(&types, measured, &linux), Ok(4));
702 let huge = types.array(int, ArrayLen::Fixed(u64::MAX));
703 assert_eq!(layout(&types, huge, &linux), Err(LayoutError::TooLarge));
704 }
705
706 #[test]
707 fn the_largest_array_is_the_largest_object_and_not_the_largest_number() {
708 let mut types = Types::new();
712 let linux = linux();
713 let max = linux.max_object_size();
714 let ch = types.int(IntKind::Char);
715 let fits = types.array(ch, ArrayLen::Fixed(max));
716 assert_eq!(layout(&types, fits, &linux), Ok(Layout::new(max, 1)));
717 let over = types.array(ch, ArrayLen::Fixed(max + 1));
718 assert_eq!(layout(&types, over, &linux), Err(LayoutError::TooLarge));
719 }
720
721 #[test]
722 fn a_record_may_be_as_large_as_an_object_may_be_and_no_larger() {
723 let mut types = Types::new();
728 let linux = linux();
729 let max = linux.max_object_size();
730 let ch = types.int(IntKind::Char);
731 let int = types.int(IntKind::Int);
732 let short = types.int(IntKind::Short);
733
734 let huge = types.array(short, ArrayLen::Fixed((1 << 62) - 256));
735 let members = [member(huge), member(int), member(int), member(int), member(int)];
736 let laid_out = lay_out(&types, RecordKind::Struct, &members);
737 assert_eq!(laid_out.layout, Layout::new((1 << 63) - 496, 4));
738
739 let brim = types.array(ch, ArrayLen::Fixed(max));
740 let laid_out = lay_out(&types, RecordKind::Struct, &[member(brim)]);
741 assert_eq!(laid_out.layout, Layout::new(max, 1));
742
743 let over = [member(brim), member(ch)];
744 let options = RecordOptions::default();
745 let error = layout_record(&types, RecordKind::Struct, &over, &options, &linux);
746 assert_eq!(error, Err(RecordError::TooLarge));
747 }
748
749 #[test]
750 fn a_bit_field_past_where_a_bit_count_fits_is_still_placed() {
751 let mut types = Types::new();
756 let linux = linux();
757 let ch = types.int(IntKind::Char);
758 let int = types.int(IntKind::Int);
759 let mut interner = Interner::new();
760 let buf = types.array(ch, ArrayLen::Fixed(linux.max_object_size() - 7));
761 let members = [member(buf), bits(&mut interner, "x", int, 1)];
762 let laid_out = lay_out(&types, RecordKind::Struct, &members);
763 assert_eq!(laid_out.layout, Layout::new(9_223_372_036_854_775_804, 4));
764 let last = laid_out.fields[1];
765 assert_eq!((last.offset, last.bit), (9_223_372_036_854_775_800, 0));
766 assert_eq!(last.bit_offset(), 73_786_976_294_838_206_400);
767 }
768
769 #[test]
770 fn two_variable_length_arrays_of_the_same_element_are_still_different_types() {
771 let mut types = Types::new();
772 let int = types.int(IntKind::Int);
773 let a = types.array(int, ArrayLen::Variable(VlaId(0)));
774 let b = types.array(int, ArrayLen::Variable(VlaId(1)));
775 assert_ne!(a, b);
776 }
777
778 #[test]
779 fn a_vector_is_rounded_up_to_a_power_of_two_and_aligned_to_the_whole_thing() {
780 let mut types = Types::new();
783 let linux = linux();
784 let int = types.int(IntKind::Int);
785 let four = types.vector(int, 4);
786 assert_eq!(layout(&types, four, &linux).unwrap(), Layout::new(16, 16));
787 let three = types.vector(int, 3);
788 assert_eq!(layout(&types, three, &linux).unwrap(), Layout::new(16, 16));
789 let three_chars = types.vector(types.int(IntKind::Char), 3);
790 assert_eq!(layout(&types, three_chars, &linux).unwrap(), Layout::new(4, 4));
791 }
792
793 #[test]
794 fn the_types_without_a_size_say_which_kind_of_without_they_are() {
795 let mut types = Types::new();
798 let linux = linux();
799 let void = types.void();
800 assert_eq!(layout(&types, void, &linux), Err(LayoutError::Incomplete));
801 let int = types.int(IntKind::Int);
802 let function = types.function(FunctionType {
803 ret: int,
804 params: Vec::new(),
805 variadic: false,
806 prototyped: true,
807 });
808 assert_eq!(layout(&types, function, &linux), Err(LayoutError::Function));
809 let pointer_to_function = types.pointer(function);
810 assert_eq!(layout(&types, pointer_to_function, &linux).unwrap(), Layout::new(8, 8));
811 }
812
813 #[test]
814 fn a_struct_puts_each_member_at_the_next_offset_it_is_allowed_to_start_at() {
815 let types = Types::new();
816 let char_ = types.int(IntKind::Char);
817 let int = types.int(IntKind::Int);
818 let laid_out = lay_out(&types, RecordKind::Struct, &[member(char_), member(int)]);
819 assert_eq!(laid_out.layout, Layout::new(8, 4));
820 assert_eq!(offsets(&laid_out), [0, 32]);
821 assert_eq!(laid_out.fields[1].offset, 4);
822
823 let long_long = types.int(IntKind::LongLong);
825 let laid_out = lay_out(&types, RecordKind::Struct, &[member(long_long), member(char_)]);
826 assert_eq!(laid_out.layout, Layout::new(16, 8));
827 }
828
829 #[test]
830 fn a_union_starts_every_member_at_zero_and_is_as_large_as_the_largest() {
831 let mut types = Types::new();
832 let char_ = types.int(IntKind::Char);
833 let int = types.int(IntKind::Int);
834 let laid_out = lay_out(&types, RecordKind::Union, &[member(char_), member(int)]);
835 assert_eq!(laid_out.layout, Layout::new(4, 4));
836 assert_eq!(offsets(&laid_out), [0, 0]);
837
838 let nine = types.array(char_, ArrayLen::Fixed(9));
841 let short = types.int(IntKind::Short);
842 let laid_out = lay_out(&types, RecordKind::Union, &[member(nine), member(short)]);
843 assert_eq!(laid_out.layout, Layout::new(10, 2));
844 }
845
846 #[test]
847 fn bit_fields_share_a_unit_until_one_of_them_would_span_two() {
848 let mut interner = Interner::new();
851 let types = Types::new();
852 let char_ = types.int(IntKind::Char);
853 let int = types.int(IntKind::Int);
854 let long_long = types.int(IntKind::LongLong);
855
856 let fields = [bits(&mut interner, "a", int, 3), bits(&mut interner, "b", int, 5)];
857 let laid_out = lay_out(&types, RecordKind::Struct, &fields);
858 assert_eq!(laid_out.layout, Layout::new(4, 4));
859 assert_eq!(offsets(&laid_out), [0, 3]);
860
861 let fields = [member(char_), bits(&mut interner, "b", int, 30)];
863 let laid_out = lay_out(&types, RecordKind::Struct, &fields);
864 assert_eq!(laid_out.layout, Layout::new(8, 4));
865 assert_eq!(offsets(&laid_out), [0, 32]);
866
867 let fields = [member(char_), bits(&mut interner, "b", long_long, 33)];
870 let laid_out = lay_out(&types, RecordKind::Struct, &fields);
871 assert_eq!(laid_out.layout, Layout::new(8, 8));
872 assert_eq!(offsets(&laid_out), [0, 8]);
873
874 let fields = [bits(&mut interner, "a", int, 3), member(char_)];
876 let laid_out = lay_out(&types, RecordKind::Struct, &fields);
877 assert_eq!(offsets(&laid_out), [0, 8]);
878 }
879
880 #[test]
881 fn a_zero_width_bit_field_moves_the_next_member_on_and_nothing_else() {
882 let types = Types::new();
883 let char_ = types.int(IntKind::Char);
884 let int = types.int(IntKind::Int);
885 let fields = [member(char_), unnamed_bits(int, 0), member(char_)];
886 let laid_out = lay_out(&types, RecordKind::Struct, &fields);
887 assert_eq!(laid_out.layout, Layout::new(5, 1));
890 assert_eq!(offsets(&laid_out), [0, 32, 32]);
891 assert_eq!(laid_out.fields.len(), 3, "one field per declaration, so indices line up");
892
893 let trailing = [member(char_), unnamed_bits(int, 0)];
897 assert_eq!(lay_out(&types, RecordKind::Struct, &trailing).layout, Layout::new(4, 1));
898
899 let only = [unnamed_bits(int, 0)];
902 assert_eq!(lay_out(&types, RecordKind::Struct, &only).layout, Layout::new(0, 1));
903 }
904
905 #[test]
906 fn an_unnamed_bit_field_does_not_raise_the_records_alignment_but_a_named_one_does() {
907 let mut interner = Interner::new();
908 let types = Types::new();
909 let char_ = types.int(IntKind::Char);
910 let int = types.int(IntKind::Int);
911
912 let unnamed = [member(char_), unnamed_bits(int, 20)];
913 let unnamed = lay_out(&types, RecordKind::Struct, &unnamed);
914 assert_eq!(unnamed.layout, Layout::new(4, 1));
915
916 let named = [member(char_), bits(&mut interner, "b", int, 20)];
917 let named = lay_out(&types, RecordKind::Struct, &named);
918 assert_eq!(named.layout, Layout::new(4, 4));
919 assert_eq!(offsets(&named), [0, 8], "the same place either way");
920
921 let wider = [member(char_), unnamed_bits(int, 30)];
924 let wider = lay_out(&types, RecordKind::Struct, &wider);
925 assert_eq!(wider.layout, Layout::new(8, 1));
926 assert_eq!(offsets(&wider), [0, 32]);
927 }
928
929 #[test]
930 fn aapcs64_lets_an_unnamed_bit_field_raise_the_records_alignment() {
931 let types = Types::new();
932 let char_ = types.int(IntKind::Char);
933 let uint = types.int(IntKind::UInt);
934
935 let fields = [member(char_), unnamed_bits(uint, 20)];
937 let arm = lay_out_on(&aapcs(), &types, RecordKind::Struct, &fields);
938 assert_eq!(arm.layout, Layout::new(4, 4));
939
940 let only = [unnamed_bits(uint, 0)];
943 assert_eq!(
944 lay_out_on(&aapcs(), &types, RecordKind::Struct, &only).layout,
945 Layout::new(0, 4)
946 );
947 assert_eq!(lay_out(&types, RecordKind::Struct, &only).layout, Layout::new(0, 1));
948
949 let pushed = [member(char_), unnamed_bits(uint, 0), member(char_)];
952 let pushed = lay_out_on(&aapcs(), &types, RecordKind::Struct, &pushed);
953 assert_eq!(pushed.layout, Layout::new(8, 4));
954 assert_eq!(offsets(&pushed), [0, 32, 32]);
955 }
956
957 #[test]
958 fn windows_allocates_a_bit_field_into_a_unit_of_its_declared_type() {
959 let mut interner = Interner::new();
960 let types = Types::new();
961 let char_ = types.int(IntKind::Char);
962 let uint = types.int(IntKind::UInt);
963 let ushort = types.int(IntKind::UShort);
964 let longlong = types.int(IntKind::LongLong);
965
966 let then_member = [bits(&mut interner, "m0", uint, 3), member(char_)];
969 let ms = lay_out_on(&windows(), &types, RecordKind::Struct, &then_member);
970 assert_eq!(ms.layout, Layout::new(8, 4));
971 assert_eq!(offsets(&ms), [0, 32]);
972 let itanium = lay_out(&types, RecordKind::Struct, &then_member);
973 assert_eq!(itanium.layout, Layout::new(4, 4));
974 assert_eq!(offsets(&itanium), [0, 8]);
975
976 let narrower = [bits(&mut interner, "m0", uint, 3), bits(&mut interner, "m1", ushort, 5)];
978 let ms = lay_out_on(&windows(), &types, RecordKind::Struct, &narrower);
979 assert_eq!(ms.layout, Layout::new(8, 4));
980 assert_eq!(offsets(&ms), [0, 32]);
981 assert_eq!(lay_out(&types, RecordKind::Struct, &narrower).layout, Layout::new(4, 4));
982
983 let wide = [member(char_), bits(&mut interner, "b", longlong, 33)];
986 let ms = lay_out_on(&windows(), &types, RecordKind::Struct, &wide);
987 assert_eq!(ms.layout, Layout::new(16, 8));
988 assert_eq!(offsets(&ms), [0, 64]);
989 let itanium = lay_out(&types, RecordKind::Struct, &wide);
990 assert_eq!(itanium.layout, Layout::new(8, 8));
991 assert_eq!(offsets(&itanium), [0, 8]);
992 }
993
994 #[test]
995 fn microsofts_zero_width_bit_field_closes_a_unit_and_does_nothing_without_one() {
996 let mut interner = Interner::new();
997 let types = Types::new();
998 let char_ = types.int(IntKind::Char);
999 let uint = types.int(IntKind::UInt);
1000
1001 let alone = [member(char_), unnamed_bits(uint, 0)];
1003 assert_eq!(
1004 lay_out_on(&windows(), &types, RecordKind::Struct, &alone).layout,
1005 Layout::new(1, 1)
1006 );
1007 assert_eq!(lay_out(&types, RecordKind::Struct, &alone).layout, Layout::new(4, 1));
1008
1009 let between = [
1011 bits(&mut interner, "m0", uint, 3),
1012 unnamed_bits(uint, 0),
1013 bits(&mut interner, "m1", uint, 5),
1014 member(char_),
1015 ];
1016 let ms = lay_out_on(&windows(), &types, RecordKind::Struct, &between);
1017 assert_eq!(ms.layout, Layout::new(12, 4));
1018 assert_eq!(offsets(&ms), [0, 32, 32, 64]);
1019 let itanium = lay_out(&types, RecordKind::Struct, &between);
1020 assert_eq!(itanium.layout, Layout::new(8, 4));
1021 assert_eq!(offsets(&itanium), [0, 32, 32, 40]);
1022 }
1023
1024 #[test]
1025 fn microsoft_gives_a_unions_bit_field_storage_and_no_say_in_the_alignment() {
1026 let mut interner = Interner::new();
1027 let types = Types::new();
1028 let char_ = types.int(IntKind::Char);
1029 let uint = types.int(IntKind::UInt);
1030
1031 let fields = [bits(&mut interner, "m0", uint, 3), member(char_)];
1034 assert_eq!(
1035 lay_out_on(&windows(), &types, RecordKind::Union, &fields).layout,
1036 Layout::new(4, 1)
1037 );
1038 assert_eq!(lay_out(&types, RecordKind::Union, &fields).layout, Layout::new(4, 4));
1039 }
1040
1041 #[test]
1042 fn a_record_with_no_storage_in_it_is_four_bytes_under_msvc_and_nothing_anywhere_else() {
1043 let mut types = Types::new();
1044 let uint = types.int(IntKind::UInt);
1045 let mingw = target("x86_64-pc-windows-gnu");
1046
1047 let none: [FieldDecl; 0] = [];
1049 let zero_width = [unnamed_bits(uint, 0)];
1050 let flexible = [member(types.array(uint, ArrayLen::Unknown))];
1051 for fields in [&none[..], &zero_width[..], &flexible[..]] {
1052 let msvc = lay_out_on(&windows(), &types, RecordKind::Struct, fields);
1053 assert_eq!(msvc.layout.size, 4, "four bytes under MSVC");
1054 assert_eq!(lay_out_on(&mingw, &types, RecordKind::Struct, fields).layout.size, 0);
1055 assert_eq!(lay_out(&types, RecordKind::Struct, fields).layout.size, 0);
1056 }
1057
1058 assert_eq!(windows().empty_record_size, 4);
1062 assert_eq!(mingw.empty_record_size, 0);
1063 }
1064
1065 #[test]
1066 fn packed_drops_every_member_to_a_byte_and_bit_fields_to_the_next_free_bit() {
1067 let mut interner = Interner::new();
1068 let types = Types::new();
1069 let char_ = types.int(IntKind::Char);
1070 let int = types.int(IntKind::Int);
1071 let packed = RecordOptions { packed: true, ..RecordOptions::default() };
1072
1073 let fields = [member(char_), member(int)];
1074 let laid_out = layout_record(&types, RecordKind::Struct, &fields, &packed, &linux())
1075 .expect("a packed struct of two complete members");
1076 assert_eq!(laid_out.layout, Layout::new(5, 1));
1077 assert_eq!(offsets(&laid_out), [0, 8]);
1078
1079 let fields = [member(char_), bits(&mut interner, "b", int, 30)];
1080 let laid_out = layout_record(&types, RecordKind::Struct, &fields, &packed, &linux())
1081 .expect("a packed struct with a bit-field");
1082 assert_eq!(laid_out.layout, Layout::new(5, 1));
1083 assert_eq!(offsets(&laid_out), [0, 8], "no boundary left to move to");
1084
1085 let fields = [member(char_), unnamed_bits(int, 0), member(char_)];
1088 let laid_out = layout_record(&types, RecordKind::Struct, &fields, &packed, &linux())
1089 .expect("a packed struct with a zero width bit-field");
1090 assert_eq!(laid_out.layout, Layout::new(5, 1));
1091 assert_eq!(offsets(&laid_out), [0, 32, 32]);
1092 }
1093
1094 #[test]
1095 fn pragma_pack_caps_alignment_and_leaves_a_bit_field_where_it_already_is() {
1096 let mut interner = Interner::new();
1097 let types = Types::new();
1098 let char_ = types.int(IntKind::Char);
1099 let int = types.int(IntKind::Int);
1100 let pack = RecordOptions { pack: Some(2), ..RecordOptions::default() };
1101
1102 let fields = [member(char_), member(int)];
1103 let laid_out = layout_record(&types, RecordKind::Struct, &fields, &pack, &linux())
1104 .expect("a packed struct of two complete members");
1105 assert_eq!(laid_out.layout, Layout::new(6, 2));
1106 assert_eq!(offsets(&laid_out), [0, 16]);
1107
1108 let fields = [member(char_), bits(&mut interner, "b", int, 30)];
1112 let laid_out = layout_record(&types, RecordKind::Struct, &fields, &pack, &linux())
1113 .expect("a packed struct with a bit-field");
1114 assert_eq!(laid_out.layout, Layout::new(6, 2));
1115 assert_eq!(offsets(&laid_out), [0, 8]);
1116
1117 let fields = [member(char_), unnamed_bits(int, 30)];
1120 let laid_out = layout_record(&types, RecordKind::Struct, &fields, &pack, &linux())
1121 .expect("a packed struct with an unnamed bit-field");
1122 assert_eq!(laid_out.layout, Layout::new(5, 1));
1123 }
1124
1125 #[test]
1126 fn an_alignment_the_program_asked_for_raises_the_member_and_the_record() {
1127 let types = Types::new();
1128 let char_ = types.int(IntKind::Char);
1129 let int = types.int(IntKind::Int);
1130
1131 let aligned = FieldDecl { align: Some(16), ..member(int) };
1132 let laid_out = lay_out(&types, RecordKind::Struct, &[member(char_), aligned]);
1133 assert_eq!(laid_out.layout, Layout::new(32, 16));
1134 assert_eq!(offsets(&laid_out), [0, 128]);
1135
1136 let options = RecordOptions { packed: true, align: Some(4), pack: None };
1139 let fields = [member(char_), member(int)];
1140 let laid_out = layout_record(&types, RecordKind::Struct, &fields, &options, &linux())
1141 .expect("a packed struct with an alignment asked for");
1142 assert_eq!(laid_out.layout, Layout::new(8, 4));
1143 assert_eq!(offsets(&laid_out), [0, 8]);
1144 }
1145
1146 #[test]
1147 fn a_flexible_array_member_costs_nothing_but_its_alignment() {
1148 let mut types = Types::new();
1150 let char_ = types.int(IntKind::Char);
1151 let int = types.int(IntKind::Int);
1152 let long_long = types.int(IntKind::LongLong);
1153
1154 let chars = types.array(char_, ArrayLen::Unknown);
1155 let laid_out = lay_out(&types, RecordKind::Struct, &[member(int), member(chars)]);
1156 assert_eq!(laid_out.layout, Layout::new(4, 4));
1157 assert_eq!(offsets(&laid_out), [0, 32]);
1158
1159 let longs = types.array(long_long, ArrayLen::Unknown);
1161 let laid_out = lay_out(&types, RecordKind::Struct, &[member(char_), member(longs)]);
1162 assert_eq!(laid_out.layout, Layout::new(8, 8));
1163 assert_eq!(offsets(&laid_out), [0, 64]);
1164
1165 let fields = [member(chars), member(int)];
1167 let error =
1168 layout_record(&types, RecordKind::Struct, &fields, &RecordOptions::default(), &linux());
1169 assert_eq!(error, Err(RecordError::Member { index: 0, error: LayoutError::Incomplete }));
1170 }
1171
1172 #[test]
1173 fn a_record_with_no_members_is_zero_bytes_aligned_to_one() {
1174 let types = Types::new();
1176 let laid_out = lay_out(&types, RecordKind::Struct, &[]);
1177 assert_eq!(laid_out.layout, Layout::new(0, 1));
1178 }
1179
1180 #[test]
1181 fn a_bit_field_wider_than_the_type_it_is_declared_with_is_refused() {
1182 let types = Types::new();
1183 let int = types.int(IntKind::Int);
1184 let fields = [unnamed_bits(int, 33)];
1185 let error =
1186 layout_record(&types, RecordKind::Struct, &fields, &RecordOptions::default(), &linux());
1187 let want = RecordError::BitFieldTooWide { index: 0, width: 33, capacity: 32 };
1188 assert_eq!(error, Err(want));
1189 }
1190
1191 #[test]
1192 fn a_record_reports_its_members_once_it_has_been_completed() {
1193 let mut interner = Interner::new();
1194 let mut types = Types::new();
1195 let char_ = types.int(IntKind::Char);
1196 let int = types.int(IntKind::Int);
1197 let name = interner.intern("count");
1198 let fields = [member(char_), FieldDecl::new(Some(name), int)];
1199 let id = types.declare_record(RecordKind::Struct, None);
1200 let laid_out = lay_out(&types, RecordKind::Struct, &fields);
1201 types.complete_record(id, laid_out);
1202 let ty = types.record(id);
1203 assert_eq!(layout(&types, ty, &linux()).unwrap(), Layout::new(8, 4));
1204 let field = types.field(id, name).expect("the member that was declared");
1205 assert_eq!(field.offset, 4);
1206 assert!(!field.is_bit_field());
1207 assert_eq!(types.field(id, interner.intern("missing")), None);
1208 }
1209
1210 #[test]
1211 fn a_nested_record_brings_its_own_alignment_with_it() {
1212 let mut types = Types::new();
1213 let char_ = types.int(IntKind::Char);
1214 let int = types.int(IntKind::Int);
1215 let inner = record(&mut types, RecordKind::Struct, &[member(char_)]);
1216 let laid_out = lay_out(&types, RecordKind::Struct, &[member(inner), member(int)]);
1217 assert_eq!(laid_out.layout, Layout::new(8, 4));
1218 assert_eq!(offsets(&laid_out), [0, 32]);
1219
1220 let anonymous = record(&mut types, RecordKind::Struct, &[member(int), member(char_)]);
1223 let laid_out = lay_out(&types, RecordKind::Struct, &[member(char_), member(anonymous)]);
1224 assert_eq!(laid_out.layout, Layout::new(12, 4));
1225 assert_eq!(offsets(&laid_out), [0, 32]);
1226 }
1227
1228 #[test]
1229 fn everything_narrower_than_an_int_promotes_to_one() {
1230 let mut types = Types::new();
1234 let linux = linux();
1235 let int = types.int(IntKind::Int);
1236 let narrow =
1237 [IntKind::Char, IntKind::SChar, IntKind::UChar, IntKind::Short, IntKind::UShort];
1238 for kind in narrow {
1239 let ty = types.int(kind);
1240 assert_eq!(promote(&mut types, ty, &linux), int, "{}", kind.as_str());
1241 }
1242 let boolean = types.boolean();
1243 assert_eq!(promote(&mut types, boolean, &linux), int, "C23 made bool a real type");
1244
1245 for kind in [IntKind::Int, IntKind::UInt, IntKind::Long, IntKind::ULongLong] {
1247 let ty = types.int(kind);
1248 assert_eq!(promote(&mut types, ty, &linux), ty, "{}", kind.as_str());
1249 }
1250 }
1251
1252 #[test]
1253 fn a_bit_int_is_not_promoted_at_all() {
1254 let mut types = Types::new();
1257 let linux = linux();
1258 let small = types.bit_int(true, 8);
1259 assert_eq!(promote(&mut types, small, &linux), small);
1260 assert_eq!(usual_arithmetic(&mut types, small, small, &linux), Some(small));
1261 }
1262
1263 #[test]
1264 fn a_bit_field_is_promoted_by_its_width_and_not_by_its_type() {
1265 let mut types = Types::new();
1266 let linux = linux();
1267 let int = types.int(IntKind::Int);
1268 let uint = types.int(IntKind::UInt);
1269 let ullong = types.int(IntKind::ULongLong);
1270
1271 assert_eq!(promote_bit_field(&mut types, uint, 3, &linux), int);
1273 assert_eq!(promote_bit_field(&mut types, uint, 32, &linux), uint);
1275 assert_eq!(promote_bit_field(&mut types, int, 20, &linux), int);
1277 let forty = types.bit_int(false, 40);
1282 assert_eq!(promote_bit_field(&mut types, ullong, 40, &linux), forty);
1283 assert_eq!(promote_bit_field(&mut types, ullong, 64, &linux), ullong);
1285 }
1286
1287 #[test]
1288 fn an_enumeration_promotes_through_what_it_is_represented_in() {
1289 let mut types = Types::new();
1290 let linux = linux();
1291 let int = types.int(IntKind::Int);
1292 let short = types.int(IntKind::Short);
1293 let uint = types.int(IntKind::UInt);
1294
1295 let fixed = types.declare_enum(None);
1297 types.complete_enum(fixed, short, true);
1298 let fixed = types.enumeration(fixed);
1299 assert_eq!(promote(&mut types, fixed, &linux), int);
1300
1301 let unsigned = types.declare_enum(None);
1304 types.complete_enum(unsigned, uint, false);
1305 let unsigned = types.enumeration(unsigned);
1306 assert_eq!(promote(&mut types, unsigned, &linux), uint);
1307
1308 let undecided = types.declare_enum(None);
1311 let undecided = types.enumeration(undecided);
1312 assert_eq!(promote(&mut types, undecided, &linux), int);
1313 }
1314
1315 #[test]
1316 fn the_qualifiers_and_the_atomic_come_off_before_anything_else() {
1317 let mut types = Types::new();
1320 let linux = linux();
1321 let int = types.int(IntKind::Int);
1322 let konst = types.qualified(int, Qualifiers::CONST);
1323 let atomic = types.atomic(konst);
1324 assert_eq!(promote(&mut types, atomic, &linux), int);
1325 assert_eq!(usual_arithmetic(&mut types, atomic, konst, &linux), Some(int));
1326 }
1327
1328 #[test]
1329 fn the_usual_arithmetic_conversions_between_the_standard_integer_types() {
1330 let mut types = Types::new();
1332 let linux = linux();
1333 let cases = [
1334 (IntKind::Int, IntKind::UInt, IntKind::UInt),
1335 (IntKind::Int, IntKind::Long, IntKind::Long),
1336 (IntKind::UInt, IntKind::Long, IntKind::Long),
1337 (IntKind::UInt, IntKind::ULong, IntKind::ULong),
1338 (IntKind::Int, IntKind::LongLong, IntKind::LongLong),
1339 (IntKind::UInt, IntKind::LongLong, IntKind::LongLong),
1340 (IntKind::ULong, IntKind::LongLong, IntKind::ULongLong),
1341 (IntKind::Char, IntKind::Char, IntKind::Int),
1342 (IntKind::UChar, IntKind::UShort, IntKind::Int),
1343 ];
1344 for (left, right, want) in cases {
1345 let left = types.int(left);
1346 let right = types.int(right);
1347 let want = types.int(want);
1348 assert_eq!(usual_arithmetic(&mut types, left, right, &linux), Some(want));
1349 assert_eq!(usual_arithmetic(&mut types, right, left, &linux), Some(want), "either way");
1350 }
1351 }
1352
1353 #[test]
1354 fn int128_is_sixteen_bytes_aligned_to_sixteen_and_outranks_long_long() {
1355 let mut types = Types::new();
1359 let linux = linux();
1360 let signed = types.int(IntKind::Int128);
1361 let unsigned = types.int(IntKind::UInt128);
1362 for id in [signed, unsigned] {
1363 let laid_out = layout(&types, id, &linux).expect("a complete type");
1364 assert_eq!(laid_out.size, 16);
1365 assert_eq!(laid_out.align, 16);
1366 }
1367
1368 let ull = types.int(IntKind::ULongLong);
1372 assert_eq!(usual_arithmetic(&mut types, signed, ull, &linux), Some(signed));
1373 assert_eq!(promote(&mut types, signed, &linux), signed);
1375 }
1376
1377 #[test]
1378 fn a_bit_int_of_a_hundred_and_twenty_eight_bits_is_not_int128() {
1379 let mut types = Types::new();
1382 let linux = linux();
1383 let int128 = types.int(IntKind::Int128);
1384 let bit_int = types.bit_int(true, 128);
1385 assert_ne!(int128, bit_int);
1386 assert!(!compatible(&types, int128, bit_int));
1387 assert_eq!(layout(&types, bit_int, &linux).expect("complete").align, 8);
1388 assert_eq!(layout(&types, int128, &linux).expect("complete").align, 16);
1389 }
1390
1391 #[test]
1392 fn the_last_arm_takes_the_unsigned_type_of_the_wider_one() {
1393 let mut types = Types::new();
1397 let linux = linux();
1398 let ulong = types.int(IntKind::ULong);
1399 let long_long = types.int(IntKind::LongLong);
1400 let want = types.int(IntKind::ULongLong);
1401 assert_eq!(usual_arithmetic(&mut types, ulong, long_long, &linux), Some(want));
1402
1403 let windows = target("x86_64-pc-windows-msvc");
1407 assert_eq!(usual_arithmetic(&mut types, ulong, long_long, &windows), Some(long_long));
1408 }
1409
1410 #[test]
1411 fn a_bit_int_is_ranked_by_its_width_against_the_standard_types() {
1412 let mut types = Types::new();
1414 let linux = linux();
1415 let b40 = types.bit_int(true, 40);
1416 let ub40 = types.bit_int(false, 40);
1417 let b8 = types.bit_int(true, 8);
1418 let b32 = types.bit_int(true, 32);
1419 let int = types.int(IntKind::Int);
1420 let uint = types.int(IntKind::UInt);
1421 let long = types.int(IntKind::Long);
1422 let char_ = types.int(IntKind::Char);
1423
1424 assert_eq!(usual_arithmetic(&mut types, b40, int, &linux), Some(b40));
1426 assert_eq!(usual_arithmetic(&mut types, b40, long, &linux), Some(long));
1428 assert_eq!(usual_arithmetic(&mut types, b32, int, &linux), Some(int));
1430 assert_eq!(usual_arithmetic(&mut types, b32, uint, &linux), Some(uint));
1431 assert_eq!(usual_arithmetic(&mut types, b8, char_, &linux), Some(int));
1434 assert_eq!(usual_arithmetic(&mut types, ub40, int, &linux), Some(ub40));
1437 assert_eq!(usual_arithmetic(&mut types, ub40, long, &linux), Some(long));
1438 assert_eq!(usual_arithmetic(&mut types, b40, ub40, &linux), Some(ub40));
1440 }
1441
1442 #[test]
1443 fn a_floating_operand_decides_the_answer_whatever_the_other_side_is() {
1444 let mut types = Types::new();
1445 let linux = linux();
1446 let float = types.float(FloatKind::Float);
1447 let double = types.float(FloatKind::Double);
1448 let long_double = types.float(FloatKind::LongDouble);
1449 let ullong = types.int(IntKind::ULongLong);
1450 let int = types.int(IntKind::Int);
1451
1452 assert_eq!(usual_arithmetic(&mut types, int, float, &linux), Some(float));
1453 assert_eq!(usual_arithmetic(&mut types, float, double, &linux), Some(double));
1454 assert_eq!(usual_arithmetic(&mut types, double, long_double, &linux), Some(long_double));
1455 assert_eq!(usual_arithmetic(&mut types, ullong, float, &linux), Some(float));
1458 }
1459
1460 #[test]
1461 fn a_mask_is_the_signed_integers_of_the_lane_width() {
1462 let mut types = Types::new();
1463 let linux = linux();
1464 let int = types.int(IntKind::Int);
1465 let float = types.float(FloatKind::Float);
1466 let short = types.int(IntKind::Short);
1467
1468 let four_ints = types.vector(int, 4);
1470 assert_eq!(mask_of(&mut types, four_ints, &linux), Some(four_ints));
1471
1472 let uint = types.int(IntKind::UInt);
1475 let four_uints = types.vector(uint, 4);
1476 assert_eq!(mask_of(&mut types, four_uints, &linux), Some(four_ints));
1477
1478 let four_floats = types.vector(float, 4);
1481 assert_eq!(mask_of(&mut types, four_floats, &linux), Some(four_ints));
1482
1483 let two_shorts = types.vector(short, 2);
1485 assert_eq!(mask_of(&mut types, two_shorts, &linux), Some(two_shorts));
1486
1487 assert_eq!(mask_of(&mut types, int, &linux), None);
1489 }
1490
1491 #[test]
1492 fn two_vectors_convert_between_each_other_when_the_bytes_line_up() {
1493 let mut types = Types::new();
1494 let linux = linux();
1495 let int = types.int(IntKind::Int);
1496 let uint = types.int(IntKind::UInt);
1497 let float = types.float(FloatKind::Float);
1498 let short = types.int(IntKind::Short);
1499
1500 let four_ints = types.vector(int, 4);
1501 let four_uints = types.vector(uint, 4);
1502 let four_floats = types.vector(float, 4);
1503 let eight_shorts = types.vector(short, 8);
1504 let two_ints = types.vector(int, 2);
1505
1506 assert!(vectors_convertible(&types, four_uints, four_ints, &linux));
1509 assert!(vectors_convertible(&types, four_ints, four_uints, &linux));
1511 assert!(vectors_convertible(&types, four_ints, eight_shorts, &linux));
1513 assert!(vectors_convertible(&types, four_floats, four_floats, &linux));
1515
1516 assert!(!vectors_convertible(&types, four_ints, four_floats, &linux));
1519 assert!(!vectors_convertible(&types, four_ints, two_ints, &linux));
1521 assert!(!vectors_convertible(&types, four_ints, int, &linux));
1523 assert!(!vectors_convertible(&types, int, four_ints, &linux));
1524 }
1525
1526 fn combines(target: &TargetInfo, a: FloatKind, b: FloatKind, expected: FloatKind) {
1531 let mut types = Types::new();
1532 let left = types.float(a);
1533 let right = types.float(b);
1534 let want = types.float(expected);
1535 assert_eq!(usual_arithmetic(&mut types, left, right, target), Some(want), "{a:?} + {b:?}");
1536 assert_eq!(usual_arithmetic(&mut types, right, left, target), Some(want), "{b:?} + {a:?}");
1537 }
1538
1539 #[test]
1540 fn two_floating_types_of_the_same_format_are_still_two_types_and_one_of_them_wins() {
1541 let x86 = linux();
1545 combines(&x86, FloatKind::Double, FloatKind::Float64, FloatKind::Float64);
1546 combines(&x86, FloatKind::Float, FloatKind::Float32, FloatKind::Float32);
1547 combines(&x86, FloatKind::Double, FloatKind::Float32x, FloatKind::Double);
1548 combines(&x86, FloatKind::LongDouble, FloatKind::Float64x, FloatKind::LongDouble);
1549 combines(&x86, FloatKind::Float128, FloatKind::LongDouble, FloatKind::Float128);
1550 combines(&x86, FloatKind::Float64x, FloatKind::Float128, FloatKind::Float128);
1551 combines(&x86, FloatKind::Double, FloatKind::LongDouble, FloatKind::LongDouble);
1552 combines(&x86, FloatKind::Float32x, FloatKind::Float64, FloatKind::Float64);
1553 combines(&x86, FloatKind::Float64x, FloatKind::Float64, FloatKind::Float64x);
1554 combines(&x86, FloatKind::LongDouble, FloatKind::Float64, FloatKind::LongDouble);
1555 }
1556
1557 #[test]
1558 fn the_widest_floating_type_is_a_question_about_the_target_and_not_about_the_names() {
1559 let mac = target("aarch64-apple-darwin");
1563 combines(&mac, FloatKind::LongDouble, FloatKind::Float64x, FloatKind::Float64x);
1564 combines(&mac, FloatKind::Double, FloatKind::LongDouble, FloatKind::LongDouble);
1565 combines(&mac, FloatKind::Float128, FloatKind::LongDouble, FloatKind::Float128);
1566 combines(&mac, FloatKind::Float64x, FloatKind::Float64, FloatKind::Float64x);
1567 combines(&mac, FloatKind::Float32x, FloatKind::Float32, FloatKind::Float32x);
1568 combines(&mac, FloatKind::Float32x, FloatKind::Float64, FloatKind::Float64);
1569 combines(&mac, FloatKind::Double, FloatKind::Float64, FloatKind::Float64);
1570 combines(&mac, FloatKind::Float16, FloatKind::Float, FloatKind::Float);
1573 combines(&mac, FloatKind::Float16, FloatKind::Double, FloatKind::Double);
1574 combines(&mac, FloatKind::Float16, FloatKind::Float16, FloatKind::Float16);
1575 }
1576
1577 #[test]
1578 fn a_complex_operand_makes_the_answer_complex_after_the_real_types_have_combined() {
1579 let mut types = Types::new();
1580 let linux = linux();
1581 let cfloat = types.complex_float(FloatKind::Float);
1582 let cdouble = types.complex_float(FloatKind::Double);
1583 let cldouble = types.complex_float(FloatKind::LongDouble);
1584 let double = types.float(FloatKind::Double);
1585 let long_double = types.float(FloatKind::LongDouble);
1586 let float = types.float(FloatKind::Float);
1587 let int = types.int(IntKind::Int);
1588
1589 assert_eq!(usual_arithmetic(&mut types, cfloat, double, &linux), Some(cdouble));
1590 assert_eq!(usual_arithmetic(&mut types, cfloat, int, &linux), Some(cfloat));
1591 assert_eq!(usual_arithmetic(&mut types, cdouble, long_double, &linux), Some(cldouble));
1592 assert_eq!(usual_arithmetic(&mut types, cfloat, float, &linux), Some(cfloat));
1593 }
1594
1595 #[test]
1596 fn an_operand_that_is_not_arithmetic_has_no_common_type() {
1597 let mut types = Types::new();
1599 let linux = linux();
1600 let int = types.int(IntKind::Int);
1601 let pointer = types.pointer(int);
1602 assert_eq!(usual_arithmetic(&mut types, pointer, int, &linux), None);
1603 assert_eq!(usual_arithmetic(&mut types, pointer, pointer, &linux), None);
1604 let void = types.void();
1605 assert_eq!(usual_arithmetic(&mut types, void, int, &linux), None);
1606 assert_eq!(promote(&mut types, pointer, &linux), pointer);
1609 }
1610
1611 #[test]
1612 fn the_conversions_read_through_sugar() {
1613 let mut interner = Interner::new();
1614 let mut types = Types::new();
1615 let linux = linux();
1616 let char_ = types.int(IntKind::Char);
1617 let name = types.typedef(interner.intern("byte"), char_);
1618 let int = types.int(IntKind::Int);
1619 assert_eq!(promote(&mut types, name, &linux), int);
1620 }
1621
1622 fn prototype(types: &mut Types, params: Vec<TypeId>, variadic: bool) -> TypeId {
1624 let ret = types.void();
1625 types.function(FunctionType { ret, params, variadic, prototyped: true })
1626 }
1627
1628 fn old_style(types: &mut Types) -> TypeId {
1630 let ret = types.void();
1631 types.function(FunctionType { ret, params: Vec::new(), variadic: false, prototyped: false })
1632 }
1633
1634 fn tagged(types: &mut Types, tag: Symbol, fields: &[FieldDecl]) -> RecordId {
1636 let id = types.declare_record(RecordKind::Struct, Some(tag));
1637 let laid_out = lay_out(types, RecordKind::Struct, fields);
1638 types.complete_record(id, laid_out);
1639 id
1640 }
1641
1642 #[test]
1643 fn a_type_is_compatible_with_itself_however_it_was_written() {
1644 let mut interner = Interner::new();
1645 let mut types = Types::new();
1646 let int = types.int(IntKind::Int);
1647 let name = types.typedef(interner.intern("int32_t"), int);
1648 assert!(compatible(&types, name, int), "the sugar is the same type underneath");
1649 assert_eq!(composite(&mut types, name, int), Some(name), "and it keeps its name");
1650
1651 let konst = types.qualified(int, Qualifiers::CONST);
1654 assert!(!compatible(&types, konst, int));
1655 let konst_pointer = types.pointer(konst);
1656 let pointer = types.pointer(int);
1657 assert!(!compatible(&types, konst_pointer, pointer));
1658 assert_eq!(composite(&mut types, konst_pointer, pointer), None);
1659
1660 let char_ = types.int(IntKind::Char);
1663 let schar = types.int(IntKind::SChar);
1664 assert!(!compatible(&types, char_, schar));
1665 let atomic = types.atomic(int);
1667 assert!(!compatible(&types, atomic, int));
1668 }
1669
1670 #[test]
1671 fn an_enumeration_is_compatible_with_the_type_it_is_represented_in() {
1672 let mut types = Types::new();
1675 let uint = types.int(IntKind::UInt);
1676 let int = types.int(IntKind::Int);
1677 let id = types.declare_enum(None);
1678 types.complete_enum(id, uint, false);
1679 let e = types.enumeration(id);
1680 assert!(compatible(&types, e, uint));
1681 assert!(compatible(&types, uint, e), "and the relation is symmetric");
1682 assert!(!compatible(&types, e, int));
1683
1684 let other = types.declare_enum(None);
1687 types.complete_enum(other, uint, false);
1688 let other = types.enumeration(other);
1689 assert!(!compatible(&types, e, other));
1690
1691 let undecided = types.declare_enum(None);
1694 let undecided = types.enumeration(undecided);
1695 assert!(!compatible(&types, undecided, uint));
1696 assert!(compatible(&types, undecided, undecided));
1697 }
1698
1699 #[test]
1700 fn an_array_without_a_size_is_compatible_with_one_that_has_it() {
1701 let mut types = Types::new();
1704 let int = types.int(IntKind::Int);
1705 let unknown = types.array(int, ArrayLen::Unknown);
1706 let four = types.array(int, ArrayLen::Fixed(4));
1707 let five = types.array(int, ArrayLen::Fixed(5));
1708 assert!(compatible(&types, unknown, four));
1709 assert!(!compatible(&types, four, five));
1710 assert_eq!(composite(&mut types, unknown, four), Some(four));
1711 assert_eq!(composite(&mut types, four, unknown), Some(four), "either way round");
1712 assert_eq!(composite(&mut types, four, five), None);
1713
1714 let vla = types.array(int, ArrayLen::Variable(VlaId(0)));
1717 assert!(compatible(&types, vla, four));
1718 assert_eq!(composite(&mut types, vla, four), Some(four));
1719
1720 let long = types.int(IntKind::Long);
1722 let longs = types.array(long, ArrayLen::Fixed(4));
1723 assert!(!compatible(&types, four, longs));
1724 }
1725
1726 #[test]
1727 fn a_parameter_declared_as_an_array_is_a_pointer() {
1728 let mut types = Types::new();
1732 let int = types.int(IntKind::Int);
1733 let three = types.array(int, ArrayLen::Fixed(3));
1734 let pointer = types.pointer(int);
1735 assert_eq!(adjust_parameter(&mut types, three), pointer);
1736
1737 let function = prototype(&mut types, vec![int], false);
1739 let function_pointer = types.pointer(function);
1740 assert_eq!(adjust_parameter(&mut types, function), function_pointer);
1741
1742 let konst = types.qualified(int, Qualifiers::CONST);
1745 assert_eq!(adjust_parameter(&mut types, konst), int);
1746 let to_konst = types.pointer(konst);
1747 assert_eq!(adjust_parameter(&mut types, to_konst), to_konst);
1748 }
1749
1750 #[test]
1751 fn an_old_style_declaration_is_compatible_with_the_prototypes_a_call_could_not_tell_from_it() {
1752 let mut types = Types::new();
1756 let old = old_style(&mut types);
1757 let int = types.int(IntKind::Int);
1758 let long = types.int(IntKind::Long);
1759 let char_ = types.int(IntKind::Char);
1760 let float = types.float(FloatKind::Float);
1761 let double = types.float(FloatKind::Double);
1762
1763 let takes_int = prototype(&mut types, vec![int], false);
1764 assert!(compatible(&types, old, takes_int));
1765 assert!(compatible(&types, takes_int, old), "and the relation is symmetric");
1766 assert_eq!(composite(&mut types, old, takes_int), Some(takes_int));
1768
1769 let pointer = types.pointer(int);
1770 for params in [vec![long], vec![double], vec![pointer], vec![int, long]] {
1771 let ty = prototype(&mut types, params, false);
1772 assert!(compatible(&types, old, ty), "nothing here is touched by a promotion");
1773 }
1774
1775 for params in [vec![char_], vec![float], vec![int, char_]] {
1778 let ty = prototype(&mut types, params, false);
1779 assert!(!compatible(&types, old, ty));
1780 assert_eq!(composite(&mut types, old, ty), None);
1781 }
1782
1783 let variadic = prototype(&mut types, vec![int], true);
1785 assert!(!compatible(&types, old, variadic));
1786
1787 let uint = types.int(IntKind::UInt);
1789 let id = types.declare_enum(None);
1790 types.complete_enum(id, uint, false);
1791 let e = types.enumeration(id);
1792 let takes_enum = prototype(&mut types, vec![e], false);
1793 assert!(compatible(&types, old, takes_enum));
1794
1795 assert!(compatible(&types, old, old));
1797
1798 let returns_int = types.function(FunctionType {
1800 ret: int,
1801 params: Vec::new(),
1802 variadic: false,
1803 prototyped: false,
1804 });
1805 assert!(!compatible(&types, returns_int, takes_int));
1806 }
1807
1808 #[test]
1809 fn from_c23_an_empty_parameter_list_is_a_prototype_and_conflicts_where_it_used_to_merge() {
1810 let mut types = Types::new();
1814 let int = types.int(IntKind::Int);
1815 let takes_int = prototype(&mut types, vec![int], false);
1816 let takes_nothing = prototype(&mut types, Vec::new(), false);
1817 let old = old_style(&mut types);
1818 assert!(!compatible(&types, takes_nothing, takes_int));
1819 assert!(compatible(&types, old, takes_int), "the C17 reading of the same source");
1820 }
1821
1822 #[test]
1823 fn two_prototypes_have_to_agree_about_everything() {
1824 let mut types = Types::new();
1825 let int = types.int(IntKind::Int);
1826 let long = types.int(IntKind::Long);
1827 let base = prototype(&mut types, vec![int, int], false);
1828 for other in [vec![int], vec![int, long], vec![int, int, int], Vec::new()] {
1829 let other = prototype(&mut types, other, false);
1830 assert!(!compatible(&types, base, other));
1831 }
1832 let variadic = prototype(&mut types, vec![int, int], true);
1833 assert!(!compatible(&types, base, variadic), "`...` is part of the type");
1834
1835 let four = types.array(int, ArrayLen::Fixed(4));
1838 let unknown = types.array(int, ArrayLen::Unknown);
1839 let to_four = types.pointer(four);
1840 let to_unknown = types.pointer(unknown);
1841 let a = prototype(&mut types, vec![to_four], false);
1842 let b = prototype(&mut types, vec![to_unknown], false);
1843 assert!(compatible(&types, a, b));
1844 assert_eq!(composite(&mut types, a, b), Some(a));
1846 }
1847
1848 #[test]
1849 fn a_pointer_composite_reaches_through_to_what_is_pointed_at() {
1850 let mut types = Types::new();
1851 let int = types.int(IntKind::Int);
1852 let four = types.array(int, ArrayLen::Fixed(4));
1853 let unknown = types.array(int, ArrayLen::Unknown);
1854 let to_four = types.pointer(four);
1855 let to_unknown = types.pointer(unknown);
1856 assert_eq!(composite(&mut types, to_unknown, to_four), Some(to_four));
1857
1858 let konst_to_unknown = types.qualified(to_unknown, Qualifiers::CONST);
1860 let konst_to_four = types.qualified(to_four, Qualifiers::CONST);
1861 assert_eq!(composite(&mut types, konst_to_unknown, konst_to_four), Some(konst_to_four));
1862 }
1863
1864 #[test]
1865 fn two_record_declarations_with_the_same_tag_and_the_same_members_are_compatible() {
1866 let mut interner = Interner::new();
1870 let mut types = Types::new();
1871 let tag = interner.intern("point");
1872 let x = interner.intern("x");
1873 let y = interner.intern("y");
1874 let int = types.int(IntKind::Int);
1875 let members = [FieldDecl::new(Some(x), int), FieldDecl::new(Some(y), int)];
1876
1877 let first = tagged(&mut types, tag, &members);
1878 let second = tagged(&mut types, tag, &members);
1879 let first = types.record(first);
1880 let second = types.record(second);
1881 assert_ne!(first, second, "still two declarations and two types");
1882 assert!(compatible(&types, first, second));
1883
1884 let z = interner.intern("z");
1887 let long = types.int(IntKind::Long);
1888 let renamed = [FieldDecl::new(Some(x), int), FieldDecl::new(Some(z), int)];
1889 let retyped = [FieldDecl::new(Some(x), int), FieldDecl::new(Some(y), long)];
1890 for other in [&renamed[..], &retyped[..], &members[..1]] {
1891 let other = tagged(&mut types, tag, other);
1892 let other = types.record(other);
1893 assert!(!compatible(&types, first, other));
1894 }
1895 let elsewhere = tagged(&mut types, interner.intern("pair"), &members);
1896 let elsewhere = types.record(elsewhere);
1897 assert!(!compatible(&types, first, elsewhere));
1898
1899 let anonymous = record(&mut types, RecordKind::Struct, &members);
1902 let also_anonymous = record(&mut types, RecordKind::Struct, &members);
1903 assert!(!compatible(&types, anonymous, also_anonymous));
1904
1905 let incomplete = types.declare_record(RecordKind::Struct, Some(tag));
1907 let incomplete = types.record(incomplete);
1908 assert!(!compatible(&types, first, incomplete));
1909 assert!(compatible(&types, incomplete, incomplete));
1910 }
1911
1912 #[test]
1913 fn a_self_referential_record_is_compared_without_going_round_forever() {
1914 let mut interner = Interner::new();
1918 let mut types = Types::new();
1919 let tag = interner.intern("node");
1920 let value = interner.intern("value");
1921 let next = interner.intern("next");
1922 let int = types.int(IntKind::Int);
1923
1924 let node = |types: &mut Types| {
1925 let id = types.declare_record(RecordKind::Struct, Some(tag));
1926 let ty = types.record(id);
1927 let pointer = types.pointer(ty);
1928 let members = [FieldDecl::new(Some(value), int), FieldDecl::new(Some(next), pointer)];
1929 let laid_out = lay_out(types, RecordKind::Struct, &members);
1930 types.complete_record(id, laid_out);
1931 ty
1932 };
1933 let first = node(&mut types);
1934 let second = node(&mut types);
1935 assert_ne!(first, second);
1936 assert!(compatible(&types, first, second));
1937
1938 let id = types.declare_record(RecordKind::Struct, Some(tag));
1941 let ty = types.record(id);
1942 let pointer = types.pointer(ty);
1943 let members = [FieldDecl::new(Some(next), pointer), FieldDecl::new(Some(value), int)];
1944 let laid_out = lay_out(&types, RecordKind::Struct, &members);
1945 types.complete_record(id, laid_out);
1946 assert!(!compatible(&types, first, ty));
1947 }
1948
1949 #[test]
1950 fn layout_reads_through_sugar() {
1951 let mut interner = Interner::new();
1952 let mut types = Types::new();
1953 let long = types.int(IntKind::Long);
1954 let name = types.typedef(interner.intern("word"), long);
1955 let array = types.array(name, ArrayLen::Fixed(4));
1956 assert_eq!(layout(&types, array, &linux()).unwrap(), Layout::new(32, 8));
1957 }
1958
1959 fn work_out(recipe: &Extent, sizes: &[u64]) -> u64 {
1965 match recipe {
1966 Extent::Bytes(count) => *count,
1967 Extent::Member(index) => sizes[*index as usize],
1968 Extent::Sum(parts) => parts.iter().map(|part| work_out(part, sizes)).sum(),
1969 Extent::RoundUp(inner, to) => work_out(inner, sizes).next_multiple_of(*to),
1970 Extent::Max(parts) => parts.iter().map(|part| work_out(part, sizes)).max().unwrap_or(0),
1971 }
1972 }
1973
1974 fn measured(types: &mut Types, which: u32) -> TypeId {
1976 let int = types.int(IntKind::Int);
1977 types.array(int, ArrayLen::Variable(VlaId(which)))
1978 }
1979
1980 #[test]
1981 fn a_member_of_no_fixed_size_leaves_the_size_and_what_follows_it_to_the_program() {
1982 let mut types = Types::new();
1983 let int = types.int(IntKind::Int);
1984 let rows = measured(&mut types, 0);
1985 let laid_out = lay_out(&types, RecordKind::Struct, &[member(rows), member(int)]);
1986
1987 assert_eq!(laid_out.layout, Layout::new(0, 4));
1990 let variable = laid_out.variable.expect("a record with a member of no fixed size");
1991 assert_eq!(variable.offsets[0], None);
1995 let after = variable.offsets[1].as_ref().expect("an offset the program works out");
1996 for count in 0..6u64 {
1997 let sizes = [4 * count, 4];
1998 assert_eq!(work_out(after, &sizes), 4 * count);
1999 assert_eq!(work_out(&variable.size, &sizes), 4 * count + 4);
2000 }
2001 assert_eq!(laid_out.fields[1].align, 4);
2002 }
2003
2004 #[test]
2005 fn a_member_after_one_of_no_fixed_size_is_rounded_up_where_its_alignment_asks_for_it() {
2006 let mut types = Types::new();
2007 let char_ty = types.int(IntKind::Char);
2008 let rows = measured(&mut types, 0);
2009 let double = types.float(FloatKind::Double);
2010 let fields = [member(char_ty), member(rows), member(double)];
2011 let laid_out = lay_out(&types, RecordKind::Struct, &fields);
2012
2013 assert_eq!(laid_out.layout, Layout::new(0, 8));
2014 assert_eq!(offsets(&laid_out)[..2], [0, 32]);
2015 let variable = laid_out.variable.expect("a record with a member of no fixed size");
2016 assert_eq!(variable.offsets[..2], [None, None]);
2017 let after = variable.offsets[2].as_ref().expect("an offset the program works out");
2018 for count in 0..6u64 {
2019 let sizes = [1, 4 * count, 8];
2020 let at = (4 + 4 * count).next_multiple_of(8);
2021 assert_eq!(work_out(after, &sizes), at);
2022 assert_eq!(work_out(&variable.size, &sizes), at + 8);
2023 }
2024 }
2025
2026 #[test]
2027 fn a_union_with_a_member_of_no_fixed_size_is_as_long_as_the_longest_of_them() {
2028 let mut types = Types::new();
2029 let rows = measured(&mut types, 0);
2030 let double = types.float(FloatKind::Double);
2031 let laid_out = lay_out(&types, RecordKind::Union, &[member(rows), member(double)]);
2032
2033 assert_eq!(laid_out.layout, Layout::new(0, 8));
2034 let variable = laid_out.variable.expect("a union with a member of no fixed size");
2035 assert!(variable.offsets.iter().all(Option::is_none));
2038 for count in 0..6u64 {
2039 let sizes = [4 * count, 8];
2040 let want = (4 * count).max(8).next_multiple_of(8);
2041 assert_eq!(work_out(&variable.size, &sizes), want);
2042 }
2043 }
2044
2045 #[test]
2046 fn packed_takes_the_rounding_out_of_a_record_the_program_measures() {
2047 let mut types = Types::new();
2048 let char_ty = types.int(IntKind::Char);
2049 let int = types.int(IntKind::Int);
2050 let rows = measured(&mut types, 0);
2051 let fields = [member(char_ty), member(rows), member(int)];
2052 let options = RecordOptions { packed: true, ..RecordOptions::default() };
2053 let laid_out = layout_record(&types, RecordKind::Struct, &fields, &options, &linux())
2054 .expect("a packed record with a member of no fixed size");
2055
2056 assert_eq!(laid_out.layout, Layout::new(0, 1));
2057 assert_eq!(laid_out.fields[2].align, 1);
2058 let variable = laid_out.variable.expect("a record with a member of no fixed size");
2059 let after = variable.offsets[2].as_ref().expect("an offset the program works out");
2060 for count in 0..6u64 {
2061 let sizes = [1, 4 * count, 4];
2062 assert_eq!(work_out(after, &sizes), 1 + 4 * count);
2063 assert_eq!(work_out(&variable.size, &sizes), 1 + 4 * count + 4);
2064 }
2065 }
2066
2067 #[test]
2068 fn a_bit_field_after_a_member_of_no_fixed_size_is_turned_down() {
2069 let mut interner = Interner::new();
2070 let mut types = Types::new();
2071 let int = types.int(IntKind::Int);
2072 let rows = measured(&mut types, 0);
2073 let fields = [member(rows), bits(&mut interner, "b", int, 3)];
2074 let options = RecordOptions::default();
2075 let failed = layout_record(&types, RecordKind::Struct, &fields, &options, &linux());
2076
2077 assert_eq!(failed, Err(RecordError::VariableBitField { index: 1 }));
2081 }
2082}