]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_ssa/mono_item.rs
Rollup merge of #65417 - weiznich:more_coherence_tests, r=nikomatsakis
[rust.git] / src / librustc_codegen_ssa / mono_item.rs
1 use rustc::hir;
2 use rustc::mir::mono::{Linkage, Visibility};
3 use rustc::ty::layout::HasTyCtxt;
4 use crate::base;
5 use crate::traits::*;
6
7 use rustc::mir::mono::MonoItem;
8
9 pub trait MonoItemExt<'a, 'tcx> {
10     fn define<Bx: BuilderMethods<'a, 'tcx>>(&self, cx: &'a Bx::CodegenCx);
11     fn predefine<Bx: BuilderMethods<'a, 'tcx>>(
12         &self,
13         cx: &'a Bx::CodegenCx,
14         linkage: Linkage,
15         visibility: Visibility
16     );
17     fn to_raw_string(&self) -> String;
18 }
19
20 impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {
21     fn define<Bx: BuilderMethods<'a, 'tcx>>(&self, cx: &'a Bx::CodegenCx) {
22         debug!("BEGIN IMPLEMENTING '{} ({})' in cgu {}",
23                self.to_string(cx.tcx(), true),
24                self.to_raw_string(),
25                cx.codegen_unit().name());
26
27         match *self {
28             MonoItem::Static(def_id) => {
29                 cx.codegen_static(def_id, cx.tcx().is_mutable_static(def_id));
30             }
31             MonoItem::GlobalAsm(hir_id) => {
32                 let item = cx.tcx().hir().expect_item(hir_id);
33                 if let hir::ItemKind::GlobalAsm(ref ga) = item.kind {
34                     cx.codegen_global_asm(ga);
35                 } else {
36                     span_bug!(item.span, "Mismatch between hir::Item type and MonoItem type")
37                 }
38             }
39             MonoItem::Fn(instance) => {
40                 base::codegen_instance::<Bx>(&cx, instance);
41             }
42         }
43
44         debug!("END IMPLEMENTING '{} ({})' in cgu {}",
45                self.to_string(cx.tcx(), true),
46                self.to_raw_string(),
47                cx.codegen_unit().name());
48     }
49
50     fn predefine<Bx: BuilderMethods<'a, 'tcx>>(
51         &self,
52         cx: &'a Bx::CodegenCx,
53         linkage: Linkage,
54         visibility: Visibility
55     ) {
56         debug!("BEGIN PREDEFINING '{} ({})' in cgu {}",
57                self.to_string(cx.tcx(), true),
58                self.to_raw_string(),
59                cx.codegen_unit().name());
60
61         let symbol_name = self.symbol_name(cx.tcx()).name.as_str();
62
63         debug!("symbol {}", &symbol_name);
64
65         match *self {
66             MonoItem::Static(def_id) => {
67                 cx.predefine_static(def_id, linkage, visibility, &symbol_name);
68             }
69             MonoItem::Fn(instance) => {
70                 cx.predefine_fn(instance, linkage, visibility, &symbol_name);
71             }
72             MonoItem::GlobalAsm(..) => {}
73         }
74
75         debug!("END PREDEFINING '{} ({})' in cgu {}",
76                self.to_string(cx.tcx(), true),
77                self.to_raw_string(),
78                cx.codegen_unit().name());
79     }
80
81     fn to_raw_string(&self) -> String {
82         match *self {
83             MonoItem::Fn(instance) => {
84                 format!("Fn({:?}, {})",
85                         instance.def,
86                         instance.substs.as_ptr() as usize)
87             }
88             MonoItem::Static(id) => {
89                 format!("Static({:?})", id)
90             }
91             MonoItem::GlobalAsm(id) => {
92                 format!("GlobalAsm({:?})", id)
93             }
94         }
95     }
96 }