Skip to content

Commit d55787a

Browse files
committed
Add typeid_for_trait_ref function
This function computes a Itanium-like typeid for a trait_ref. This is required for the VFE optimization in LLVM. It is used to map `llvm.type.checked.load` invocations, that is loading the function from a vtable, to the vtables this function could be from. It is important to note that `typeid`s are not unique. So multiple vtables of the same trait can share `typeid`s.
1 parent 20f597f commit d55787a

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

compiler/rustc_symbol_mangling/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ pub fn typeid_for_fnabi<'tcx>(tcx: TyCtxt<'tcx>, fn_abi: &FnAbi<'tcx, Ty<'tcx>>)
155155
v0::mangle_typeid_for_fnabi(tcx, fn_abi)
156156
}
157157

158+
pub fn typeid_for_trait_ref<'tcx>(
159+
tcx: TyCtxt<'tcx>,
160+
trait_ref: ty::PolyExistentialTraitRef<'tcx>,
161+
) -> String {
162+
v0::mangle_typeid_for_trait_ref(tcx, trait_ref)
163+
}
164+
158165
/// Computes the symbol name for the given instance. This function will call
159166
/// `compute_instantiating_crate` if it needs to factor the instantiating crate
160167
/// into the symbol name.

compiler/rustc_symbol_mangling/src/v0.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,24 @@ pub(super) fn mangle_typeid_for_fnabi<'tcx>(
9595
format!("typeid{}", arg_count)
9696
}
9797

98+
pub(super) fn mangle_typeid_for_trait_ref<'tcx>(
99+
tcx: TyCtxt<'tcx>,
100+
trait_ref: ty::PolyExistentialTraitRef<'tcx>,
101+
) -> String {
102+
// FIXME(flip1995): See comment in `mangle_typeid_for_fnabi`.
103+
let mut cx = &mut SymbolMangler {
104+
tcx,
105+
start_offset: 0,
106+
paths: FxHashMap::default(),
107+
types: FxHashMap::default(),
108+
consts: FxHashMap::default(),
109+
binders: vec![],
110+
out: String::new(),
111+
};
112+
cx = cx.print_def_path(trait_ref.def_id(), &[]).unwrap();
113+
std::mem::take(&mut cx.out)
114+
}
115+
98116
struct BinderLevel {
99117
/// The range of distances from the root of what's
100118
/// being printed, to the lifetimes in a binder.

0 commit comments

Comments
 (0)