@@ -8,17 +8,14 @@ use crate::value::Value;
88use libc:: c_uint;
99use rustc_codegen_ssa:: traits:: * ;
1010use rustc_data_structures:: const_cstr;
11- use rustc_hir as hir;
1211use rustc_hir:: def_id:: DefId ;
13- use rustc_hir:: Node ;
1412use rustc_middle:: middle:: codegen_fn_attrs:: { CodegenFnAttrFlags , CodegenFnAttrs } ;
1513use rustc_middle:: mir:: interpret:: {
1614 read_target_uint, Allocation , ErrorHandled , GlobalAlloc , Pointer ,
1715} ;
1816use rustc_middle:: mir:: mono:: MonoItem ;
1917use rustc_middle:: ty:: { self , Instance , Ty } ;
2018use rustc_middle:: { bug, span_bug} ;
21- use rustc_span:: symbol:: sym;
2219use rustc_target:: abi:: { AddressSpace , Align , HasDataLayout , LayoutOf , Primitive , Scalar , Size } ;
2320use tracing:: debug;
2421
@@ -209,70 +206,42 @@ impl CodegenCx<'ll, 'tcx> {
209206
210207 let ty = instance. ty ( self . tcx , ty:: ParamEnv :: reveal_all ( ) ) ;
211208 let sym = self . tcx . symbol_name ( instance) . name ;
209+ let fn_attrs = self . tcx . codegen_fn_attrs ( def_id) ;
212210
213- debug ! ( "get_static: sym={} instance={:?}" , sym, instance) ;
211+ debug ! ( "get_static: sym={} instance={:?} fn_attrs={:?} " , sym, instance, fn_attrs ) ;
214212
215- let g = if let Some ( local_def_id) = def_id. as_local ( ) {
216- let id = self . tcx . hir ( ) . local_def_id_to_hir_id ( local_def_id) ;
213+ let g = if def_id. is_local ( ) && !self . tcx . is_foreign_item ( def_id) {
217214 let llty = self . layout_of ( ty) . llvm_type ( self ) ;
218- // FIXME: refactor this to work without accessing the HIR
219- let ( g, attrs) = match self . tcx . hir ( ) . get ( id) {
220- Node :: Item ( & hir:: Item { attrs, kind : hir:: ItemKind :: Static ( ..) , .. } ) => {
221- if let Some ( g) = self . get_declared_value ( sym) {
222- if self . val_ty ( g) != self . type_ptr_to ( llty) {
223- span_bug ! ( self . tcx. def_span( def_id) , "Conflicting types for static" ) ;
224- }
225- }
226-
227- let g = self . declare_global ( sym, llty) ;
228-
229- if !self . tcx . is_reachable_non_generic ( local_def_id) {
230- unsafe {
231- llvm:: LLVMRustSetVisibility ( g, llvm:: Visibility :: Hidden ) ;
232- }
233- }
234-
235- ( g, attrs)
215+ if let Some ( g) = self . get_declared_value ( sym) {
216+ if self . val_ty ( g) != self . type_ptr_to ( llty) {
217+ span_bug ! ( self . tcx. def_span( def_id) , "Conflicting types for static" ) ;
236218 }
219+ }
237220
238- Node :: ForeignItem ( & hir:: ForeignItem {
239- ref attrs,
240- kind : hir:: ForeignItemKind :: Static ( ..) ,
241- ..
242- } ) => {
243- let fn_attrs = self . tcx . codegen_fn_attrs ( local_def_id) ;
244- ( check_and_apply_linkage ( & self , & fn_attrs, ty, sym, def_id) , & * * attrs)
245- }
246-
247- item => bug ! ( "get_static: expected static, found {:?}" , item) ,
248- } ;
249-
250- debug ! ( "get_static: sym={} attrs={:?}" , sym, attrs) ;
221+ let g = self . declare_global ( sym, llty) ;
251222
252- for attr in attrs {
253- if self . tcx . sess . check_name ( attr , sym :: thread_local ) {
254- llvm:: set_thread_local_mode ( g, self . tls_model ) ;
223+ if ! self . tcx . is_reachable_non_generic ( def_id ) {
224+ unsafe {
225+ llvm:: LLVMRustSetVisibility ( g, llvm :: Visibility :: Hidden ) ;
255226 }
256227 }
257228
258229 g
259230 } else {
260- // FIXME(nagisa): perhaps the map of externs could be offloaded to llvm somehow?
261- debug ! ( "get_static: sym={} item_attr={:?}" , sym , self . tcx . item_attrs ( def_id ) ) ;
231+ check_and_apply_linkage ( & self , & fn_attrs , ty , sym , def_id )
232+ } ;
262233
263- let attrs = self . tcx . codegen_fn_attrs ( def_id) ;
264- let g = check_and_apply_linkage ( & self , & attrs, ty, sym, def_id) ;
265-
266- // Thread-local statics in some other crate need to *always* be linked
267- // against in a thread-local fashion, so we need to be sure to apply the
268- // thread-local attribute locally if it was present remotely. If we
269- // don't do this then linker errors can be generated where the linker
270- // complains that one object files has a thread local version of the
271- // symbol and another one doesn't.
272- if attrs. flags . contains ( CodegenFnAttrFlags :: THREAD_LOCAL ) {
273- llvm:: set_thread_local_mode ( g, self . tls_model ) ;
274- }
234+ // Thread-local statics in some other crate need to *always* be linked
235+ // against in a thread-local fashion, so we need to be sure to apply the
236+ // thread-local attribute locally if it was present remotely. If we
237+ // don't do this then linker errors can be generated where the linker
238+ // complains that one object files has a thread local version of the
239+ // symbol and another one doesn't.
240+ if fn_attrs. flags . contains ( CodegenFnAttrFlags :: THREAD_LOCAL ) {
241+ llvm:: set_thread_local_mode ( g, self . tls_model ) ;
242+ }
275243
244+ if !def_id. is_local ( ) {
276245 let needs_dll_storage_attr = self . use_dll_storage_attrs && !self . tcx . is_foreign_item ( def_id) &&
277246 // ThinLTO can't handle this workaround in all cases, so we don't
278247 // emit the attrs. Instead we make them unnecessary by disallowing
@@ -304,8 +273,7 @@ impl CodegenCx<'ll, 'tcx> {
304273 }
305274 }
306275 }
307- g
308- } ;
276+ }
309277
310278 if self . use_dll_storage_attrs && self . tcx . is_dllimport_foreign_item ( def_id) {
311279 // For foreign (native) libs we know the exact storage type to use.
0 commit comments