2018-10-03 13:49:57 +02:00
|
|
|
use rustc::hir;
|
|
|
|
use rustc::mir::mono::{Linkage, Visibility};
|
|
|
|
use rustc::ty::layout::HasTyCtxt;
|
2019-02-09 23:31:47 +09:00
|
|
|
use crate::base;
|
|
|
|
use crate::traits::*;
|
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,
|
|
|
|
visibility: Visibility
|
|
|
|
);
|
|
|
|
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) {
|
|
|
|
debug!("BEGIN IMPLEMENTING '{} ({})' in cgu {}",
|
2019-05-24 12:26:30 -05:00
|
|
|
self.to_string(cx.tcx(), true),
|
|
|
|
self.to_raw_string(),
|
2018-10-03 13:49:57 +02:00
|
|
|
cx.codegen_unit().name());
|
|
|
|
|
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);
|
2018-10-03 13:49:57 +02:00
|
|
|
if let hir::ItemKind::GlobalAsm(ref ga) = item.node {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug!("END IMPLEMENTING '{} ({})' in cgu {}",
|
2019-05-24 12:26:30 -05:00
|
|
|
self.to_string(cx.tcx(), true),
|
|
|
|
self.to_raw_string(),
|
2018-10-03 13:49:57 +02:00
|
|
|
cx.codegen_unit().name());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn predefine<Bx: BuilderMethods<'a, 'tcx>>(
|
|
|
|
&self,
|
|
|
|
cx: &'a Bx::CodegenCx,
|
|
|
|
linkage: Linkage,
|
|
|
|
visibility: Visibility
|
|
|
|
) {
|
|
|
|
debug!("BEGIN PREDEFINING '{} ({})' in cgu {}",
|
2019-05-24 12:26:30 -05:00
|
|
|
self.to_string(cx.tcx(), true),
|
|
|
|
self.to_raw_string(),
|
2018-10-03 13:49:57 +02:00
|
|
|
cx.codegen_unit().name());
|
|
|
|
|
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(..) => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug!("END PREDEFINING '{} ({})' in cgu {}",
|
2019-05-24 12:26:30 -05:00
|
|
|
self.to_string(cx.tcx(), true),
|
|
|
|
self.to_raw_string(),
|
2018-10-03 13:49:57 +02:00
|
|
|
cx.codegen_unit().name());
|
|
|
|
}
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
format!("Fn({:?}, {})",
|
|
|
|
instance.def,
|
|
|
|
instance.substs.as_ptr() as usize)
|
|
|
|
}
|
|
|
|
MonoItem::Static(id) => {
|
|
|
|
format!("Static({:?})", id)
|
|
|
|
}
|
|
|
|
MonoItem::GlobalAsm(id) => {
|
|
|
|
format!("GlobalAsm({:?})", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|