/src/librustc_trans/debuginfo/namespace.rs

https://gitlab.com/alx741/rust · Rust · 91 lines · 67 code · 14 blank · 10 comment · 7 complexity · 4ee4138364bc7dda2491fdb6c5f7bd5c 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::{file_metadata, NO_FILE_METADATA, UNKNOWN_LINE_NUMBER};
  12. use super::utils::{DIB, debug_context, span_start};
  13. use llvm;
  14. use llvm::debuginfo::DIScope;
  15. use rustc::hir::def_id::DefId;
  16. use rustc::hir::map::DefPathData;
  17. use common::CrateContext;
  18. use libc::c_uint;
  19. use std::ffi::CString;
  20. use std::ptr;
  21. use syntax::codemap::DUMMY_SP;
  22. pub fn mangled_name_of_item(ccx: &CrateContext, def_id: DefId, extra: &str) -> String {
  23. fn fill_nested(ccx: &CrateContext, def_id: DefId, extra: &str, output: &mut String) {
  24. let def_key = ccx.tcx().def_key(def_id);
  25. if let Some(parent) = def_key.parent {
  26. fill_nested(ccx, DefId {
  27. krate: def_id.krate,
  28. index: parent
  29. }, "", output);
  30. }
  31. let name = match def_key.disambiguated_data.data {
  32. DefPathData::CrateRoot => ccx.tcx().crate_name(def_id.krate),
  33. data => data.as_interned_str()
  34. };
  35. output.push_str(&(name.len() + extra.len()).to_string());
  36. output.push_str(&name);
  37. output.push_str(extra);
  38. }
  39. let mut name = String::from("_ZN");
  40. fill_nested(ccx, def_id, extra, &mut name);
  41. name.push('E');
  42. name
  43. }
  44. pub fn item_namespace(ccx: &CrateContext, def_id: DefId) -> DIScope {
  45. if let Some(&scope) = debug_context(ccx).namespace_map.borrow().get(&def_id) {
  46. return scope;
  47. }
  48. let def_key = ccx.tcx().def_key(def_id);
  49. let parent_scope = def_key.parent.map_or(ptr::null_mut(), |parent| {
  50. item_namespace(ccx, DefId {
  51. krate: def_id.krate,
  52. index: parent
  53. })
  54. });
  55. let namespace_name = match def_key.disambiguated_data.data {
  56. DefPathData::CrateRoot => ccx.tcx().crate_name(def_id.krate),
  57. data => data.as_interned_str()
  58. };
  59. let namespace_name = CString::new(namespace_name.as_bytes()).unwrap();
  60. let span = ccx.tcx().map.def_id_span(def_id, DUMMY_SP);
  61. let (file, line) = if span != DUMMY_SP {
  62. let loc = span_start(ccx, span);
  63. (file_metadata(ccx, &loc.file.name), loc.line as c_uint)
  64. } else {
  65. (NO_FILE_METADATA, UNKNOWN_LINE_NUMBER)
  66. };
  67. let scope = unsafe {
  68. llvm::LLVMDIBuilderCreateNameSpace(
  69. DIB(ccx),
  70. parent_scope,
  71. namespace_name.as_ptr(),
  72. file,
  73. line as c_uint)
  74. };
  75. debug_context(ccx).namespace_map.borrow_mut().insert(def_id, scope);
  76. scope
  77. }