Skip to content

Commit 59035a3

Browse files
committed
Move FieldIdx and Layout to rustc_target
1 parent b0cac4d commit 59035a3

File tree

4 files changed

+84
-81
lines changed

4 files changed

+84
-81
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4463,6 +4463,7 @@ dependencies = [
44634463
"rustc_data_structures",
44644464
"rustc_feature",
44654465
"rustc_fs_util",
4466+
"rustc_index",
44664467
"rustc_macros",
44674468
"rustc_serialize",
44684469
"rustc_span",

compiler/rustc_abi/src/lib.rs

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,32 +1103,6 @@ impl Scalar {
11031103
}
11041104
}
11051105

1106-
rustc_index::newtype_index! {
1107-
/// The *source-order* index of a field in a variant.
1108-
///
1109-
/// This is how most code after type checking refers to fields, rather than
1110-
/// using names (as names have hygiene complications and more complex lookup).
1111-
///
1112-
/// Particularly for `repr(Rust)` types, this may not be the same as *layout* order.
1113-
/// (It is for `repr(C)` `struct`s, however.)
1114-
///
1115-
/// For example, in the following types,
1116-
/// ```rust
1117-
/// # enum Never {}
1118-
/// # #[repr(u16)]
1119-
/// enum Demo1 {
1120-
/// Variant0 { a: Never, b: i32 } = 100,
1121-
/// Variant1 { c: u8, d: u64 } = 10,
1122-
/// }
1123-
/// struct Demo2 { e: u8, f: u16, g: u8 }
1124-
/// ```
1125-
/// `b` is `FieldIdx(1)` in `VariantIdx(0)`,
1126-
/// `d` is `FieldIdx(1)` in `VariantIdx(1)`, and
1127-
/// `f` is `FieldIdx(1)` in `VariantIdx(0)`.
1128-
#[derive(HashStable_Generic)]
1129-
pub struct FieldIdx {}
1130-
}
1131-
11321106
/// Describes how the fields of a type are located in memory.
11331107
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
11341108
#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]
@@ -1621,61 +1595,6 @@ where
16211595
}
16221596
}
16231597

1624-
#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable_Generic)]
1625-
#[rustc_pass_by_value]
1626-
pub struct Layout<'a>(pub Interned<'a, LayoutS<FieldIdx>>);
1627-
1628-
impl<'a> fmt::Debug for Layout<'a> {
1629-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1630-
// See comment on `<LayoutS as Debug>::fmt` above.
1631-
self.0.0.fmt(f)
1632-
}
1633-
}
1634-
1635-
impl<'a> Layout<'a> {
1636-
pub fn fields(self) -> &'a FieldsShape<FieldIdx> {
1637-
&self.0.0.fields
1638-
}
1639-
1640-
pub fn variants(self) -> &'a Variants<FieldIdx> {
1641-
&self.0.0.variants
1642-
}
1643-
1644-
pub fn abi(self) -> Abi {
1645-
self.0.0.abi
1646-
}
1647-
1648-
pub fn largest_niche(self) -> Option<Niche> {
1649-
self.0.0.largest_niche
1650-
}
1651-
1652-
pub fn align(self) -> AbiAndPrefAlign {
1653-
self.0.0.align
1654-
}
1655-
1656-
pub fn size(self) -> Size {
1657-
self.0.0.size
1658-
}
1659-
1660-
pub fn max_repr_align(self) -> Option<Align> {
1661-
self.0.0.max_repr_align
1662-
}
1663-
1664-
pub fn unadjusted_abi_align(self) -> Align {
1665-
self.0.0.unadjusted_abi_align
1666-
}
1667-
1668-
/// Whether the layout is from a type that implements [`std::marker::PointerLike`].
1669-
///
1670-
/// Currently, that means that the type is pointer-sized, pointer-aligned,
1671-
/// and has a scalar ABI.
1672-
pub fn is_pointer_like(self, data_layout: &TargetDataLayout) -> bool {
1673-
self.size() == data_layout.pointer_size
1674-
&& self.align().abi == data_layout.pointer_align.abi
1675-
&& matches!(self.abi(), Abi::Scalar(..))
1676-
}
1677-
}
1678-
16791598
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
16801599
pub enum PointerKind {
16811600
/// Shared reference. `frozen` indicates the absence of any `UnsafeCell`.

compiler/rustc_target/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ rustc_feature = { path = "../rustc_feature" }
1414
rustc_macros = { path = "../rustc_macros" }
1515
rustc_serialize = { path = "../rustc_serialize" }
1616
rustc_span = { path = "../rustc_span" }
17+
rustc_index = { path = "../rustc_index" }
1718

1819
[dependencies.object]
1920
version = "0.32.0"

compiler/rustc_target/src/abi/mod.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_data_structures::intern::Interned;
12
pub use Integer::*;
23
pub use Primitive::*;
34

@@ -18,6 +19,87 @@ impl ToJson for Endian {
1819
}
1920
}
2021

