]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/debuginfo/namespace.rs
preserve order if blocks are between items
[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 std::ffi::CString;
25 use std::ptr;
26
27 pub fn mangled_name_of_instance<'a, 'tcx>(
28     cx: &CodegenCx<'a, 'tcx>,
29     instance: Instance<'tcx>,
30 ) -> ty::SymbolName {
31      let tcx = cx.tcx;
32      tcx.symbol_name(instance)
33 }
34
35 pub fn item_namespace(cx: &CodegenCx, def_id: DefId) -> DIScope {
36     if let Some(&scope) = debug_context(cx).namespace_map.borrow().get(&def_id) {
37         return scope;
38     }
39
40     let def_key = cx.tcx.def_key(def_id);
41     let parent_scope = def_key.parent.map_or(ptr::null_mut(), |parent| {
42         item_namespace(cx, DefId {
43             krate: def_id.krate,
44             index: parent
45         })
46     });
47
48     let namespace_name = match def_key.disambiguated_data.data {
49         DefPathData::CrateRoot => cx.tcx.crate_name(def_id.krate).as_str(),
50         data => data.as_interned_str().as_str()
51     };
52
53     let namespace_name = CString::new(namespace_name.as_bytes()).unwrap();
54
55     let scope = unsafe {
56         llvm::LLVMRustDIBuilderCreateNameSpace(
57             DIB(cx),
58             parent_scope,
59             namespace_name.as_ptr(),
60             unknown_file_metadata(cx),
61             UNKNOWN_LINE_NUMBER)
62     };
63
64     debug_context(cx).namespace_map.borrow_mut().insert(def_id, scope);
65     scope
66 }