4 use crate::context
::CodegenCx
;
6 use crate::type_of
::LayoutLlvmExt
;
7 use rustc_codegen_ssa
::traits
::*;
8 use rustc_hir
::def_id
::{DefId, LOCAL_CRATE}
;
9 pub use rustc_middle
::mir
::mono
::MonoItem
;
10 use rustc_middle
::mir
::mono
::{Linkage, Visibility}
;
11 use rustc_middle
::ty
::layout
::FnAbiExt
;
12 use rustc_middle
::ty
::{self, Instance, TypeFoldable}
;
13 use rustc_target
::abi
::LayoutOf
;
16 impl PreDefineMethods
<'tcx
> for CodegenCx
<'ll
, 'tcx
> {
21 visibility
: Visibility
,
24 let instance
= Instance
::mono(self.tcx
, def_id
);
25 let ty
= instance
.ty(self.tcx
, ty
::ParamEnv
::reveal_all());
26 let llty
= self.layout_of(ty
).llvm_type(self);
28 let g
= self.define_global(symbol_name
, llty
).unwrap_or_else(|| {
29 self.sess().span_fatal(
30 self.tcx
.def_span(def_id
),
31 &format
!("symbol `{}` is already defined", symbol_name
),
36 llvm
::LLVMRustSetLinkage(g
, base
::linkage_to_llvm(linkage
));
37 llvm
::LLVMRustSetVisibility(g
, base
::visibility_to_llvm(visibility
));
40 self.instances
.borrow_mut().insert(instance
, g
);
45 instance
: Instance
<'tcx
>,
47 visibility
: Visibility
,
50 assert
!(!instance
.substs
.needs_infer());
52 let fn_abi
= FnAbi
::of_instance(self, instance
, &[]);
53 let lldecl
= self.declare_fn(symbol_name
, &fn_abi
);
54 unsafe { llvm::LLVMRustSetLinkage(lldecl, base::linkage_to_llvm(linkage)) }
;
55 let attrs
= self.tcx
.codegen_fn_attrs(instance
.def_id());
56 base
::set_link_section(lldecl
, &attrs
);
57 if linkage
== Linkage
::LinkOnceODR
|| linkage
== Linkage
::WeakODR
{
58 llvm
::SetUniqueComdat(self.llmod
, lldecl
);
61 // If we're compiling the compiler-builtins crate, e.g., the equivalent of
62 // compiler-rt, then we want to implicitly compile everything with hidden
63 // visibility as we're going to link this object all over the place but
64 // don't want the symbols to get exported.
65 if linkage
!= Linkage
::Internal
66 && linkage
!= Linkage
::Private
67 && self.tcx
.is_compiler_builtins(LOCAL_CRATE
)
70 llvm
::LLVMRustSetVisibility(lldecl
, llvm
::Visibility
::Hidden
);
74 llvm
::LLVMRustSetVisibility(lldecl
, base
::visibility_to_llvm(visibility
));
78 debug
!("predefine_fn: instance = {:?}", instance
);
80 attributes
::from_fn_attrs(self, lldecl
, instance
);
82 self.instances
.borrow_mut().insert(instance
, lldecl
);