diff --git a/src/librustc_codegen_llvm/consts.rs b/src/librustc_codegen_llvm/consts.rs index 38090cb26bc41..07c1e64a6e510 100644 --- a/src/librustc_codegen_llvm/consts.rs +++ b/src/librustc_codegen_llvm/consts.rs @@ -402,7 +402,9 @@ impl StaticMethods for CodegenCx<'ll, 'tcx> { } } - debuginfo::create_global_var_metadata(&self, def_id, g); + if let Some(dbg_cx) = self.dbg_cx.as_ref() { + debuginfo::create_global_var_metadata(dbg_cx, def_id, g); + } if attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) { llvm::set_thread_local_mode(g, self.tls_model); diff --git a/src/librustc_codegen_llvm/context.rs b/src/librustc_codegen_llvm/context.rs index 6b31f14410d2f..58f99f5730d7f 100644 --- a/src/librustc_codegen_llvm/context.rs +++ b/src/librustc_codegen_llvm/context.rs @@ -298,7 +298,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> { let (llcx, llmod) = (&*llvm_module.llcx, llvm_module.llmod()); let dbg_cx = if tcx.sess.opts.debuginfo != DebugInfo::None { - let dctx = debuginfo::CrateDebugContext::new(llmod); + let dctx = debuginfo::CrateDebugContext::new(tcx, llmod); debuginfo::metadata::compile_unit_metadata(tcx, &codegen_unit.name().as_str(), &dctx); Some(dctx) } else { diff --git a/src/librustc_codegen_llvm/debuginfo/create_scope_map.rs b/src/librustc_codegen_llvm/debuginfo/create_scope_map.rs index eba05ed5d7787..79ffe0b73a6e1 100644 --- a/src/librustc_codegen_llvm/debuginfo/create_scope_map.rs +++ b/src/librustc_codegen_llvm/debuginfo/create_scope_map.rs @@ -1,8 +1,8 @@ +use super::CrateDebugContext; use super::metadata::file_metadata; -use super::utils::{span_start, DIB}; +use super::utils::span_start; use rustc_codegen_ssa::mir::debuginfo::{DebugScope, FunctionDebugContext}; -use crate::common::CodegenCx; use crate::llvm; use crate::llvm::debuginfo::{DIScope, DISubprogram}; use rustc::mir::{Body, SourceScope}; @@ -16,7 +16,7 @@ use rustc_index::vec::Idx; /// Produces DIScope DIEs for each MIR Scope which has variables defined in it. pub fn compute_mir_scopes( - cx: &CodegenCx<'ll, '_>, + dbg_cx: &CrateDebugContext<'ll, '_>, mir: &Body<'_>, fn_metadata: &'ll DISubprogram, debug_context: &mut FunctionDebugContext<&'ll DIScope>, @@ -32,12 +32,12 @@ pub fn compute_mir_scopes( // Instantiate all scopes. for idx in 0..mir.source_scopes.len() { let scope = SourceScope::new(idx); - make_mir_scope(cx, &mir, fn_metadata, &has_variables, debug_context, scope); + make_mir_scope(dbg_cx, &mir, fn_metadata, &has_variables, debug_context, scope); } } fn make_mir_scope( - cx: &CodegenCx<'ll, '_>, + dbg_cx: &CrateDebugContext<'ll, '_>, mir: &Body<'_>, fn_metadata: &'ll DISubprogram, has_variables: &BitSet, @@ -50,11 +50,11 @@ fn make_mir_scope( let scope_data = &mir.source_scopes[scope]; let parent_scope = if let Some(parent) = scope_data.parent_scope { - make_mir_scope(cx, mir, fn_metadata, has_variables, debug_context, parent); + make_mir_scope(dbg_cx, mir, fn_metadata, has_variables, debug_context, parent); debug_context.scopes[parent] } else { // The root is the function itself. - let loc = span_start(cx, mir.span); + let loc = span_start(dbg_cx.tcx, mir.span); debug_context.scopes[scope] = DebugScope { scope_metadata: Some(fn_metadata), file_start_pos: loc.file.start_pos, @@ -67,21 +67,16 @@ fn make_mir_scope( // Do not create a DIScope if there are no variables // defined in this MIR Scope, to avoid debuginfo bloat. - // However, we don't skip creating a nested scope if - // our parent is the root, because we might want to - // put arguments in the root and not have shadowing. - if parent_scope.scope_metadata.unwrap() != fn_metadata { - debug_context.scopes[scope] = parent_scope; - return; - } + debug_context.scopes[scope] = parent_scope; + return; } - let loc = span_start(cx, scope_data.span); - let file_metadata = file_metadata(cx, &loc.file.name, debug_context.defining_crate); + let loc = span_start(dbg_cx.tcx, scope_data.span); + let file_metadata = file_metadata(dbg_cx, &loc.file.name, debug_context.defining_crate); let scope_metadata = unsafe { Some(llvm::LLVMRustDIBuilderCreateLexicalBlock( - DIB(cx), + dbg_cx.builder, parent_scope.scope_metadata.unwrap(), file_metadata, loc.line as c_uint, diff --git a/src/librustc_codegen_llvm/debuginfo/doc.rs b/src/librustc_codegen_llvm/debuginfo/doc.rs index b3a8fa2988785..cfa701d77739a 100644 --- a/src/librustc_codegen_llvm/debuginfo/doc.rs +++ b/src/librustc_codegen_llvm/debuginfo/doc.rs @@ -28,7 +28,7 @@ //! utilizing a cache. The way to get a shared metadata node when needed is //! thus to just call the corresponding function in this module: //! -//! let file_metadata = file_metadata(crate_context, path); +//! let file_metadata = file_metadata(debug_context, path); //! //! The function will take care of probing the cache for an existing node for //! that exact file path. diff --git a/src/librustc_codegen_llvm/debuginfo/metadata.rs b/src/librustc_codegen_llvm/debuginfo/metadata.rs index d1f70ad43bd28..297548b73aca2 100644 --- a/src/librustc_codegen_llvm/debuginfo/metadata.rs +++ b/src/librustc_codegen_llvm/debuginfo/metadata.rs @@ -2,15 +2,12 @@ use self::EnumDiscriminantInfo::*; use self::MemberDescriptionFactory::*; use self::RecursiveTypeDescription::*; -use super::namespace::mangled_name_of_instance; use super::type_names::compute_debuginfo_type_name; use super::utils::{ - create_DIArray, debug_context, get_namespace_for_item, is_node_local_to_unit, span_start, DIB, + create_DIArray, get_namespace_for_item, is_node_local_to_unit, span_start, }; use super::CrateDebugContext; -use crate::abi; -use crate::common::CodegenCx; use crate::llvm; use crate::llvm::debuginfo::{ DIArray, DICompositeType, DIDescriptor, DIFile, DIFlags, DILexicalBlock, DIScope, DIType, @@ -32,7 +29,6 @@ use rustc::ty::subst::{GenericArgKind, SubstsRef}; use rustc::ty::Instance; use rustc::ty::{self, AdtKind, ParamEnv, Ty, TyCtxt}; use rustc::{bug, span_bug}; -use rustc_codegen_ssa::traits::*; use rustc_data_structures::const_cstr; use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fx::FxHashMap; @@ -179,9 +175,9 @@ impl TypeMap<'ll, 'tcx> { /// Gets the `UniqueTypeId` for the given type. If the `UniqueTypeId` for the given /// type has been requested before, this is just a table lookup. Otherwise, an /// ID will be generated and stored for later lookup. - fn get_unique_type_id_of_type<'a>( + fn get_unique_type_id_of_type( &mut self, - cx: &CodegenCx<'a, 'tcx>, + dbg_cx: &CrateDebugContext<'_, 'tcx>, type_: Ty<'tcx>, ) -> UniqueTypeId { // Let's see if we already have something in the cache. @@ -193,8 +189,8 @@ impl TypeMap<'ll, 'tcx> { // The hasher we are using to generate the UniqueTypeId. We want // something that provides more than the 64 bits of the DefaultHasher. let mut hasher = StableHasher::new(); - let mut hcx = cx.tcx.create_stable_hashing_context(); - let type_ = cx.tcx.erase_regions(&type_); + let mut hcx = dbg_cx.tcx.create_stable_hashing_context(); + let type_ = dbg_cx.tcx.erase_regions(&type_); hcx.while_hashing_spans(false, |hcx| { hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| { type_.hash_stable(hcx, &mut hasher); @@ -211,13 +207,13 @@ impl TypeMap<'ll, 'tcx> { /// Gets the `UniqueTypeId` for an enum variant. Enum variants are not really /// types of their own, so they need special handling. We still need a /// `UniqueTypeId` for them, since to debuginfo they *are* real types. - fn get_unique_type_id_of_enum_variant<'a>( + fn get_unique_type_id_of_enum_variant( &mut self, - cx: &CodegenCx<'a, 'tcx>, + dbg_cx: &CrateDebugContext<'_, 'tcx>, enum_type: Ty<'tcx>, variant_name: &str, ) -> UniqueTypeId { - let enum_type_id = self.get_unique_type_id_of_type(cx, enum_type); + let enum_type_id = self.get_unique_type_id_of_type(dbg_cx, enum_type); let enum_variant_type_id = format!("{}::{}", self.get_unique_type_id_as_string(enum_type_id), variant_name); let interner_key = self.unique_id_interner.intern(&enum_variant_type_id); @@ -252,7 +248,7 @@ enum RecursiveTypeDescription<'ll, 'tcx> { } fn create_and_register_recursive_type_forward_declaration( - cx: &CodegenCx<'ll, 'tcx>, + dbg_cx: &CrateDebugContext<'ll, 'tcx>, unfinished_type: Ty<'tcx>, unique_type_id: UniqueTypeId, metadata_stub: &'ll DICompositeType, @@ -260,7 +256,7 @@ fn create_and_register_recursive_type_forward_declaration( member_description_factory: MemberDescriptionFactory<'ll, 'tcx>, ) -> RecursiveTypeDescription<'ll, 'tcx> { // Insert the stub into the `TypeMap` in order to allow for recursive references. - let mut type_map = debug_context(cx).type_map.borrow_mut(); + let mut type_map = dbg_cx.type_map.borrow_mut(); type_map.register_unique_id_with_metadata(unique_type_id, metadata_stub); type_map.register_type_with_metadata(unfinished_type, metadata_stub); @@ -277,7 +273,7 @@ impl RecursiveTypeDescription<'ll, 'tcx> { /// Finishes up the description of the type in question (mostly by providing /// descriptions of the fields of the given type) and returns the final type /// metadata. - fn finalize(&self, cx: &CodegenCx<'ll, 'tcx>) -> MetadataCreationResult<'ll> { + fn finalize(&self, dbg_cx: &CrateDebugContext<'ll, 'tcx>) -> MetadataCreationResult<'ll> { match *self { FinalMetadata(metadata) => MetadataCreationResult::new(metadata, false), UnfinishedMetadata { @@ -294,7 +290,7 @@ impl RecursiveTypeDescription<'ll, 'tcx> { // `create_and_register_recursive_type_forward_declaration()` // function. { - let type_map = debug_context(cx).type_map.borrow(); + let type_map = dbg_cx.type_map.borrow(); if type_map.find_metadata_for_unique_id(unique_type_id).is_none() || type_map.find_metadata_for_type(unfinished_type).is_none() { @@ -307,11 +303,11 @@ impl RecursiveTypeDescription<'ll, 'tcx> { } // ... then create the member descriptions ... - let member_descriptions = member_description_factory.create_member_descriptions(cx); + let member_descriptions = member_description_factory.create_member_descriptions(dbg_cx); // ... and attach them to the stub to complete it. set_members_of_composite_type( - cx, + dbg_cx, unfinished_type, member_holding_stub, member_descriptions, @@ -325,9 +321,9 @@ impl RecursiveTypeDescription<'ll, 'tcx> { /// Returns from the enclosing function if the type metadata with the given /// unique ID can be found in the type map. macro_rules! return_if_metadata_created_in_meantime { - ($cx: expr, $unique_type_id: expr) => { + ($dbg_cx: expr, $unique_type_id: expr) => { if let Some(metadata) = - debug_context($cx).type_map.borrow().find_metadata_for_unique_id($unique_type_id) + $dbg_cx.type_map.borrow().find_metadata_for_unique_id($unique_type_id) { return MetadataCreationResult::new(metadata, true); } @@ -335,30 +331,30 @@ macro_rules! return_if_metadata_created_in_meantime { } fn fixed_vec_metadata( - cx: &CodegenCx<'ll, 'tcx>, + dbg_cx: &CrateDebugContext<'ll, 'tcx>, unique_type_id: UniqueTypeId, array_or_slice_type: Ty<'tcx>, element_type: Ty<'tcx>, span: Span, ) -> MetadataCreationResult<'ll> { - let element_type_metadata = type_metadata(cx, element_type, span); + let element_type_metadata = type_metadata(dbg_cx, element_type, span); - return_if_metadata_created_in_meantime!(cx, unique_type_id); + return_if_metadata_created_in_meantime!(dbg_cx, unique_type_id); - let (size, align) = cx.size_and_align_of(array_or_slice_type); + let (size, align) = dbg_cx.size_and_align_of(array_or_slice_type); let upper_bound = match array_or_slice_type.kind { - ty::Array(_, len) => len.eval_usize(cx.tcx, ty::ParamEnv::reveal_all()) as c_longlong, + ty::Array(_, len) => len.eval_usize(dbg_cx.tcx, ty::ParamEnv::reveal_all()) as c_longlong, _ => -1, }; let subrange = - unsafe { Some(llvm::LLVMRustDIBuilderGetOrCreateSubrange(DIB(cx), 0, upper_bound)) }; + unsafe { Some(llvm::LLVMRustDIBuilderGetOrCreateSubrange(dbg_cx.builder, 0, upper_bound)) }; - let subscripts = create_DIArray(DIB(cx), &[subrange]); + let subscripts = create_DIArray(dbg_cx.builder, &[subrange]); let metadata = unsafe { llvm::LLVMRustDIBuilderCreateArrayType( - DIB(cx), + dbg_cx.builder, size.bits(), align.bits() as u32, element_type_metadata, @@ -370,22 +366,22 @@ fn fixed_vec_metadata( } fn vec_slice_metadata( - cx: &CodegenCx<'ll, 'tcx>, + dbg_cx: &CrateDebugContext<'ll, 'tcx>, slice_ptr_type: Ty<'tcx>, element_type: Ty<'tcx>, unique_type_id: UniqueTypeId, span: Span, ) -> MetadataCreationResult<'ll> { - let data_ptr_type = cx.tcx.mk_imm_ptr(element_type); + let data_ptr_type = dbg_cx.tcx.mk_imm_ptr(element_type); - let data_ptr_metadata = type_metadata(cx, data_ptr_type, span); + let data_ptr_metadata = type_metadata(dbg_cx, data_ptr_type, span); - return_if_metadata_created_in_meantime!(cx, unique_type_id); + return_if_metadata_created_in_meantime!(dbg_cx, unique_type_id); - let slice_type_name = compute_debuginfo_type_name(cx.tcx, slice_ptr_type, true); + let slice_type_name = compute_debuginfo_type_name(dbg_cx.tcx, slice_ptr_type, true); - let (pointer_size, pointer_align) = cx.size_and_align_of(data_ptr_type); - let (usize_size, usize_align) = cx.size_and_align_of(cx.tcx.types.usize); + let (pointer_size, pointer_align) = dbg_cx.size_and_align_of(data_ptr_type); + let (usize_size, usize_align) = dbg_cx.size_and_align_of(dbg_cx.tcx.types.usize); let member_descriptions = vec![ MemberDescription { @@ -399,7 +395,7 @@ fn vec_slice_metadata( }, MemberDescription { name: "length".to_owned(), - type_metadata: type_metadata(cx, cx.tcx.types.usize, span), + type_metadata: type_metadata(dbg_cx, dbg_cx.tcx.types.usize, span), offset: pointer_size, size: usize_size, align: usize_align, @@ -408,10 +404,10 @@ fn vec_slice_metadata( }, ]; - let file_metadata = unknown_file_metadata(cx); + let file_metadata = unknown_file_metadata(dbg_cx); let metadata = composite_type_metadata( - cx, + dbg_cx, slice_ptr_type, &slice_type_name[..], unique_type_id, @@ -424,35 +420,35 @@ fn vec_slice_metadata( } fn subroutine_type_metadata( - cx: &CodegenCx<'ll, 'tcx>, + dbg_cx: &CrateDebugContext<'ll, 'tcx>, unique_type_id: UniqueTypeId, signature: ty::PolyFnSig<'tcx>, span: Span, ) -> MetadataCreationResult<'ll> { let signature = - cx.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &signature); + dbg_cx.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &signature); let signature_metadata: Vec<_> = iter::once( // return type match signature.output().kind { ty::Tuple(ref tys) if tys.is_empty() => None, - _ => Some(type_metadata(cx, signature.output(), span)), + _ => Some(type_metadata(dbg_cx, signature.output(), span)), }, ) .chain( // regular arguments - signature.inputs().iter().map(|argument_type| Some(type_metadata(cx, argument_type, span))), + signature.inputs().iter().map(|argument_type| Some(type_metadata(dbg_cx, argument_type, span))), ) .collect(); - return_if_metadata_created_in_meantime!(cx, unique_type_id); + return_if_metadata_created_in_meantime!(dbg_cx, unique_type_id); return MetadataCreationResult::new( unsafe { llvm::LLVMRustDIBuilderCreateSubroutineType( - DIB(cx), - unknown_file_metadata(cx), - create_DIArray(DIB(cx), &signature_metadata[..]), + dbg_cx.builder, + unknown_file_metadata(dbg_cx), + create_DIArray(dbg_cx.builder, &signature_metadata[..]), ) }, false, @@ -466,7 +462,7 @@ fn subroutine_type_metadata( // of a DST struct, there is no `trait_object_type` and the results of this // function will be a little bit weird. fn trait_pointer_metadata( - cx: &CodegenCx<'ll, 'tcx>, + dbg_cx: &CrateDebugContext<'ll, 'tcx>, trait_type: Ty<'tcx>, trait_object_type: Option>, unique_type_id: UniqueTypeId, @@ -477,7 +473,7 @@ fn trait_pointer_metadata( let containing_scope = match trait_type.kind { ty::Dynamic(ref data, ..) => { - data.principal_def_id().map(|did| get_namespace_for_item(cx, did)) + data.principal_def_id().map(|did| get_namespace_for_item(dbg_cx, did)) } _ => { bug!( @@ -489,23 +485,23 @@ fn trait_pointer_metadata( }; let trait_object_type = trait_object_type.unwrap_or(trait_type); - let trait_type_name = compute_debuginfo_type_name(cx.tcx, trait_object_type, false); + let trait_type_name = compute_debuginfo_type_name(dbg_cx.tcx, trait_object_type, false); - let file_metadata = unknown_file_metadata(cx); + let file_metadata = unknown_file_metadata(dbg_cx); - let layout = cx.layout_of(cx.tcx.mk_mut_ptr(trait_type)); + let layout = dbg_cx.layout_of(dbg_cx.tcx.mk_mut_ptr(trait_type)); - assert_eq!(abi::FAT_PTR_ADDR, 0); - assert_eq!(abi::FAT_PTR_EXTRA, 1); + assert_eq!(layout::FAT_PTR_ADDR, 0); + assert_eq!(layout::FAT_PTR_EXTRA, 1); - let data_ptr_field = layout.field(cx, 0); - let vtable_field = layout.field(cx, 1); + let data_ptr_field = layout.field(dbg_cx, 0); + let vtable_field = layout.field(dbg_cx, 1); let member_descriptions = vec![ MemberDescription { name: "pointer".to_owned(), type_metadata: type_metadata( - cx, - cx.tcx.mk_mut_ptr(cx.tcx.types.u8), + dbg_cx, + dbg_cx.tcx.mk_mut_ptr(dbg_cx.tcx.types.u8), rustc_span::DUMMY_SP, ), offset: layout.fields.offset(0), @@ -516,7 +512,7 @@ fn trait_pointer_metadata( }, MemberDescription { name: "vtable".to_owned(), - type_metadata: type_metadata(cx, vtable_field.ty, rustc_span::DUMMY_SP), + type_metadata: type_metadata(dbg_cx, vtable_field.ty, rustc_span::DUMMY_SP), offset: layout.fields.offset(1), size: vtable_field.size, align: vtable_field.align.abi, @@ -526,7 +522,7 @@ fn trait_pointer_metadata( ]; composite_type_metadata( - cx, + dbg_cx, trait_object_type, &trait_type_name[..], unique_type_id, @@ -537,10 +533,10 @@ fn trait_pointer_metadata( ) } -pub fn type_metadata(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>, usage_site_span: Span) -> &'ll DIType { +pub fn type_metadata(dbg_cx: &CrateDebugContext<'ll, 'tcx>, t: Ty<'tcx>, usage_site_span: Span) -> &'ll DIType { // Get the unique type ID of this type. let unique_type_id = { - let mut type_map = debug_context(cx).type_map.borrow_mut(); + let mut type_map = dbg_cx.type_map.borrow_mut(); // First, try to find the type in `TypeMap`. If we have seen it before, we // can exit early here. match type_map.find_metadata_for_type(t) { @@ -552,7 +548,7 @@ pub fn type_metadata(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>, usage_site_span: Sp // an equivalent type (e.g., only differing in region arguments). // In order to find out, generate the unique type ID and look // that up. - let unique_type_id = type_map.get_unique_type_id_of_type(cx, t); + let unique_type_id = type_map.get_unique_type_id_of_type(dbg_cx, t); match type_map.find_metadata_for_unique_id(unique_type_id) { Some(metadata) => { // There is already an equivalent type in the TypeMap. @@ -574,41 +570,41 @@ pub fn type_metadata(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>, usage_site_span: Sp debug!("type_metadata: {:?}", t); let ptr_metadata = |ty: Ty<'tcx>| match ty.kind { - ty::Slice(typ) => Ok(vec_slice_metadata(cx, t, typ, unique_type_id, usage_site_span)), - ty::Str => Ok(vec_slice_metadata(cx, t, cx.tcx.types.u8, unique_type_id, usage_site_span)), + ty::Slice(typ) => Ok(vec_slice_metadata(dbg_cx, t, typ, unique_type_id, usage_site_span)), + ty::Str => Ok(vec_slice_metadata(dbg_cx, t, dbg_cx.tcx.types.u8, unique_type_id, usage_site_span)), ty::Dynamic(..) => Ok(MetadataCreationResult::new( - trait_pointer_metadata(cx, ty, Some(t), unique_type_id), + trait_pointer_metadata(dbg_cx, ty, Some(t), unique_type_id), false, )), _ => { - let pointee_metadata = type_metadata(cx, ty, usage_site_span); + let pointee_metadata = type_metadata(dbg_cx, ty, usage_site_span); if let Some(metadata) = - debug_context(cx).type_map.borrow().find_metadata_for_unique_id(unique_type_id) + dbg_cx.type_map.borrow().find_metadata_for_unique_id(unique_type_id) { return Err(metadata); } - Ok(MetadataCreationResult::new(pointer_type_metadata(cx, t, pointee_metadata), false)) + Ok(MetadataCreationResult::new(pointer_type_metadata(dbg_cx, t, pointee_metadata), false)) } }; let MetadataCreationResult { metadata, already_stored_in_typemap } = match t.kind { ty::Never | ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) => { - MetadataCreationResult::new(basic_type_metadata(cx, t), false) + MetadataCreationResult::new(basic_type_metadata(dbg_cx, t), false) } ty::Tuple(ref elements) if elements.is_empty() => { - MetadataCreationResult::new(basic_type_metadata(cx, t), false) + MetadataCreationResult::new(basic_type_metadata(dbg_cx, t), false) } ty::Array(typ, _) | ty::Slice(typ) => { - fixed_vec_metadata(cx, unique_type_id, t, typ, usage_site_span) + fixed_vec_metadata(dbg_cx, unique_type_id, t, typ, usage_site_span) } - ty::Str => fixed_vec_metadata(cx, unique_type_id, t, cx.tcx.types.i8, usage_site_span), + ty::Str => fixed_vec_metadata(dbg_cx, unique_type_id, t, dbg_cx.tcx.types.i8, usage_site_span), ty::Dynamic(..) => { - MetadataCreationResult::new(trait_pointer_metadata(cx, t, None, unique_type_id), false) + MetadataCreationResult::new(trait_pointer_metadata(dbg_cx, t, None, unique_type_id), false) } ty::Foreign(..) => { - MetadataCreationResult::new(foreign_type_metadata(cx, t, unique_type_id), false) + MetadataCreationResult::new(foreign_type_metadata(dbg_cx, t, unique_type_id), false) } ty::RawPtr(ty::TypeAndMut { ty, .. }) | ty::Ref(_, ty, _) => match ptr_metadata(ty) { Ok(res) => res, @@ -620,7 +616,7 @@ pub fn type_metadata(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>, usage_site_span: Sp }, ty::FnDef(..) | ty::FnPtr(_) => { if let Some(metadata) = - debug_context(cx).type_map.borrow().find_metadata_for_unique_id(unique_type_id) + dbg_cx.type_map.borrow().find_metadata_for_unique_id(unique_type_id) { return metadata; } @@ -639,9 +635,9 @@ pub fn type_metadata(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>, usage_site_span: Sp // anything reading the debuginfo for a recursive // type is going to see *somthing* weird - the only // question is what exactly it will see. - let (size, align) = cx.size_and_align_of(t); + let (size, align) = dbg_cx.size_and_align_of(t); llvm::LLVMRustDIBuilderCreateBasicType( - DIB(cx), + dbg_cx.builder, SmallCStr::new("").as_ptr(), size.bits(), align.bits() as u32, @@ -650,62 +646,62 @@ pub fn type_metadata(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>, usage_site_span: Sp } }; - let type_map = &debug_context(cx).type_map; + let type_map = &dbg_cx.type_map; type_map.borrow_mut().register_type_with_metadata(t, temp_type); let fn_metadata = - subroutine_type_metadata(cx, unique_type_id, t.fn_sig(cx.tcx), usage_site_span) + subroutine_type_metadata(dbg_cx, unique_type_id, t.fn_sig(dbg_cx.tcx), usage_site_span) .metadata; type_map.borrow_mut().remove_type(t); // This is actually a function pointer, so wrap it in pointer DI. - MetadataCreationResult::new(pointer_type_metadata(cx, t, fn_metadata), false) + MetadataCreationResult::new(pointer_type_metadata(dbg_cx, t, fn_metadata), false) } ty::Closure(def_id, substs) => { - let upvar_tys: Vec<_> = substs.as_closure().upvar_tys(def_id, cx.tcx).collect(); - let containing_scope = get_namespace_for_item(cx, def_id); + let upvar_tys: Vec<_> = substs.as_closure().upvar_tys(def_id, dbg_cx.tcx).collect(); + let containing_scope = get_namespace_for_item(dbg_cx, def_id); prepare_tuple_metadata( - cx, + dbg_cx, t, &upvar_tys, unique_type_id, usage_site_span, Some(containing_scope), ) - .finalize(cx) + .finalize(dbg_cx) } ty::Generator(def_id, substs, _) => { let upvar_tys: Vec<_> = substs .as_generator() - .prefix_tys(def_id, cx.tcx) - .map(|t| cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), t)) + .prefix_tys(def_id, dbg_cx.tcx) + .map(|t| dbg_cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), t)) .collect(); - prepare_enum_metadata(cx, t, def_id, unique_type_id, usage_site_span, upvar_tys) - .finalize(cx) + prepare_enum_metadata(dbg_cx, t, def_id, unique_type_id, usage_site_span, upvar_tys) + .finalize(dbg_cx) } ty::Adt(def, ..) => match def.adt_kind() { AdtKind::Struct => { - prepare_struct_metadata(cx, t, unique_type_id, usage_site_span).finalize(cx) + prepare_struct_metadata(dbg_cx, t, unique_type_id, usage_site_span).finalize(dbg_cx) } AdtKind::Union => { - prepare_union_metadata(cx, t, unique_type_id, usage_site_span).finalize(cx) + prepare_union_metadata(dbg_cx, t, unique_type_id, usage_site_span).finalize(dbg_cx) } AdtKind::Enum => { - prepare_enum_metadata(cx, t, def.did, unique_type_id, usage_site_span, vec![]) - .finalize(cx) + prepare_enum_metadata(dbg_cx, t, def.did, unique_type_id, usage_site_span, vec![]) + .finalize(dbg_cx) } }, ty::Tuple(ref elements) => { let tys: Vec<_> = elements.iter().map(|k| k.expect_ty()).collect(); - prepare_tuple_metadata(cx, t, &tys, unique_type_id, usage_site_span, NO_SCOPE_METADATA) - .finalize(cx) + prepare_tuple_metadata(dbg_cx, t, &tys, unique_type_id, usage_site_span, NO_SCOPE_METADATA) + .finalize(dbg_cx) } _ => bug!("debuginfo: unexpected type in type_metadata: {:?}", t), }; { - let mut type_map = debug_context(cx).type_map.borrow_mut(); + let mut type_map = dbg_cx.type_map.borrow_mut(); if already_stored_in_typemap { // Also make sure that we already have a `TypeMap` entry for the unique type ID. @@ -752,7 +748,7 @@ pub fn type_metadata(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>, usage_site_span: Sp } pub fn file_metadata( - cx: &CodegenCx<'ll, '_>, + dbg_cx: &CrateDebugContext<'ll, '_>, file_name: &FileName, defining_crate: CrateNum, ) -> &'ll DIFile { @@ -760,27 +756,27 @@ pub fn file_metadata( let file_name = Some(file_name.to_string()); let directory = if defining_crate == LOCAL_CRATE { - Some(cx.sess().working_dir.0.to_string_lossy().to_string()) + Some(dbg_cx.tcx.sess.working_dir.0.to_string_lossy().to_string()) } else { // If the path comes from an upstream crate we assume it has been made // independent of the compiler's working directory one way or another. None }; - file_metadata_raw(cx, file_name, directory) + file_metadata_raw(dbg_cx, file_name, directory) } -pub fn unknown_file_metadata(cx: &CodegenCx<'ll, '_>) -> &'ll DIFile { - file_metadata_raw(cx, None, None) +pub fn unknown_file_metadata(dbg_cx: &CrateDebugContext<'ll, '_>) -> &'ll DIFile { + file_metadata_raw(dbg_cx, None, None) } fn file_metadata_raw( - cx: &CodegenCx<'ll, '_>, + dbg_cx: &CrateDebugContext<'ll, '_>, file_name: Option, directory: Option, ) -> &'ll DIFile { let key = (file_name, directory); - match debug_context(cx).created_files.borrow_mut().entry(key) { + match dbg_cx.created_files.borrow_mut().entry(key) { Entry::Occupied(o) => return o.get(), Entry::Vacant(v) => { let (file_name, directory) = v.key(); @@ -795,7 +791,7 @@ fn file_metadata_raw( SmallCStr::new(if let Some(directory) = directory { &directory } else { "" }); let file_metadata = unsafe { - llvm::LLVMRustDIBuilderCreateFile(DIB(cx), file_name.as_ptr(), directory.as_ptr()) + llvm::LLVMRustDIBuilderCreateFile(dbg_cx.builder, file_name.as_ptr(), directory.as_ptr()) }; v.insert(file_metadata); @@ -804,7 +800,7 @@ fn file_metadata_raw( } } -fn basic_type_metadata(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) -> &'ll DIType { +fn basic_type_metadata(dbg_cx: &CrateDebugContext<'ll, 'tcx>, t: Ty<'tcx>) -> &'ll DIType { debug!("basic_type_metadata: {:?}", t); let (name, encoding) = match t.kind { @@ -818,11 +814,11 @@ fn basic_type_metadata(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) -> &'ll DIType { _ => bug!("debuginfo::basic_type_metadata - `t` is invalid type"), }; - let (size, align) = cx.size_and_align_of(t); + let (size, align) = dbg_cx.size_and_align_of(t); let name = SmallCStr::new(name); let ty_metadata = unsafe { llvm::LLVMRustDIBuilderCreateBasicType( - DIB(cx), + dbg_cx.builder, name.as_ptr(), size.bits(), align.bits() as u32, @@ -834,27 +830,27 @@ fn basic_type_metadata(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) -> &'ll DIType { } fn foreign_type_metadata( - cx: &CodegenCx<'ll, 'tcx>, + dbg_cx: &CrateDebugContext<'ll, 'tcx>, t: Ty<'tcx>, unique_type_id: UniqueTypeId, ) -> &'ll DIType { debug!("foreign_type_metadata: {:?}", t); - let name = compute_debuginfo_type_name(cx.tcx, t, false); - create_struct_stub(cx, t, &name, unique_type_id, NO_SCOPE_METADATA) + let name = compute_debuginfo_type_name(dbg_cx.tcx, t, false); + create_struct_stub(dbg_cx, t, &name, unique_type_id, NO_SCOPE_METADATA) } fn pointer_type_metadata( - cx: &CodegenCx<'ll, 'tcx>, + dbg_cx: &CrateDebugContext<'ll, 'tcx>, pointer_type: Ty<'tcx>, pointee_type_metadata: &'ll DIType, ) -> &'ll DIType { - let (pointer_size, pointer_align) = cx.size_and_align_of(pointer_type); - let name = compute_debuginfo_type_name(cx.tcx, pointer_type, false); + let (pointer_size, pointer_align) = dbg_cx.size_and_align_of(pointer_type); + let name = compute_debuginfo_type_name(dbg_cx.tcx, pointer_type, false); let name = SmallCStr::new(&name); unsafe { llvm::LLVMRustDIBuilderCreatePointerType( - DIB(cx), + dbg_cx.builder, pointee_type_metadata, pointer_size.bits(), pointer_align.bits() as u32, @@ -1018,23 +1014,23 @@ struct MemberDescription<'ll> { impl<'ll> MemberDescription<'ll> { fn into_metadata( self, - cx: &CodegenCx<'ll, '_>, + dbg_cx: &CrateDebugContext<'ll, '_>, composite_type_metadata: &'ll DIScope, ) -> &'ll DIType { let member_name = CString::new(self.name).unwrap(); unsafe { llvm::LLVMRustDIBuilderCreateVariantMemberType( - DIB(cx), + dbg_cx.builder, composite_type_metadata, member_name.as_ptr(), - unknown_file_metadata(cx), + unknown_file_metadata(dbg_cx), UNKNOWN_LINE_NUMBER, self.size.bits(), self.align.bits() as u32, self.offset.bits(), match self.discriminant { None => None, - Some(value) => Some(cx.const_u64(value)), + Some(value) => Some(llvm::LLVMConstInt(llvm::LLVMInt64TypeInContext(dbg_cx.llcontext), value, llvm::False)), }, self.flags, self.type_metadata, @@ -1056,13 +1052,13 @@ enum MemberDescriptionFactory<'ll, 'tcx> { } impl MemberDescriptionFactory<'ll, 'tcx> { - fn create_member_descriptions(&self, cx: &CodegenCx<'ll, 'tcx>) -> Vec> { + fn create_member_descriptions(&self, dbg_cx: &CrateDebugContext<'ll, 'tcx>) -> Vec> { match *self { - StructMDF(ref this) => this.create_member_descriptions(cx), - TupleMDF(ref this) => this.create_member_descriptions(cx), - EnumMDF(ref this) => this.create_member_descriptions(cx), - UnionMDF(ref this) => this.create_member_descriptions(cx), - VariantMDF(ref this) => this.create_member_descriptions(cx), + StructMDF(ref this) => this.create_member_descriptions(dbg_cx), + TupleMDF(ref this) => this.create_member_descriptions(dbg_cx), + EnumMDF(ref this) => this.create_member_descriptions(dbg_cx), + UnionMDF(ref this) => this.create_member_descriptions(dbg_cx), + VariantMDF(ref this) => this.create_member_descriptions(dbg_cx), } } } @@ -1079,8 +1075,8 @@ struct StructMemberDescriptionFactory<'tcx> { } impl<'tcx> StructMemberDescriptionFactory<'tcx> { - fn create_member_descriptions(&self, cx: &CodegenCx<'ll, 'tcx>) -> Vec> { - let layout = cx.layout_of(self.ty); + fn create_member_descriptions(&self, dbg_cx: &CrateDebugContext<'ll, 'tcx>) -> Vec> { + let layout = dbg_cx.layout_of(self.ty); self.variant .fields .iter() @@ -1091,10 +1087,10 @@ impl<'tcx> StructMemberDescriptionFactory<'tcx> { } else { f.ident.to_string() }; - let field = layout.field(cx, i); + let field = layout.field(dbg_cx, i); MemberDescription { name, - type_metadata: type_metadata(cx, field.ty, self.span), + type_metadata: type_metadata(dbg_cx, field.ty, self.span), offset: layout.fields.offset(i), size: field.size, align: field.align.abi, @@ -1107,25 +1103,25 @@ impl<'tcx> StructMemberDescriptionFactory<'tcx> { } fn prepare_struct_metadata( - cx: &CodegenCx<'ll, 'tcx>, + dbg_cx: &CrateDebugContext<'ll, 'tcx>, struct_type: Ty<'tcx>, unique_type_id: UniqueTypeId, span: Span, ) -> RecursiveTypeDescription<'ll, 'tcx> { - let struct_name = compute_debuginfo_type_name(cx.tcx, struct_type, false); + let struct_name = compute_debuginfo_type_name(dbg_cx.tcx, struct_type, false); let (struct_def_id, variant) = match struct_type.kind { ty::Adt(def, _) => (def.did, def.non_enum_variant()), _ => bug!("prepare_struct_metadata on a non-ADT"), }; - let containing_scope = get_namespace_for_item(cx, struct_def_id); + let containing_scope = get_namespace_for_item(dbg_cx, struct_def_id); let struct_metadata_stub = - create_struct_stub(cx, struct_type, &struct_name, unique_type_id, Some(containing_scope)); + create_struct_stub(dbg_cx, struct_type, &struct_name, unique_type_id, Some(containing_scope)); create_and_register_recursive_type_forward_declaration( - cx, + dbg_cx, struct_type, unique_type_id, struct_metadata_stub, @@ -1146,16 +1142,16 @@ struct TupleMemberDescriptionFactory<'tcx> { } impl<'tcx> TupleMemberDescriptionFactory<'tcx> { - fn create_member_descriptions(&self, cx: &CodegenCx<'ll, 'tcx>) -> Vec> { - let layout = cx.layout_of(self.ty); + fn create_member_descriptions(&self, dbg_cx: &CrateDebugContext<'ll, 'tcx>) -> Vec> { + let layout = dbg_cx.layout_of(self.ty); self.component_types .iter() .enumerate() .map(|(i, &component_type)| { - let (size, align) = cx.size_and_align_of(component_type); + let (size, align) = dbg_cx.size_and_align_of(component_type); MemberDescription { name: format!("__{}", i), - type_metadata: type_metadata(cx, component_type, self.span), + type_metadata: type_metadata(dbg_cx, component_type, self.span), offset: layout.fields.offset(i), size, align, @@ -1168,20 +1164,20 @@ impl<'tcx> TupleMemberDescriptionFactory<'tcx> { } fn prepare_tuple_metadata( - cx: &CodegenCx<'ll, 'tcx>, + dbg_cx: &CrateDebugContext<'ll, 'tcx>, tuple_type: Ty<'tcx>, component_types: &[Ty<'tcx>], unique_type_id: UniqueTypeId, span: Span, containing_scope: Option<&'ll DIScope>, ) -> RecursiveTypeDescription<'ll, 'tcx> { - let tuple_name = compute_debuginfo_type_name(cx.tcx, tuple_type, false); + let tuple_name = compute_debuginfo_type_name(dbg_cx.tcx, tuple_type, false); let struct_stub = - create_struct_stub(cx, tuple_type, &tuple_name[..], unique_type_id, containing_scope); + create_struct_stub(dbg_cx, tuple_type, &tuple_name[..], unique_type_id, containing_scope); create_and_register_recursive_type_forward_declaration( - cx, + dbg_cx, tuple_type, unique_type_id, struct_stub, @@ -1205,16 +1201,16 @@ struct UnionMemberDescriptionFactory<'tcx> { } impl<'tcx> UnionMemberDescriptionFactory<'tcx> { - fn create_member_descriptions(&self, cx: &CodegenCx<'ll, 'tcx>) -> Vec> { + fn create_member_descriptions(&self, dbg_cx: &CrateDebugContext<'ll, 'tcx>) -> Vec> { self.variant .fields .iter() .enumerate() .map(|(i, f)| { - let field = self.layout.field(cx, i); + let field = self.layout.field(dbg_cx, i); MemberDescription { name: f.ident.to_string(), - type_metadata: type_metadata(cx, field.ty, self.span), + type_metadata: type_metadata(dbg_cx, field.ty, self.span), offset: Size::ZERO, size: field.size, align: field.align.abi, @@ -1227,30 +1223,30 @@ impl<'tcx> UnionMemberDescriptionFactory<'tcx> { } fn prepare_union_metadata( - cx: &CodegenCx<'ll, 'tcx>, + dbg_cx: &CrateDebugContext<'ll, 'tcx>, union_type: Ty<'tcx>, unique_type_id: UniqueTypeId, span: Span, ) -> RecursiveTypeDescription<'ll, 'tcx> { - let union_name = compute_debuginfo_type_name(cx.tcx, union_type, false); + let union_name = compute_debuginfo_type_name(dbg_cx.tcx, union_type, false); let (union_def_id, variant) = match union_type.kind { ty::Adt(def, _) => (def.did, def.non_enum_variant()), _ => bug!("prepare_union_metadata on a non-ADT"), }; - let containing_scope = get_namespace_for_item(cx, union_def_id); + let containing_scope = get_namespace_for_item(dbg_cx, union_def_id); let union_metadata_stub = - create_union_stub(cx, union_type, &union_name, unique_type_id, containing_scope); + create_union_stub(dbg_cx, union_type, &union_name, unique_type_id, containing_scope); create_and_register_recursive_type_forward_declaration( - cx, + dbg_cx, union_type, unique_type_id, union_metadata_stub, union_metadata_stub, - UnionMDF(UnionMemberDescriptionFactory { layout: cx.layout_of(union_type), variant, span }), + UnionMDF(UnionMemberDescriptionFactory { layout: dbg_cx.layout_of(union_type), variant, span }), ) } @@ -1266,10 +1262,10 @@ fn prepare_union_metadata( /// contains an early version of the DWARF variant support, and will /// crash when handling the new debug info format. This function /// decides which representation will be emitted. -fn use_enum_fallback(cx: &CodegenCx<'_, '_>) -> bool { +fn use_enum_fallback(dbg_cx: &CrateDebugContext<'_, '_>) -> bool { // On MSVC we have to use the fallback mode, because LLVM doesn't // lower variant parts to PDB. - return cx.sess().target.target.options.is_like_msvc + return dbg_cx.tcx.sess.target.target.options.is_like_msvc // LLVM version 7 did not release with an important bug fix; // but the required patch is in the LLVM 8. Rust LLVM reports // 8 as well. @@ -1328,10 +1324,10 @@ struct EnumMemberDescriptionFactory<'ll, 'tcx> { } impl EnumMemberDescriptionFactory<'ll, 'tcx> { - fn create_member_descriptions(&self, cx: &CodegenCx<'ll, 'tcx>) -> Vec> { + fn create_member_descriptions(&self, dbg_cx: &CrateDebugContext<'ll, 'tcx>) -> Vec> { let generator_variant_info_data = match self.enum_type.kind { ty::Generator(def_id, ..) => { - Some(generator_layout_and_saved_local_names(cx.tcx, def_id)) + Some(generator_layout_and_saved_local_names(dbg_cx.tcx, def_id)) } _ => None, }; @@ -1352,11 +1348,11 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { }; // This will always find the metadata in the type map. - let fallback = use_enum_fallback(cx); + let fallback = use_enum_fallback(dbg_cx); let self_metadata = if fallback { self.containing_scope } else { - type_metadata(cx, self.enum_type, self.span) + type_metadata(dbg_cx, self.enum_type, self.span) }; match self.layout.variants { @@ -1369,7 +1365,7 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { let variant_info = variant_info_for(index); let (variant_type_metadata, member_description_factory) = describe_enum_variant( - cx, + dbg_cx, self.layout, variant_info, NoDiscriminant, @@ -1377,10 +1373,10 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { self.span, ); - let member_descriptions = member_description_factory.create_member_descriptions(cx); + let member_descriptions = member_description_factory.create_member_descriptions(dbg_cx); set_members_of_composite_type( - cx, + dbg_cx, self.enum_type, variant_type_metadata, member_descriptions, @@ -1413,10 +1409,10 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { variants .iter_enumerated() .map(|(i, _)| { - let variant = self.layout.for_variant(cx, i); + let variant = self.layout.for_variant(dbg_cx, i); let variant_info = variant_info_for(i); let (variant_type_metadata, member_desc_factory) = describe_enum_variant( - cx, + dbg_cx, variant, variant_info, discriminant_info, @@ -1425,10 +1421,10 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { ); let member_descriptions = - member_desc_factory.create_member_descriptions(cx); + member_desc_factory.create_member_descriptions(dbg_cx); set_members_of_composite_type( - cx, + dbg_cx, self.enum_type, variant_type_metadata, member_descriptions, @@ -1446,7 +1442,7 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { align: self.layout.align.abi, flags: DIFlags::FlagZero, discriminant: Some( - self.layout.ty.discriminant_for_variant(cx.tcx, i).unwrap().val + self.layout.ty.discriminant_for_variant(dbg_cx.tcx, i).unwrap().val as u64, ), } @@ -1461,10 +1457,10 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { discr_index, } => { if fallback { - let variant = self.layout.for_variant(cx, dataful_variant); + let variant = self.layout.for_variant(dbg_cx, dataful_variant); // Create a description of the non-null variant. let (variant_type_metadata, member_description_factory) = describe_enum_variant( - cx, + dbg_cx, variant, variant_info_for(dataful_variant), OptimizedDiscriminant, @@ -1473,10 +1469,10 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { ); let variant_member_descriptions = - member_description_factory.create_member_descriptions(cx); + member_description_factory.create_member_descriptions(dbg_cx); set_members_of_composite_type( - cx, + dbg_cx, self.enum_type, variant_type_metadata, variant_member_descriptions, @@ -1487,8 +1483,8 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { let mut name = String::from("RUST$ENCODED$ENUM$"); // Right now it's not even going to work for `niche_start > 0`, // and for multiple niche variants it only supports the first. - fn compute_field_path<'a, 'tcx>( - cx: &CodegenCx<'a, 'tcx>, + fn compute_field_path<'tcx>( + dbg_cx: &CrateDebugContext<'_, 'tcx>, name: &mut String, layout: TyLayout<'tcx>, offset: Size, @@ -1500,19 +1496,19 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { continue; } let inner_offset = offset - field_offset; - let field = layout.field(cx, i); + let field = layout.field(dbg_cx, i); if inner_offset + size <= field.size { write!(name, "{}$", i).unwrap(); - compute_field_path(cx, name, field, inner_offset, size); + compute_field_path(dbg_cx, name, field, inner_offset, size); } } } compute_field_path( - cx, + dbg_cx, &mut name, self.layout, self.layout.fields.offset(discr_index), - self.layout.field(cx, discr_index).size, + self.layout.field(dbg_cx, discr_index).size, ); variant_info_for(*niche_variants.start()).map_struct_name(|variant_name| { name.push_str(variant_name); @@ -1532,11 +1528,11 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { variants .iter_enumerated() .map(|(i, _)| { - let variant = self.layout.for_variant(cx, i); + let variant = self.layout.for_variant(dbg_cx, i); let variant_info = variant_info_for(i); let (variant_type_metadata, member_desc_factory) = describe_enum_variant( - cx, + dbg_cx, variant, variant_info, OptimizedDiscriminant, @@ -1545,10 +1541,10 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { ); let member_descriptions = - member_desc_factory.create_member_descriptions(cx); + member_desc_factory.create_member_descriptions(dbg_cx); set_members_of_composite_type( - cx, + dbg_cx, self.enum_type, variant_type_metadata, member_descriptions, @@ -1560,7 +1556,7 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { let value = (i.as_u32() as u128) .wrapping_sub(niche_variants.start().as_u32() as u128) .wrapping_add(niche_start); - let value = truncate(value, discr.value.size(cx)); + let value = truncate(value, discr.value.size(dbg_cx)); // NOTE(eddyb) do *NOT* remove this assert, until // we pass the full 128-bit value to LLVM, otherwise // truncation will be silent and remain undetected. @@ -1595,23 +1591,23 @@ struct VariantMemberDescriptionFactory<'ll, 'tcx> { } impl VariantMemberDescriptionFactory<'ll, 'tcx> { - fn create_member_descriptions(&self, cx: &CodegenCx<'ll, 'tcx>) -> Vec> { + fn create_member_descriptions(&self, dbg_cx: &CrateDebugContext<'ll, 'tcx>) -> Vec> { self.args .iter() .enumerate() .map(|(i, &(ref name, ty))| { - let (size, align) = cx.size_and_align_of(ty); + let (size, align) = dbg_cx.size_and_align_of(ty); MemberDescription { name: name.to_string(), - type_metadata: if use_enum_fallback(cx) { + type_metadata: if use_enum_fallback(dbg_cx) { match self.discriminant_type_metadata { // Discriminant is always the first field of our variant // when using the enum fallback. Some(metadata) if i == 0 => metadata, - _ => type_metadata(cx, ty, self.span), + _ => type_metadata(dbg_cx, ty, self.span), } } else { - type_metadata(cx, ty, self.span) + type_metadata(dbg_cx, ty, self.span) }, offset: self.offsets[i], size, @@ -1690,7 +1686,7 @@ impl<'tcx> VariantInfo<'_, 'tcx> { /// fields of the variant. This is a rudimentary version of a full /// `RecursiveTypeDescription`. fn describe_enum_variant( - cx: &CodegenCx<'ll, 'tcx>, + dbg_cx: &CrateDebugContext<'ll, 'tcx>, layout: layout::TyLayout<'tcx>, variant: VariantInfo<'_, 'tcx>, discriminant_info: EnumDiscriminantInfo<'ll>, @@ -1698,23 +1694,23 @@ fn describe_enum_variant( span: Span, ) -> (&'ll DICompositeType, MemberDescriptionFactory<'ll, 'tcx>) { let metadata_stub = variant.map_struct_name(|variant_name| { - let unique_type_id = debug_context(cx) + let unique_type_id = dbg_cx .type_map .borrow_mut() - .get_unique_type_id_of_enum_variant(cx, layout.ty, &variant_name); - create_struct_stub(cx, layout.ty, &variant_name, unique_type_id, Some(containing_scope)) + .get_unique_type_id_of_enum_variant(dbg_cx, layout.ty, &variant_name); + create_struct_stub(dbg_cx, layout.ty, &variant_name, unique_type_id, Some(containing_scope)) }); // Build an array of (field name, field type) pairs to be captured in the factory closure. - let (offsets, args) = if use_enum_fallback(cx) { + let (offsets, args) = if use_enum_fallback(dbg_cx) { // If this is not a univariant enum, there is also the discriminant field. let (discr_offset, discr_arg) = match discriminant_info { RegularDiscriminant { discr_field, .. } => { // We have the layout of an enum variant, we need the layout of the outer enum - let enum_layout = cx.layout_of(layout.ty); + let enum_layout = dbg_cx.layout_of(layout.ty); let offset = enum_layout.fields.offset(discr_field.as_usize()); let args = - ("RUST$ENUM$DISR".to_owned(), enum_layout.field(cx, discr_field.as_usize()).ty); + ("RUST$ENUM$DISR".to_owned(), enum_layout.field(dbg_cx, discr_field.as_usize()).ty); (Some(offset), Some(args)) } _ => (None, None), @@ -1728,7 +1724,7 @@ fn describe_enum_variant( .into_iter() .chain( (0..layout.fields.count()) - .map(|i| (variant.field_name(i), layout.field(cx, i).ty)), + .map(|i| (variant.field_name(i), layout.field(dbg_cx, i).ty)), ) .collect(), ) @@ -1736,7 +1732,7 @@ fn describe_enum_variant( ( (0..layout.fields.count()).map(|i| layout.fields.offset(i)).collect(), (0..layout.fields.count()) - .map(|i| (variant.field_name(i), layout.field(cx, i).ty)) + .map(|i| (variant.field_name(i), layout.field(dbg_cx, i).ty)) .collect(), ) }; @@ -1755,34 +1751,34 @@ fn describe_enum_variant( } fn prepare_enum_metadata( - cx: &CodegenCx<'ll, 'tcx>, + dbg_cx: &CrateDebugContext<'ll, 'tcx>, enum_type: Ty<'tcx>, enum_def_id: DefId, unique_type_id: UniqueTypeId, span: Span, outer_field_tys: Vec>, ) -> RecursiveTypeDescription<'ll, 'tcx> { - let enum_name = compute_debuginfo_type_name(cx.tcx, enum_type, false); + let enum_name = compute_debuginfo_type_name(dbg_cx.tcx, enum_type, false); - let containing_scope = get_namespace_for_item(cx, enum_def_id); + let containing_scope = get_namespace_for_item(dbg_cx, enum_def_id); // FIXME: This should emit actual file metadata for the enum, but we // currently can't get the necessary information when it comes to types // imported from other crates. Formerly we violated the ODR when performing // LTO because we emitted debuginfo for the same type with varying file // metadata, so as a workaround we pretend that the type comes from // - let file_metadata = unknown_file_metadata(cx); + let file_metadata = unknown_file_metadata(dbg_cx); let discriminant_type_metadata = |discr: layout::Primitive| { let enumerators_metadata: Vec<_> = match enum_type.kind { ty::Adt(def, _) => def - .discriminants(cx.tcx) + .discriminants(dbg_cx.tcx) .zip(&def.variants) .map(|((_, discr), v)| { let name = SmallCStr::new(&v.ident.as_str()); unsafe { Some(llvm::LLVMRustDIBuilderCreateEnumerator( - DIB(cx), + dbg_cx.builder, name.as_ptr(), // FIXME: what if enumeration has i128 discriminant? discr.val as u64, @@ -1792,12 +1788,12 @@ fn prepare_enum_metadata( .collect(), ty::Generator(_, substs, _) => substs .as_generator() - .variant_range(enum_def_id, cx.tcx) + .variant_range(enum_def_id, dbg_cx.tcx) .map(|variant_index| { let name = SmallCStr::new(&substs.as_generator().variant_name(variant_index)); unsafe { Some(llvm::LLVMRustDIBuilderCreateEnumerator( - DIB(cx), + dbg_cx.builder, name.as_ptr(), // FIXME: what if enumeration has i128 discriminant? variant_index.as_usize() as u64, @@ -1810,36 +1806,36 @@ fn prepare_enum_metadata( let disr_type_key = (enum_def_id, discr); let cached_discriminant_type_metadata = - debug_context(cx).created_enum_disr_types.borrow().get(&disr_type_key).cloned(); + dbg_cx.created_enum_disr_types.borrow().get(&disr_type_key).cloned(); match cached_discriminant_type_metadata { Some(discriminant_type_metadata) => discriminant_type_metadata, None => { - let (discriminant_size, discriminant_align) = (discr.size(cx), discr.align(cx)); + let (discriminant_size, discriminant_align) = (discr.size(dbg_cx), discr.align(dbg_cx)); let discriminant_base_type_metadata = - type_metadata(cx, discr.to_ty(cx.tcx), rustc_span::DUMMY_SP); + type_metadata(dbg_cx, discr.to_ty(dbg_cx.tcx), rustc_span::DUMMY_SP); let discriminant_name = match enum_type.kind { - ty::Adt(..) => SmallCStr::new(&cx.tcx.item_name(enum_def_id).as_str()), + ty::Adt(..) => SmallCStr::new(&dbg_cx.tcx.item_name(enum_def_id).as_str()), ty::Generator(..) => SmallCStr::new(&enum_name), _ => bug!(), }; let discriminant_type_metadata = unsafe { llvm::LLVMRustDIBuilderCreateEnumerationType( - DIB(cx), + dbg_cx.builder, containing_scope, discriminant_name.as_ptr(), file_metadata, UNKNOWN_LINE_NUMBER, discriminant_size.bits(), discriminant_align.abi.bits() as u32, - create_DIArray(DIB(cx), &enumerators_metadata), + create_DIArray(dbg_cx.builder, &enumerators_metadata), discriminant_base_type_metadata, true, ) }; - debug_context(cx) + dbg_cx .created_enum_disr_types .borrow_mut() .insert(disr_type_key, discriminant_type_metadata); @@ -1849,7 +1845,7 @@ fn prepare_enum_metadata( } }; - let layout = cx.layout_of(enum_type); + let layout = dbg_cx.layout_of(enum_type); match (&layout.abi, &layout.variants) { ( @@ -1865,10 +1861,10 @@ fn prepare_enum_metadata( let enum_name = SmallCStr::new(&enum_name); let unique_type_id_str = SmallCStr::new( - debug_context(cx).type_map.borrow().get_unique_type_id_as_string(unique_type_id), + dbg_cx.type_map.borrow().get_unique_type_id_as_string(unique_type_id), ); - if use_enum_fallback(cx) { + if use_enum_fallback(dbg_cx) { let discriminant_type_metadata = match layout.variants { layout::Variants::Single { .. } | layout::Variants::Multiple { @@ -1884,7 +1880,7 @@ fn prepare_enum_metadata( let enum_metadata = unsafe { llvm::LLVMRustDIBuilderCreateUnionType( - DIB(cx), + dbg_cx.builder, containing_scope, enum_name.as_ptr(), file_metadata, @@ -1899,7 +1895,7 @@ fn prepare_enum_metadata( }; return create_and_register_recursive_type_forward_declaration( - cx, + dbg_cx, enum_type, unique_type_id, enum_metadata, @@ -1930,21 +1926,21 @@ fn prepare_enum_metadata( .. } => { // Find the integer type of the correct size. - let size = discr.value.size(cx); - let align = discr.value.align(cx); + let size = discr.value.size(dbg_cx); + let align = discr.value.align(dbg_cx); let discr_type = match discr.value { layout::Int(t, _) => t, layout::F32 => Integer::I32, layout::F64 => Integer::I64, - layout::Pointer => cx.data_layout().ptr_sized_integer(), + layout::Pointer => dbg_cx.data_layout().ptr_sized_integer(), } - .to_ty(cx.tcx, false); + .to_ty(dbg_cx.tcx, false); - let discr_metadata = basic_type_metadata(cx, discr_type); + let discr_metadata = basic_type_metadata(dbg_cx, discr_type); unsafe { Some(llvm::LLVMRustDIBuilderCreateMemberType( - DIB(cx), + dbg_cx.builder, containing_scope, discriminator_name, file_metadata, @@ -1964,13 +1960,13 @@ fn prepare_enum_metadata( discr_index, .. } => { - let discr_type = discr.value.to_ty(cx.tcx); - let (size, align) = cx.size_and_align_of(discr_type); + let discr_type = discr.value.to_ty(dbg_cx.tcx); + let (size, align) = dbg_cx.size_and_align_of(discr_type); - let discr_metadata = basic_type_metadata(cx, discr_type); + let discr_metadata = basic_type_metadata(dbg_cx, discr_type); unsafe { Some(llvm::LLVMRustDIBuilderCreateMemberType( - DIB(cx), + dbg_cx.builder, containing_scope, discriminator_name, file_metadata, @@ -1994,23 +1990,23 @@ fn prepare_enum_metadata( span, }; tuple_mdf - .create_member_descriptions(cx) + .create_member_descriptions(dbg_cx) .into_iter() - .map(|desc| Some(desc.into_metadata(cx, containing_scope))) + .map(|desc| Some(desc.into_metadata(dbg_cx, containing_scope))) .collect() } }; let variant_part_unique_type_id_str = SmallCStr::new( - debug_context(cx) + dbg_cx .type_map .borrow_mut() .get_unique_type_id_str_of_enum_variant_part(unique_type_id), ); - let empty_array = create_DIArray(DIB(cx), &[]); + let empty_array = create_DIArray(dbg_cx.builder, &[]); let variant_part = unsafe { llvm::LLVMRustDIBuilderCreateVariantPart( - DIB(cx), + dbg_cx.builder, containing_scope, ptr::null_mut(), file_metadata, @@ -2026,10 +2022,10 @@ fn prepare_enum_metadata( outer_fields.push(Some(variant_part)); // The variant part must be wrapped in a struct according to DWARF. - let type_array = create_DIArray(DIB(cx), &outer_fields); + let type_array = create_DIArray(dbg_cx.builder, &outer_fields); let struct_wrapper = unsafe { llvm::LLVMRustDIBuilderCreateStructType( - DIB(cx), + dbg_cx.builder, Some(containing_scope), enum_name.as_ptr(), file_metadata, @@ -2046,7 +2042,7 @@ fn prepare_enum_metadata( }; return create_and_register_recursive_type_forward_declaration( - cx, + dbg_cx, enum_type, unique_type_id, struct_wrapper, @@ -2066,7 +2062,7 @@ fn prepare_enum_metadata( /// /// Examples of Rust types to use this are: structs, tuples, boxes, vecs, and enums. fn composite_type_metadata( - cx: &CodegenCx<'ll, 'tcx>, + dbg_cx: &CrateDebugContext<'ll, 'tcx>, composite_type: Ty<'tcx>, composite_type_name: &str, composite_type_unique_id: UniqueTypeId, @@ -2080,20 +2076,20 @@ fn composite_type_metadata( ) -> &'ll DICompositeType { // Create the (empty) struct metadata node ... let composite_type_metadata = create_struct_stub( - cx, + dbg_cx, composite_type, composite_type_name, composite_type_unique_id, containing_scope, ); // ... and immediately create and add the member descriptions. - set_members_of_composite_type(cx, composite_type, composite_type_metadata, member_descriptions); + set_members_of_composite_type(dbg_cx, composite_type, composite_type_metadata, member_descriptions); composite_type_metadata } fn set_members_of_composite_type( - cx: &CodegenCx<'ll, 'tcx>, + dbg_cx: &CrateDebugContext<'ll, 'tcx>, composite_type: Ty<'tcx>, composite_type_metadata: &'ll DICompositeType, member_descriptions: Vec>, @@ -2106,7 +2102,7 @@ fn set_members_of_composite_type( // regression. { let mut composite_types_completed = - debug_context(cx).composite_types_completed.borrow_mut(); + dbg_cx.composite_types_completed.borrow_mut(); if !composite_types_completed.insert(&composite_type_metadata) { bug!( "debuginfo::set_members_of_composite_type() - \ @@ -2117,14 +2113,14 @@ fn set_members_of_composite_type( let member_metadata: Vec<_> = member_descriptions .into_iter() - .map(|desc| Some(desc.into_metadata(cx, composite_type_metadata))) + .map(|desc| Some(desc.into_metadata(dbg_cx, composite_type_metadata))) .collect(); - let type_params = compute_type_parameters(cx, composite_type); + let type_params = compute_type_parameters(dbg_cx, composite_type); unsafe { - let type_array = create_DIArray(DIB(cx), &member_metadata[..]); + let type_array = create_DIArray(dbg_cx.builder, &member_metadata[..]); llvm::LLVMRustDICompositeTypeReplaceArrays( - DIB(cx), + dbg_cx.builder, composite_type_metadata, Some(type_array), type_params, @@ -2133,28 +2129,28 @@ fn set_members_of_composite_type( } /// Computes the type parameters for a type, if any, for the given metadata. -fn compute_type_parameters(cx: &CodegenCx<'ll, 'tcx>, ty: Ty<'tcx>) -> Option<&'ll DIArray> { +fn compute_type_parameters(dbg_cx: &CrateDebugContext<'ll, 'tcx>, ty: Ty<'tcx>) -> Option<&'ll DIArray> { if let ty::Adt(def, substs) = ty.kind { if !substs.types().next().is_none() { - let generics = cx.tcx.generics_of(def.did); - let names = get_parameter_names(cx, generics); + let generics = dbg_cx.tcx.generics_of(def.did); + let names = get_parameter_names(dbg_cx.tcx, generics); let template_params: Vec<_> = substs .iter() .zip(names) .filter_map(|(kind, name)| { if let GenericArgKind::Type(ty) = kind.unpack() { let actual_type = - cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), ty); + dbg_cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), ty); let actual_type_metadata = - type_metadata(cx, actual_type, rustc_span::DUMMY_SP); + type_metadata(dbg_cx, actual_type, rustc_span::DUMMY_SP); let name = SmallCStr::new(&name.as_str()); Some(unsafe { Some(llvm::LLVMRustDIBuilderCreateTemplateTypeParameter( - DIB(cx), + dbg_cx.builder, None, name.as_ptr(), actual_type_metadata, - unknown_file_metadata(cx), + unknown_file_metadata(dbg_cx), 0, 0, )) @@ -2165,15 +2161,15 @@ fn compute_type_parameters(cx: &CodegenCx<'ll, 'tcx>, ty: Ty<'tcx>) -> Option<&' }) .collect(); - return Some(create_DIArray(DIB(cx), &template_params[..])); + return Some(create_DIArray(dbg_cx.builder, &template_params[..])); } } - return Some(create_DIArray(DIB(cx), &[])); + return Some(create_DIArray(dbg_cx.builder, &[])); - fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec { + fn get_parameter_names(tcx: TyCtxt<'_>, generics: &ty::Generics) -> Vec { let mut names = generics .parent - .map_or(vec![], |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id))); + .map_or(vec![], |def_id| get_parameter_names(tcx, tcx.generics_of(def_id))); names.extend(generics.params.iter().map(|param| param.name)); names } @@ -2183,29 +2179,29 @@ fn compute_type_parameters(cx: &CodegenCx<'ll, 'tcx>, ty: Ty<'tcx>) -> Option<&' /// any caching, does not add any fields to the struct. This can be done later /// with `set_members_of_composite_type()`. fn create_struct_stub( - cx: &CodegenCx<'ll, 'tcx>, + dbg_cx: &CrateDebugContext<'ll, 'tcx>, struct_type: Ty<'tcx>, struct_type_name: &str, unique_type_id: UniqueTypeId, containing_scope: Option<&'ll DIScope>, ) -> &'ll DICompositeType { - let (struct_size, struct_align) = cx.size_and_align_of(struct_type); + let (struct_size, struct_align) = dbg_cx.size_and_align_of(struct_type); let name = SmallCStr::new(struct_type_name); let unique_type_id = SmallCStr::new( - debug_context(cx).type_map.borrow().get_unique_type_id_as_string(unique_type_id), + dbg_cx.type_map.borrow().get_unique_type_id_as_string(unique_type_id), ); let metadata_stub = unsafe { // `LLVMRustDIBuilderCreateStructType()` wants an empty array. A null // pointer will lead to hard to trace and debug LLVM assertions // later on in `llvm/lib/IR/Value.cpp`. - let empty_array = create_DIArray(DIB(cx), &[]); + let empty_array = create_DIArray(dbg_cx.builder, &[]); llvm::LLVMRustDIBuilderCreateStructType( - DIB(cx), + dbg_cx.builder, containing_scope, name.as_ptr(), - unknown_file_metadata(cx), + unknown_file_metadata(dbg_cx), UNKNOWN_LINE_NUMBER, struct_size.bits(), struct_align.bits() as u32, @@ -2222,29 +2218,29 @@ fn create_struct_stub( } fn create_union_stub( - cx: &CodegenCx<'ll, 'tcx>, + dbg_cx: &CrateDebugContext<'ll, 'tcx>, union_type: Ty<'tcx>, union_type_name: &str, unique_type_id: UniqueTypeId, containing_scope: &'ll DIScope, ) -> &'ll DICompositeType { - let (union_size, union_align) = cx.size_and_align_of(union_type); + let (union_size, union_align) = dbg_cx.size_and_align_of(union_type); let name = SmallCStr::new(union_type_name); let unique_type_id = SmallCStr::new( - debug_context(cx).type_map.borrow().get_unique_type_id_as_string(unique_type_id), + dbg_cx.type_map.borrow().get_unique_type_id_as_string(unique_type_id), ); let metadata_stub = unsafe { // `LLVMRustDIBuilderCreateUnionType()` wants an empty array. A null // pointer will lead to hard to trace and debug LLVM assertions // later on in `llvm/lib/IR/Value.cpp`. - let empty_array = create_DIArray(DIB(cx), &[]); + let empty_array = create_DIArray(dbg_cx.builder, &[]); llvm::LLVMRustDIBuilderCreateUnionType( - DIB(cx), + dbg_cx.builder, containing_scope, name.as_ptr(), - unknown_file_metadata(cx), + unknown_file_metadata(dbg_cx), UNKNOWN_LINE_NUMBER, union_size.bits(), union_align.bits() as u32, @@ -2261,12 +2257,8 @@ fn create_union_stub( /// Creates debug information for the given global variable. /// /// Adds the created metadata nodes directly to the crate's IR. -pub fn create_global_var_metadata(cx: &CodegenCx<'ll, '_>, def_id: DefId, global: &'ll Value) { - if cx.dbg_cx.is_none() { - return; - } - - let tcx = cx.tcx; +pub fn create_global_var_metadata(dbg_cx: &CrateDebugContext<'ll, '_>, def_id: DefId, global: &'ll Value) { + let tcx = dbg_cx.tcx; let attrs = tcx.codegen_fn_attrs(def_id); if attrs.flags.contains(CodegenFnAttrFlags::NO_DEBUG) { @@ -2276,32 +2268,32 @@ pub fn create_global_var_metadata(cx: &CodegenCx<'ll, '_>, def_id: DefId, global let no_mangle = attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE); // We may want to remove the namespace scope if we're in an extern block (see // https://github.com/rust-lang/rust/pull/46457#issuecomment-351750952). - let var_scope = get_namespace_for_item(cx, def_id); + let var_scope = get_namespace_for_item(dbg_cx, def_id); let span = tcx.def_span(def_id); let (file_metadata, line_number) = if !span.is_dummy() { - let loc = span_start(cx, span); - (file_metadata(cx, &loc.file.name, LOCAL_CRATE), loc.line as c_uint) + let loc = span_start(tcx, span); + (file_metadata(dbg_cx, &loc.file.name, LOCAL_CRATE), loc.line as c_uint) } else { - (unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER) + (unknown_file_metadata(dbg_cx), UNKNOWN_LINE_NUMBER) }; - let is_local_to_unit = is_node_local_to_unit(cx, def_id); - let variable_type = Instance::mono(cx.tcx, def_id).monomorphic_ty(cx.tcx); - let type_metadata = type_metadata(cx, variable_type, span); + let is_local_to_unit = is_node_local_to_unit(tcx, def_id); + let variable_type = Instance::mono(dbg_cx.tcx, def_id).monomorphic_ty(dbg_cx.tcx); + let type_metadata = type_metadata(dbg_cx, variable_type, span); let var_name = SmallCStr::new(&tcx.item_name(def_id).as_str()); let linkage_name = if no_mangle { None } else { - let linkage_name = mangled_name_of_instance(cx, Instance::mono(tcx, def_id)); + let linkage_name = dbg_cx.tcx.symbol_name(Instance::mono(tcx, def_id)); Some(SmallCStr::new(&linkage_name.name.as_str())) }; - let global_align = cx.align_of(variable_type); + let global_align = dbg_cx.layout_of(variable_type).align.abi; unsafe { llvm::LLVMRustDIBuilderCreateStaticVariable( - DIB(cx), + dbg_cx.builder, Some(var_scope), var_name.as_ptr(), // If null, linkage_name field is omitted, @@ -2322,18 +2314,14 @@ pub fn create_global_var_metadata(cx: &CodegenCx<'ll, '_>, def_id: DefId, global /// given type. /// /// Adds the created metadata nodes directly to the crate's IR. -pub fn create_vtable_metadata(cx: &CodegenCx<'ll, 'tcx>, ty: Ty<'tcx>, vtable: &'ll Value) { - if cx.dbg_cx.is_none() { - return; - } - - let type_metadata = type_metadata(cx, ty, rustc_span::DUMMY_SP); +pub fn create_vtable_metadata(dbg_cx: &CrateDebugContext<'ll, 'tcx>, ty: Ty<'tcx>, vtable: &'ll Value) { + let type_metadata = type_metadata(dbg_cx, ty, rustc_span::DUMMY_SP); unsafe { // `LLVMRustDIBuilderCreateStructType()` wants an empty array. A null // pointer will lead to hard to trace and debug LLVM assertions // later on in `llvm/lib/IR/Value.cpp`. - let empty_array = create_DIArray(DIB(cx), &[]); + let empty_array = create_DIArray(dbg_cx.builder, &[]); let name = const_cstr!("vtable"); @@ -2341,13 +2329,13 @@ pub fn create_vtable_metadata(cx: &CodegenCx<'ll, 'tcx>, ty: Ty<'tcx>, vtable: & // here, because each vtable will refer to a unique containing // type. let vtable_type = llvm::LLVMRustDIBuilderCreateStructType( - DIB(cx), + dbg_cx.builder, NO_SCOPE_METADATA, name.as_ptr(), - unknown_file_metadata(cx), + unknown_file_metadata(dbg_cx), UNKNOWN_LINE_NUMBER, Size::ZERO.bits(), - cx.tcx.data_layout.pointer_align.abi.bits() as u32, + dbg_cx.tcx.data_layout.pointer_align.abi.bits() as u32, DIFlags::FlagArtificial, None, empty_array, @@ -2357,11 +2345,11 @@ pub fn create_vtable_metadata(cx: &CodegenCx<'ll, 'tcx>, ty: Ty<'tcx>, vtable: & ); llvm::LLVMRustDIBuilderCreateStaticVariable( - DIB(cx), + dbg_cx.builder, NO_SCOPE_METADATA, name.as_ptr(), ptr::null(), - unknown_file_metadata(cx), + unknown_file_metadata(dbg_cx), UNKNOWN_LINE_NUMBER, vtable_type, true, @@ -2374,11 +2362,11 @@ pub fn create_vtable_metadata(cx: &CodegenCx<'ll, 'tcx>, ty: Ty<'tcx>, vtable: & /// Creates an "extension" of an existing `DIScope` into another file. pub fn extend_scope_to_file( - cx: &CodegenCx<'ll, '_>, + dbg_cx: &CrateDebugContext<'ll, '_>, scope_metadata: &'ll DIScope, file: &rustc_span::SourceFile, defining_crate: CrateNum, ) -> &'ll DILexicalBlock { - let file_metadata = file_metadata(cx, &file.name, defining_crate); - unsafe { llvm::LLVMRustDIBuilderCreateLexicalBlockFile(DIB(cx), scope_metadata, file_metadata) } + let file_metadata = file_metadata(dbg_cx, &file.name, defining_crate); + unsafe { llvm::LLVMRustDIBuilderCreateLexicalBlockFile(dbg_cx.builder, scope_metadata, file_metadata) } } diff --git a/src/librustc_codegen_llvm/debuginfo/mod.rs b/src/librustc_codegen_llvm/debuginfo/mod.rs index c4a52a73e25d9..4b48bf79f561b 100644 --- a/src/librustc_codegen_llvm/debuginfo/mod.rs +++ b/src/librustc_codegen_llvm/debuginfo/mod.rs @@ -4,10 +4,9 @@ mod doc; use rustc_codegen_ssa::mir::debuginfo::VariableKind::*; use self::metadata::{file_metadata, type_metadata, TypeMap}; -use self::namespace::mangled_name_of_instance; use self::source_loc::InternalDebugLocation::{self, UnknownLocation}; use self::type_names::compute_debuginfo_type_name; -use self::utils::{create_DIArray, is_node_local_to_unit, span_start, DIB}; +use self::utils::{create_DIArray, is_node_local_to_unit, span_start, DIB, debug_context}; use crate::llvm; use crate::llvm::debuginfo::{ @@ -23,7 +22,7 @@ use crate::common::CodegenCx; use crate::value::Value; use rustc::mir; use rustc::session::config::{self, DebugInfo}; -use rustc::ty::{self, Instance, InstanceDef, ParamEnv, Ty}; +use rustc::ty::{self, Instance, InstanceDef, ParamEnv, Ty, TyCtxt}; use rustc_codegen_ssa::debuginfo::type_names; use rustc_codegen_ssa::mir::debuginfo::{DebugScope, FunctionDebugContext, VariableKind}; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; @@ -35,10 +34,10 @@ use log::debug; use std::cell::RefCell; use std::ffi::CString; -use rustc::ty::layout::{self, HasTyCtxt, LayoutOf, Size}; +use rustc::ty::layout::{self, HasTyCtxt, LayoutOf, Size, Align, TyLayout, LayoutError}; use rustc_codegen_ssa::traits::*; use rustc_span::symbol::Symbol; -use rustc_span::{self, BytePos, Pos, Span}; +use rustc_span::{self, BytePos, Pos, Span, DUMMY_SP}; use smallvec::SmallVec; use syntax::ast; @@ -61,6 +60,7 @@ const DW_TAG_arg_variable: c_uint = 0x101; /// A context object for maintaining all state needed by the debuginfo module. pub struct CrateDebugContext<'a, 'tcx> { + tcx: TyCtxt<'tcx>, llcontext: &'a llvm::Context, llmod: &'a llvm::Module, builder: &'a mut DIBuilder<'a>, @@ -84,12 +84,13 @@ impl Drop for CrateDebugContext<'a, 'tcx> { } impl<'a, 'tcx> CrateDebugContext<'a, 'tcx> { - pub fn new(llmod: &'a llvm::Module) -> Self { + pub fn new(tcx: TyCtxt<'tcx>, llmod: &'a llvm::Module) -> Self { debug!("CrateDebugContext::new"); let builder = unsafe { llvm::LLVMRustDIBuilderCreate(llmod) }; // DIBuilder inherits context from the module, so we'd better use the same one let llcontext = unsafe { llvm::LLVMGetModuleContext(llmod) }; CrateDebugContext { + tcx, llcontext, llmod, builder, @@ -102,6 +103,50 @@ impl<'a, 'tcx> CrateDebugContext<'a, 'tcx> { } } +impl layout::HasDataLayout for CrateDebugContext<'_, 'tcx> { + fn data_layout(&self) -> &ty::layout::TargetDataLayout { + &self.tcx.data_layout + } +} + +impl layout::HasTyCtxt<'tcx> for CrateDebugContext<'_, 'tcx> { + fn tcx(&self) -> TyCtxt<'tcx> { + self.tcx + } +} + +impl layout::HasParamEnv<'tcx> for CrateDebugContext<'_, 'tcx> { + fn param_env(&self) -> ty::ParamEnv<'tcx> { + ty::ParamEnv::reveal_all() + } +} + +impl LayoutOf for CrateDebugContext<'_, 'tcx> { + type Ty = Ty<'tcx>; + type TyLayout = TyLayout<'tcx>; + + fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout { + self.spanned_layout_of(ty, DUMMY_SP) + } + + fn spanned_layout_of(&self, ty: Ty<'tcx>, span: Span) -> Self::TyLayout { + self.tcx.layout_of(ty::ParamEnv::reveal_all().and(ty)).unwrap_or_else(|e| { + if let LayoutError::SizeOverflow(_) = e { + self.tcx.sess.span_fatal(span, &e.to_string()) + } else { + rustc::bug!("failed to get layout for `{}`: {}", ty, e) + } + }) + } +} + +impl CrateDebugContext<'_, 'tcx> { + pub fn size_and_align_of(&self, ty: Ty<'tcx>) -> (Size, Align) { + let layout = self.layout_of(ty); + (layout.size, layout.align.abi) + } +} + /// Creates any deferred debug metadata nodes pub fn finalize(cx: &CodegenCx<'_, '_>) { if cx.dbg_cx.is_none() { @@ -159,7 +204,7 @@ impl DebugInfoBuilderMethods for Builder<'a, 'll, 'tcx> { assert!(!dbg_context.source_locations_enabled); let cx = self.cx(); - let loc = span_start(cx, span); + let loc = span_start(cx.tcx, span); // Convert the direct and indirect offsets to address ops. let op_deref = || unsafe { llvm::LLVMRustDIBuilderCreateOpDeref() }; @@ -266,8 +311,8 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> { let def_id = instance.def_id(); let containing_scope = get_containing_scope(self, instance); - let loc = span_start(self, span); - let file_metadata = file_metadata(self, &loc.file.name, def_id.krate); + let loc = span_start(self.tcx, span); + let file_metadata = file_metadata(debug_context(self), &loc.file.name, def_id.krate); let function_type_metadata = unsafe { let fn_signature = get_function_signature(self, fn_abi); @@ -288,7 +333,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> { get_template_parameters(self, &generics, substs, file_metadata, &mut name); // Get the linkage_name, which is just the symbol name - let linkage_name = mangled_name_of_instance(self, instance); + let linkage_name = self.tcx().symbol_name(instance); // FIXME(eddyb) does this need to be separate from `loc.line` for some reason? let scope_line = loc.line; @@ -303,7 +348,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> { } let mut spflags = DISPFlags::SPFlagDefinition; - if is_node_local_to_unit(self, def_id) { + if is_node_local_to_unit(self.tcx, def_id) { spflags |= DISPFlags::SPFlagLocalToUnit; } if self.sess().opts.optimize != config::OptLevel::No { @@ -347,7 +392,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> { }; // Fill in all the scopes, with the information from the MIR body. - compute_mir_scopes(self, mir, fn_metadata, &mut fn_debug_context); + compute_mir_scopes(debug_context(self), mir, fn_metadata, &mut fn_debug_context); return Some(fn_debug_context); @@ -365,7 +410,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> { signature.push(if fn_abi.ret.is_ignore() { None } else { - Some(type_metadata(cx, fn_abi.ret.layout.ty, rustc_span::DUMMY_SP)) + Some(type_metadata(debug_context(cx), fn_abi.ret.layout.ty, rustc_span::DUMMY_SP)) }); // Arguments types @@ -390,14 +435,14 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> { } _ => t, }; - Some(type_metadata(cx, t, rustc_span::DUMMY_SP)) + Some(type_metadata(debug_context(cx), t, rustc_span::DUMMY_SP)) })); } else { signature.extend( fn_abi .args .iter() - .map(|arg| Some(type_metadata(cx, arg.layout.ty, rustc_span::DUMMY_SP))), + .map(|arg| Some(type_metadata(debug_context(cx), arg.layout.ty, rustc_span::DUMMY_SP))), ); } @@ -440,7 +485,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> { let actual_type = cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), ty); let actual_type_metadata = - type_metadata(cx, actual_type, rustc_span::DUMMY_SP); + type_metadata(debug_context(cx), actual_type, rustc_span::DUMMY_SP); let name = SmallCStr::new(&name.as_str()); Some(unsafe { Some(llvm::LLVMRustDIBuilderCreateTemplateTypeParameter( @@ -493,7 +538,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> { // so avoid methods on other types (e.g., `<*mut T>::null`). match impl_self_ty.kind { ty::Adt(def, ..) if !def.is_box() => { - Some(type_metadata(cx, impl_self_ty, rustc_span::DUMMY_SP)) + Some(type_metadata(debug_context(cx), impl_self_ty, rustc_span::DUMMY_SP)) } _ => None, } @@ -506,7 +551,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> { self_type.unwrap_or_else(|| { namespace::item_namespace( - cx, + debug_context(cx), DefId { krate: instance.def_id().krate, index: cx @@ -521,7 +566,9 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> { } fn create_vtable_metadata(&self, ty: Ty<'tcx>, vtable: Self::Value) { - metadata::create_vtable_metadata(self, ty, vtable) + if let Some(dbg_cx) = self.dbg_cx.as_ref() { + metadata::create_vtable_metadata(dbg_cx, ty, vtable) + } } fn extend_scope_to_file( @@ -530,7 +577,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> { file: &rustc_span::SourceFile, defining_crate: CrateNum, ) -> &'ll DILexicalBlock { - metadata::extend_scope_to_file(&self, scope_metadata, file, defining_crate) + metadata::extend_scope_to_file(debug_context(self), scope_metadata, file, defining_crate) } fn debuginfo_finalize(&self) { @@ -548,16 +595,17 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> { variable_kind: VariableKind, span: Span, ) -> &'ll DIVariable { - let loc = span_start(self, span); - let file_metadata = file_metadata(self, &loc.file.name, dbg_context.defining_crate); + let loc = span_start(self.tcx, span); + let file_metadata = file_metadata(debug_context(self), &loc.file.name, dbg_context.defining_crate); - let type_metadata = type_metadata(self, variable_type, span); + let type_metadata = type_metadata(debug_context(self), variable_type, span); let (argument_index, dwarf_tag) = match variable_kind { ArgumentVariable(index) => (index as c_uint, DW_TAG_arg_variable), LocalVariable => (0, DW_TAG_auto_variable), }; - let align = self.align_of(variable_type); + + let align = self.layout_of(variable_type).align.abi; let name = SmallCStr::new(&variable_name.as_str()); unsafe { diff --git a/src/librustc_codegen_llvm/debuginfo/namespace.rs b/src/librustc_codegen_llvm/debuginfo/namespace.rs index 582f495207455..2982b7cec07d9 100644 --- a/src/librustc_codegen_llvm/debuginfo/namespace.rs +++ b/src/librustc_codegen_llvm/debuginfo/namespace.rs @@ -1,10 +1,8 @@ // Namespace Handling. use super::metadata::{unknown_file_metadata, UNKNOWN_LINE_NUMBER}; -use super::utils::{debug_context, DIB}; -use rustc::ty::{self, Instance}; -use crate::common::CodegenCx; +use crate::debuginfo::CrateDebugContext; use crate::llvm; use crate::llvm::debuginfo::DIScope; use rustc::hir::map::DefPathData; @@ -12,26 +10,21 @@ use rustc_hir::def_id::DefId; use rustc_data_structures::small_c_str::SmallCStr; -pub fn mangled_name_of_instance<'a, 'tcx>( - cx: &CodegenCx<'a, 'tcx>, - instance: Instance<'tcx>, -) -> ty::SymbolName { - let tcx = cx.tcx; - tcx.symbol_name(instance) -} - -pub fn item_namespace(cx: &CodegenCx<'ll, '_>, def_id: DefId) -> &'ll DIScope { - if let Some(&scope) = debug_context(cx).namespace_map.borrow().get(&def_id) { +pub fn item_namespace( + dbg_cx: &CrateDebugContext<'ll, '_>, + def_id: DefId, +) -> &'ll DIScope { + if let Some(&scope) = dbg_cx.namespace_map.borrow().get(&def_id) { return scope; } - let def_key = cx.tcx.def_key(def_id); - let parent_scope = def_key - .parent - .map(|parent| item_namespace(cx, DefId { krate: def_id.krate, index: parent })); + let def_key = dbg_cx.tcx.def_key(def_id); + let parent_scope = def_key.parent.map(|parent| { + item_namespace(dbg_cx, DefId { krate: def_id.krate, index: parent }) + }); let namespace_name = match def_key.disambiguated_data.data { - DefPathData::CrateRoot => cx.tcx.crate_name(def_id.krate), + DefPathData::CrateRoot => dbg_cx.tcx.crate_name(def_id.krate), data => data.as_symbol(), }; @@ -39,14 +32,14 @@ pub fn item_namespace(cx: &CodegenCx<'ll, '_>, def_id: DefId) -> &'ll DIScope { let scope = unsafe { llvm::LLVMRustDIBuilderCreateNameSpace( - DIB(cx), + dbg_cx.builder, parent_scope, namespace_name.as_ptr(), - unknown_file_metadata(cx), + unknown_file_metadata(dbg_cx), UNKNOWN_LINE_NUMBER, ) }; - debug_context(cx).namespace_map.borrow_mut().insert(def_id, scope); + dbg_cx.namespace_map.borrow_mut().insert(def_id, scope); scope } diff --git a/src/librustc_codegen_llvm/debuginfo/source_loc.rs b/src/librustc_codegen_llvm/debuginfo/source_loc.rs index 6afaca44e0e96..61cfe75d2bf9d 100644 --- a/src/librustc_codegen_llvm/debuginfo/source_loc.rs +++ b/src/librustc_codegen_llvm/debuginfo/source_loc.rs @@ -24,7 +24,7 @@ pub fn set_source_location( ) { let dbg_loc = if debug_context.source_locations_enabled { debug!("set_source_location: {}", bx.sess().source_map().span_to_string(span)); - let loc = span_start(bx.cx(), span); + let loc = span_start(bx.cx().tcx, span); InternalDebugLocation::new(scope, loc.line, loc.col.to_usize()) } else { UnknownLocation diff --git a/src/librustc_codegen_llvm/debuginfo/utils.rs b/src/librustc_codegen_llvm/debuginfo/utils.rs index 4e17387e057f9..863576b6a441e 100644 --- a/src/librustc_codegen_llvm/debuginfo/utils.rs +++ b/src/librustc_codegen_llvm/debuginfo/utils.rs @@ -3,17 +3,16 @@ use super::namespace::item_namespace; use super::CrateDebugContext; -use rustc::ty::DefIdTree; +use rustc::ty::{DefIdTree, TyCtxt}; use rustc_hir::def_id::DefId; use crate::common::CodegenCx; use crate::llvm; use crate::llvm::debuginfo::{DIArray, DIBuilder, DIDescriptor, DIScope}; -use rustc_codegen_ssa::traits::*; use rustc_span::Span; -pub fn is_node_local_to_unit(cx: &CodegenCx<'_, '_>, def_id: DefId) -> bool { +pub fn is_node_local_to_unit(tcx: TyCtxt<'_>, def_id: DefId) -> bool { // The is_local_to_unit flag indicates whether a function is local to the // current compilation unit (i.e., if it is *static* in the C-sense). The // *reachable* set should provide a good approximation of this, as it @@ -22,7 +21,7 @@ pub fn is_node_local_to_unit(cx: &CodegenCx<'_, '_>, def_id: DefId) -> bool { // visible). It might better to use the `exported_items` set from // `driver::CrateAnalysis` in the future, but (atm) this set is not // available in the codegen pass. - !cx.tcx.is_reachable_non_generic(def_id) + !tcx.is_reachable_non_generic(def_id) } #[allow(non_snake_case)] @@ -33,8 +32,8 @@ pub fn create_DIArray(builder: &DIBuilder<'ll>, arr: &[Option<&'ll DIDescriptor> } /// Returns rustc_span::Loc corresponding to the beginning of the span -pub fn span_start(cx: &CodegenCx<'_, '_>, span: Span) -> rustc_span::Loc { - cx.sess().source_map().lookup_char_pos(span.lo()) +pub fn span_start(tcx: TyCtxt<'_>, span: Span) -> rustc_span::Loc { + tcx.sess.source_map().lookup_char_pos(span.lo()) } #[inline] @@ -48,6 +47,6 @@ pub fn DIB(cx: &'a CodegenCx<'ll, '_>) -> &'a DIBuilder<'ll> { cx.dbg_cx.as_ref().unwrap().builder } -pub fn get_namespace_for_item(cx: &CodegenCx<'ll, '_>, def_id: DefId) -> &'ll DIScope { - item_namespace(cx, cx.tcx.parent(def_id).expect("get_namespace_for_item: missing parent?")) +pub fn get_namespace_for_item(dbg_cx: &CrateDebugContext<'ll, '_>, def_id: DefId) -> &'ll DIScope { + item_namespace(dbg_cx, dbg_cx.tcx.parent(def_id).expect("get_namespace_for_item: missing parent?")) }