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

librustc: Implement a Pod kind for types that can be memcpy'd. #10924

Merged
merged 4 commits into from
Dec 17, 2013
Merged
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
6 changes: 6 additions & 0 deletions doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -3329,6 +3329,12 @@ The kinds are:
This kind includes scalars, owning pointers, owned closures, and
structural types containing only other owned types.
All `Send` types are `'static`.
`Pod`
: Types of this kind consist of "Plain Old Data"
which can be copied by simply moving bits.
All values of this kind can be implicitly copied.
This kind includes scalars and immutable references,
as well as structural types containing other `Pod` types.
`'static`
: Types of this kind do not contain any borrowed pointers;
this can be a useful guarantee for code
Expand Down
1 change: 1 addition & 0 deletions src/librustc/metadata/tyencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ fn enc_bounds(w: @mut MemWriter, cx: @ctxt, bs: &ty::ParamBounds) {
ty::BoundFreeze => mywrite!(w, "K"),
ty::BoundStatic => mywrite!(w, "O"),
ty::BoundSized => mywrite!(w, "Z"),
ty::BoundPod => mywrite!(w, "P"),
}
}

Expand Down
100 changes: 52 additions & 48 deletions src/librustc/middle/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use driver::session::Session;
use metadata::csearch::each_lang_item;
use metadata::cstore::iter_crate_data;
use middle::ty::{BuiltinBound, BoundFreeze, BoundSend, BoundSized};
use middle::ty::{BuiltinBound, BoundFreeze, BoundPod, BoundSend, BoundSized};
use syntax::ast;
use syntax::ast_util::local_def;
use syntax::attr::AttrMetaMethods;
Expand Down Expand Up @@ -83,6 +83,8 @@ impl LanguageItems {
Some(BoundSend)
} else if Some(id) == self.sized_trait() {
Some(BoundSized)
} else if Some(id) == self.pod_trait() {
Some(BoundPod)
} else {
None
}
Expand Down Expand Up @@ -206,56 +208,58 @@ pub fn collect_language_items(crate: &ast::Crate,
}

lets_do_this! {
There are 41 lang items.
There are 42 lang items.

// ID, Variant name, Name, Method name;
0, FreezeTraitLangItem, "freeze", freeze_trait;
1, SendTraitLangItem, "send", send_trait;
2, SizedTraitLangItem, "sized", sized_trait;

3, DropTraitLangItem, "drop", drop_trait;

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

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

19, StrEqFnLangItem, "str_eq", str_eq_fn;
20, UniqStrEqFnLangItem, "uniq_str_eq", uniq_str_eq_fn;
21, FailFnLangItem, "fail_", fail_fn;
22, FailBoundsCheckFnLangItem, "fail_bounds_check", fail_bounds_check_fn;
23, ExchangeMallocFnLangItem, "exchange_malloc", exchange_malloc_fn;
24, ClosureExchangeMallocFnLangItem, "closure_exchange_malloc", closure_exchange_malloc_fn;
25, ExchangeFreeFnLangItem, "exchange_free", exchange_free_fn;
26, MallocFnLangItem, "malloc", malloc_fn;
27, FreeFnLangItem, "free", free_fn;
28, BorrowAsImmFnLangItem, "borrow_as_imm", borrow_as_imm_fn;
29, BorrowAsMutFnLangItem, "borrow_as_mut", borrow_as_mut_fn;
30, ReturnToMutFnLangItem, "return_to_mut", return_to_mut_fn;
31, CheckNotBorrowedFnLangItem, "check_not_borrowed", check_not_borrowed_fn;
32, StrDupUniqFnLangItem, "strdup_uniq", strdup_uniq_fn;
33, RecordBorrowFnLangItem, "record_borrow", record_borrow_fn;
34, UnrecordBorrowFnLangItem, "unrecord_borrow", unrecord_borrow_fn;

35, StartFnLangItem, "start", start_fn;

36, TyDescStructLangItem, "ty_desc", ty_desc;
37, TyVisitorTraitLangItem, "ty_visitor", ty_visitor;
38, OpaqueStructLangItem, "opaque", opaque;

39, EventLoopFactoryLangItem, "event_loop_factory", event_loop_factory;

40, TypeIdLangItem, "type_id", type_id;
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, BorrowAsImmFnLangItem, "borrow_as_imm", borrow_as_imm_fn;
30, BorrowAsMutFnLangItem, "borrow_as_mut", borrow_as_mut_fn;
31, ReturnToMutFnLangItem, "return_to_mut", return_to_mut_fn;
32, CheckNotBorrowedFnLangItem, "check_not_borrowed", check_not_borrowed_fn;
33, StrDupUniqFnLangItem, "strdup_uniq", strdup_uniq_fn;
34, RecordBorrowFnLangItem, "record_borrow", record_borrow_fn;
35, UnrecordBorrowFnLangItem, "unrecord_borrow", unrecord_borrow_fn;

36, StartFnLangItem, "start", start_fn;

37, TyDescStructLangItem, "ty_desc", ty_desc;
38, TyVisitorTraitLangItem, "ty_visitor", ty_visitor;
39, OpaqueStructLangItem, "opaque", opaque;

40, EventLoopFactoryLangItem, "event_loop_factory", event_loop_factory;

41, TypeIdLangItem, "type_id", type_id;
}

