Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add num_variants intrinsic for enums and safe wrapper in std::reflect. #13860

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/librustc/middle/trans/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,17 @@ pub fn trans_intrinsic(ccx: &CrateContext,
let lltp_ty = type_of::type_of(ccx, tp_ty);
Ret(bcx, C_uint(ccx, machine::llalign_of_pref(ccx, lltp_ty) as uint));
}
"num_variants" => {
let tp_ty = *substs.tys.get(0);
let ref sty = ty::get(tp_ty).sty;
let num_variants = match *sty {
ty::ty_enum(did, _) => {
ty::enum_variants(ccx.tcx(), did).len()
}
_ => 0
};
Ret(bcx, C_uint(ccx, num_variants));
}
"get_tydesc" => {
let tp_ty = *substs.tys.get(0);
let static_ti = get_tydesc(ccx, tp_ty);
Expand Down
1 change: 1 addition & 0 deletions src/librustc/middle/typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4139,6 +4139,7 @@ pub fn check_intrinsic_type(ccx: &CrateCtxt, it: &ast::ForeignItem) {
}
"needs_drop" => (1u, Vec::new(), ty::mk_bool()),
"owns_managed" => (1u, Vec::new(), ty::mk_bool()),
"num_variants" => (1u, Vec::new(), ty::mk_uint()),

"get_tydesc" => {
let tydesc_ty = match ty::get_tydesc_ty(ccx.tcx) {
Expand Down
4 changes: 4 additions & 0 deletions src/libstd/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ extern "rust-intrinsic" {
pub fn min_align_of<T>() -> uint;
pub fn pref_align_of<T>() -> uint;

/// Gets the number of variants in an enum
#[cfg(not(stage0))]
pub fn num_variants<T>() -> uint;

/// Get a static pointer to a type descriptor.
pub fn get_tydesc<T>() -> *TyDesc;

Expand Down
8 changes: 8 additions & 0 deletions src/libstd/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ pub fn align(size: uint, align: uint) -> uint {
((size + align) - 1u) & !(align - 1u)
}

/// Get the number of variants in an enum
#[cfg(not(stage0))]
#[inline]
pub fn num_variants<T>() -> uint {
use intrinsics::num_variants;
unsafe { num_variants::<T>() }
}

/// Adaptor to wrap around visitors implementing MovePtr.
pub struct MovePtrAdaptor<V> {
inner: V
Expand Down