Skip to content

Improve the documentation of primitive types #11409

New issue

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

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

Already on GitHub? # to your account

Closed
wants to merge 4 commits 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
1 change: 0 additions & 1 deletion src/libextra/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ Examples of string representations:
use std::str;
use std::vec;
use std::num::FromStrRadix;
use std::char::Char;
use std::container::Container;
use std::to_str::ToStr;
use std::rand;
Expand Down
5 changes: 5 additions & 0 deletions src/librustc/metadata/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ pub static tag_macro_registrar_fn: uint = 0x110;
pub static tag_exported_macros: uint = 0x111;
pub static tag_macro_def: uint = 0x112;

pub static tag_prim_dids: uint = 0x113;
pub static tag_prim_did: uint = 0x114;
pub static tag_prim_did_ty: uint = 0x115;
pub static tag_prim_did_did: uint = 0x116;

#[deriving(Clone)]
pub struct LinkMeta {
crateid: CrateId,
Expand Down
7 changes: 7 additions & 0 deletions src/librustc/metadata/csearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,10 @@ pub fn get_exported_macros(cstore: @cstore::CStore,
let cdata = cstore.get_crate_data(crate_num);
decoder::get_exported_macros(cdata)
}

pub fn prim_dids(cstore: @cstore::CStore,
crate_num: ast::CrateNum,
tcx: ty::ctxt) -> ~[(ty::t, ast::DefId)] {
let cdata = cstore.get_crate_data(crate_num);
decoder::prim_dids(cdata, tcx)
}
16 changes: 16 additions & 0 deletions src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1292,3 +1292,19 @@ pub fn get_exported_macros(cdata: Cmd) -> ~[@ast::Item] {
});
result
}

pub fn prim_dids(cdata: Cmd, tcx: ty::ctxt) -> ~[(ty::t, ast::DefId)] {
let dids = reader::get_doc(reader::Doc(cdata.data()), tag_prim_dids);
let mut result = ~[];
reader::tagged_docs(dids, tag_prim_did, |did_doc| {
let ty_doc = reader::get_doc(did_doc, tag_prim_did_ty);
let id_doc = reader::get_doc(did_doc, tag_prim_did_did);
let did = ast::DefId {
node: reader::doc_as_u32(id_doc),
crate: cdata.cnum,
};
result.push((doc_type(ty_doc, tcx, cdata), did));
true
});
return result;
}
40 changes: 40 additions & 0 deletions src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ struct Stats {
macro_registrar_fn_bytes: Cell<u64>,
macro_defs_bytes: Cell<u64>,
impl_bytes: Cell<u64>,
prim_did_bytes: Cell<u64>,
misc_bytes: Cell<u64>,
item_bytes: Cell<u64>,
index_bytes: Cell<u64>,
Expand Down Expand Up @@ -1455,6 +1456,21 @@ fn encode_info_for_items(ecx: &EncodeContext,
visit::walk_crate(&mut visitor, crate, ());
}

for (_, did) in ty::prim_dids(ecx.tcx).move_iter() {
if !ast_util::is_local(did) { continue }
{
let mut index = index.borrow_mut();
index.get().push(entry {
val: did.node as i64,
pos: ebml_w.writer.tell(),
});
}
ebml_w.start_tag(tag_items_data_item);
encode_def_id(ebml_w, did);
encode_inherent_implementations(ecx, ebml_w, did);
ebml_w.end_tag();
}

ebml_w.end_tag();
return /*bad*/(*index).get();
}
Expand Down Expand Up @@ -1818,6 +1834,23 @@ fn encode_misc_info(ecx: &EncodeContext,
ebml_w.end_tag();
}

fn encode_prim_dids(ecx: &EncodeContext, ebml_w: &mut writer::Encoder) {
ebml_w.start_tag(tag_prim_dids);
for (ty, did) in ty::prim_dids(ecx.tcx).move_iter() {
if !ast_util::is_local(did) { continue }
ebml_w.start_tag(tag_prim_did);

ebml_w.start_tag(tag_prim_did_ty);
encode_type(ecx, ebml_w, ty);
ebml_w.end_tag();

ebml_w.wr_tagged_u32(tag_prim_did_did, did.node);

ebml_w.end_tag();
}
ebml_w.end_tag();
}

