1
1
use rustc_ast:: { NestedMetaItem , CRATE_NODE_ID } ;
2
2
use rustc_attr as attr;
3
3
use rustc_data_structures:: fx:: FxHashSet ;
4
- use rustc_hir as hir;
5
- use rustc_hir:: def:: DefKind ;
4
+ use rustc_middle:: query:: LocalCrate ;
6
5
use rustc_middle:: ty:: { List , ParamEnv , ParamEnvAnd , Ty , TyCtxt } ;
7
6
use rustc_session:: config:: CrateType ;
8
- use rustc_session:: cstore:: { DllCallingConvention , DllImport , NativeLib , PeImportNameType } ;
7
+ use rustc_session:: cstore:: {
8
+ DllCallingConvention , DllImport , ForeignModule , NativeLib , PeImportNameType ,
9
+ } ;
9
10
use rustc_session:: parse:: feature_err;
10
11
use rustc_session:: search_paths:: PathKind ;
11
12
use rustc_session:: utils:: NativeLibKind ;
12
13
use rustc_session:: Session ;
14
+ use rustc_span:: def_id:: { DefId , LOCAL_CRATE } ;
13
15
use rustc_span:: symbol:: { sym, Symbol } ;
14
16
use rustc_target:: spec:: abi:: Abi ;
15
17
@@ -66,10 +68,12 @@ fn find_bundled_library(
66
68
None
67
69
}
68
70
69
- pub ( crate ) fn collect ( tcx : TyCtxt < ' _ > ) -> Vec < NativeLib > {
71
+ pub ( crate ) fn collect ( tcx : TyCtxt < ' _ > , LocalCrate : LocalCrate ) -> Vec < NativeLib > {
70
72
let mut collector = Collector { tcx, libs : Vec :: new ( ) } ;
71
- for id in tcx. hir ( ) . items ( ) {
72
- collector. process_item ( id) ;
73
+ if tcx. sess . opts . unstable_opts . link_directives {
74
+ for module in tcx. foreign_modules ( LOCAL_CRATE ) . values ( ) {
75
+ collector. process_module ( module) ;
76
+ }
73
77
}
74
78
collector. process_command_line ( ) ;
75
79
collector. libs
@@ -88,29 +92,20 @@ struct Collector<'tcx> {
88
92
}
89
93
90
94
impl < ' tcx > Collector < ' tcx > {
91
- fn process_item ( & mut self , id : rustc_hir:: ItemId ) {
92
- if !matches ! ( self . tcx. def_kind( id. owner_id) , DefKind :: ForeignMod ) {
93
- return ;
94
- }
95
+ fn process_module ( & mut self , module : & ForeignModule ) {
96
+ let ForeignModule { def_id, abi, ref foreign_items } = * module;
97
+ let def_id = def_id. expect_local ( ) ;
95
98
96
- let it = self . tcx . hir ( ) . item ( id) ;
97
- let hir:: ItemKind :: ForeignMod { abi, items : foreign_mod_items } = it. kind else {
98
- return ;
99
- } ;
99
+ let sess = self . tcx . sess ;
100
100
101
101
if matches ! ( abi, Abi :: Rust | Abi :: RustIntrinsic | Abi :: PlatformIntrinsic ) {
102
102
return ;
103
103
}
104
104
105
105
// Process all of the #[link(..)]-style arguments
106
- let sess = self . tcx . sess ;
107
106
let features = self . tcx . features ( ) ;
108
107
109
- if !sess. opts . unstable_opts . link_directives {
110
- return ;
111
- }
112
-
113
- for m in self . tcx . hir ( ) . attrs ( it. hir_id ( ) ) . iter ( ) . filter ( |a| a. has_name ( sym:: link) ) {
108
+ for m in self . tcx . get_attrs ( def_id, sym:: link) {
114
109
let Some ( items) = m. meta_item_list ( ) else {
115
110
continue ;
116
111
} ;
@@ -340,9 +335,9 @@ impl<'tcx> Collector<'tcx> {
340
335
if name. as_str ( ) . contains ( '\0' ) {
341
336
sess. emit_err ( errors:: RawDylibNoNul { span : name_span } ) ;
342
337
}
343
- foreign_mod_items
338
+ foreign_items
344
339
. iter ( )
345
- . map ( |child_item| {
340
+ . map ( |& child_item| {
346
341
self . build_dll_import (
347
342
abi,
348
343
import_name_type. map ( |( import_name_type, _) | import_name_type) ,
@@ -352,21 +347,12 @@ impl<'tcx> Collector<'tcx> {
352
347
. collect ( )
353
348
}
354
349
_ => {
355
- for child_item in foreign_mod_items {
356
- if self . tcx . def_kind ( child_item. id . owner_id ) . has_codegen_attrs ( )
357
- && self
358
- . tcx
359
- . codegen_fn_attrs ( child_item. id . owner_id )
360
- . link_ordinal
361
- . is_some ( )
350
+ for & child_item in foreign_items {
351
+ if self . tcx . def_kind ( child_item) . has_codegen_attrs ( )
352
+ && self . tcx . codegen_fn_attrs ( child_item) . link_ordinal . is_some ( )
362
353
{
363
- let link_ordinal_attr = self
364
- . tcx
365
- . hir ( )
366
- . attrs ( child_item. id . owner_id . into ( ) )
367
- . iter ( )
368
- . find ( |a| a. has_name ( sym:: link_ordinal) )
369
- . unwrap ( ) ;
354
+ let link_ordinal_attr =
355
+ self . tcx . get_attr ( child_item, sym:: link_ordinal) . unwrap ( ) ;
370
356
sess. emit_err ( errors:: LinkOrdinalRawDylib {
371
357
span : link_ordinal_attr. span ,
372
358
} ) ;
@@ -384,7 +370,7 @@ impl<'tcx> Collector<'tcx> {
384
370
filename,
385
371
kind,
386
372
cfg,
387
- foreign_module : Some ( it . owner_id . to_def_id ( ) ) ,
373
+ foreign_module : Some ( def_id . to_def_id ( ) ) ,
388
374
verbatim,
389
375
dll_imports,
390
376
} ) ;
@@ -476,10 +462,10 @@ impl<'tcx> Collector<'tcx> {
476
462
}
477
463
}
478
464
479
- fn i686_arg_list_size ( & self , item : & hir :: ForeignItemRef ) -> usize {
465
+ fn i686_arg_list_size ( & self , item : DefId ) -> usize {
480
466
let argument_types: & List < Ty < ' _ > > = self . tcx . erase_late_bound_regions (
481
467
self . tcx
482
- . type_of ( item. id . owner_id )
468
+ . type_of ( item)
483
469
. instantiate_identity ( )
484
470
. fn_sig ( self . tcx )
485
471
. inputs ( )
@@ -505,8 +491,10 @@ impl<'tcx> Collector<'tcx> {
505
491
& self ,
506
492
abi : Abi ,
507
493
import_name_type : Option < PeImportNameType > ,
508
- item : & hir :: ForeignItemRef ,
494
+ item : DefId ,
509
495
) -> DllImport {
496
+ let span = self . tcx . def_span ( item) ;
497
+
510
498
let calling_convention = if self . tcx . sess . target . arch == "x86" {
511
499
match abi {
512
500
Abi :: C { .. } | Abi :: Cdecl { .. } => DllCallingConvention :: C ,
@@ -520,29 +508,29 @@ impl<'tcx> Collector<'tcx> {
520
508
DllCallingConvention :: Vectorcall ( self . i686_arg_list_size ( item) )
521
509
}
522
510
_ => {
523
- self . tcx . sess . emit_fatal ( errors:: UnsupportedAbiI686 { span : item . span } ) ;
511
+ self . tcx . sess . emit_fatal ( errors:: UnsupportedAbiI686 { span } ) ;
524
512
}
525
513
}
526
514
} else {
527
515
match abi {
528
516
Abi :: C { .. } | Abi :: Win64 { .. } | Abi :: System { .. } => DllCallingConvention :: C ,
529
517
_ => {
530
- self . tcx . sess . emit_fatal ( errors:: UnsupportedAbi { span : item . span } ) ;
518
+ self . tcx . sess . emit_fatal ( errors:: UnsupportedAbi { span } ) ;
531
519
}
532
520
}
533
521
} ;
534
522
535
- let codegen_fn_attrs = self . tcx . codegen_fn_attrs ( item. id . owner_id ) ;
523
+ let codegen_fn_attrs = self . tcx . codegen_fn_attrs ( item) ;
536
524
let import_name_type = codegen_fn_attrs
537
525
. link_ordinal
538
526
. map_or ( import_name_type, |ord| Some ( PeImportNameType :: Ordinal ( ord) ) ) ;
539
527
540
528
DllImport {
541
- name : codegen_fn_attrs. link_name . unwrap_or ( item . ident . name ) ,
529
+ name : codegen_fn_attrs. link_name . unwrap_or ( self . tcx . item_name ( item ) ) ,
542
530
import_name_type,
543
531
calling_convention,
544
- span : item . span ,
545
- is_fn : self . tcx . def_kind ( item. id . owner_id ) . is_fn_like ( ) ,
532
+ span,
533
+ is_fn : self . tcx . def_kind ( item) . is_fn_like ( ) ,
546
534
}
547
535
}
548
536
}
0 commit comments