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));
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 {
ret if ty::type_is_fp(tcx, t) {
float
} else if ty::type_is_native(tcx, t) {
native_
} else if ty::type_is_native(tcx, t) ||
ty::type_is_unsafe_ptr(tcx, t) {
pointer
} else if ty::type_is_integral(tcx, t) {
integral
} 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)
} 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)
}
{in: native_., out: integral.} {
{in: pointer., out: integral.} {
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)
}
_ { ccx.sess.bug("Translating unsupported cast.") }

View File

@ -153,6 +153,7 @@ export type_is_bot;
export type_is_box;
export type_is_boxed;
export type_is_unique_box;
export type_is_unsafe_ptr;
export type_is_vec;
export type_is_fp;
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 {
ret alt struct(cx, ty) {
ty_vec(_) { true }