rust/src/librustc_trans/debuginfo/utils.rs

88 lines
2.9 KiB
Rust
Raw Normal View History

2015-04-24 03:36:43 +00:00
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
2015-04-24 05:25:35 +00:00
// http://rust-lang.org/COPYRIGHT.
2015-04-24 03:36:43 +00:00
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2015-04-24 05:25:35 +00: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 03:36:43 +00:00
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Utility Functions.
use super::{CrateDebugContext};
use super::namespace::item_namespace;
2015-04-24 03:36:43 +00:00
use rustc::hir::def_id::DefId;
2015-08-16 10:32:28 +00:00
2015-04-24 03:36:43 +00:00
use llvm;
2015-04-29 06:14:37 +00:00
use llvm::debuginfo::{DIScope, DIBuilderRef, DIDescriptor, DIArray};
use machine;
use common::{CrateContext};
use type_::Type;
2015-04-24 03:36:43 +00:00
use syntax_pos::{self, Span};
use syntax::ast;
2015-04-29 06:14:37 +00:00
2017-02-05 07:19:27 +00:00
use std::ops;
2015-04-29 06:14:37 +00:00
pub fn is_node_local_to_unit(cx: &CrateContext, node_id: ast::NodeId) -> bool
{
// The is_local_to_unit flag indicates whether a function is local to the
// current compilation unit (i.e. if it is *static* in the C-sense). The
// *reachable* set should provide a good approximation of this, as it
// contains everything that might leak out of the current crate (by being
// externally visible or by being inlined into something externally
// visible). It might better to use the `exported_items` set from
// `driver::CrateAnalysis` in the future, but (atm) this set is not
// available in the translation pass.
!cx.exported_symbols().contains(&node_id)
2015-04-29 06:14:37 +00:00
}
2015-04-24 03:36:43 +00:00
2015-04-29 06:14:37 +00:00
#[allow(non_snake_case)]
pub fn create_DIArray(builder: DIBuilderRef, arr: &[DIDescriptor]) -> DIArray {
return unsafe {
llvm::LLVMRustDIBuilderGetOrCreateArray(builder, arr.as_ptr(), arr.len() as u32)
2015-04-29 06:14:37 +00:00
};
}
2015-04-24 03:36:43 +00:00
/// Return syntax_pos::Loc corresponding to the beginning of the span
pub fn span_start(cx: &CrateContext, span: Span) -> syntax_pos::Loc {
2015-04-24 03:36:43 +00:00
cx.sess().codemap().lookup_char_pos(span.lo)
}
2017-02-05 07:19:27 +00:00
pub fn size_and_align_of(cx: &CrateContext, llvm_type: Type) -> (u64, u32) {
(machine::llsize_of_alloc(cx, llvm_type), machine::llalign_of_min(cx, llvm_type))
2015-04-24 03:36:43 +00:00
}
2017-02-05 07:19:27 +00:00
pub fn bytes_to_bits<T>(bytes: T) -> T
where T: ops::Mul<Output=T> + From<u8> {
bytes * 8u8.into()
2015-04-24 03:36:43 +00:00
}
#[inline]
pub fn debug_context<'a, 'tcx>(cx: &'a CrateContext<'a, 'tcx>)
-> &'a CrateDebugContext<'tcx> {
2015-11-10 17:42:37 +00:00
cx.dbg_cx().as_ref().unwrap()
2015-04-24 03:36:43 +00:00
}
#[inline]
#[allow(non_snake_case)]
pub fn DIB(cx: &CrateContext) -> DIBuilderRef {
cx.dbg_cx().as_ref().unwrap().builder
}
2015-08-16 10:32:28 +00:00
pub fn get_namespace_and_span_for_item(cx: &CrateContext, def_id: DefId)
2015-04-24 03:36:43 +00:00
-> (DIScope, Span) {
let containing_scope = item_namespace(cx, DefId {
krate: def_id.krate,
index: cx.tcx().def_key(def_id).parent
.expect("get_namespace_and_span_for_item: missing parent?")
});
// Try to get some span information, if we have an inlined item.
let definition_span = cx.tcx().def_span(def_id);
2015-04-24 03:36:43 +00:00
(containing_scope, definition_span)
}