Skip to content

Commit

Permalink
Move FieldIdx and Layout to rustc_target
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Oct 2, 2023
1 parent b47ad3b commit f14b7c9
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 81 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4473,6 +4473,7 @@ dependencies = [
"rustc_data_structures",
"rustc_feature",
"rustc_fs_util",
"rustc_index",
"rustc_macros",
"rustc_serialize",
"rustc_span",
Expand Down
81 changes: 0 additions & 81 deletions compiler/rustc_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1105,32 +1105,6 @@ impl Scalar {
}
}

rustc_index::newtype_index! {
/// The *source-order* index of a field in a variant.
///
/// This is how most code after type checking refers to fields, rather than
/// using names (as names have hygiene complications and more complex lookup).
///
/// Particularly for `repr(Rust)` types, this may not be the same as *layout* order.
/// (It is for `repr(C)` `struct`s, however.)
///
/// For example, in the following types,
/// ```rust
/// # enum Never {}
/// # #[repr(u16)]
/// enum Demo1 {
/// Variant0 { a: Never, b: i32 } = 100,
/// Variant1 { c: u8, d: u64 } = 10,
/// }
/// struct Demo2 { e: u8, f: u16, g: u8 }
/// ```
/// `b` is `FieldIdx(1)` in `VariantIdx(0)`,
/// `d` is `FieldIdx(1)` in `VariantIdx(1)`, and
/// `f` is `FieldIdx(1)` in `VariantIdx(0)`.
#[derive(HashStable_Generic)]
pub struct FieldIdx {}
}

/// Describes how the fields of a type are located in memory.
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]
Expand Down Expand Up @@ -1624,61 +1598,6 @@ where
}
}

#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable_Generic)]
#[rustc_pass_by_value]
pub struct Layout<'a>(pub Interned<'a, LayoutS<FieldIdx>>);

impl<'a> fmt::Debug for Layout<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// See comment on `<LayoutS as Debug>::fmt` above.
self.0.0.fmt(f)
}
}

impl<'a> Layout<'a> {
pub fn fields(self) -> &'a FieldsShape<FieldIdx> {
&self.0.0.fields
}

pub fn variants(self) -> &'a Variants<FieldIdx> {
&self.0.0.variants
}

pub fn abi(self) -> Abi {
self.0.0.abi
}

pub fn largest_niche(self) -> Option<Niche> {
self.0.0.largest_niche
}

pub fn align(self) -> AbiAndPrefAlign {
self.0.0.align
}

pub fn size(self) -> Size {
self.0.0.size
}

pub fn max_repr_align(self) -> Option<Align> {
self.0.0.max_repr_align
}

pub fn unadjusted_abi_align(self) -> Align {
self.0.0.unadjusted_abi_align
}

/// Whether the layout is from a type that implements [`std::marker::PointerLike`].
///
/// Currently, that means that the type is pointer-sized, pointer-aligned,
/// and has a scalar ABI.
pub fn is_pointer_like(self, data_layout: &TargetDataLayout) -> bool {
self.size() == data_layout.pointer_size
&& self.align().abi == data_layout.pointer_align.abi
&& matches!(self.abi(), Abi::Scalar(..))
}
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum PointerKind {
/// Shared reference. `frozen` indicates the absence of any `UnsafeCell`.
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_target/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ rustc_feature = { path = "../rustc_feature" }
rustc_macros = { path = "../rustc_macros" }
rustc_serialize = { path = "../rustc_serialize" }
rustc_span = { path = "../rustc_span" }
rustc_index = { path = "../rustc_index" }

[dependencies.object]
version = "0.32.0"
Expand Down
82 changes: 82 additions & 0 deletions compiler/rustc_target/src/abi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use rustc_data_structures::intern::Interned;
pub use Integer::*;
pub use Primitive::*;

Expand All @@ -18,6 +19,87 @@ impl ToJson for Endian {
}
}

rustc_index::newtype_index! {
/// The *source-order* index of a field in a variant.
///
/// This is how most code after type checking refers to fields, rather than
/// using names (as names have hygiene complications and more complex lookup).
///
/// Particularly for `repr(Rust)` types, this may not be the same as *layout* order.
/// (It is for `repr(C)` `struct`s, however.)
///
/// For example, in the following types,
/// ```rust
/// # enum Never {}
/// # #[repr(u16)]
/// enum Demo1 {
/// Variant0 { a: Never, b: i32 } = 100,
/// Variant1 { c: u8, d: u64 } = 10,
/// }
/// struct Demo2 { e: u8, f: u16, g: u8 }
/// ```
/// `b` is `FieldIdx(1)` in `VariantIdx(0)`,
/// `d` is `FieldIdx(1)` in `VariantIdx(1)`, and
/// `f` is `FieldIdx(1)` in `VariantIdx(0)`.
#[derive(HashStable_Generic)]
pub struct FieldIdx {}
}

#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable_Generic)]
#[rustc_pass_by_value]
pub struct Layout<'a>(pub Interned<'a, LayoutS<FieldIdx>>);

impl<'a> fmt::Debug for Layout<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// See comment on `<LayoutS as Debug>::fmt` above.
self.0.0.fmt(f)
}
}

impl<'a> Layout<'a> {
pub fn fields(self) -> &'a FieldsShape<FieldIdx> {
&self.0.0.fields
}

pub fn variants(self) -> &'a Variants<FieldIdx> {
&self.0.0.variants
}

pub fn abi(self) -> Abi {
self.0.0.abi
}

pub fn largest_niche(self) -> Option<Niche> {
self.0.0.largest_niche
}

pub fn align(self) -> AbiAndPrefAlign {
self.0.0.align
}

pub fn size(self) -> Size {
self.0.0.size
}

pub fn max_repr_align(self) -> Option<Align> {
self.0.0.max_repr_align
}

pub fn unadjusted_abi_align(self) -> Align {
self.0.0.unadjusted_abi_align
}

/// Whether the layout is from a type that implements [`std::marker::PointerLike`].
///
/// Currently, that means that the type is pointer-sized, pointer-aligned,
/// and has a scalar ABI.
pub fn is_pointer_like(self, data_layout: &TargetDataLayout) -> bool {
self.size() == data_layout.pointer_size
&& self.align().abi == data_layout.pointer_align.abi
&& matches!(self.abi(), Abi::Scalar(..))
}
}

/// The layout of a type, alongside the type itself.
/// Provides various type traversal APIs (e.g., recursing into fields).
///
Expand Down

0 comments on commit f14b7c9

Please sign in to comment.