11use rustc_ast:: { NestedMetaItem , CRATE_NODE_ID } ;
22use rustc_attr as attr;
33use rustc_data_structures:: fx:: FxHashSet ;
4- use rustc_hir as hir;
5- use rustc_hir:: def:: DefKind ;
4+ use rustc_middle:: query:: LocalCrate ;
65use rustc_middle:: ty:: { List , ParamEnv , ParamEnvAnd , Ty , TyCtxt } ;
76use 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+ } ;
910use rustc_session:: parse:: feature_err;
1011use rustc_session:: search_paths:: PathKind ;
1112use rustc_session:: utils:: NativeLibKind ;
1213use rustc_session:: Session ;
14+ use rustc_span:: def_id:: { DefId , LOCAL_CRATE } ;
1315use rustc_span:: symbol:: { sym, Symbol } ;
1416use rustc_target:: spec:: abi:: Abi ;
1517
@@ -66,10 +68,12 @@ fn find_bundled_library(
6668 None
6769}
6870
69- pub ( crate ) fn collect ( tcx : TyCtxt < ' _ > ) -> Vec < NativeLib > {
71+ pub ( crate ) fn collect ( tcx : TyCtxt < ' _ > , LocalCrate : LocalCrate ) -> Vec < NativeLib > {
7072 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+ }
7377 }
7478 collector. process_command_line ( ) ;
7579 collector. libs
@@ -88,29 +92,20 @@ struct Collector<'tcx> {
8892}
8993
9094impl < ' 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 ( ) ;
9598
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 ;
100100
101101 if matches ! ( abi, Abi :: Rust | Abi :: RustIntrinsic | Abi :: PlatformIntrinsic ) {
102102 return ;
103103 }
104104
105105 // Process all of the #[link(..)]-style arguments
106- let sess = self . tcx . sess ;
107106 let features = self . tcx . features ( ) ;
108107
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) {
114109 let Some ( items) = m. meta_item_list ( ) else {
115110 continue ;
116111 } ;
@@ -340,9 +335,9 @@ impl<'tcx> Collector<'tcx> {
340335 if name. as_str ( ) . contains ( '\0' ) {
341336 sess. emit_err ( errors:: RawDylibNoNul { span : name_span } ) ;
342337 }
343- foreign_mod_items
338+ foreign_items
344339 . iter ( )
345- . map ( |child_item| {
340+ . map ( |& child_item| {
346341 self . build_dll_import (
347342 abi,
348343 import_name_type. map ( |( import_name_type, _) | import_name_type) ,
@@ -352,21 +347,12 @@ impl<'tcx> Collector<'tcx> {
352347 . collect ( )
353348 }
354349 _ => {
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 ( )
362353 {
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 ( ) ;
370356 sess. emit_err ( errors:: LinkOrdinalRawDylib {
371357 span : link_ordinal_attr. span ,
372358 } ) ;
@@ -384,7 +370,7 @@ impl<'tcx> Collector<'tcx> {
384370 filename,
385371 kind,
386372 cfg,
387- foreign_module : Some ( it . owner_id . to_def_id ( ) ) ,
373+ foreign_module : Some ( def_id . to_def_id ( ) ) ,
388374 verbatim,
389375 dll_imports,
390376 } ) ;
@@ -476,10 +462,10 @@ impl<'tcx> Collector<'tcx> {
476462 }
477463 }
478464
479- fn i686_arg_list_size ( & self , item : & hir :: ForeignItemRef ) -> usize {
465+ fn i686_arg_list_size ( & self , item : DefId ) -> usize {
480466 let argument_types: & List < Ty < ' _ > > = self . tcx . erase_late_bound_regions (
481467 self . tcx
482- . type_of ( item. id . owner_id )
468+ . type_of ( item)
483469 . instantiate_identity ( )
484470 . fn_sig ( self . tcx )
485471 . inputs ( )
@@ -505,8 +491,10 @@ impl<'tcx> Collector<'tcx> {
505491 & self ,
506492 abi : Abi ,
507493 import_name_type : Option < PeImportNameType > ,
508- item : & hir :: ForeignItemRef ,
494+ item : DefId ,
509495 ) -> DllImport {
496+ let span = self . tcx . def_span ( item) ;
497+
510498 let calling_convention = if self . tcx . sess . target . arch == "x86" {
511499 match abi {
512500 Abi :: C { .. } | Abi :: Cdecl { .. } => DllCallingConvention :: C ,
@@ -520,29 +508,29 @@ impl<'tcx> Collector<'tcx> {
520508 DllCallingConvention :: Vectorcall ( self . i686_arg_list_size ( item) )
521509 }
522510 _ => {
523- self . tcx . sess . emit_fatal ( errors:: UnsupportedAbiI686 { span : item . span } ) ;
511+ self . tcx . sess . emit_fatal ( errors:: UnsupportedAbiI686 { span } ) ;
524512 }
525513 }
526514 } else {
527515 match abi {
528516 Abi :: C { .. } | Abi :: Win64 { .. } | Abi :: System { .. } => DllCallingConvention :: C ,
529517 _ => {
530- self . tcx . sess . emit_fatal ( errors:: UnsupportedAbi { span : item . span } ) ;
518+ self . tcx . sess . emit_fatal ( errors:: UnsupportedAbi { span } ) ;
531519 }
532520 }
533521 } ;
534522
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) ;
536524 let import_name_type = codegen_fn_attrs
537525 . link_ordinal
538526 . map_or ( import_name_type, |ord| Some ( PeImportNameType :: Ordinal ( ord) ) ) ;
539527
540528 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 ) ) ,
542530 import_name_type,
543531 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 ( ) ,
546534 }
547535 }
548536}
0 commit comments