]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/debuginfo/namespace.rs
Rollup merge of #56416 - GuillaumeGomez:css-body, r=QuietMisdreavus
[rust.git] / src / librustc_codegen_llvm / debuginfo / namespace.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Namespace Handling.
12
13 use super::metadata::{unknown_file_metadata, UNKNOWN_LINE_NUMBER};
14 use super::utils::{DIB, debug_context};
15 use monomorphize::Instance;
16 use rustc::ty;
17
18 use llvm;
19 use llvm::debuginfo::DIScope;
20 use rustc::hir::def_id::DefId;
21 use rustc::hir::map::DefPathData;
22 use common::CodegenCx;
23
24 use rustc_data_structures::small_c_str::SmallCStr;
25
26 pub fn mangled_name_of_instance<'a, 'tcx>(
27     cx: &CodegenCx<'a, 'tcx>,
28     instance: Instance<'tcx>,
29 ) -> ty::SymbolName {
30      let tcx = cx.tcx;
31      tcx.symbol_name(instance)
32 }
33
34 pub fn item_namespace(cx: &CodegenCx<'ll, '_>, def_id: DefId) -> &'ll DIScope {
35     if let Some(&scope) = debug_context(cx).namespace_map.borrow().get(&def_id) {
36         return scope;
37     }
38
39     let def_key = cx.tcx.def_key(def_id);
40     let parent_scope = def_key.parent.map(|parent| {
41         item_namespace(cx, DefId {
42             krate: def_id.krate,
43             index: parent
44         })
45     });
46
47     let namespace_name = match def_key.disambiguated_data.data {
48         DefPathData::CrateRoot => cx.tcx.crate_name(def_id.krate).as_str(),
49         data => data.as_interned_str().as_str()
50     };
51
52     let namespace_name = SmallCStr::new(&namespace_name);
53
54     let scope = unsafe {
55         llvm::LLVMRustDIBuilderCreateNameSpace(
56             DIB(cx),
57             parent_scope,
58             namespace_name.as_ptr(),
59             unknown_file_metadata(cx),
60             UNKNOWN_LINE_NUMBER)
61     };
62
63     debug_context(cx).namespace_map.borrow_mut().insert(def_id, scope);
64     scope
65 }