rustc_codegen_llvm/debuginfo/
namespace.rs

1// Namespace Handling.
2
3use rustc_codegen_ssa::debuginfo::type_names;
4use rustc_hir::def_id::DefId;
5use rustc_middle::ty::{self, Instance};
6
7use super::utils::{DIB, debug_context};
8use crate::common::CodegenCx;
9use crate::llvm;
10use crate::llvm::debuginfo::DIScope;
11
12pub(crate) fn mangled_name_of_instance<'a, 'tcx>(
13    cx: &CodegenCx<'a, 'tcx>,
14    instance: Instance<'tcx>,
15) -> ty::SymbolName<'tcx> {
16    cx.tcx.symbol_name(instance)
17}
18
19pub(crate) fn item_namespace<'ll>(cx: &CodegenCx<'ll, '_>, def_id: DefId) -> &'ll DIScope {
20    if let Some(&scope) = debug_context(cx).namespace_map.borrow().get(&def_id) {
21        return scope;
22    }
23
24    let def_key = cx.tcx.def_key(def_id);
25    let parent_scope = def_key
26        .parent
27        .map(|parent| item_namespace(cx, DefId { krate: def_id.krate, index: parent }));
28
29    let namespace_name_string = {
30        let mut output = String::with_capacity(64);
31        type_names::push_item_name(cx.tcx, def_id, false, &mut output);
32        output
33    };
34
35    let scope = unsafe {
36        llvm::LLVMDIBuilderCreateNameSpace(
37            DIB(cx),
38            parent_scope,
39            namespace_name_string.as_ptr(),
40            namespace_name_string.len(),
41            llvm::False, // ExportSymbols (only relevant for C++ anonymous namespaces)
42        )
43    };
44
45    debug_context(cx).namespace_map.borrow_mut().insert(def_id, scope);
46    scope
47}