|
3 | 3 | //! This trait is currently the main interface between the Rust compiler,
|
4 | 4 | //! and the `stable_mir` crate.
|
5 | 5 |
|
| 6 | +#![allow(rustc::usage_of_qualified_ty)] |
| 7 | + |
| 8 | +use rustc_abi::HasDataLayout; |
6 | 9 | use rustc_middle::ty;
|
| 10 | +use rustc_middle::ty::layout::{ |
| 11 | + FnAbiOf, FnAbiOfHelpers, HasParamEnv, HasTyCtxt, LayoutOf, LayoutOfHelpers, |
| 12 | +}; |
7 | 13 | use rustc_middle::ty::print::{with_forced_trimmed_paths, with_no_trimmed_paths};
|
8 | 14 | use rustc_middle::ty::{
|
9 |
| - GenericPredicates, Instance, ParamEnv, ScalarInt, TypeVisitableExt, ValTree, |
| 15 | + GenericPredicates, Instance, List, ParamEnv, ScalarInt, TyCtxt, TypeVisitableExt, ValTree, |
10 | 16 | };
|
11 | 17 | use rustc_span::def_id::LOCAL_CRATE;
|
| 18 | +use stable_mir::abi::{FnAbi, Layout, LayoutShape}; |
12 | 19 | use stable_mir::compiler_interface::Context;
|
13 | 20 | use stable_mir::mir::alloc::GlobalAlloc;
|
14 | 21 | use stable_mir::mir::mono::{InstanceDef, StaticDef};
|
@@ -280,7 +287,6 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
|
280 | 287 | tables.tcx.mk_ty_from_kind(internal_kind).stable(&mut *tables)
|
281 | 288 | }
|
282 | 289 |
|
283 |
| - #[allow(rustc::usage_of_qualified_ty)] |
284 | 290 | fn new_box_ty(&self, ty: stable_mir::ty::Ty) -> stable_mir::ty::Ty {
|
285 | 291 | let mut tables = self.0.borrow_mut();
|
286 | 292 | let inner = ty.internal(&mut *tables);
|
@@ -335,6 +341,12 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
|
335 | 341 | instance.ty(tables.tcx, ParamEnv::reveal_all()).stable(&mut *tables)
|
336 | 342 | }
|
337 | 343 |
|
| 344 | + fn instance_abi(&self, def: InstanceDef) -> Result<FnAbi, Error> { |
| 345 | + let mut tables = self.0.borrow_mut(); |
| 346 | + let instance = tables.instances[def]; |
| 347 | + Ok(tables.fn_abi_of_instance(instance, List::empty())?.stable(&mut *tables)) |
| 348 | + } |
| 349 | + |
338 | 350 | fn instance_def_id(&self, def: InstanceDef) -> stable_mir::DefId {
|
339 | 351 | let mut tables = self.0.borrow_mut();
|
340 | 352 | let def_id = tables.instances[def].def_id();
|
@@ -473,6 +485,65 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
|
473 | 485 | )
|
474 | 486 | }
|
475 | 487 | }
|
| 488 | + |
| 489 | + fn ty_layout(&self, ty: Ty) -> Result<Layout, Error> { |
| 490 | + let mut tables = self.0.borrow_mut(); |
| 491 | + let ty = ty.internal(&mut *tables); |
| 492 | + let layout = tables.layout_of(ty)?.layout; |
| 493 | + Ok(layout.stable(&mut *tables)) |
| 494 | + } |
| 495 | + |
| 496 | + fn layout_shape(&self, id: Layout) -> LayoutShape { |
| 497 | + let mut tables = self.0.borrow_mut(); |
| 498 | + id.internal(&mut *tables).0.stable(&mut *tables) |
| 499 | + } |
476 | 500 | }
|
477 | 501 |
|
478 | 502 | pub struct TablesWrapper<'tcx>(pub RefCell<Tables<'tcx>>);
|
| 503 | + |
| 504 | +/// Implement error handling for extracting function ABI information. |
| 505 | +impl<'tcx> FnAbiOfHelpers<'tcx> for Tables<'tcx> { |
| 506 | + type FnAbiOfResult = Result<&'tcx rustc_target::abi::call::FnAbi<'tcx, ty::Ty<'tcx>>, Error>; |
| 507 | + |
| 508 | + #[inline] |
| 509 | + fn handle_fn_abi_err( |
| 510 | + &self, |
| 511 | + err: ty::layout::FnAbiError<'tcx>, |
| 512 | + _span: rustc_span::Span, |
| 513 | + fn_abi_request: ty::layout::FnAbiRequest<'tcx>, |
| 514 | + ) -> Error { |
| 515 | + Error::new(format!("Failed to get ABI for `{fn_abi_request:?}`: {err:?}")) |
| 516 | + } |
| 517 | +} |
| 518 | + |
| 519 | +impl<'tcx> LayoutOfHelpers<'tcx> for Tables<'tcx> { |
| 520 | + type LayoutOfResult = Result<ty::layout::TyAndLayout<'tcx>, Error>; |
| 521 | + |
| 522 | + #[inline] |
| 523 | + fn handle_layout_err( |
| 524 | + &self, |
| 525 | + err: ty::layout::LayoutError<'tcx>, |
| 526 | + _span: rustc_span::Span, |
| 527 | + ty: ty::Ty<'tcx>, |
| 528 | + ) -> Error { |
| 529 | + Error::new(format!("Failed to get layout for `{ty}`: {err}")) |
| 530 | + } |
| 531 | +} |
| 532 | + |
| 533 | +impl<'tcx> HasParamEnv<'tcx> for Tables<'tcx> { |
| 534 | + fn param_env(&self) -> ty::ParamEnv<'tcx> { |
| 535 | + ty::ParamEnv::reveal_all() |
| 536 | + } |
| 537 | +} |
| 538 | + |
| 539 | +impl<'tcx> HasTyCtxt<'tcx> for Tables<'tcx> { |
| 540 | + fn tcx(&self) -> TyCtxt<'tcx> { |
| 541 | + self.tcx |
| 542 | + } |
| 543 | +} |
| 544 | + |
| 545 | +impl<'tcx> HasDataLayout for Tables<'tcx> { |
| 546 | + fn data_layout(&self) -> &rustc_abi::TargetDataLayout { |
| 547 | + self.tcx.data_layout() |
| 548 | + } |
| 549 | +} |
0 commit comments