fn encode_crate_dep(ecx: &EncodeContext,
ebml_w: &mut writer::Encoder,
dep: decoder::CrateDep) {
Expand Down Expand Up @@ -1865,6 +1898,7 @@ fn encode_metadata_inner(wr: &mut MemWriter, parms: EncodeParams, crate: &Crate)
macro_registrar_fn_bytes: Cell::new(0),
macro_defs_bytes: Cell::new(0),
impl_bytes: Cell::new(0),
prim_did_bytes: Cell::new(0),
misc_bytes: Cell::new(0),
item_bytes: Cell::new(0),
index_bytes: Cell::new(0),
Expand Down Expand Up @@ -1937,6 +1971,11 @@ fn encode_metadata_inner(wr: &mut MemWriter, parms: EncodeParams, crate: &Crate)
encode_impls(&ecx, crate, &mut ebml_w);
ecx.stats.impl_bytes.set(ebml_w.writer.tell() - i);

// Encode the def IDs of primitives (if we have any)
i = ebml_w.writer.tell();
encode_prim_dids(&ecx, &mut ebml_w);
ecx.stats.prim_did_bytes.set(ebml_w.writer.tell() - i);

// Encode miscellaneous info.
i = ebml_w.writer.tell();
encode_misc_info(&ecx, crate, &mut ebml_w);
Expand Down Expand Up @@ -1969,6 +2008,7 @@ fn encode_metadata_inner(wr: &mut MemWriter, parms: EncodeParams, crate: &Crate)
println!(" dep bytes: {}", ecx.stats.dep_bytes.get());
println!(" lang item bytes: {}", ecx.stats.lang_item_bytes.get());
println!(" native bytes: {}", ecx.stats.native_lib_bytes.get());
println!(" prim did bytes: {}", ecx.stats.prim_did_bytes.get());
println!("macro registrar bytes: {}", ecx.stats.macro_registrar_fn_bytes.get());
println!(" macro def bytes: {}", ecx.stats.macro_defs_bytes.get());
println!(" impl bytes: {}", ecx.stats.impl_bytes.get());
Expand Down
155 changes: 79 additions & 76 deletions src/librustc/middle/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,41 +33,28 @@ use std::hashmap::HashMap;
use std::iter::Enumerate;
use std::vec;


// Get the last "argument" (has to be done recursively to avoid phoney local ambiguity error)
macro_rules! last {
( $first:expr, $( $remainder:expr, )+ ) => ( last!( $( $remainder, )+ ) );
( $first:expr, ) => ( $first )
}

// The actual lang items defined come at the end of this file in one handy table.
// So you probably just want to nip down to the end.
macro_rules! lets_do_this {
// secondary rule to allow us to use `$num` as both an expression
// and a pattern.
(
$( $num:tt, $variant:ident, $name:expr, $method:ident; )*
) => {
lets_do_this!(count = 1 + last!($($num,)*),
$($num, $variant, $name, $method; )*)
};

(
count = $num_lang_items:expr, $( $num:pat, $variant:ident, $name:expr, $method:ident; )*
$( $variant:ident, $name:expr, $method:ident; )*
) => {

#[deriving(FromPrimitive)]
pub enum LangItem {
$($variant),*
}

pub struct LanguageItems {
items: [Option<ast::DefId>, ..$num_lang_items]
items: ~[Option<ast::DefId>],
}

impl LanguageItems {
pub fn new() -> LanguageItems {
fn foo(_: LangItem) -> Option<ast::DefId> { None }

LanguageItems {
items: [ None, ..$num_lang_items ]
items: ~[$(foo($variant)),*]
}
}

Expand All @@ -76,9 +63,10 @@ impl LanguageItems {
}

pub fn item_name(index: uint) -> &'static str {
match index {
$( $num => $name, )*
_ => "???"
let item: Option<LangItem> = FromPrimitive::from_uint(index);
match item {
$( Some($variant) => $name, )*
None => "???"
}
}

Expand Down Expand Up @@ -208,69 +196,84 @@ pub fn extract(attrs: &[ast::Attribute]) -> Option<@str> {
}

pub fn collect_language_items(crate: &ast::Crate,
session: Session)
-> LanguageItems {
session: Session) -> @LanguageItems {
let mut collector = LanguageItemCollector::new(session);
collector.collect(crate);
let LanguageItemCollector { items, .. } = collector;
session.abort_if_errors();
items
@items
}

// End of the macro
}
}

lets_do_this! {
// ID, Variant name, Name, Method name;
0, FreezeTraitLangItem, "freeze", freeze_trait;
1, SendTraitLangItem, "send", send_trait;
2, SizedTraitLangItem, "sized", sized_trait;
3, PodTraitLangItem, "pod", pod_trait;

4, DropTraitLangItem, "drop", drop_trait;

5, AddTraitLangItem, "add", add_trait;
6, SubTraitLangItem, "sub", sub_trait;
7, MulTraitLangItem, "mul", mul_trait;
8, DivTraitLangItem, "div", div_trait;
9, RemTraitLangItem, "rem", rem_trait;
10, NegTraitLangItem, "neg", neg_trait;
11, NotTraitLangItem, "not", not_trait;
12, BitXorTraitLangItem, "bitxor", bitxor_trait;
13, BitAndTraitLangItem, "bitand", bitand_trait;
14, BitOrTraitLangItem, "bitor", bitor_trait;
15, ShlTraitLangItem, "shl", shl_trait;
16, ShrTraitLangItem, "shr", shr_trait;
17, IndexTraitLangItem, "index", index_trait;

18, EqTraitLangItem, "eq", eq_trait;
19, OrdTraitLangItem, "ord", ord_trait;

20, StrEqFnLangItem, "str_eq", str_eq_fn;
21, UniqStrEqFnLangItem, "uniq_str_eq", uniq_str_eq_fn;
22, FailFnLangItem, "fail_", fail_fn;
23, FailBoundsCheckFnLangItem, "fail_bounds_check", fail_bounds_check_fn;
24, ExchangeMallocFnLangItem, "exchange_malloc", exchange_malloc_fn;
25, ClosureExchangeMallocFnLangItem, "closure_exchange_malloc", closure_exchange_malloc_fn;
26, ExchangeFreeFnLangItem, "exchange_free", exchange_free_fn;
27, MallocFnLangItem, "malloc", malloc_fn;
28, FreeFnLangItem, "free", free_fn;
29, StrDupUniqFnLangItem, "strdup_uniq", strdup_uniq_fn;

30, StartFnLangItem, "start", start_fn;

31, TyDescStructLangItem, "ty_desc", ty_desc;
32, TyVisitorTraitLangItem, "ty_visitor", ty_visitor;
33, OpaqueStructLangItem, "opaque", opaque;

34, EventLoopFactoryLangItem, "event_loop_factory", event_loop_factory;

35, TypeIdLangItem, "type_id", type_id;

36, EhPersonalityLangItem, "eh_personality", eh_personality_fn;

37, ManagedHeapLangItem, "managed_heap", managed_heap;
38, ExchangeHeapLangItem, "exchange_heap", exchange_heap;
39, GcLangItem, "gc", gc;
// Variant name, Name, Method name;
FreezeTraitLangItem, "freeze", freeze_trait;
SendTraitLangItem, "send", send_trait;
SizedTraitLangItem, "sized", sized_trait;
PodTraitLangItem, "pod", pod_trait;

DropTraitLangItem, "drop", drop_trait;

AddTraitLangItem, "add", add_trait;
SubTraitLangItem, "sub", sub_trait;
MulTraitLangItem, "mul", mul_trait;
DivTraitLangItem, "div", div_trait;
RemTraitLangItem, "rem", rem_trait;
NegTraitLangItem, "neg", neg_trait;
NotTraitLangItem, "not", not_trait;
BitXorTraitLangItem, "bitxor", bitxor_trait;
BitAndTraitLangItem, "bitand", bitand_trait;
BitOrTraitLangItem, "bitor", bitor_trait;
ShlTraitLangItem, "shl", shl_trait;
ShrTraitLangItem, "shr", shr_trait;
IndexTraitLangItem, "index", index_trait;

EqTraitLangItem, "eq", eq_trait;
OrdTraitLangItem, "ord", ord_trait;

StrEqFnLangItem, "str_eq", str_eq_fn;
UniqStrEqFnLangItem, "uniq_str_eq", uniq_str_eq_fn;
FailFnLangItem, "fail_", fail_fn;
FailBoundsCheckFnLangItem, "fail_bounds_check", fail_bounds_check_fn;
ExchangeMallocFnLangItem, "exchange_malloc", exchange_malloc_fn;
ClosureExchangeMallocFnLangItem, "closure_exchange_malloc", closure_exchange_malloc_fn;
ExchangeFreeFnLangItem, "exchange_free", exchange_free_fn;
MallocFnLangItem, "malloc", malloc_fn;
FreeFnLangItem, "free", free_fn;
StrDupUniqFnLangItem, "strdup_uniq", strdup_uniq_fn;

StartFnLangItem, "start", start_fn;

TyDescStructLangItem, "ty_desc", ty_desc;
TyVisitorTraitLangItem, "ty_visitor", ty_visitor;
OpaqueStructLangItem, "opaque", opaque;

EventLoopFactoryLangItem, "event_loop_factory", event_loop_factory;

TypeIdLangItem, "type_id", type_id;

ManagedHeapLangItem, "managed_heap", managed_heap;
ExchangeHeapLangItem, "exchange_heap", exchange_heap;
GcLangItem, "gc", gc;

EhPersonalityLangItem, "eh_personality", eh_personality_fn;

CharImplLangItem, "char_impl", char_impl;
IntImplLangItem, "int_impl", int_impl;
I8ImplLangItem, "i8_impl", i8_impl;
I16ImplLangItem, "i16_impl", i16_impl;
I32ImplLangItem, "i32_impl", i32_impl;
I64ImplLangItem, "i64_impl", i64_impl;
UintImplLangItem, "uint_impl", uint_impl;
U8ImplLangItem, "u8_impl", u8_impl;
U16ImplLangItem, "u16_impl", u16_impl;
U32ImplLangItem, "u32_impl", u32_impl;
U64ImplLangItem, "u64_impl", u64_impl;
BoolImplLangItem, "bool_impl", bool_impl;
NilImplLangItem, "nil_impl", nil_impl;
F32ImplLangItem, "f32_impl", f32_impl;
F64ImplLangItem, "f64_impl", f64_impl;
}
6 changes: 3 additions & 3 deletions src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ fn namespace_error_to_str(ns: NamespaceError) -> &'static str {
}

fn Resolver(session: Session,
lang_items: LanguageItems,
lang_items: @LanguageItems,
crate_span: Span) -> Resolver {
let graph_root = @NameBindings();

Expand Down Expand Up @@ -837,7 +837,7 @@ fn Resolver(session: Session,
/// The main resolver class.
struct Resolver {
session: @Session,
lang_items: LanguageItems,
lang_items: @LanguageItems,

intr: @IdentInterner,

Expand Down Expand Up @@ -5656,7 +5656,7 @@ pub struct CrateMap {

/// Entry point to crate resolution.
pub fn resolve_crate(session: Session,
lang_items: LanguageItems,
lang_items: @LanguageItems,
crate: &Crate)
-> CrateMap {
let mut resolver = Resolver(session, lang_items, crate.span);
Expand Down
Loading