45 changes: 36 additions & 9 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ pub enum BuiltinBound {
BoundSend,
BoundFreeze,
BoundSized,
BoundPod,
}

pub fn EmptyBuiltinBounds() -> BuiltinBounds {
Expand Down Expand Up @@ -1805,6 +1806,9 @@ def_type_content_sets!(
// Things that prevent values from being considered sized
Nonsized = 0b0000__00000000__0001,

// Things that make values considered not POD (same as `Moves`)
Nonpod = 0b0000__00001111__0000,

// Bits to set when a managed value is encountered
//
// [1] Do not set the bits TC::OwnsManaged or
Expand All @@ -1828,6 +1832,7 @@ impl TypeContents {
BoundFreeze => self.is_freezable(cx),
BoundSend => self.is_sendable(cx),
BoundSized => self.is_sized(cx),
BoundPod => self.is_pod(cx),
}
}

Expand Down Expand Up @@ -1859,6 +1864,10 @@ impl TypeContents {
!self.intersects(TC::Nonsized)
}

pub fn is_pod(&self, _: ctxt) -> bool {
!self.intersects(TC::Nonpod)
}

pub fn moves_by_default(&self, _: ctxt) -> bool {
self.intersects(TC::Moves)
}
Expand All @@ -1876,15 +1885,32 @@ impl TypeContents {
*self & (TC::OwnsAll | TC::ReachesAll))
}

pub fn other_pointer(&self, bits: TypeContents) -> TypeContents {
pub fn reference(&self, bits: TypeContents) -> TypeContents {
/*!
* Includes only those bits that still apply
* when indirected through a non-owning pointer (`&`, `@`)
* when indirected through a reference (`&`)
*/
bits | (
*self & TC::ReachesAll)
}

pub fn managed_pointer(&self) -> TypeContents {
/*!
* Includes only those bits that still apply
* when indirected through a managed pointer (`@`)
*/
TC::Managed | (
*self & TC::ReachesAll)
}

pub fn unsafe_pointer(&self) -> TypeContents {
/*!
* Includes only those bits that still apply
* when indirected through an unsafe pointer (`*`)
*/
*self & TC::ReachesAll
}

pub fn union<T>(v: &[T], f: |&T| -> TypeContents) -> TypeContents {
v.iter().fold(TC::None, |tc, t| tc | f(t))
}
Expand Down Expand Up @@ -1994,19 +2020,19 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {
}

ty_box(mt) => {
tc_mt(cx, mt, cache).other_pointer(TC::Managed)
tc_mt(cx, mt, cache).managed_pointer()
}

ty_trait(_, _, store, mutbl, bounds) => {
object_contents(cx, store, mutbl, bounds)
}

ty_ptr(ref mt) => {
tc_ty(cx, mt.ty, cache).other_pointer(TC::None)
tc_ty(cx, mt.ty, cache).unsafe_pointer()
}

ty_rptr(r, ref mt) => {
tc_ty(cx, mt.ty, cache).other_pointer(
tc_ty(cx, mt.ty, cache).reference(
borrowed_contents(r, mt.mutbl))
}

Expand All @@ -2019,11 +2045,11 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {
}

ty_evec(mt, vstore_box) => {
tc_mt(cx, mt, cache).other_pointer(TC::Managed)
tc_mt(cx, mt, cache).managed_pointer()
}

ty_evec(ref mt, vstore_slice(r)) => {
tc_ty(cx, mt.ty, cache).other_pointer(
tc_ty(cx, mt.ty, cache).reference(
borrowed_contents(r, mt.mutbl))
}

Expand Down Expand Up @@ -2193,10 +2219,10 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {
contents.owned_pointer()
}
BoxTraitStore => {
contents.other_pointer(TC::Managed)
contents.managed_pointer()
}
RegionTraitStore(r) => {
contents.other_pointer(borrowed_contents(r, mutbl))
contents.reference(borrowed_contents(r, mutbl))
}
}
}
Expand All @@ -2213,6 +2239,7 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {
BoundSend => TC::Nonsendable,
BoundFreeze => TC::Nonfreezable,
BoundSized => TC::Nonsized,
BoundPod => TC::Nonpod,
};
});
return tc;
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/util/ppaux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ impl Repr for ty::ParamBounds {
ty::BoundSend => ~"Send",
ty::BoundFreeze => ~"Freeze",
ty::BoundSized => ~"Sized",
ty::BoundPod => ~"Pod",
});
}
for t in self.trait_bounds.iter() {
Expand Down Expand Up @@ -926,6 +927,7 @@ impl UserString for ty::BuiltinBound {
ty::BoundSend => ~"Send",
ty::BoundFreeze => ~"Freeze",
ty::BoundSized => ~"Sized",
ty::BoundPod => ~"Pod",
}
}
}
Expand Down
Loading