@@ -143,6 +143,46 @@ impl<'tcx, B: Bridge> SmirCtxt<'tcx, B> {
143143 self . tcx . valtree_to_const_val ( key)
144144 }
145145
146+ /// Return whether the instance as a body available.
147+ ///
148+ /// Items and intrinsics may have a body available from its definition.
149+ /// Shims body may be generated depending on their type.
150+ pub ( crate ) fn instance_has_body ( & self , instance : Instance < ' tcx > ) -> bool {
151+ let def_id = instance. def_id ( ) ;
152+ self . item_has_body ( def_id)
153+ || !matches ! (
154+ instance. def,
155+ ty:: InstanceKind :: Virtual ( ..)
156+ | ty:: InstanceKind :: Intrinsic ( ..)
157+ | ty:: InstanceKind :: Item ( ..)
158+ )
159+ }
160+
161+ /// Return whether the item has a body defined by the user.
162+ ///
163+ /// Note that intrinsics may have a placeholder body that shouldn't be used in practice.
164+ /// In StableMIR, we handle this case as if the body is not available.
165+ pub ( crate ) fn item_has_body ( & self , def_id : DefId ) -> bool {
166+ let must_override = if let Some ( intrinsic) = self . tcx . intrinsic ( def_id) {
167+ intrinsic. must_be_overridden
168+ } else {
169+ false
170+ } ;
171+ !must_override && self . tcx . is_mir_available ( def_id)
172+ }
173+
174+ fn filter_fn_def ( & self , def_id : DefId ) -> Option < DefId > {
175+ if matches ! ( self . tcx. def_kind( def_id) , DefKind :: Fn | DefKind :: AssocFn ) {
176+ Some ( def_id)
177+ } else {
178+ None
179+ }
180+ }
181+
182+ fn filter_static_def ( & self , def_id : DefId ) -> Option < DefId > {
183+ matches ! ( self . tcx. def_kind( def_id) , DefKind :: Static { .. } ) . then ( || def_id)
184+ }
185+
146186 pub fn target_endian ( & self ) -> Endian {
147187 self . tcx . data_layout . endian
148188 }
@@ -167,22 +207,22 @@ impl<'tcx, B: Bridge> SmirCtxt<'tcx, B> {
167207 }
168208
169209 /// Check whether the body of a function is available.
170- pub fn has_body ( & self , def : DefId , tables : & mut Tables < ' _ , B > ) -> bool {
171- tables . item_has_body ( def)
210+ pub fn has_body ( & self , def : DefId ) -> bool {
211+ self . item_has_body ( def)
172212 }
173213
174214 pub fn foreign_modules ( & self , crate_num : CrateNum ) -> Vec < DefId > {
175215 self . tcx . foreign_modules ( crate_num) . keys ( ) . map ( |mod_def_id| * mod_def_id) . collect ( )
176216 }
177217
178218 /// Retrieve all functions defined in this crate.
179- pub fn crate_functions ( & self , crate_num : CrateNum , tables : & mut Tables < ' _ , B > ) -> Vec < DefId > {
180- filter_def_ids ( self . tcx , crate_num, |def_id| tables . filter_fn_def ( def_id) )
219+ pub fn crate_functions ( & self , crate_num : CrateNum ) -> Vec < DefId > {
220+ filter_def_ids ( self . tcx , crate_num, |def_id| self . filter_fn_def ( def_id) )
181221 }
182222
183223 /// Retrieve all static items defined in this crate.
184- pub fn crate_statics ( & self , crate_num : CrateNum , tables : & mut Tables < ' _ , B > ) -> Vec < DefId > {
185- filter_def_ids ( self . tcx , crate_num, |def_id| tables . filter_static_def ( def_id) )
224+ pub fn crate_statics ( & self , crate_num : CrateNum ) -> Vec < DefId > {
225+ filter_def_ids ( self . tcx , crate_num, |def_id| self . filter_static_def ( def_id) )
186226 }
187227
188228 pub fn foreign_module ( & self , mod_def : DefId ) -> & ForeignModule {
@@ -574,14 +614,8 @@ impl<'tcx, B: Bridge> SmirCtxt<'tcx, B> {
574614 }
575615
576616 /// Get the body of an Instance which is already monomorphized.
577- pub fn instance_body (
578- & self ,
579- instance : ty:: Instance < ' tcx > ,
580- tables : & mut Tables < ' tcx , B > ,
581- ) -> Option < Body < ' tcx > > {
582- tables
583- . instance_has_body ( instance)
584- . then ( || BodyBuilder :: new ( self . tcx , instance) . build ( tables) )
617+ pub fn instance_body ( & self , instance : ty:: Instance < ' tcx > ) -> Option < Body < ' tcx > > {
618+ self . instance_has_body ( instance) . then ( || BodyBuilder :: new ( self . tcx , instance) . build ( ) )
585619 }
586620
587621 /// Get the instance type with generic instantiations applied and lifetimes erased.
@@ -599,18 +633,13 @@ impl<'tcx, B: Bridge> SmirCtxt<'tcx, B> {
599633 pub fn instance_abi (
600634 & self ,
601635 instance : ty:: Instance < ' tcx > ,
602- tables : & mut Tables < ' tcx , B > ,
603636 ) -> Result < & FnAbi < ' tcx , Ty < ' tcx > > , B :: Error > {
604- Ok ( tables . fn_abi_of_instance ( instance, List :: empty ( ) ) ?)
637+ Ok ( self . fn_abi_of_instance ( instance, List :: empty ( ) ) ?)
605638 }
606639
607640 /// Get the ABI of a function pointer.
608- pub fn fn_ptr_abi (
609- & self ,
610- sig : PolyFnSig < ' tcx > ,
611- tables : & mut Tables < ' tcx , B > ,
612- ) -> Result < & FnAbi < ' tcx , Ty < ' tcx > > , B :: Error > {
613- Ok ( tables. fn_abi_of_fn_ptr ( sig, List :: empty ( ) ) ?)
641+ pub fn fn_ptr_abi ( & self , sig : PolyFnSig < ' tcx > ) -> Result < & FnAbi < ' tcx , Ty < ' tcx > > , B :: Error > {
642+ Ok ( self . fn_abi_of_fn_ptr ( sig, List :: empty ( ) ) ?)
614643 }
615644
616645 /// Get the instance.
@@ -737,12 +766,8 @@ impl<'tcx, B: Bridge> SmirCtxt<'tcx, B> {
737766 }
738767
739768 /// Get the layout of a type.
740- pub fn ty_layout (
741- & self ,
742- ty : Ty < ' tcx > ,
743- tables : & mut Tables < ' tcx , B > ,
744- ) -> Result < Layout < ' tcx > , B :: Error > {
745- let layout = tables. layout_of ( ty) ?. layout ;
769+ pub fn ty_layout ( & self , ty : Ty < ' tcx > ) -> Result < Layout < ' tcx > , B :: Error > {
770+ let layout = self . layout_of ( ty) ?. layout ;
746771 Ok ( layout)
747772 }
748773
0 commit comments