Skip to content

Commit c64471a

Browse files
Add trait object field types to back/abi.rs, and use them
I've added trt_field_vtable, trt_field_box, and trt_field_tydesc, and inserted them in place of the "magic numbers" used to access trait object fields through GEPi().
1 parent 08dd625 commit c64471a

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

src/librustc/back/abi.rs

+7
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ pub static n_tydesc_fields: uint = 8u;
5757
pub static fn_field_code: uint = 0u;
5858
pub static fn_field_box: uint = 1u;
5959

60+
// The three fields of a trait object/trait instance: vtable, box, and type
61+
// description.
62+
pub static trt_field_vtable: uint = 0u;
63+
pub static trt_field_box: uint = 1u;
64+
// This field is only present in unique trait objects, so it comes last.
65+
pub static trt_field_tydesc: uint = 2u;
66+
6067
pub static vec_elt_fill: uint = 0u;
6168

6269
pub static vec_elt_alloc: uint = 1u;

src/librustc/middle/trans/glue.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -549,12 +549,12 @@ pub fn make_drop_glue(bcx: block, v0: ValueRef, t: ty::t) {
549549
closure::make_closure_glue(bcx, v0, t, drop_ty)
550550
}
551551
ty::ty_trait(_, _, ty::BoxTraitStore, _) => {
552-
let llbox = Load(bcx, GEPi(bcx, v0, [0u, 1u]));
552+
let llbox = Load(bcx, GEPi(bcx, v0, [0u, abi::trt_field_box]));
553553
decr_refcnt_maybe_free(bcx, llbox, ty::mk_opaque_box(ccx.tcx))
554554
}
555555
ty::ty_trait(_, _, ty::UniqTraitStore, _) => {
556-
let lluniquevalue = GEPi(bcx, v0, [0, 1]);
557-
let lltydesc = Load(bcx, GEPi(bcx, v0, [0, 2]));
556+
let lluniquevalue = GEPi(bcx, v0, [0, abi::trt_field_box]);
557+
let lltydesc = Load(bcx, GEPi(bcx, v0, [0, abi::trt_field_tydesc]));
558558
call_tydesc_glue_full(bcx, lluniquevalue, lltydesc,
559559
abi::tydesc_field_free_glue, None);
560560
bcx
@@ -613,13 +613,13 @@ pub fn make_take_glue(bcx: block, v: ValueRef, t: ty::t) {
613613
closure::make_closure_glue(bcx, v, t, take_ty)
614614
}
615615
ty::ty_trait(_, _, ty::BoxTraitStore, _) => {
616-
let llbox = Load(bcx, GEPi(bcx, v, [0u, 1u]));
616+
let llbox = Load(bcx, GEPi(bcx, v, [0u, abi::trt_field_box]));
617617
incr_refcnt_of_boxed(bcx, llbox);
618618
bcx
619619
}
620620
ty::ty_trait(_, _, ty::UniqTraitStore, _) => {
621-
let llval = GEPi(bcx, v, [0, 1]);
622-
let lltydesc = Load(bcx, GEPi(bcx, v, [0, 2]));
621+
let llval = GEPi(bcx, v, [0, abi::trt_field_box]);
622+
let lltydesc = Load(bcx, GEPi(bcx, v, [0, abi::trt_field_tydesc]));
623623
call_tydesc_glue_full(bcx, llval, lltydesc,
624624
abi::tydesc_field_take_glue, None);
625625
bcx

src/librustc/middle/trans/meth.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -637,14 +637,15 @@ pub fn trans_trait_callee_from_llval(bcx: block,
637637
val_str(bcx.ccx().tn, llpair));
638638
let llvtable = Load(bcx,
639639
PointerCast(bcx,
640-
GEPi(bcx, llpair, [0u, 0u]),
640+
GEPi(bcx, llpair,
641+
[0u, abi::trt_field_vtable]),
641642
T_ptr(T_ptr(T_vtable()))));
642643
643644
// Load the box from the @Trait pair and GEP over the box header if
644645
// necessary:
645646
let mut llself;
646647
debug!("(translating trait callee) loading second index from pair");
647-
let llbox = Load(bcx, GEPi(bcx, llpair, [0u, 1u]));
648+
let llbox = Load(bcx, GEPi(bcx, llpair, [0u, abi::trt_field_box]));
648649
649650
// Munge `llself` appropriately for the type of `self` in the method.
650651
let self_mode;
@@ -845,27 +846,30 @@ pub fn trans_trait_cast(bcx: block,
845846

846847
match store {
847848
ty::RegionTraitStore(_) | ty::BoxTraitStore => {
848-
let mut llboxdest = GEPi(bcx, lldest, [0u, 1u]);
849-
// Just store the pointer into the pair.
849+
let mut llboxdest = GEPi(bcx, lldest, [0u, abi::trt_field_box]);
850+
// Just store the pointer into the pair. (Region/borrowed
851+
// and boxed trait objects are represented as pairs, and
852+
// have no type descriptor field.)
850853
llboxdest = PointerCast(bcx,
851854
llboxdest,
852855
T_ptr(type_of(bcx.ccx(), v_ty)));
853856
bcx = expr::trans_into(bcx, val, SaveIn(llboxdest));
854857
}
855858
ty::UniqTraitStore => {
856-
// Translate the uniquely-owned value into the second element of
857-
// the triple. (The first element is the vtable.)
858-
let mut llvaldest = GEPi(bcx, lldest, [0, 1]);
859+
// Translate the uniquely-owned value in the
860+
// triple. (Unique trait objects are represented as
861+
// triples.)
862+
let mut llvaldest = GEPi(bcx, lldest, [0, abi::trt_field_box]);
859863
llvaldest = PointerCast(bcx,
860864
llvaldest,
861865
T_ptr(type_of(bcx.ccx(), v_ty)));
862866
bcx = expr::trans_into(bcx, val, SaveIn(llvaldest));
863867

864-
// Get the type descriptor of the wrapped value and store it into
865-
// the third element of the triple as well.
868+
// Get the type descriptor of the wrapped value and store
869+
// it in the triple as well.
866870
let tydesc = get_tydesc(bcx.ccx(), v_ty);
867871
glue::lazily_emit_all_tydesc_glue(bcx.ccx(), tydesc);
868-
let lltydescdest = GEPi(bcx, lldest, [0, 2]);
872+
let lltydescdest = GEPi(bcx, lldest, [0, abi::trt_field_tydesc]);
869873
Store(bcx, tydesc.tydesc, lltydescdest);
870874
}
871875
}
@@ -875,7 +879,7 @@ pub fn trans_trait_cast(bcx: block,
875879
let orig = resolve_vtable_in_fn_ctxt(bcx.fcx, orig);
876880
let vtable = get_vtable(bcx.ccx(), orig);
877881
Store(bcx, vtable, PointerCast(bcx,
878-
GEPi(bcx, lldest, [0u, 0u]),
882+
GEPi(bcx, lldest, [0u, abi::trt_field_vtable]),
879883
T_ptr(val_ty(vtable))));
880884

881885
bcx

0 commit comments

Comments
 (0)