]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/trans/debuginfo/utils.rs
c019a0a950cba5bfd9b0ef2cf3ab6686bf875912
[rust.git] / src / librustc_trans / 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::{FunctionDebugContext, CrateDebugContext};
14 use super::namespace::namespace_for_item;
15
16 use middle::def_id::DefId;
17
18 use llvm;
19 use llvm::debuginfo::{DIScope, DIBuilderRef, DIDescriptor, DIArray};
20 use trans::machine;
21 use trans::common::{CrateContext, FunctionContext};
22 use trans::type_::Type;
23
24 use rustc_front::hir;
25
26 use syntax::codemap::Span;
27 use syntax::{ast, codemap};
28
29 pub fn is_node_local_to_unit(cx: &CrateContext, node_id: ast::NodeId) -> bool
30 {
31     // The is_local_to_unit flag indicates whether a function is local to the
32     // current compilation unit (i.e. if it is *static* in the C-sense). The
33     // *reachable* set should provide a good approximation of this, as it
34     // contains everything that might leak out of the current crate (by being
35     // externally visible or by being inlined into something externally
36     // visible). It might better to use the `exported_items` set from
37     // `driver::CrateAnalysis` in the future, but (atm) this set is not
38     // available in the translation pass.
39     !cx.reachable().contains(&node_id)
40 }
41
42 #[allow(non_snake_case)]
43 pub fn create_DIArray(builder: DIBuilderRef, arr: &[DIDescriptor]) -> DIArray {
44     return unsafe {
45         llvm::LLVMDIBuilderGetOrCreateArray(builder, arr.as_ptr(), arr.len() as u32)
46     };
47 }
48
49 pub fn contains_nodebug_attribute(attributes: &[hir::Attribute]) -> bool {
50     attributes.iter().any(|attr| {
51         let meta_item: &hir::MetaItem = &*attr.node.value;
52         match meta_item.node {
53             hir::MetaWord(ref value) => &value[..] == "no_debug",
54             _ => false
55         }
56     })
57 }
58
59 /// Return codemap::Loc corresponding to the beginning of the span
60 pub fn span_start(cx: &CrateContext, span: Span) -> codemap::Loc {
61     cx.sess().codemap().lookup_char_pos(span.lo)
62 }
63
64 pub fn size_and_align_of(cx: &CrateContext, llvm_type: Type) -> (u64, u64) {
65     (machine::llsize_of_alloc(cx, llvm_type), machine::llalign_of_min(cx, llvm_type) as u64)
66 }
67
68 pub fn bytes_to_bits(bytes: u64) -> u64 {
69     bytes * 8
70 }
71
72 #[inline]
73 pub fn debug_context<'a, 'tcx>(cx: &'a CrateContext<'a, 'tcx>)
74                            -> &'a CrateDebugContext<'tcx> {
75     let debug_context: &'a CrateDebugContext<'tcx> = cx.dbg_cx().as_ref().unwrap();
76     debug_context
77 }
78
79 #[inline]
80 #[allow(non_snake_case)]
81 pub fn DIB(cx: &CrateContext) -> DIBuilderRef {
82     cx.dbg_cx().as_ref().unwrap().builder
83 }
84
85 pub fn fn_should_be_ignored(fcx: &FunctionContext) -> bool {
86     match fcx.debug_context {
87         FunctionDebugContext::RegularContext(_) => false,
88         _ => true
89     }
90 }
91
92 pub fn assert_type_for_node_id(cx: &CrateContext,
93                            node_id: ast::NodeId,
94                            error_reporting_span: Span) {
95     if !cx.tcx().node_types().contains_key(&node_id) {
96         cx.sess().span_bug(error_reporting_span,
97                            "debuginfo: Could not find type for node id!");
98     }
99 }
100
101 pub fn get_namespace_and_span_for_item(cx: &CrateContext, def_id: DefId)
102                                    -> (DIScope, Span) {
103     let containing_scope = namespace_for_item(cx, def_id).scope;
104     let definition_span = if def_id.is_local() {
105         cx.tcx().map.span(def_id.node)
106     } else {
107         // For external items there is no span information
108         codemap::DUMMY_SP
109     };
110
111     (containing_scope, definition_span)
112 }