]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_ssa/src/mono_item.rs
Rollup merge of #82091 - henryboisdequin:use-place-ref-more, r=RalfJung
[rust.git] / compiler / rustc_codegen_ssa / src / mono_item.rs
1 use crate::base;
2 use crate::traits::*;
3 use rustc_hir as hir;
4 use rustc_middle::mir::mono::{Linkage, Visibility};
5 use rustc_middle::ty::layout::HasTyCtxt;
6
7 use rustc_middle::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!(
23             "BEGIN IMPLEMENTING '{} ({})' in cgu {}",
24             self,
25             self.to_raw_string(),
26             cx.codegen_unit().name()
27         );
28
29         match *self {
30             MonoItem::Static(def_id) => {
31                 cx.codegen_static(def_id, cx.tcx().is_mutable_static(def_id));
32             }
33             MonoItem::GlobalAsm(item_id) => {
34                 let item = cx.tcx().hir().item(item_id);
35                 if let hir::ItemKind::GlobalAsm(ref ga) = item.kind {
36                     cx.codegen_global_asm(ga);
37                 } else {
38                     span_bug!(item.span, "Mismatch between hir::Item type and MonoItem type")
39                 }
40             }
41             MonoItem::Fn(instance) => {
42                 base::codegen_instance::<Bx>(&cx, instance);
43             }
44         }
45
46         debug!(
47             "END IMPLEMENTING '{} ({})' in cgu {}",
48             self,
49             self.to_raw_string(),
50             cx.codegen_unit().name()
51         );
52     }
53
54     fn predefine<Bx: BuilderMethods<'a, 'tcx>>(
55         &self,
56         cx: &'a Bx::CodegenCx,
57         linkage: Linkage,
58         visibility: Visibility,
59     ) {
60         debug!(
61             "BEGIN PREDEFINING '{} ({})' in cgu {}",
62             self,
63             self.to_raw_string(),
64             cx.codegen_unit().name()
65         );
66
67         let symbol_name = self.symbol_name(cx.tcx()).name;
68
69         debug!("symbol {}", &symbol_name);
70
71         match *self {
72             MonoItem::Static(def_id) => {
73                 cx.predefine_static(def_id, linkage, visibility, &symbol_name);
74             }
75             MonoItem::Fn(instance) => {
76                 cx.predefine_fn(instance, linkage, visibility, &symbol_name);
77             }
78             MonoItem::GlobalAsm(..) => {}
79         }
80
81         debug!(
82             "END PREDEFINING '{} ({})' in cgu {}",
83             self,
84             self.to_raw_string(),
85             cx.codegen_unit().name()
86         );
87     }
88
89     fn to_raw_string(&self) -> String {
90         match *self {
91             MonoItem::Fn(instance) => {
92                 format!("Fn({:?}, {})", instance.def, instance.substs.as_ptr() as usize)
93             }
94             MonoItem::Static(id) => format!("Static({:?})", id),
95             MonoItem::GlobalAsm(id) => format!("GlobalAsm({:?})", id),
96         }
97     }
98 }