From 04df19c5ca78d94e01dacbfb99471509128c6f8a Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Tue, 2 Apr 2013 11:47:23 -0700 Subject: [PATCH] librustc: Take primitive types out of the type hash table. --- src/librustc/middle/ty.rs | 198 +++++++++++++++++++++++++++++--------- 1 file changed, 153 insertions(+), 45 deletions(-) diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index ef0b2070a0e..72c2cfedd98 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -307,7 +307,7 @@ struct ctxt_ { used_unsafe: @mut HashSet, } -enum tbox_flag { +pub enum tbox_flag { has_params = 1, has_self = 2, needs_infer = 4, @@ -320,9 +320,9 @@ enum tbox_flag { needs_subst = 1 | 2 | 8 } -type t_box = &'static t_box_; +pub type t_box = &'static t_box_; -struct t_box_ { +pub struct t_box_ { sty: sty, id: uint, flags: uint, @@ -513,6 +513,57 @@ pub struct substs { tps: ~[t] } +mod primitives { + use super::{sty, t_box_}; + + use core::option::None; + use syntax::ast; + + macro_rules! def_prim_ty( + ($name:ident, $sty:expr, $id:expr) => ( + pub static $name: t_box_ = t_box_ { + sty: $sty, + id: $id, + flags: 0, + o_def_id: None, + }; + ) + ) + + def_prim_ty!(TY_NIL, super::ty_nil, 0) + def_prim_ty!(TY_BOOL, super::ty_bool, 1) + def_prim_ty!(TY_INT, super::ty_int(ast::ty_i), 2) + def_prim_ty!(TY_CHAR, super::ty_int(ast::ty_char), 3) + def_prim_ty!(TY_I8, super::ty_int(ast::ty_i8), 4) + def_prim_ty!(TY_I16, super::ty_int(ast::ty_i16), 5) + def_prim_ty!(TY_I32, super::ty_int(ast::ty_i32), 6) + def_prim_ty!(TY_I64, super::ty_int(ast::ty_i64), 7) + def_prim_ty!(TY_UINT, super::ty_uint(ast::ty_u), 8) + def_prim_ty!(TY_U8, super::ty_uint(ast::ty_u8), 9) + def_prim_ty!(TY_U16, super::ty_uint(ast::ty_u16), 10) + def_prim_ty!(TY_U32, super::ty_uint(ast::ty_u32), 11) + def_prim_ty!(TY_U64, super::ty_uint(ast::ty_u64), 12) + def_prim_ty!(TY_FLOAT, super::ty_float(ast::ty_f), 13) + def_prim_ty!(TY_F32, super::ty_float(ast::ty_f32), 14) + def_prim_ty!(TY_F64, super::ty_float(ast::ty_f64), 15) + + pub static TY_BOT: t_box_ = t_box_ { + sty: super::ty_bot, + id: 16, + flags: super::has_ty_bot as uint, + o_def_id: None, + }; + + pub static TY_ERR: t_box_ = t_box_ { + sty: super::ty_err, + id: 17, + flags: super::has_ty_err as uint, + o_def_id: None, + }; + + pub static LAST_PRIMITIVE_ID: uint = 18; +} + // NB: If you change this, you'll probably want to change the corresponding // AST structure in libsyntax/ast.rs as well. #[deriving(Eq)] @@ -852,7 +903,7 @@ pub fn mk_ctxt(s: session::Session, @ctxt_ { diag: s.diagnostic(), interner: @mut HashMap::new(), - next_id: @mut 0, + next_id: @mut primitives::LAST_PRIMITIVE_ID, vecs_implicitly_copyable: vecs_implicitly_copyable, legacy_modes: legacy_modes, cstore: s.cstore, @@ -901,6 +952,17 @@ fn mk_t(cx: ctxt, +st: sty) -> t { mk_t_with_id(cx, st, None) } // Interns a type/name combination, stores the resulting box in cx.interner, // and returns the box as cast to an unsafe ptr (see comments for t above). fn mk_t_with_id(cx: ctxt, +st: sty, o_def_id: Option) -> t { + // Check for primitive types. + match st { + ty_nil => return mk_nil(cx), + ty_err => return mk_err(cx), + ty_bool => return mk_bool(cx), + ty_int(i) => return mk_mach_int(cx, i), + ty_uint(u) => return mk_mach_uint(cx, u), + ty_float(f) => return mk_mach_float(cx, f), + _ => {} + }; + let key = intern_key { sty: to_unsafe_ptr(&st), o_def_id: o_def_id }; match cx.interner.find(&key) { Some(t) => unsafe { return cast::transmute(&t.sty); }, @@ -996,49 +1058,95 @@ fn mk_t_with_id(cx: ctxt, +st: sty, o_def_id: Option) -> t { } } -pub fn mk_nil(cx: ctxt) -> t { mk_t(cx, ty_nil) } - -pub fn mk_err(cx: ctxt) -> t { mk_t(cx, ty_err) } - -pub fn mk_bot(cx: ctxt) -> t { mk_t(cx, ty_bot) } - -pub fn mk_bool(cx: ctxt) -> t { mk_t(cx, ty_bool) } - -pub fn mk_int(cx: ctxt) -> t { mk_t(cx, ty_int(ast::ty_i)) } - -pub fn mk_i8(cx: ctxt) -> t { mk_t(cx, ty_int(ast::ty_i8)) } - -pub fn mk_i16(cx: ctxt) -> t { mk_t(cx, ty_int(ast::ty_i16)) } - -pub fn mk_i32(cx: ctxt) -> t { mk_t(cx, ty_int(ast::ty_i32)) } - -pub fn mk_i64(cx: ctxt) -> t { mk_t(cx, ty_int(ast::ty_i64)) } - -pub fn mk_float(cx: ctxt) -> t { mk_t(cx, ty_float(ast::ty_f)) } - -pub fn mk_uint(cx: ctxt) -> t { mk_t(cx, ty_uint(ast::ty_u)) } - -pub fn mk_u8(cx: ctxt) -> t { mk_t(cx, ty_uint(ast::ty_u8)) } - -pub fn mk_u16(cx: ctxt) -> t { mk_t(cx, ty_uint(ast::ty_u16)) } - -pub fn mk_u32(cx: ctxt) -> t { mk_t(cx, ty_uint(ast::ty_u32)) } - -pub fn mk_u64(cx: ctxt) -> t { mk_t(cx, ty_uint(ast::ty_u64)) } - -pub fn mk_f32(cx: ctxt) -> t { mk_t(cx, ty_float(ast::ty_f32)) } - -pub fn mk_f64(cx: ctxt) -> t { mk_t(cx, ty_float(ast::ty_f64)) } - -pub fn mk_mach_int(cx: ctxt, tm: ast::int_ty) -> t { mk_t(cx, ty_int(tm)) } - -pub fn mk_mach_uint(cx: ctxt, tm: ast::uint_ty) -> t { mk_t(cx, ty_uint(tm)) } - -pub fn mk_mach_float(cx: ctxt, tm: ast::float_ty) -> t { - mk_t(cx, ty_float(tm)) +#[inline(always)] +pub fn mk_prim_t(cx: ctxt, primitive: &'static t_box_) -> t { + unsafe { + cast::transmute::<&'static t_box_, t>(primitive) + } } -pub fn mk_char(cx: ctxt) -> t { mk_t(cx, ty_int(ast::ty_char)) } +#[inline(always)] +pub fn mk_nil(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_NIL) } + +#[inline(always)] +pub fn mk_err(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_ERR) } + +#[inline(always)] +pub fn mk_bot(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_BOT) } + +#[inline(always)] +pub fn mk_bool(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_BOOL) } + +#[inline(always)] +pub fn mk_int(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_INT) } + +#[inline(always)] +pub fn mk_i8(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_I8) } + +#[inline(always)] +pub fn mk_i16(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_I16) } + +#[inline(always)] +pub fn mk_i32(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_I32) } + +#[inline(always)] +pub fn mk_i64(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_I64) } + +#[inline(always)] +pub fn mk_float(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_FLOAT) } + +#[inline(always)] +pub fn mk_f32(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_F32) } + +#[inline(always)] +pub fn mk_f64(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_F64) } + +#[inline(always)] +pub fn mk_uint(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_UINT) } + +#[inline(always)] +pub fn mk_u8(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_U8) } + +#[inline(always)] +pub fn mk_u16(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_U16) } + +#[inline(always)] +pub fn mk_u32(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_U32) } + +#[inline(always)] +pub fn mk_u64(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_U64) } + +pub fn mk_mach_int(cx: ctxt, tm: ast::int_ty) -> t { + match tm { + ast::ty_i => mk_int(cx), + ast::ty_char => mk_char(cx), + ast::ty_i8 => mk_i8(cx), + ast::ty_i16 => mk_i16(cx), + ast::ty_i32 => mk_i32(cx), + ast::ty_i64 => mk_i64(cx), + } +} + +pub fn mk_mach_uint(cx: ctxt, tm: ast::uint_ty) -> t { + match tm { + ast::ty_u => mk_uint(cx), + ast::ty_u8 => mk_u8(cx), + ast::ty_u16 => mk_u16(cx), + ast::ty_u32 => mk_u32(cx), + ast::ty_u64 => mk_u64(cx), + } +} + +pub fn mk_mach_float(cx: ctxt, tm: ast::float_ty) -> t { + match tm { + ast::ty_f => mk_float(cx), + ast::ty_f32 => mk_f32(cx), + ast::ty_f64 => mk_f64(cx), + } +} + +#[inline(always)] +pub fn mk_char(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_CHAR) } pub fn mk_estr(cx: ctxt, t: vstore) -> t { mk_t(cx, ty_estr(t))