1
Fork 0

Make it possible to cast unsafe pointers with the 'as' operator

This commit is contained in:
Marijn Haverbeke 2011-11-02 12:15:50 +01:00
parent 0a20eed2db
commit 5b0c103b39
2 changed files with 15 additions and 6 deletions

View file

@ -3269,12 +3269,13 @@ fn trans_cast(cx: @block_ctxt, e: @ast::expr, id: ast::node_id,
check (type_has_static_size(ccx, t_out)); check (type_has_static_size(ccx, t_out));
let ll_t_out = type_of(ccx, e.span, t_out); let ll_t_out = type_of(ccx, e.span, t_out);
tag kind { native_; integral; float; other; } tag kind { pointer; integral; float; other; }
fn t_kind(tcx: ty::ctxt, t: ty::t) -> kind { fn t_kind(tcx: ty::ctxt, t: ty::t) -> kind {
ret if ty::type_is_fp(tcx, t) { ret if ty::type_is_fp(tcx, t) {
float float
} else if ty::type_is_native(tcx, t) { } else if ty::type_is_native(tcx, t) ||
native_ ty::type_is_unsafe_ptr(tcx, t) {
pointer
} else if ty::type_is_integral(tcx, t) { } else if ty::type_is_integral(tcx, t) {
integral integral
} else { other }; } else { other };
@ -3301,13 +3302,13 @@ fn trans_cast(cx: @block_ctxt, e: @ast::expr, id: ast::node_id,
FPToSI(e_res.bcx, e_res.val, ll_t_out) FPToSI(e_res.bcx, e_res.val, ll_t_out)
} else { FPToUI(e_res.bcx, e_res.val, ll_t_out) } } else { FPToUI(e_res.bcx, e_res.val, ll_t_out) }
} }
{in: integral., out: native_.} { {in: integral., out: pointer.} {
IntToPtr(e_res.bcx, e_res.val, ll_t_out) IntToPtr(e_res.bcx, e_res.val, ll_t_out)
} }
{in: native_., out: integral.} { {in: pointer., out: integral.} {
PtrToInt(e_res.bcx, e_res.val, ll_t_out) PtrToInt(e_res.bcx, e_res.val, ll_t_out)
} }
{in: native_., out: native_.} { {in: pointer., out: pointer.} {
PointerCast(e_res.bcx, e_res.val, ll_t_out) PointerCast(e_res.bcx, e_res.val, ll_t_out)
} }
_ { ccx.sess.bug("Translating unsupported cast.") } _ { ccx.sess.bug("Translating unsupported cast.") }

View file

@ -153,6 +153,7 @@ export type_is_bot;
export type_is_box; export type_is_box;
export type_is_boxed; export type_is_boxed;
export type_is_unique_box; export type_is_unique_box;
export type_is_unsafe_ptr;
export type_is_vec; export type_is_vec;
export type_is_fp; export type_is_fp;
export type_allows_implicit_copy; export type_allows_implicit_copy;
@ -885,6 +886,13 @@ pure fn type_is_unique_box(cx: ctxt, ty: t) -> bool {
} }
} }
pure fn type_is_unsafe_ptr(cx: ctxt, ty: t) -> bool {
alt struct(cx, ty) {
ty_ptr(_) { ret true; }
_ { ret false; }
}
}
pure fn type_is_vec(cx: ctxt, ty: t) -> bool { pure fn type_is_vec(cx: ctxt, ty: t) -> bool {
ret alt struct(cx, ty) { ret alt struct(cx, ty) {
ty_vec(_) { true } ty_vec(_) { true }