]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/debuginfo/utils.rs
Rollup merge of #41249 - GuillaumeGomez:rustdoc-render, r=steveklabnik,frewsxcv
[rust.git] / src / librustc_trans / debuginfo / utils.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 // Utility Functions.
12
13 use super::{CrateDebugContext};
14 use super::namespace::item_namespace;
15
16 use rustc::hir::def_id::DefId;
17 use rustc::ty::DefIdTree;
18
19 use llvm;
20 use llvm::debuginfo::{DIScope, DIBuilderRef, DIDescriptor, DIArray};
21 use machine;
22 use common::{CrateContext};
23 use type_::Type;
24
25 use syntax_pos::{self, Span};
26 use syntax::ast;
27
28 use std::ops;
29
30 pub fn is_node_local_to_unit(cx: &CrateContext, node_id: ast::NodeId) -> bool
31 {
32     // The is_local_to_unit flag indicates whether a function is local to the
33     // current compilation unit (i.e. if it is *static* in the C-sense). The
34     // *reachable* set should provide a good approximation of this, as it
35     // contains everything that might leak out of the current crate (by being
36     // externally visible or by being inlined into something externally
37     // visible). It might better to use the `exported_items` set from
38     // `driver::CrateAnalysis` in the future, but (atm) this set is not
39     // available in the translation pass.
40     !cx.shared().exported_symbols().contains(&node_id)
41 }
42
43 #[allow(non_snake_case)]
44 pub fn create_DIArray(builder: DIBuilderRef, arr: &[DIDescriptor]) -> DIArray {
45     return unsafe {
46         llvm::LLVMRustDIBuilderGetOrCreateArray(builder, arr.as_ptr(), arr.len() as u32)
47     };
48 }
49
50 /// Return syntax_pos::Loc corresponding to the beginning of the span
51 pub fn span_start(cx: &CrateContext, span: Span) -> syntax_pos::Loc {
52     cx.sess().codemap().lookup_char_pos(span.lo)
53 }
54
55 pub fn size_and_align_of(cx: &CrateContext, llvm_type: Type) -> (u64, u32) {
56     (machine::llsize_of_alloc(cx, llvm_type), machine::llalign_of_min(cx, llvm_type))
57 }
58
59 pub fn bytes_to_bits<T>(bytes: T) -> T
60     where T: ops::Mul<Output=T> + From<u8> {
61     bytes * 8u8.into()
62 }
63
64 #[inline]
65 pub fn debug_context<'a, 'tcx>(cx: &'a CrateContext<'a, 'tcx>)
66                            -> &'a CrateDebugContext<'tcx> {
67     cx.dbg_cx().as_ref().unwrap()
68 }
69
70 #[inline]
71 #[allow(non_snake_case)]
72 pub fn DIB(cx: &CrateContext) -> DIBuilderRef {
73     cx.dbg_cx().as_ref().unwrap().builder
74 }
75
76 pub fn get_namespace_and_span_for_item(cx: &CrateContext, def_id: DefId)
77                                    -> (DIScope, Span) {
78     let containing_scope = item_namespace(cx, cx.tcx().parent(def_id)
79         .expect("get_namespace_and_span_for_item: missing parent?"));
80
81     // Try to get some span information, if we have an inlined item.
82     let definition_span = cx.tcx().def_span(def_id);
83
84     (containing_scope, definition_span)
85 }