Skip to content

Commit 0206973

Browse files
Ariel Ben-Yehudaarielb1
authored andcommitted
---
yaml --- r: 207311 b: refs/heads/master c: 32fe2e3 h: refs/heads/master i: 207309: 6d9c1b2 207307: 68eebf3 207303: b6328c8 207295: 67964d6 v: v3
1 parent 4b2b28a commit 0206973

File tree

15 files changed

+124
-104
lines changed

15 files changed

+124
-104
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: de4b0e996192d6784e0a65c66d095a6198894c47
2+
refs/heads/master: 32fe2e3ad452128d17ab1ce15f2c77b8cb42a3ea
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 857ef6e272e5634cb9f3e6ee50eb6bc2a2e71651
55
refs/heads/try: 7b4ef47b7805a402d756fb8157101f64880a522f

trunk/src/librustc/middle/cast.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use middle::ty::{self, Ty};
1515

1616
use syntax::ast;
1717

18-
18+
/// Types that are represented as ints.
1919
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
2020
pub enum IntTy {
2121
U(ast::UintTy),
@@ -28,10 +28,16 @@ pub enum IntTy {
2828
// Valid types for the result of a non-coercion cast
2929
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
3030
pub enum CastTy<'tcx> {
31+
/// Various types that are represented as ints and handled mostly
32+
/// in the same way, merged for easier matching.
3133
Int(IntTy),
34+
/// Floating-Point types
3235
Float,
33-
FPtr,
36+
/// Function Pointers
37+
FnPtr,
38+
/// Raw pointers
3439
Ptr(&'tcx ty::mt<'tcx>),
40+
/// References
3541
RPtr(&'tcx ty::mt<'tcx>),
3642
}
3743

@@ -47,13 +53,13 @@ pub enum CastKind {
4753
PrimIntCast,
4854
U8CharCast,
4955
ArrayPtrCast,
50-
FPtrPtrCast,
51-
FPtrAddrCast
56+
FnPtrPtrCast,
57+
FnPtrAddrCast
5258
}
5359

5460
impl<'tcx> CastTy<'tcx> {
55-
pub fn recognize(tcx: &ty::ctxt<'tcx>, t: Ty<'tcx>)
56-
-> Option<CastTy<'tcx>> {
61+
pub fn from_ty(tcx: &ty::ctxt<'tcx>, t: Ty<'tcx>)
62+
-> Option<CastTy<'tcx>> {
5763
match t.sty {
5864
ty::ty_bool => Some(CastTy::Int(IntTy::Bool)),
5965
ty::ty_char => Some(CastTy::Int(IntTy::Char)),
@@ -64,7 +70,7 @@ impl<'tcx> CastTy<'tcx> {
6470
tcx, t) => Some(CastTy::Int(IntTy::CEnum)),
6571
ty::ty_ptr(ref mt) => Some(CastTy::Ptr(mt)),
6672
ty::ty_rptr(_, ref mt) => Some(CastTy::RPtr(mt)),
67-
ty::ty_bare_fn(..) => Some(CastTy::FPtr),
73+
ty::ty_bare_fn(..) => Some(CastTy::FnPtr),
6874
_ => None,
6975
}
7076
}

trunk/src/librustc/middle/check_const.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,11 +443,11 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
443443
debug!("Checking const cast(id={})", from.id);
444444
match v.tcx.cast_kinds.borrow().get(&from.id) {
445445
None => v.tcx.sess.span_bug(e.span, "no kind for cast"),
446-
Some(&CastKind::PtrAddrCast) | Some(&CastKind::FPtrAddrCast) => {
446+
Some(&CastKind::PtrAddrCast) | Some(&CastKind::FnPtrAddrCast) => {
447447
v.add_qualif(ConstQualif::NOT_CONST);
448448
if v.mode != Mode::Var {
449449
span_err!(v.tcx.sess, e.span, E0018,
450-
"can not cast a pointer to an integer in {}s", v.msg());
450+
"can't cast a pointer to an integer in {}s", v.msg());
451451
}
452452
}
453453
_ => {}

trunk/src/librustc_trans/trans/consts.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -617,41 +617,41 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
617617
}
618618
}
619619
ast::ExprCast(ref base, _) => {
620-
let t_1 = ety;
621-
let llty = type_of::type_of(cx, t_1);
622-
let (v, t_e) = const_expr(cx, &**base, param_substs);
623-
debug!("trans_const_cast({} as {})", t_e.repr(cx.tcx()), t_1.repr(cx.tcx()));
624-
if expr::cast_is_noop(cx.tcx(), base, t_e, t_1) {
620+
let t_cast = ety;
621+
let llty = type_of::type_of(cx, t_cast);
622+
let (v, t_expr) = const_expr(cx, &**base, param_substs);
623+
debug!("trans_const_cast({} as {})", t_expr.repr(cx.tcx()), t_cast.repr(cx.tcx()));
624+
if expr::cast_is_noop(cx.tcx(), base, t_expr, t_cast) {
625625
return v;
626626
}
627-
if type_is_fat_ptr(cx.tcx(), t_e) {
627+
if type_is_fat_ptr(cx.tcx(), t_expr) {
628628
// Fat pointer casts.
629-
let t_1_inner = ty::deref(t_1, true).expect("cast to non-pointer").ty;
630-
let ptr_ty = type_of::in_memory_type_of(cx, t_1_inner).ptr_to();
629+
let t_cast_inner = ty::deref(t_cast, true).expect("cast to non-pointer").ty;
630+
let ptr_ty = type_of::in_memory_type_of(cx, t_cast_inner).ptr_to();
631631
let addr = ptrcast(const_get_elt(cx, v, &[abi::FAT_PTR_ADDR as u32]),
632632
ptr_ty);
633-
if type_is_fat_ptr(cx.tcx(), t_1) {
633+
if type_is_fat_ptr(cx.tcx(), t_cast) {
634634
let info = const_get_elt(cx, v, &[abi::FAT_PTR_EXTRA as u32]);
635635
return C_struct(cx, &[addr, info], false)
636636
} else {
637637
return addr;
638638
}
639639
}
640-
match (CastTy::recognize(cx.tcx(), t_e).expect("bad input type for cast"),
641-
CastTy::recognize(cx.tcx(), t_1).expect("bad output type for cast")) {
640+
match (CastTy::from_ty(cx.tcx(), t_expr).expect("bad input type for cast"),
641+
CastTy::from_ty(cx.tcx(), t_cast).expect("bad output type for cast")) {
642642
(CastTy::Int(IntTy::CEnum), CastTy::Int(_)) => {
643-
let repr = adt::represent_type(cx, t_e);
643+
let repr = adt::represent_type(cx, t_expr);
644644
let discr = adt::const_get_discrim(cx, &*repr, v);
645645
let iv = C_integral(cx.int_type(), discr, false);
646646
let s = adt::is_discr_signed(&*repr) as Bool;
647647
llvm::LLVMConstIntCast(iv, llty.to_ref(), s)
648648
}
649649
(CastTy::Int(_), CastTy::Int(_)) => {
650-
let s = ty::type_is_signed(t_e) as Bool;
650+
let s = ty::type_is_signed(t_expr) as Bool;
651651
llvm::LLVMConstIntCast(v, llty.to_ref(), s)
652652
}
653653
(CastTy::Int(_), CastTy::Float) => {
654-
if ty::type_is_signed(t_e) {
654+
if ty::type_is_signed(t_expr) {
655655
llvm::LLVMConstSIToFP(v, llty.to_ref())
656656
} else {
657657
llvm::LLVMConstUIToFP(v, llty.to_ref())
@@ -661,18 +661,18 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
661661
llvm::LLVMConstFPCast(v, llty.to_ref())
662662
}
663663
(CastTy::Float, CastTy::Int(_)) => {
664-
if ty::type_is_signed(t_1) { llvm::LLVMConstFPToSI(v, llty.to_ref()) }
664+
if ty::type_is_signed(t_expr) { llvm::LLVMConstFPToSI(v, llty.to_ref()) }
665665
else { llvm::LLVMConstFPToUI(v, llty.to_ref()) }
666666
}
667-
(CastTy::Ptr(_), CastTy::Ptr(_)) | (CastTy::FPtr, CastTy::Ptr(_))
667+
(CastTy::Ptr(_), CastTy::Ptr(_)) | (CastTy::FnPtr, CastTy::Ptr(_))
668668
| (CastTy::RPtr(_), CastTy::Ptr(_)) => {
669669
ptrcast(v, llty)
670670
}
671-
(CastTy::FPtr, CastTy::FPtr) => ptrcast(v, llty), // isn't this a coercion?
671+
(CastTy::FnPtr, CastTy::FnPtr) => ptrcast(v, llty), // isn't this a coercion?
672672
(CastTy::Int(_), CastTy::Ptr(_)) => {
673673
llvm::LLVMConstIntToPtr(v, llty.to_ref())
674674
}
675-
(CastTy::Ptr(_), CastTy::Int(_)) | (CastTy::FPtr, CastTy::Int(_)) => {
675+
(CastTy::Ptr(_), CastTy::Int(_)) | (CastTy::FnPtr, CastTy::Int(_)) => {
676676
llvm::LLVMConstPtrToInt(v, llty.to_ref())
677677
}
678678
_ => {

trunk/src/librustc_trans/trans/expr.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,8 +2086,8 @@ fn trans_imm_cast<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
20862086
}
20872087
}
20882088

2089-
let r_t_in = CastTy::recognize(bcx.tcx(), t_in).expect("bad input type for cast");
2090-
let r_t_out = CastTy::recognize(bcx.tcx(), t_out).expect("bad output type for cast");
2089+
let r_t_in = CastTy::from_ty(bcx.tcx(), t_in).expect("bad input type for cast");
2090+
let r_t_out = CastTy::from_ty(bcx.tcx(), t_out).expect("bad output type for cast");
20912091

20922092
let (llexpr, signed) = if let Int(CEnum) = r_t_in {
20932093
let repr = adt::represent_type(ccx, t_in);
@@ -2102,8 +2102,10 @@ fn trans_imm_cast<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
21022102
};
21032103

21042104
let newval = match (r_t_in, r_t_out) {
2105-
(Ptr(_), Ptr(_)) | (FPtr, Ptr(_)) | (RPtr(_), Ptr(_)) => PointerCast(bcx, llexpr, ll_t_out),
2106-
(Ptr(_), Int(_)) | (FPtr, Int(_)) => PtrToInt(bcx, llexpr, ll_t_out),
2105+
(Ptr(_), Ptr(_)) | (FnPtr, Ptr(_)) | (RPtr(_), Ptr(_)) => {
2106+
PointerCast(bcx, llexpr, ll_t_out)
2107+
}
2108+
(Ptr(_), Int(_)) | (FnPtr, Int(_)) => PtrToInt(bcx, llexpr, ll_t_out),
21072109
(Int(_), Ptr(_)) => IntToPtr(bcx, llexpr, ll_t_out),
21082110

21092111
(Int(_), Int(_)) => int_cast(bcx, ll_t_out, ll_t_in, llexpr, signed),

0 commit comments

Comments
 (0)