2015-04-24 16:00:47 +12:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
2015-04-24 17:25:35 +12:00
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
2015-04-24 16:00:47 +12:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
2015-04-24 17:25:35 +12:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
2015-04-24 16:00:47 +12:00
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-04-24 17:25:35 +12:00
|
|
|
// Namespace Handling.
|
2015-04-24 16:00:47 +12:00
|
|
|
|
2016-07-07 08:15:10 -04:00
|
|
|
use super::metadata::{file_metadata, unknown_file_metadata, UNKNOWN_LINE_NUMBER};
|
2016-04-06 15:37:19 +03:00
|
|
|
use super::utils::{DIB, debug_context, span_start};
|
2015-04-24 16:00:47 +12:00
|
|
|
|
|
|
|
use llvm;
|
|
|
|
use llvm::debuginfo::DIScope;
|
2016-03-29 12:54:26 +03:00
|
|
|
use rustc::hir::def_id::DefId;
|
2016-04-06 15:37:19 +03:00
|
|
|
use rustc::hir::map::DefPathData;
|
2016-03-22 19:23:36 +02:00
|
|
|
use common::CrateContext;
|
2015-04-24 16:00:47 +12:00
|
|
|
|
2016-04-06 15:37:19 +03:00
|
|
|
use libc::c_uint;
|
2015-04-24 16:00:47 +12:00
|
|
|
use std::ffi::CString;
|
|
|
|
use std::ptr;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::DUMMY_SP;
|
2016-04-06 15:37:19 +03:00
|
|
|
|
|
|
|
pub fn mangled_name_of_item(ccx: &CrateContext, def_id: DefId, extra: &str) -> String {
|
|
|
|
fn fill_nested(ccx: &CrateContext, def_id: DefId, extra: &str, output: &mut String) {
|
|
|
|
let def_key = ccx.tcx().def_key(def_id);
|
|
|
|
if let Some(parent) = def_key.parent {
|
|
|
|
fill_nested(ccx, DefId {
|
|
|
|
krate: def_id.krate,
|
|
|
|
index: parent
|
|
|
|
}, "", output);
|
2015-04-24 16:00:47 +12:00
|
|
|
}
|
|
|
|
|
2016-04-06 15:37:19 +03:00
|
|
|
let name = match def_key.disambiguated_data.data {
|
2016-11-16 10:52:37 +00:00
|
|
|
DefPathData::CrateRoot => ccx.tcx().crate_name(def_id.krate).as_str(),
|
2016-04-06 15:37:19 +03:00
|
|
|
data => data.as_interned_str()
|
2015-04-24 16:00:47 +12:00
|
|
|
};
|
|
|
|
|
2016-04-06 15:37:19 +03:00
|
|
|
output.push_str(&(name.len() + extra.len()).to_string());
|
|
|
|
output.push_str(&name);
|
|
|
|
output.push_str(extra);
|
|
|
|
}
|
2016-04-06 13:51:55 +03:00
|
|
|
|
2016-04-06 15:37:19 +03:00
|
|
|
let mut name = String::from("_ZN");
|
|
|
|
fill_nested(ccx, def_id, extra, &mut name);
|
|
|
|
name.push('E');
|
|
|
|
name
|
|
|
|
}
|
2016-04-06 13:51:55 +03:00
|
|
|
|
2016-04-06 15:37:19 +03:00
|
|
|
pub fn item_namespace(ccx: &CrateContext, def_id: DefId) -> DIScope {
|
|
|
|
if let Some(&scope) = debug_context(ccx).namespace_map.borrow().get(&def_id) {
|
|
|
|
return scope;
|
2016-04-06 13:51:55 +03:00
|
|
|
}
|
|
|
|
|
2016-04-06 15:37:19 +03:00
|
|
|
let def_key = ccx.tcx().def_key(def_id);
|
|
|
|
let parent_scope = def_key.parent.map_or(ptr::null_mut(), |parent| {
|
|
|
|
item_namespace(ccx, DefId {
|
|
|
|
krate: def_id.krate,
|
|
|
|
index: parent
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
let namespace_name = match def_key.disambiguated_data.data {
|
2016-11-16 10:52:37 +00:00
|
|
|
DefPathData::CrateRoot => ccx.tcx().crate_name(def_id.krate).as_str(),
|
2016-04-06 15:37:19 +03:00
|
|
|
data => data.as_interned_str()
|
|
|
|
};
|
|
|
|
|
|
|
|
let namespace_name = CString::new(namespace_name.as_bytes()).unwrap();
|
2016-11-24 01:39:13 +02:00
|
|
|
let span = ccx.tcx().def_span(def_id);
|
2016-04-06 15:37:19 +03:00
|
|
|
let (file, line) = if span != DUMMY_SP {
|
|
|
|
let loc = span_start(ccx, span);
|
2017-04-24 19:01:19 +02:00
|
|
|
(file_metadata(ccx, &loc.file.name, def_id.krate), loc.line as c_uint)
|
2016-04-06 15:37:19 +03:00
|
|
|
} else {
|
2016-07-07 08:15:10 -04:00
|
|
|
(unknown_file_metadata(ccx), UNKNOWN_LINE_NUMBER)
|
2016-04-06 15:37:19 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
let scope = unsafe {
|
2016-08-02 02:35:09 +03:00
|
|
|
llvm::LLVMRustDIBuilderCreateNameSpace(
|
2016-04-06 15:37:19 +03:00
|
|
|
DIB(ccx),
|
|
|
|
parent_scope,
|
|
|
|
namespace_name.as_ptr(),
|
|
|
|
file,
|
|
|
|
line as c_uint)
|
|
|
|
};
|
|
|
|
|
|
|
|
debug_context(ccx).namespace_map.borrow_mut().insert(def_id, scope);
|
|
|
|
scope
|
2015-04-24 16:00:47 +12:00
|
|
|
}
|