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