@@ -2,7 +2,7 @@ use self::MemberDescriptionFactory::*;
22use self :: RecursiveTypeDescription :: * ;
33
44use super :: namespace:: mangled_name_of_instance;
5- use super :: type_names:: compute_debuginfo_type_name;
5+ use super :: type_names:: { compute_debuginfo_type_name, compute_debuginfo_vtable_name } ;
66use super :: utils:: {
77 create_DIArray, debug_context, get_namespace_for_item, is_node_local_to_unit, DIB ,
88} ;
@@ -30,8 +30,9 @@ use rustc_middle::ich::NodeIdHashingMode;
3030use rustc_middle:: mir:: { self , GeneratorLayout } ;
3131use rustc_middle:: ty:: layout:: { self , IntegerExt , LayoutOf , PrimitiveExt , TyAndLayout } ;
3232use rustc_middle:: ty:: subst:: GenericArgKind ;
33- use rustc_middle:: ty:: Instance ;
34- use rustc_middle:: ty:: { self , AdtKind , GeneratorSubsts , ParamEnv , Ty , TyCtxt } ;
33+ use rustc_middle:: ty:: {
34+ self , AdtKind , GeneratorSubsts , Instance , ParamEnv , Ty , TyCtxt , COMMON_VTABLE_ENTRIES ,
35+ } ;
3536use rustc_middle:: { bug, span_bug} ;
3637use rustc_session:: config:: { self , DebugInfo } ;
3738use rustc_span:: symbol:: Symbol ;
@@ -2591,11 +2592,45 @@ pub fn create_global_var_metadata(cx: &CodegenCx<'ll, '_>, def_id: DefId, global
25912592 }
25922593}
25932594
2595+ /// Generates LLVM debuginfo for a vtable.
2596+ fn vtable_type_metadata (
2597+ cx : & CodegenCx < ' ll , ' tcx > ,
2598+ ty : Ty < ' tcx > ,
2599+ poly_trait_ref : Option < ty:: PolyExistentialTraitRef < ' tcx > > ,
2600+ ) -> & ' ll DIType {
2601+ let tcx = cx. tcx ;
2602+
2603+ let vtable_entries = if let Some ( poly_trait_ref) = poly_trait_ref {
2604+ let trait_ref = poly_trait_ref. with_self_ty ( tcx, ty) ;
2605+ let trait_ref = tcx. erase_regions ( trait_ref) ;
2606+
2607+ tcx. vtable_entries ( trait_ref)
2608+ } else {
2609+ COMMON_VTABLE_ENTRIES
2610+ } ;
2611+
2612+ // FIXME: We describe the vtable as an array of *const () pointers. The length of the array is
2613+ // correct - but we could create a more accurate description, e.g. by describing it
2614+ // as a struct where each field has a name that corresponds to the name of the method
2615+ // it points to.
2616+ // However, this is not entirely straightforward because there might be multiple
2617+ // methods with the same name if the vtable is for multiple traits. So for now we keep
2618+ // things simple instead of adding some ad-hoc disambiguation scheme.
2619+ let vtable_type = tcx. mk_array ( tcx. mk_imm_ptr ( tcx. types . unit ) , vtable_entries. len ( ) as u64 ) ;
2620+
2621+ type_metadata ( cx, vtable_type, rustc_span:: DUMMY_SP )
2622+ }
2623+
25942624/// Creates debug information for the given vtable, which is for the
25952625/// given type.
25962626///
25972627/// Adds the created metadata nodes directly to the crate's IR.
2598- pub fn create_vtable_metadata ( cx : & CodegenCx < ' ll , ' tcx > , ty : Ty < ' tcx > , vtable : & ' ll Value ) {
2628+ pub fn create_vtable_metadata (
2629+ cx : & CodegenCx < ' ll , ' tcx > ,
2630+ ty : Ty < ' tcx > ,
2631+ poly_trait_ref : Option < ty:: PolyExistentialTraitRef < ' tcx > > ,
2632+ vtable : & ' ll Value ,
2633+ ) {
25992634 if cx. dbg_cx . is_none ( ) {
26002635 return ;
26012636 }
@@ -2605,42 +2640,16 @@ pub fn create_vtable_metadata(cx: &CodegenCx<'ll, 'tcx>, ty: Ty<'tcx>, vtable: &
26052640 return ;
26062641 }
26072642
2608- let type_metadata = type_metadata ( cx, ty, rustc_span:: DUMMY_SP ) ;
2643+ let vtable_name = compute_debuginfo_vtable_name ( cx. tcx , ty, poly_trait_ref) ;
2644+ let vtable_type = vtable_type_metadata ( cx, ty, poly_trait_ref) ;
26092645
26102646 unsafe {
2611- // `LLVMRustDIBuilderCreateStructType()` wants an empty array. A null
2612- // pointer will lead to hard to trace and debug LLVM assertions
2613- // later on in `llvm/lib/IR/Value.cpp`.
2614- let empty_array = create_DIArray ( DIB ( cx) , & [ ] ) ;
2615- let name = "vtable" ;
2616-
2617- // Create a new one each time. We don't want metadata caching
2618- // here, because each vtable will refer to a unique containing
2619- // type.
2620- let vtable_type = llvm:: LLVMRustDIBuilderCreateStructType (
2621- DIB ( cx) ,
2622- NO_SCOPE_METADATA ,
2623- name. as_ptr ( ) . cast ( ) ,
2624- name. len ( ) ,
2625- unknown_file_metadata ( cx) ,
2626- UNKNOWN_LINE_NUMBER ,
2627- Size :: ZERO . bits ( ) ,
2628- cx. tcx . data_layout . pointer_align . abi . bits ( ) as u32 ,
2629- DIFlags :: FlagArtificial ,
2630- None ,
2631- empty_array,
2632- 0 ,
2633- Some ( type_metadata) ,
2634- name. as_ptr ( ) . cast ( ) ,
2635- name. len ( ) ,
2636- ) ;
2637-
26382647 let linkage_name = "" ;
26392648 llvm:: LLVMRustDIBuilderCreateStaticVariable (
26402649 DIB ( cx) ,
26412650 NO_SCOPE_METADATA ,
2642- name . as_ptr ( ) . cast ( ) ,
2643- name . len ( ) ,
2651+ vtable_name . as_ptr ( ) . cast ( ) ,
2652+ vtable_name . len ( ) ,
26442653 linkage_name. as_ptr ( ) . cast ( ) ,
26452654 linkage_name. len ( ) ,
26462655 unknown_file_metadata ( cx) ,
0 commit comments