2019-12-22 17:42:04 -05:00
|
|
|
use crate::base;
|
|
|
|
use crate::traits::*;
|
2018-10-03 13:49:57 +02:00
|
|
|
use rustc::mir::mono::{Linkage, Visibility};
|
|
|
|
use rustc::ty::layout::HasTyCtxt;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
2018-10-03 13:49:57 +02:00
|
|
|
|
2019-05-24 10:57:30 -05:00
|
|
|
use rustc::mir::mono::MonoItem;
|
2018-10-03 13:49:57 +02:00
|
|
|
|
2019-06-14 19:39:39 +03:00
|
|
|
pub trait MonoItemExt<'a, 'tcx> {
|
2019-05-24 12:26:30 -05:00
|
|
|
fn define<Bx: BuilderMethods<'a, 'tcx>>(&self, cx: &'a Bx::CodegenCx);
|
|
|
|
fn predefine<Bx: BuilderMethods<'a, 'tcx>>(
|
|
|
|
&self,
|
|
|
|
cx: &'a Bx::CodegenCx,
|
|
|
|
linkage: Linkage,
|
2019-12-22 17:42:04 -05:00
|
|
|
visibility: Visibility,
|
2019-05-24 12:26:30 -05:00
|
|
|
);
|
|
|
|
fn to_raw_string(&self) -> String;
|
|
|
|
}
|
2018-10-03 13:49:57 +02:00
|
|
|
|
2019-06-16 12:33:47 +03:00
|
|
|
impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {
|
2018-10-03 13:49:57 +02:00
|
|
|
fn define<Bx: BuilderMethods<'a, 'tcx>>(&self, cx: &'a Bx::CodegenCx) {
|
2019-12-22 17:42:04 -05:00
|
|
|
debug!(
|
|
|
|
"BEGIN IMPLEMENTING '{} ({})' in cgu {}",
|
|
|
|
self.to_string(cx.tcx(), true),
|
|
|
|
self.to_raw_string(),
|
|
|
|
cx.codegen_unit().name()
|
|
|
|
);
|
2018-10-03 13:49:57 +02:00
|
|
|
|
2019-05-24 12:26:30 -05:00
|
|
|
match *self {
|
2018-10-03 13:49:57 +02:00
|
|
|
MonoItem::Static(def_id) => {
|
2019-04-19 23:32:26 +03:00
|
|
|
cx.codegen_static(def_id, cx.tcx().is_mutable_static(def_id));
|
2018-10-03 13:49:57 +02:00
|
|
|
}
|
2019-03-04 09:00:30 +01:00
|
|
|
MonoItem::GlobalAsm(hir_id) => {
|
2019-06-14 18:58:55 +02:00
|
|
|
let item = cx.tcx().hir().expect_item(hir_id);
|
2019-09-26 17:51:36 +01:00
|
|
|
if let hir::ItemKind::GlobalAsm(ref ga) = item.kind {
|
2018-10-03 13:49:57 +02:00
|
|
|
cx.codegen_global_asm(ga);
|
|
|
|
} else {
|
|
|
|
span_bug!(item.span, "Mismatch between hir::Item type and MonoItem type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MonoItem::Fn(instance) => {
|
|
|
|
base::codegen_instance::<Bx>(&cx, instance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
debug!(
|
|
|
|
"END IMPLEMENTING '{} ({})' in cgu {}",
|
|
|
|
self.to_string(cx.tcx(), true),
|
|
|
|
self.to_raw_string(),
|
|
|
|
cx.codegen_unit().name()
|
|
|
|
);
|
2018-10-03 13:49:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn predefine<Bx: BuilderMethods<'a, 'tcx>>(
|
|
|
|
&self,
|
|
|
|
cx: &'a Bx::CodegenCx,
|
|
|
|
linkage: Linkage,
|
2019-12-22 17:42:04 -05:00
|
|
|
visibility: Visibility,
|
2018-10-03 13:49:57 +02:00
|
|
|
) {
|
2019-12-22 17:42:04 -05:00
|
|
|
debug!(
|
|
|
|
"BEGIN PREDEFINING '{} ({})' in cgu {}",
|
|
|
|
self.to_string(cx.tcx(), true),
|
|
|
|
self.to_raw_string(),
|
|
|
|
cx.codegen_unit().name()
|
|
|
|
);
|
2018-10-03 13:49:57 +02:00
|
|
|
|
2019-09-03 16:06:42 +10:00
|
|
|
let symbol_name = self.symbol_name(cx.tcx()).name.as_str();
|
2018-10-03 13:49:57 +02:00
|
|
|
|
|
|
|
debug!("symbol {}", &symbol_name);
|
|
|
|
|
2019-05-24 12:26:30 -05:00
|
|
|
match *self {
|
2018-10-03 13:49:57 +02:00
|
|
|
MonoItem::Static(def_id) => {
|
|
|
|
cx.predefine_static(def_id, linkage, visibility, &symbol_name);
|
|
|
|
}
|
|
|
|
MonoItem::Fn(instance) => {
|
|
|
|
cx.predefine_fn(instance, linkage, visibility, &symbol_name);
|
|
|
|
}
|
|
|
|
MonoItem::GlobalAsm(..) => {}
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
debug!(
|
|
|
|
"END PREDEFINING '{} ({})' in cgu {}",
|
|
|
|
self.to_string(cx.tcx(), true),
|
|
|
|
self.to_raw_string(),
|
|
|
|
cx.codegen_unit().name()
|
|
|
|
);
|
2018-10-03 13:49:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn to_raw_string(&self) -> String {
|
2019-05-24 12:26:30 -05:00
|
|
|
match *self {
|
2018-10-03 13:49:57 +02:00
|
|
|
MonoItem::Fn(instance) => {
|
2019-12-22 17:42:04 -05:00
|
|
|
format!("Fn({:?}, {})", instance.def, instance.substs.as_ptr() as usize)
|
2018-10-03 13:49:57 +02:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
MonoItem::Static(id) => format!("Static({:?})", id),
|
|
|
|
MonoItem::GlobalAsm(id) => format!("GlobalAsm({:?})", id),
|
2018-10-03 13:49:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|