Rust · 1918 bytes Raw Blame History
1 //! Default kind selection knobs (sprint 32 #504 / #4).
2 //!
3 //! `-fdefault-integer-8` and `-fdefault-real-8` change the implicit
4 //! kind for unsuffixed `integer` / `real` declarations. The
5 //! information needs to reach two places that don't share the
6 //! Options struct directly:
7 //! - `crate::ir::lower::extract_kind*` — for IR type lowering
8 //! - `crate::sema::type_layout::size_of_type` — for struct layout
9 //!
10 //! Plumbing it through every type_spec_to_type_info call would touch
11 //! ~20 sites; instead we publish per-thread defaults that the driver
12 //! sets at the start of `compile()` and the consumers read.
13 //!
14 //! Per-thread (not process-wide) because cargo runs tests in parallel
15 //! within one process — a global AtomicU8 lets one test's
16 //! -fdefault-integer-8 bleed into another test running on a
17 //! different thread. thread_local Cell isolates them.
18
19 use std::cell::Cell;
20
21 thread_local! {
22 /// Default kind for unsuffixed `integer` declarations. 4 (the
23 /// Fortran standard's processor default) unless
24 /// `-fdefault-integer-8` is in effect.
25 static DEFAULT_INT_KIND: Cell<u8> = const { Cell::new(4) };
26
27 /// Default kind for unsuffixed `real` declarations. 4 unless
28 /// `-fdefault-real-8` is in effect.
29 static DEFAULT_REAL_KIND: Cell<u8> = const { Cell::new(4) };
30 }
31
32 /// Reset to the standard defaults. Called by the driver at the
33 /// start of every compile() so a previous run with non-default
34 /// kinds doesn't leak into the current one.
35 pub fn reset() {
36 DEFAULT_INT_KIND.with(|c| c.set(4));
37 DEFAULT_REAL_KIND.with(|c| c.set(4));
38 }
39
40 pub fn set_default_int_kind(k: u8) {
41 DEFAULT_INT_KIND.with(|c| c.set(k));
42 }
43
44 pub fn set_default_real_kind(k: u8) {
45 DEFAULT_REAL_KIND.with(|c| c.set(k));
46 }
47
48 pub fn default_int_kind() -> u8 {
49 DEFAULT_INT_KIND.with(|c| c.get())
50 }
51
52 pub fn default_real_kind() -> u8 {
53 DEFAULT_REAL_KIND.with(|c| c.get())
54 }
55