/src/librustc_codegen_llvm/debuginfo/namespace.rs

https://gitlab.com/jianglu/rust · Rust · 66 lines · 45 code · 11 blank · 10 comment · 2 complexity · d165870904c85f189bfdfdd32048af9d MD5 · raw file

  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. // Namespace Handling.
  11. use super::metadata::{unknown_file_metadata, UNKNOWN_LINE_NUMBER};
  12. use super::utils::{DIB, debug_context};
  13. use monomorphize::Instance;
  14. use rustc::ty;
  15. use llvm;
  16. use llvm::debuginfo::DIScope;
  17. use rustc::hir::def_id::DefId;
  18. use rustc::hir::map::DefPathData;
  19. use common::CodegenCx;
  20. use std::ffi::CString;
  21. use std::ptr;
  22. pub fn mangled_name_of_instance<'a, 'tcx>(
  23. cx: &CodegenCx<'a, 'tcx>,
  24. instance: Instance<'tcx>,
  25. ) -> ty::SymbolName {
  26. let tcx = cx.tcx;
  27. tcx.symbol_name(instance)
  28. }
  29. pub fn item_namespace(cx: &CodegenCx, def_id: DefId) -> DIScope {
  30. if let Some(&scope) = debug_context(cx).namespace_map.borrow().get(&def_id) {
  31. return scope;
  32. }
  33. let def_key = cx.tcx.def_key(def_id);
  34. let parent_scope = def_key.parent.map_or(ptr::null_mut(), |parent| {
  35. item_namespace(cx, DefId {
  36. krate: def_id.krate,
  37. index: parent
  38. })
  39. });
  40. let namespace_name = match def_key.disambiguated_data.data {
  41. DefPathData::CrateRoot => cx.tcx.crate_name(def_id.krate).as_str(),
  42. data => data.as_interned_str().as_str()
  43. };
  44. let namespace_name = CString::new(namespace_name.as_bytes()).unwrap();
  45. let scope = unsafe {
  46. llvm::LLVMRustDIBuilderCreateNameSpace(
  47. DIB(cx),
  48. parent_scope,
  49. namespace_name.as_ptr(),
  50. unknown_file_metadata(cx),
  51. UNKNOWN_LINE_NUMBER)
  52. };
  53. debug_context(cx).namespace_map.borrow_mut().insert(def_id, scope);
  54. scope
  55. }