@@ -8,17 +8,14 @@ use crate::value::Value;
8
8
use libc:: c_uint;
9
9
use rustc_codegen_ssa:: traits:: * ;
10
10
use rustc_data_structures:: const_cstr;
11
- use rustc_hir as hir;
12
11
use rustc_hir:: def_id:: DefId ;
13
- use rustc_hir:: Node ;
14
12
use rustc_middle:: middle:: codegen_fn_attrs:: { CodegenFnAttrFlags , CodegenFnAttrs } ;
15
13
use rustc_middle:: mir:: interpret:: {
16
14
read_target_uint, Allocation , ErrorHandled , GlobalAlloc , Pointer ,
17
15
} ;
18
16
use rustc_middle:: mir:: mono:: MonoItem ;
19
17
use rustc_middle:: ty:: { self , Instance , Ty } ;
20
18
use rustc_middle:: { bug, span_bug} ;
21
- use rustc_span:: symbol:: sym;
22
19
use rustc_target:: abi:: { AddressSpace , Align , HasDataLayout , LayoutOf , Primitive , Scalar , Size } ;
23
20
use tracing:: debug;
24
21
@@ -209,70 +206,42 @@ impl CodegenCx<'ll, 'tcx> {
209
206
210
207
let ty = instance. ty ( self . tcx , ty:: ParamEnv :: reveal_all ( ) ) ;
211
208
let sym = self . tcx . symbol_name ( instance) . name ;
209
+ let fn_attrs = self . tcx . codegen_fn_attrs ( def_id) ;
212
210
213
- debug ! ( "get_static: sym={} instance={:?}" , sym, instance) ;
211
+ debug ! ( "get_static: sym={} instance={:?} fn_attrs={:?} " , sym, instance, fn_attrs ) ;
214
212
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) {
217
214
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" ) ;
236
218
}
219
+ }
237
220
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) ;
251
222
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 ) ;
255
226
}
256
227
}
257
228
258
229
g
259
230
} 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
+ } ;
262
233
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
+ }
275
243
244
+ if !def_id. is_local ( ) {
276
245
let needs_dll_storage_attr = self . use_dll_storage_attrs && !self . tcx . is_foreign_item ( def_id) &&
277
246
// ThinLTO can't handle this workaround in all cases, so we don't
278
247
// emit the attrs. Instead we make them unnecessary by disallowing
@@ -304,8 +273,7 @@ impl CodegenCx<'ll, 'tcx> {
304
273
}
305
274
}
306
275
}
307
- g
308
- } ;
276
+ }
309
277
310
278
if self . use_dll_storage_attrs && self . tcx . is_dllimport_foreign_item ( def_id) {
311
279
// For foreign (native) libs we know the exact storage type to use.
0 commit comments