22+
rustc_index::newtype_index! {
23+
/// The *source-order* index of a field in a variant.
24+
///
25+
/// This is how most code after type checking refers to fields, rather than
26+
/// using names (as names have hygiene complications and more complex lookup).
27+
///
28+
/// Particularly for `repr(Rust)` types, this may not be the same as *layout* order.
29+
/// (It is for `repr(C)` `struct`s, however.)
30+
///
31+
/// For example, in the following types,
32+
/// ```rust
33+
/// # enum Never {}
34+
/// # #[repr(u16)]
35+
/// enum Demo1 {
36+
/// Variant0 { a: Never, b: i32 } = 100,
37+
/// Variant1 { c: u8, d: u64 } = 10,
38+
/// }
39+
/// struct Demo2 { e: u8, f: u16, g: u8 }
40+
/// ```
41+
/// `b` is `FieldIdx(1)` in `VariantIdx(0)`,
42+
/// `d` is `FieldIdx(1)` in `VariantIdx(1)`, and
43+
/// `f` is `FieldIdx(1)` in `VariantIdx(0)`.
44+
#[derive(HashStable_Generic)]
45+
pub struct FieldIdx {}
46+
}
47+
48+
#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable_Generic)]
49+
#[rustc_pass_by_value]
50+
pub struct Layout<'a>(pub Interned<'a, LayoutS<FieldIdx>>);
51+
52+
impl<'a> fmt::Debug for Layout<'a> {
53+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54+
// See comment on `<LayoutS as Debug>::fmt` above.
55+
self.0.0.fmt(f)
56+
}
57+
}
58+
59+
impl<'a> Layout<'a> {
60+
pub fn fields(self) -> &'a FieldsShape<FieldIdx> {
61+
&self.0.0.fields
62+
}
63+
64+
pub fn variants(self) -> &'a Variants<FieldIdx> {
65+
&self.0.0.variants
66+
}
67+
68+
pub fn abi(self) -> Abi {
69+
self.0.0.abi
70+
}
71+
72+
pub fn largest_niche(self) -> Option<Niche> {
73+
self.0.0.largest_niche
74+
}
75+
76+
pub fn align(self) -> AbiAndPrefAlign {
77+
self.0.0.align
78+
}
79+
80+
pub fn size(self) -> Size {
81+
self.0.0.size
82+
}
83+
84+
pub fn max_repr_align(self) -> Option<Align> {
85+
self.0.0.max_repr_align
86+
}
87+
88+
pub fn unadjusted_abi_align(self) -> Align {
89+
self.0.0.unadjusted_abi_align
90+
}
91+
92+
/// Whether the layout is from a type that implements [`std::marker::PointerLike`].
93+
///
94+
/// Currently, that means that the type is pointer-sized, pointer-aligned,
95+
/// and has a scalar ABI.
96+
pub fn is_pointer_like(self, data_layout: &TargetDataLayout) -> bool {
97+
self.size() == data_layout.pointer_size
98+
&& self.align().abi == data_layout.pointer_align.abi
99+
&& matches!(self.abi(), Abi::Scalar(..))
100+
}
101+
}
102+
21103
/// The layout of a type, alongside the type itself.
22104
/// Provides various type traversal APIs (e.g., recursing into fields).
23105
///

0 commit comments

Comments
 (0)