Skip to content

Commit f14b7c9

Browse files
committed
Move FieldIdx and Layout to rustc_target
1 parent b47ad3b commit f14b7c9

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
@@ -4473,6 +4473,7 @@ dependencies = [
44734473
"rustc_data_structures",
44744474
"rustc_feature",
44754475
"rustc_fs_util",
4476+
"rustc_index",
44764477
"rustc_macros",
44774478
"rustc_serialize",
44784479
"rustc_span",

compiler/rustc_abi/src/lib.rs

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

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

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