about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPatrick Walton <pcwalton@mimiga.net>2013-04-02 11:47:23 -0700
committerPatrick Walton <pcwalton@mimiga.net>2013-04-19 11:53:33 -0700
commit04df19c5ca78d94e01dacbfb99471509128c6f8a (patch)
tree751a1c3d348969509c5484c87437e89250006c28
parent3ffaaab9e9e3a2437fd9ed5b04cf3ba3695cc2d2 (diff)
downloadrust-04df19c5ca78d94e01dacbfb99471509128c6f8a.tar.gz
rust-04df19c5ca78d94e01dacbfb99471509128c6f8a.zip
librustc: Take primitive types out of the type hash table.
-rw-r--r--src/librustc/middle/ty.rs158
1 files changed, 133 insertions, 25 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<ast::node_id>,
 }
 
-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<ast::def_id>) -> 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<ast::def_id>) -> t {
     }
 }
 
-pub fn mk_nil(cx: ctxt) -> t { mk_t(cx, ty_nil) }
+#[inline(always)]
+pub fn mk_prim_t(cx: ctxt, primitive: &'static t_box_) -> t {
+    unsafe {
+        cast::transmute::<&'static t_box_, t>(primitive)
+    }
+}
+
+#[inline(always)]
+pub fn mk_nil(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_NIL) }
 
-pub fn mk_err(cx: ctxt) -> t { mk_t(cx, ty_err) }
+#[inline(always)]
+pub fn mk_err(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_ERR) }
 
-pub fn mk_bot(cx: ctxt) -> t { mk_t(cx, ty_bot) }
+#[inline(always)]
+pub fn mk_bot(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_BOT) }
 
-pub fn mk_bool(cx: ctxt) -> t { mk_t(cx, ty_bool) }
+#[inline(always)]
+pub fn mk_bool(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_BOOL) }
 
-pub fn mk_int(cx: ctxt) -> t { mk_t(cx, ty_int(ast::ty_i)) }
+#[inline(always)]
+pub fn mk_int(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_INT) }
 
-pub fn mk_i8(cx: ctxt) -> t { mk_t(cx, ty_int(ast::ty_i8)) }
+#[inline(always)]
+pub fn mk_i8(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_I8) }
 
-pub fn mk_i16(cx: ctxt) -> t { mk_t(cx, ty_int(ast::ty_i16)) }
+#[inline(always)]
+pub fn mk_i16(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_I16) }
 
-pub fn mk_i32(cx: ctxt) -> t { mk_t(cx, ty_int(ast::ty_i32)) }
+#[inline(always)]
+pub fn mk_i32(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_I32) }
 
-pub fn mk_i64(cx: ctxt) -> t { mk_t(cx, ty_int(ast::ty_i64)) }
+#[inline(always)]
+pub fn mk_i64(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_I64) }
 
-pub fn mk_float(cx: ctxt) -> t { mk_t(cx, ty_float(ast::ty_f)) }
+#[inline(always)]
+pub fn mk_float(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_FLOAT) }
 
-pub fn mk_uint(cx: ctxt) -> t { mk_t(cx, ty_uint(ast::ty_u)) }
+#[inline(always)]
+pub fn mk_f32(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_F32) }
 
-pub fn mk_u8(cx: ctxt) -> t { mk_t(cx, ty_uint(ast::ty_u8)) }
+#[inline(always)]
+pub fn mk_f64(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_F64) }
 
-pub fn mk_u16(cx: ctxt) -> t { mk_t(cx, ty_uint(ast::ty_u16)) }
+#[inline(always)]
+pub fn mk_uint(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_UINT) }
 
-pub fn mk_u32(cx: ctxt) -> t { mk_t(cx, ty_uint(ast::ty_u32)) }
+#[inline(always)]
+pub fn mk_u8(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_U8) }
 
-pub fn mk_u64(cx: ctxt) -> t { mk_t(cx, ty_uint(ast::ty_u64)) }
+#[inline(always)]
+pub fn mk_u16(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_U16) }
 
-pub fn mk_f32(cx: ctxt) -> t { mk_t(cx, ty_float(ast::ty_f32)) }
+#[inline(always)]
+pub fn mk_u32(cx: ctxt) -> t { mk_prim_t(cx, &primitives::TY_U32) }
 
-pub fn mk_f64(cx: ctxt) -> t { mk_t(cx, ty_float(ast::ty_f64)) }
+#[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 { mk_t(cx, ty_int(tm)) }
+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 { mk_t(cx, ty_uint(tm)) }
+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 {
-    mk_t(cx, ty_float(tm))
+    match tm {
+        ast::ty_f    => mk_float(cx),
+        ast::ty_f32  => mk_f32(cx),
+        ast::ty_f64  => mk_f64(cx),
+    }
 }
 
-pub fn mk_char(cx: ctxt) -> t { mk_t(cx, ty_int(ast::ty_char)) }
+#[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))