1
Fork 0

Refactor float Primitives to a separate Float type

This commit is contained in:
beetrees 2024-05-06 13:27:40 +01:00
parent 8cef37dbb6
commit 3769fddba2
No known key found for this signature in database
GPG key ID: 8791BD754191EBD6
23 changed files with 156 additions and 96 deletions

View file

@ -114,16 +114,35 @@ impl Integer {
}
}
#[extension(pub trait FloatExt)]
impl Float {
#[inline]
fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
match *self {
F16 => tcx.types.f16,
F32 => tcx.types.f32,
F64 => tcx.types.f64,
F128 => tcx.types.f128,
}
}
fn from_float_ty(fty: ty::FloatTy) -> Self {
match fty {
ty::FloatTy::F16 => F16,
ty::FloatTy::F32 => F32,
ty::FloatTy::F64 => F64,
ty::FloatTy::F128 => F128,
}
}
}
#[extension(pub trait PrimitiveExt)]
impl Primitive {
#[inline]
fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
match *self {
Int(i, signed) => i.to_ty(tcx, signed),
F16 => tcx.types.f16,
F32 => tcx.types.f32,
F64 => tcx.types.f64,
F128 => tcx.types.f128,
Float(f) => f.to_ty(tcx),
// FIXME(erikdesjardins): handle non-default addrspace ptr sizes
Pointer(_) => Ty::new_mut_ptr(tcx, tcx.types.unit),
}
@ -140,7 +159,7 @@ impl Primitive {
let signed = false;
tcx.data_layout().ptr_sized_integer().to_ty(tcx, signed)
}
F16 | F32 | F64 | F128 => bug!("floats do not have an int type"),
Float(_) => bug!("floats do not have an int type"),
}
}
}

View file

@ -2,7 +2,7 @@
use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use crate::query::{IntoQueryParam, Providers};
use crate::ty::layout::IntegerExt;
use crate::ty::layout::{FloatExt, IntegerExt};
use crate::ty::{
self, FallibleTypeFolder, ToPredicate, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
TypeVisitableExt,
@ -20,7 +20,7 @@ use rustc_index::bit_set::GrowableBitSet;
use rustc_macros::{extension, HashStable, TyDecodable, TyEncodable};
use rustc_session::Limit;
use rustc_span::sym;
use rustc_target::abi::{Integer, IntegerType, Primitive, Size};
use rustc_target::abi::{Float, Integer, IntegerType, Size};
use rustc_target::spec::abi::Abi;
use smallvec::{smallvec, SmallVec};
use std::{fmt, iter};
@ -1145,8 +1145,7 @@ impl<'tcx> Ty<'tcx> {
ty::Char => Size::from_bytes(4),
ty::Int(ity) => Integer::from_int_ty(&tcx, ity).size(),
ty::Uint(uty) => Integer::from_uint_ty(&tcx, uty).size(),
ty::Float(ty::FloatTy::F32) => Primitive::F32.size(&tcx),
ty::Float(ty::FloatTy::F64) => Primitive::F64.size(&tcx),
ty::Float(fty) => Float::from_float_ty(fty).size(),
_ => bug!("non primitive type"),
}
}