rustc_public/
compiler_interface.rs

1//! Define the interface with the Rust compiler.
2//!
3//! rustc_public users should not use any of the items in this module directly.
4//! These APIs have no stability guarantee.
5
6use std::cell::{Cell, RefCell};
7
8use rustc_hir::def::DefKind;
9use rustc_public_bridge::context::CompilerCtxt;
10use rustc_public_bridge::{Bridge, Tables};
11use tracing::debug;
12
13use crate::abi::{FnAbi, Layout, LayoutShape, ReprOptions};
14use crate::crate_def::Attribute;
15use crate::mir::alloc::{AllocId, GlobalAlloc};
16use crate::mir::mono::{Instance, InstanceDef, StaticDef};
17use crate::mir::{BinOp, Body, Place, UnOp};
18use crate::target::{MachineInfo, MachineSize};
19use crate::ty::{
20    AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, CoroutineDef, Discr, FieldDef, FnDef,
21    ForeignDef, ForeignItemKind, ForeignModule, ForeignModuleDef, GenericArgs, GenericPredicates,
22    Generics, ImplDef, ImplTrait, IntrinsicDef, LineInfo, MirConst, PolyFnSig, RigidTy, Span,
23    TraitDecl, TraitDef, Ty, TyConst, TyConstId, TyKind, UintTy, VariantDef, VariantIdx,
24};
25use crate::unstable::{RustcInternal, Stable, new_item_kind};
26use crate::{
27    AssocItems, Crate, CrateDef, CrateItem, CrateItems, CrateNum, DefId, Error, Filename,
28    ImplTraitDecls, ItemKind, Symbol, ThreadLocalIndex, TraitDecls, alloc, mir,
29};
30
31pub struct BridgeTys;
32
33impl Bridge for BridgeTys {
34    type DefId = crate::DefId;
35    type AllocId = crate::mir::alloc::AllocId;
36    type Span = crate::ty::Span;
37    type Ty = crate::ty::Ty;
38    type InstanceDef = crate::mir::mono::InstanceDef;
39    type TyConstId = crate::ty::TyConstId;
40    type MirConstId = crate::ty::MirConstId;
41    type Layout = crate::abi::Layout;
42
43    type Error = crate::Error;
44    type CrateItem = crate::CrateItem;
45    type AdtDef = crate::ty::AdtDef;
46    type ForeignModuleDef = crate::ty::ForeignModuleDef;
47    type ForeignDef = crate::ty::ForeignDef;
48    type FnDef = crate::ty::FnDef;
49    type ClosureDef = crate::ty::ClosureDef;
50    type CoroutineDef = crate::ty::CoroutineDef;
51    type CoroutineClosureDef = crate::ty::CoroutineClosureDef;
52    type AliasDef = crate::ty::AliasDef;
53    type ParamDef = crate::ty::ParamDef;
54    type BrNamedDef = crate::ty::BrNamedDef;
55    type TraitDef = crate::ty::TraitDef;
56    type GenericDef = crate::ty::GenericDef;
57    type ConstDef = crate::ty::ConstDef;
58    type ImplDef = crate::ty::ImplDef;
59    type RegionDef = crate::ty::RegionDef;
60    type CoroutineWitnessDef = crate::ty::CoroutineWitnessDef;
61    type AssocDef = crate::ty::AssocDef;
62    type OpaqueDef = crate::ty::OpaqueDef;
63    type Prov = crate::ty::Prov;
64    type StaticDef = crate::mir::mono::StaticDef;
65
66    type Allocation = crate::ty::Allocation;
67}
68
69/// Public API for querying compiler information.
70///
71/// All queries are delegated to [`rustc_public_bridge::context::CompilerCtxt`]
72/// that provides similar APIs but based on internal rustc constructs.
73///
74/// Do not use this directly. This is currently used in the macro expansion.
75pub(crate) struct CompilerInterface<'tcx> {
76    pub tables: RefCell<Tables<'tcx, BridgeTys>>,
77    pub cx: RefCell<CompilerCtxt<'tcx, BridgeTys>>,
78}
79
80impl<'tcx> CompilerInterface<'tcx> {
81    pub(crate) fn entry_fn(&self) -> Option<CrateItem> {
82        let mut tables = self.tables.borrow_mut();
83        let cx = &*self.cx.borrow();
84        let did = cx.entry_fn();
85        Some(tables.crate_item(did?))
86    }
87
88    /// Retrieve all items of the local crate that have a MIR associated with them.
89    pub(crate) fn all_local_items(&self) -> CrateItems {
90        let mut tables = self.tables.borrow_mut();
91        let cx = &*self.cx.borrow();
92        cx.all_local_items().iter().map(|did| tables.crate_item(*did)).collect()
93    }
94
95    /// Retrieve the body of a function.
96    /// This function will panic if the body is not available.
97    pub(crate) fn mir_body(&self, item: DefId) -> mir::Body {
98        let mut tables = self.tables.borrow_mut();
99        let cx = &*self.cx.borrow();
100        let did = tables[item];
101        cx.mir_body(did).stable(&mut *tables, cx)
102    }
103
104    /// Check whether the body of a function is available.
105    pub(crate) fn has_body(&self, item: DefId) -> bool {
106        let mut tables = self.tables.borrow_mut();
107        let cx = &*self.cx.borrow();
108        let def = item.internal(&mut *tables, cx.tcx);
109        cx.has_body(def)
110    }
111
112    pub(crate) fn foreign_modules(&self, crate_num: CrateNum) -> Vec<ForeignModuleDef> {
113        let mut tables = self.tables.borrow_mut();
114        let cx = &*self.cx.borrow();
115        cx.foreign_modules(crate_num.internal(&mut *tables, cx.tcx))
116            .iter()
117            .map(|did| tables.foreign_module_def(*did))
118            .collect()
119    }
120
121    /// Retrieve all functions defined in this crate.
122    pub(crate) fn crate_functions(&self, crate_num: CrateNum) -> Vec<FnDef> {
123        let mut tables = self.tables.borrow_mut();
124        let cx = &*self.cx.borrow();
125        let krate = crate_num.internal(&mut *tables, cx.tcx);
126        cx.crate_functions(krate).iter().map(|did| tables.fn_def(*did)).collect()
127    }
128
129    /// Retrieve all static items defined in this crate.
130    pub(crate) fn crate_statics(&self, crate_num: CrateNum) -> Vec<StaticDef> {
131        let mut tables = self.tables.borrow_mut();
132        let cx = &*self.cx.borrow();
133        let krate = crate_num.internal(&mut *tables, cx.tcx);
134        cx.crate_statics(krate).iter().map(|did| tables.static_def(*did)).collect()
135    }
136
137    pub(crate) fn foreign_module(&self, mod_def: ForeignModuleDef) -> ForeignModule {
138        let mut tables = self.tables.borrow_mut();
139        let cx = &*self.cx.borrow();
140        let did = tables[mod_def.def_id()];
141        cx.foreign_module(did).stable(&mut *tables, cx)
142    }
143
144    pub(crate) fn foreign_items(&self, mod_def: ForeignModuleDef) -> Vec<ForeignDef> {
145        let mut tables = self.tables.borrow_mut();
146        let cx = &*self.cx.borrow();
147        let did = tables[mod_def.def_id()];
148        cx.foreign_items(did).iter().map(|did| tables.foreign_def(*did)).collect()
149    }
150
151    pub(crate) fn all_trait_decls(&self) -> TraitDecls {
152        let mut tables = self.tables.borrow_mut();
153        let cx = &*self.cx.borrow();
154        cx.all_trait_decls().map(|did| tables.trait_def(did)).collect()
155    }
156
157    pub(crate) fn trait_decls(&self, crate_num: CrateNum) -> TraitDecls {
158        let mut tables = self.tables.borrow_mut();
159        let cx = &*self.cx.borrow();
160        let krate = crate_num.internal(&mut *tables, cx.tcx);
161        cx.trait_decls(krate).iter().map(|did| tables.trait_def(*did)).collect()
162    }
163
164    pub(crate) fn trait_decl(&self, trait_def: &TraitDef) -> TraitDecl {
165        let mut tables = self.tables.borrow_mut();
166        let cx = &*self.cx.borrow();
167        let did = tables[trait_def.0];
168        cx.trait_decl(did).stable(&mut *tables, cx)
169    }
170
171    pub(crate) fn all_trait_impls(&self) -> ImplTraitDecls {
172        let mut tables = self.tables.borrow_mut();
173        let cx = &*self.cx.borrow();
174        cx.all_trait_impls().iter().map(|did| tables.impl_def(*did)).collect()
175    }
176
177    pub(crate) fn trait_impls(&self, crate_num: CrateNum) -> ImplTraitDecls {
178        let mut tables = self.tables.borrow_mut();
179        let cx = &*self.cx.borrow();
180        let krate = crate_num.internal(&mut *tables, cx.tcx);
181        cx.trait_impls(krate).iter().map(|did| tables.impl_def(*did)).collect()
182    }
183
184    pub(crate) fn trait_impl(&self, trait_impl: &ImplDef) -> ImplTrait {
185        let mut tables = self.tables.borrow_mut();
186        let cx = &*self.cx.borrow();
187        let did = tables[trait_impl.0];
188        cx.trait_impl(did).stable(&mut *tables, cx)
189    }
190
191    pub(crate) fn generics_of(&self, def_id: DefId) -> Generics {
192        let mut tables = self.tables.borrow_mut();
193        let cx = &*self.cx.borrow();
194        let did = tables[def_id];
195        cx.generics_of(did).stable(&mut *tables, cx)
196    }
197
198    pub(crate) fn predicates_of(&self, def_id: DefId) -> GenericPredicates {
199        let mut tables = self.tables.borrow_mut();
200        let cx = &*self.cx.borrow();
201        let did = tables[def_id];
202        let (parent, kinds) = cx.predicates_of(did);
203        crate::ty::GenericPredicates {
204            parent: parent.map(|did| tables.trait_def(did)),
205            predicates: kinds
206                .iter()
207                .map(|(kind, span)| (kind.stable(&mut *tables, cx), span.stable(&mut *tables, cx)))
208                .collect(),
209        }
210    }
211
212    pub(crate) fn explicit_predicates_of(&self, def_id: DefId) -> GenericPredicates {
213        let mut tables = self.tables.borrow_mut();
214        let cx = &*self.cx.borrow();
215        let did = tables[def_id];
216        let (parent, kinds) = cx.explicit_predicates_of(did);
217        crate::ty::GenericPredicates {
218            parent: parent.map(|did| tables.trait_def(did)),
219            predicates: kinds
220                .iter()
221                .map(|(kind, span)| (kind.stable(&mut *tables, cx), span.stable(&mut *tables, cx)))
222                .collect(),
223        }
224    }
225
226    /// Get information about the local crate.
227    pub(crate) fn local_crate(&self) -> Crate {
228        let cx = &*self.cx.borrow();
229        smir_crate(cx, cx.local_crate_num())
230    }
231
232    /// Retrieve a list of all external crates.
233    pub(crate) fn external_crates(&self) -> Vec<Crate> {
234        let cx = &*self.cx.borrow();
235        cx.external_crates().iter().map(|crate_num| smir_crate(cx, *crate_num)).collect()
236    }
237
238    /// Find a crate with the given name.
239    pub(crate) fn find_crates(&self, name: &str) -> Vec<Crate> {
240        let cx = &*self.cx.borrow();
241        cx.find_crates(name).iter().map(|crate_num| smir_crate(cx, *crate_num)).collect()
242    }
243
244    /// Returns the name of given `DefId`.
245    pub(crate) fn def_name(&self, def_id: DefId, trimmed: bool) -> Symbol {
246        let tables = self.tables.borrow();
247        let cx = &*self.cx.borrow();
248        let did = tables[def_id];
249        cx.def_name(did, trimmed)
250    }
251
252    /// Returns the parent of the given `DefId`.
253    pub(crate) fn def_parent(&self, def_id: DefId) -> Option<DefId> {
254        let mut tables = self.tables.borrow_mut();
255        let cx = &*self.cx.borrow();
256        let did = tables[def_id];
257        cx.def_parent(did).map(|did| tables.create_def_id(did))
258    }
259
260    /// Return registered tool attributes with the given attribute name.
261    ///
262    /// FIXME(jdonszelmann): may panic on non-tool attributes. After more attribute work, non-tool
263    /// attributes will simply return an empty list.
264    ///
265    /// Single segmented name like `#[clippy]` is specified as `&["clippy".to_string()]`.
266    /// Multi-segmented name like `#[rustfmt::skip]` is specified as `&["rustfmt".to_string(), "skip".to_string()]`.
267    pub(crate) fn tool_attrs(&self, def_id: DefId, attr: &[Symbol]) -> Vec<Attribute> {
268        let mut tables = self.tables.borrow_mut();
269        let cx = &*self.cx.borrow();
270        let did = tables[def_id];
271        cx.tool_attrs(did, attr)
272            .into_iter()
273            .map(|(attr_str, span)| Attribute::new(attr_str, span.stable(&mut *tables, cx)))
274            .collect()
275    }
276
277    /// Get all tool attributes of a definition.
278    pub(crate) fn all_tool_attrs(&self, def_id: DefId) -> Vec<Attribute> {
279        let mut tables = self.tables.borrow_mut();
280        let cx = &*self.cx.borrow();
281        let did = tables[def_id];
282        cx.all_tool_attrs(did)
283            .into_iter()
284            .map(|(attr_str, span)| Attribute::new(attr_str, span.stable(&mut *tables, cx)))
285            .collect()
286    }
287
288    /// Returns printable, human readable form of `Span`.
289    pub(crate) fn span_to_string(&self, span: Span) -> String {
290        let tables = self.tables.borrow_mut();
291        let cx = &*self.cx.borrow();
292        let sp = tables.spans[span];
293        cx.span_to_string(sp)
294    }
295
296    /// Return filename from given `Span`, for diagnostic purposes.
297    pub(crate) fn get_filename(&self, span: &Span) -> Filename {
298        let tables = self.tables.borrow_mut();
299        let cx = &*self.cx.borrow();
300        let sp = tables.spans[*span];
301        cx.get_filename(sp)
302    }
303
304    /// Return lines corresponding to this `Span`.
305    pub(crate) fn get_lines(&self, span: &Span) -> LineInfo {
306        let tables = self.tables.borrow_mut();
307        let cx = &*self.cx.borrow();
308        let sp = tables.spans[*span];
309        let lines = cx.get_lines(sp);
310        LineInfo::from(lines)
311    }
312
313    /// Returns the `kind` of given `DefId`.
314    pub(crate) fn item_kind(&self, item: CrateItem) -> ItemKind {
315        let tables = self.tables.borrow();
316        let cx = &*self.cx.borrow();
317        let did = tables[item.0];
318        new_item_kind(cx.def_kind(did))
319    }
320
321    /// Returns whether this is a foreign item.
322    pub(crate) fn is_foreign_item(&self, item: DefId) -> bool {
323        let tables = self.tables.borrow();
324        let cx = &*self.cx.borrow();
325        let did = tables[item];
326        cx.is_foreign_item(did)
327    }
328
329    /// Returns the kind of a given foreign item.
330    pub(crate) fn foreign_item_kind(&self, def: ForeignDef) -> ForeignItemKind {
331        let mut tables = self.tables.borrow_mut();
332        let cx = &*self.cx.borrow();
333        let def_id = tables[def.def_id()];
334        let def_kind = cx.foreign_item_kind(def_id);
335        match def_kind {
336            DefKind::Fn => ForeignItemKind::Fn(tables.fn_def(def_id)),
337            DefKind::Static { .. } => ForeignItemKind::Static(tables.static_def(def_id)),
338            DefKind::ForeignTy => {
339                use rustc_public_bridge::context::TyHelpers;
340                ForeignItemKind::Type(tables.intern_ty(cx.new_foreign(def_id)))
341            }
342            def_kind => unreachable!("Unexpected kind for a foreign item: {:?}", def_kind),
343        }
344    }
345
346    /// Returns the kind of a given algebraic data type.
347    pub(crate) fn adt_kind(&self, def: AdtDef) -> AdtKind {
348        let mut tables = self.tables.borrow_mut();
349        let cx = &*self.cx.borrow();
350        cx.adt_kind(def.internal(&mut *tables, cx.tcx)).stable(&mut *tables, cx)
351    }
352
353    /// Returns if the ADT is a box.
354    pub(crate) fn adt_is_box(&self, def: AdtDef) -> bool {
355        let mut tables = self.tables.borrow_mut();
356        let cx = &*self.cx.borrow();
357        cx.adt_is_box(def.internal(&mut *tables, cx.tcx))
358    }
359
360    /// Returns whether this ADT is simd.
361    pub(crate) fn adt_is_simd(&self, def: AdtDef) -> bool {
362        let mut tables = self.tables.borrow_mut();
363        let cx = &*self.cx.borrow();
364        cx.adt_is_simd(def.internal(&mut *tables, cx.tcx))
365    }
366
367    /// Returns whether this definition is a C string.
368    pub(crate) fn adt_is_cstr(&self, def: AdtDef) -> bool {
369        let mut tables = self.tables.borrow_mut();
370        let cx = &*self.cx.borrow();
371        cx.adt_is_cstr(def.0.internal(&mut *tables, cx.tcx))
372    }
373
374    /// Returns the representation options for this ADT
375    pub(crate) fn adt_repr(&self, def: AdtDef) -> ReprOptions {
376        let mut tables = self.tables.borrow_mut();
377        let cx = &*self.cx.borrow();
378        cx.adt_repr(def.internal(&mut *tables, cx.tcx)).stable(&mut *tables, cx)
379    }
380
381    /// Retrieve the function signature for the given generic arguments.
382    pub(crate) fn fn_sig(&self, def: FnDef, args: &GenericArgs) -> PolyFnSig {
383        let mut tables = self.tables.borrow_mut();
384        let cx = &*self.cx.borrow();
385        let def_id = def.0.internal(&mut *tables, cx.tcx);
386        let args_ref = args.internal(&mut *tables, cx.tcx);
387        cx.fn_sig(def_id, args_ref).stable(&mut *tables, cx)
388    }
389
390    /// Retrieve the intrinsic definition if the item corresponds one.
391    pub(crate) fn intrinsic(&self, item: DefId) -> Option<IntrinsicDef> {
392        let mut tables = self.tables.borrow_mut();
393        let cx = &*self.cx.borrow();
394        let def_id = item.internal(&mut *tables, cx.tcx);
395        cx.intrinsic(def_id).map(|_| IntrinsicDef(item))
396    }
397
398    /// Retrieve the plain function name of an intrinsic.
399    pub(crate) fn intrinsic_name(&self, def: IntrinsicDef) -> Symbol {
400        let mut tables = self.tables.borrow_mut();
401        let cx = &*self.cx.borrow();
402        let def_id = def.0.internal(&mut *tables, cx.tcx);
403        cx.intrinsic_name(def_id)
404    }
405
406    /// Retrieve the closure signature for the given generic arguments.
407    pub(crate) fn closure_sig(&self, args: &GenericArgs) -> PolyFnSig {
408        let mut tables = self.tables.borrow_mut();
409        let cx = &*self.cx.borrow();
410        let args_ref = args.internal(&mut *tables, cx.tcx);
411        cx.closure_sig(args_ref).stable(&mut *tables, cx)
412    }
413
414    /// The number of variants in this ADT.
415    pub(crate) fn adt_variants_len(&self, def: AdtDef) -> usize {
416        let mut tables = self.tables.borrow_mut();
417        let cx = &*self.cx.borrow();
418        cx.adt_variants_len(def.internal(&mut *tables, cx.tcx))
419    }
420
421    /// Discriminant for a given variant index of AdtDef.
422    pub(crate) fn adt_discr_for_variant(&self, adt: AdtDef, variant: VariantIdx) -> Discr {
423        let mut tables = self.tables.borrow_mut();
424        let cx = &*self.cx.borrow();
425        cx.adt_discr_for_variant(
426            adt.internal(&mut *tables, cx.tcx),
427            variant.internal(&mut *tables, cx.tcx),
428        )
429        .stable(&mut *tables, cx)
430    }
431
432    /// Discriminant for a given variand index and args of a coroutine.
433    pub(crate) fn coroutine_discr_for_variant(
434        &self,
435        coroutine: CoroutineDef,
436        args: &GenericArgs,
437        variant: VariantIdx,
438    ) -> Discr {
439        let mut tables = self.tables.borrow_mut();
440        let cx = &*self.cx.borrow();
441        let tcx = cx.tcx;
442        let def = coroutine.def_id().internal(&mut *tables, tcx);
443        let args_ref = args.internal(&mut *tables, tcx);
444        cx.coroutine_discr_for_variant(def, args_ref, variant.internal(&mut *tables, tcx))
445            .stable(&mut *tables, cx)
446    }
447
448    /// The name of a variant.
449    pub(crate) fn variant_name(&self, def: VariantDef) -> Symbol {
450        let mut tables = self.tables.borrow_mut();
451        let cx = &*self.cx.borrow();
452        cx.variant_name(def.internal(&mut *tables, cx.tcx))
453    }
454
455    pub(crate) fn variant_fields(&self, def: VariantDef) -> Vec<FieldDef> {
456        let mut tables = self.tables.borrow_mut();
457        let cx = &*self.cx.borrow();
458        def.internal(&mut *tables, cx.tcx)
459            .fields
460            .iter()
461            .map(|f| f.stable(&mut *tables, cx))
462            .collect()
463    }
464
465    /// Evaluate constant as a target usize.
466    pub(crate) fn eval_target_usize(&self, mir_const: &MirConst) -> Result<u64, Error> {
467        let mut tables = self.tables.borrow_mut();
468        let cx = &*self.cx.borrow();
469        let cnst = mir_const.internal(&mut *tables, cx.tcx);
470        cx.eval_target_usize(cnst)
471    }
472
473    pub(crate) fn eval_target_usize_ty(&self, ty_const: &TyConst) -> Result<u64, Error> {
474        let mut tables = self.tables.borrow_mut();
475        let cx = &*self.cx.borrow();
476        let cnst = ty_const.internal(&mut *tables, cx.tcx);
477        cx.eval_target_usize_ty(cnst)
478    }
479
480    /// Create a new zero-sized constant.
481    pub(crate) fn try_new_const_zst(&self, ty: Ty) -> Result<MirConst, Error> {
482        let mut tables = self.tables.borrow_mut();
483        let cx = &*self.cx.borrow();
484        let ty_internal = ty.internal(&mut *tables, cx.tcx);
485        cx.try_new_const_zst(ty_internal).map(|cnst| cnst.stable(&mut *tables, cx))
486    }
487
488    /// Create a new constant that represents the given string value.
489    pub(crate) fn new_const_str(&self, value: &str) -> MirConst {
490        let mut tables = self.tables.borrow_mut();
491        let cx = &*self.cx.borrow();
492        cx.new_const_str(value).stable(&mut *tables, cx)
493    }
494
495    /// Create a new constant that represents the given boolean value.
496    pub(crate) fn new_const_bool(&self, value: bool) -> MirConst {
497        let mut tables = self.tables.borrow_mut();
498        let cx = &*self.cx.borrow();
499        cx.new_const_bool(value).stable(&mut *tables, cx)
500    }
501
502    /// Create a new constant that represents the given value.
503    pub(crate) fn try_new_const_uint(
504        &self,
505        value: u128,
506        uint_ty: UintTy,
507    ) -> Result<MirConst, Error> {
508        let mut tables = self.tables.borrow_mut();
509        let cx = &*self.cx.borrow();
510        let ty = cx.ty_new_uint(uint_ty.internal(&mut *tables, cx.tcx));
511        cx.try_new_const_uint(value, ty).map(|cnst| cnst.stable(&mut *tables, cx))
512    }
513
514    pub(crate) fn try_new_ty_const_uint(
515        &self,
516        value: u128,
517        uint_ty: UintTy,
518    ) -> Result<TyConst, Error> {
519        let mut tables = self.tables.borrow_mut();
520        let cx = &*self.cx.borrow();
521        let ty = cx.ty_new_uint(uint_ty.internal(&mut *tables, cx.tcx));
522        cx.try_new_ty_const_uint(value, ty).map(|cnst| cnst.stable(&mut *tables, cx))
523    }
524
525    /// Create a new type from the given kind.
526    pub(crate) fn new_rigid_ty(&self, kind: RigidTy) -> Ty {
527        let mut tables = self.tables.borrow_mut();
528        let cx = &*self.cx.borrow();
529        let internal_kind = kind.internal(&mut *tables, cx.tcx);
530        cx.new_rigid_ty(internal_kind).stable(&mut *tables, cx)
531    }
532
533    /// Create a new box type, `Box<T>`, for the given inner type `T`.
534    pub(crate) fn new_box_ty(&self, ty: Ty) -> Ty {
535        let mut tables = self.tables.borrow_mut();
536        let cx = &*self.cx.borrow();
537        let inner = ty.internal(&mut *tables, cx.tcx);
538        cx.new_box_ty(inner).stable(&mut *tables, cx)
539    }
540
541    /// Returns the type of given crate item.
542    pub(crate) fn def_ty(&self, item: DefId) -> Ty {
543        let mut tables = self.tables.borrow_mut();
544        let cx = &*self.cx.borrow();
545        let inner = item.internal(&mut *tables, cx.tcx);
546        cx.def_ty(inner).stable(&mut *tables, cx)
547    }
548
549    /// Returns the type of given definition instantiated with the given arguments.
550    pub(crate) fn def_ty_with_args(&self, item: DefId, args: &GenericArgs) -> Ty {
551        let mut tables = self.tables.borrow_mut();
552        let cx = &*self.cx.borrow();
553        let inner = item.internal(&mut *tables, cx.tcx);
554        let args_ref = args.internal(&mut *tables, cx.tcx);
555        cx.def_ty_with_args(inner, args_ref).stable(&mut *tables, cx)
556    }
557
558    /// Returns literal value of a const as a string.
559    pub(crate) fn mir_const_pretty(&self, cnst: &MirConst) -> String {
560        let mut tables = self.tables.borrow_mut();
561        let cx = &*self.cx.borrow();
562        cnst.internal(&mut *tables, cx.tcx).to_string()
563    }
564
565    /// `Span` of an item.
566    pub(crate) fn span_of_an_item(&self, def_id: DefId) -> Span {
567        let mut tables = self.tables.borrow_mut();
568        let cx = &*self.cx.borrow();
569        let did = tables[def_id];
570        cx.span_of_an_item(did).stable(&mut *tables, cx)
571    }
572
573    pub(crate) fn ty_const_pretty(&self, ct: TyConstId) -> String {
574        let tables = self.tables.borrow_mut();
575        let cx = &*self.cx.borrow();
576        cx.ty_const_pretty(tables.ty_consts[ct])
577    }
578
579    /// Obtain the representation of a type.
580    pub(crate) fn ty_pretty(&self, ty: Ty) -> String {
581        let tables = self.tables.borrow_mut();
582        let cx = &*self.cx.borrow();
583        cx.ty_pretty(tables.types[ty])
584    }
585
586    /// Obtain the kind of a type.
587    pub(crate) fn ty_kind(&self, ty: Ty) -> TyKind {
588        let mut tables = self.tables.borrow_mut();
589        let cx = &*self.cx.borrow();
590        cx.ty_kind(tables.types[ty]).stable(&mut *tables, cx)
591    }
592
593    /// Get the discriminant Ty for this Ty if there's one.
594    pub(crate) fn rigid_ty_discriminant_ty(&self, ty: &RigidTy) -> Ty {
595        let mut tables = self.tables.borrow_mut();
596        let cx = &*self.cx.borrow();
597        let internal_kind = ty.internal(&mut *tables, cx.tcx);
598        cx.rigid_ty_discriminant_ty(internal_kind).stable(&mut *tables, cx)
599    }
600
601    /// Get the body of an Instance which is already monomorphized.
602    pub(crate) fn instance_body(&self, instance: InstanceDef) -> Option<Body> {
603        let mut tables = self.tables.borrow_mut();
604        let cx = &*self.cx.borrow();
605        let instance = tables.instances[instance];
606        cx.instance_body(instance).map(|body| body.stable(&mut *tables, cx))
607    }
608
609    /// Get the instance type with generic instantiations applied and lifetimes erased.
610    pub(crate) fn instance_ty(&self, instance: InstanceDef) -> Ty {
611        let mut tables = self.tables.borrow_mut();
612        let cx = &*self.cx.borrow();
613        let instance = tables.instances[instance];
614        cx.instance_ty(instance).stable(&mut *tables, cx)
615    }
616
617    /// Get the instantiation types.
618    pub(crate) fn instance_args(&self, def: InstanceDef) -> GenericArgs {
619        let mut tables = self.tables.borrow_mut();
620        let cx = &*self.cx.borrow();
621        let instance = tables.instances[def];
622        cx.instance_args(instance).stable(&mut *tables, cx)
623    }
624
625    /// Get the instance.
626    pub(crate) fn instance_def_id(&self, instance: InstanceDef) -> DefId {
627        let mut tables = self.tables.borrow_mut();
628        let cx = &*self.cx.borrow();
629        let instance = tables.instances[instance];
630        cx.instance_def_id(instance, &mut *tables)
631    }
632
633    /// Get the instance mangled name.
634    pub(crate) fn instance_mangled_name(&self, instance: InstanceDef) -> Symbol {
635        let tables = self.tables.borrow_mut();
636        let cx = &*self.cx.borrow();
637        let instance = tables.instances[instance];
638        cx.instance_mangled_name(instance)
639    }
640
641    /// Check if this is an empty DropGlue shim.
642    pub(crate) fn is_empty_drop_shim(&self, def: InstanceDef) -> bool {
643        let tables = self.tables.borrow_mut();
644        let cx = &*self.cx.borrow();
645        let instance = tables.instances[def];
646        cx.is_empty_drop_shim(instance)
647    }
648
649    /// Convert a non-generic crate item into an instance.
650    /// This function will panic if the item is generic.
651    pub(crate) fn mono_instance(&self, def_id: DefId) -> Instance {
652        let mut tables = self.tables.borrow_mut();
653        let cx = &*self.cx.borrow();
654        let did = tables[def_id];
655        cx.mono_instance(did).stable(&mut *tables, cx)
656    }
657
658    /// Item requires monomorphization.
659    pub(crate) fn requires_monomorphization(&self, def_id: DefId) -> bool {
660        let tables = self.tables.borrow();
661        let cx = &*self.cx.borrow();
662        let did = tables[def_id];
663        cx.requires_monomorphization(did)
664    }
665
666    /// Resolve an instance from the given function definition and generic arguments.
667    pub(crate) fn resolve_instance(&self, def: FnDef, args: &GenericArgs) -> Option<Instance> {
668        let mut tables = self.tables.borrow_mut();
669        let cx = &*self.cx.borrow();
670        let def_id = def.0.internal(&mut *tables, cx.tcx);
671        let args_ref = args.internal(&mut *tables, cx.tcx);
672        cx.resolve_instance(def_id, args_ref).map(|inst| inst.stable(&mut *tables, cx))
673    }
674
675    /// Resolve an instance for drop_in_place for the given type.
676    pub(crate) fn resolve_drop_in_place(&self, ty: Ty) -> Instance {
677        let mut tables = self.tables.borrow_mut();
678        let cx = &*self.cx.borrow();
679        let internal_ty = ty.internal(&mut *tables, cx.tcx);
680
681        cx.resolve_drop_in_place(internal_ty).stable(&mut *tables, cx)
682    }
683
684    /// Resolve instance for a function pointer.
685    pub(crate) fn resolve_for_fn_ptr(&self, def: FnDef, args: &GenericArgs) -> Option<Instance> {
686        let mut tables = self.tables.borrow_mut();
687        let cx = &*self.cx.borrow();
688        let def_id = def.0.internal(&mut *tables, cx.tcx);
689        let args_ref = args.internal(&mut *tables, cx.tcx);
690        cx.resolve_for_fn_ptr(def_id, args_ref).stable(&mut *tables, cx)
691    }
692
693    /// Resolve instance for a closure with the requested type.
694    pub(crate) fn resolve_closure(
695        &self,
696        def: ClosureDef,
697        args: &GenericArgs,
698        kind: ClosureKind,
699    ) -> Option<Instance> {
700        let mut tables = self.tables.borrow_mut();
701        let cx = &*self.cx.borrow();
702        let def_id = def.0.internal(&mut *tables, cx.tcx);
703        let args_ref = args.internal(&mut *tables, cx.tcx);
704        let closure_kind = kind.internal(&mut *tables, cx.tcx);
705        cx.resolve_closure(def_id, args_ref, closure_kind).map(|inst| inst.stable(&mut *tables, cx))
706    }
707
708    /// Evaluate a static's initializer.
709    pub(crate) fn eval_static_initializer(&self, def: StaticDef) -> Result<Allocation, Error> {
710        let mut tables = self.tables.borrow_mut();
711        let cx = &*self.cx.borrow();
712        let def_id = def.0.internal(&mut *tables, cx.tcx);
713
714        cx.eval_static_initializer(def_id).stable(&mut *tables, cx)
715    }
716
717    /// Try to evaluate an instance into a constant.
718    pub(crate) fn eval_instance(
719        &self,
720        def: InstanceDef,
721        const_ty: Ty,
722    ) -> Result<Allocation, Error> {
723        let mut tables = self.tables.borrow_mut();
724        let instance = tables.instances[def];
725        let cx = &*self.cx.borrow();
726        let const_ty = const_ty.internal(&mut *tables, cx.tcx);
727        cx.eval_instance(instance)
728            .map(|const_val| alloc::try_new_allocation(const_ty, const_val, &mut *tables, cx))
729            .map_err(|e| e.stable(&mut *tables, cx))?
730    }
731
732    /// Retrieve global allocation for the given allocation ID.
733    pub(crate) fn global_alloc(&self, id: AllocId) -> GlobalAlloc {
734        let mut tables = self.tables.borrow_mut();
735        let cx = &*self.cx.borrow();
736        let alloc_id = id.internal(&mut *tables, cx.tcx);
737        cx.global_alloc(alloc_id).stable(&mut *tables, cx)
738    }
739
740    /// Retrieve the id for the virtual table.
741    pub(crate) fn vtable_allocation(&self, global_alloc: &GlobalAlloc) -> Option<AllocId> {
742        let mut tables = self.tables.borrow_mut();
743        let GlobalAlloc::VTable(ty, trait_ref) = global_alloc else {
744            return None;
745        };
746        let cx = &*self.cx.borrow();
747        let ty = ty.internal(&mut *tables, cx.tcx);
748        let trait_ref = trait_ref.internal(&mut *tables, cx.tcx);
749        let alloc_id = cx.vtable_allocation(ty, trait_ref);
750        Some(alloc_id.stable(&mut *tables, cx))
751    }
752
753    pub(crate) fn krate(&self, def_id: DefId) -> Crate {
754        let tables = self.tables.borrow();
755        let cx = &*self.cx.borrow();
756        smir_crate(cx, tables[def_id].krate)
757    }
758
759    pub(crate) fn instance_name(&self, def: InstanceDef, trimmed: bool) -> Symbol {
760        let tables = self.tables.borrow_mut();
761        let cx = &*self.cx.borrow();
762        let instance = tables.instances[def];
763        cx.instance_name(instance, trimmed)
764    }
765
766    /// Return information about the target machine.
767    pub(crate) fn target_info(&self) -> MachineInfo {
768        let mut tables = self.tables.borrow_mut();
769        let cx = &*self.cx.borrow();
770        MachineInfo {
771            endian: cx.target_endian().stable(&mut *tables, cx),
772            pointer_width: MachineSize::from_bits(cx.target_pointer_size()),
773        }
774    }
775
776    /// Get an instance ABI.
777    pub(crate) fn instance_abi(&self, def: InstanceDef) -> Result<FnAbi, Error> {
778        let mut tables = self.tables.borrow_mut();
779        let cx = &*self.cx.borrow();
780        let instance = tables.instances[def];
781        cx.instance_abi(instance).map(|fn_abi| fn_abi.stable(&mut *tables, cx))
782    }
783
784    /// Get the ABI of a function pointer.
785    pub(crate) fn fn_ptr_abi(&self, fn_ptr: PolyFnSig) -> Result<FnAbi, Error> {
786        let mut tables = self.tables.borrow_mut();
787        let cx = &*self.cx.borrow();
788        let sig = fn_ptr.internal(&mut *tables, cx.tcx);
789        cx.fn_ptr_abi(sig).map(|fn_abi| fn_abi.stable(&mut *tables, cx))
790    }
791
792    /// Get the layout of a type.
793    pub(crate) fn ty_layout(&self, ty: Ty) -> Result<Layout, Error> {
794        let mut tables = self.tables.borrow_mut();
795        let cx = &*self.cx.borrow();
796        let internal_ty = ty.internal(&mut *tables, cx.tcx);
797        cx.ty_layout(internal_ty).map(|layout| layout.stable(&mut *tables, cx))
798    }
799
800    /// Get the layout shape.
801    pub(crate) fn layout_shape(&self, id: Layout) -> LayoutShape {
802        let mut tables = self.tables.borrow_mut();
803        let cx = &*self.cx.borrow();
804        id.internal(&mut *tables, cx.tcx).0.stable(&mut *tables, cx)
805    }
806
807    /// Get a debug string representation of a place.
808    pub(crate) fn place_pretty(&self, place: &Place) -> String {
809        let mut tables = self.tables.borrow_mut();
810        let cx = &*self.cx.borrow();
811
812        format!("{:?}", place.internal(&mut *tables, cx.tcx))
813    }
814
815    /// Get the resulting type of binary operation.
816    pub(crate) fn binop_ty(&self, bin_op: BinOp, rhs: Ty, lhs: Ty) -> Ty {
817        let mut tables = self.tables.borrow_mut();
818        let cx = &*self.cx.borrow();
819        let rhs_internal = rhs.internal(&mut *tables, cx.tcx);
820        let lhs_internal = lhs.internal(&mut *tables, cx.tcx);
821        let bin_op_internal = bin_op.internal(&mut *tables, cx.tcx);
822        cx.binop_ty(bin_op_internal, rhs_internal, lhs_internal).stable(&mut *tables, cx)
823    }
824
825    /// Get the resulting type of unary operation.
826    pub(crate) fn unop_ty(&self, un_op: UnOp, arg: Ty) -> Ty {
827        let mut tables = self.tables.borrow_mut();
828        let cx = &*self.cx.borrow();
829        let un_op = un_op.internal(&mut *tables, cx.tcx);
830        let arg = arg.internal(&mut *tables, cx.tcx);
831        cx.unop_ty(un_op, arg).stable(&mut *tables, cx)
832    }
833
834    /// Get all associated items of a definition.
835    pub(crate) fn associated_items(&self, def_id: DefId) -> AssocItems {
836        let mut tables = self.tables.borrow_mut();
837        let cx = &*self.cx.borrow();
838        let did = tables[def_id];
839        cx.associated_items(did).iter().map(|assoc| assoc.stable(&mut *tables, cx)).collect()
840    }
841}
842
843// A thread local variable that stores a pointer to [`CompilerInterface`].
844scoped_tls::scoped_thread_local!(static TLV: Cell<*const ()>);
845
846// remove this cfg when we have a stable driver.
847#[cfg(feature = "rustc_internal")]
848pub(crate) fn run<'tcx, F, T>(interface: &CompilerInterface<'tcx>, f: F) -> Result<T, Error>
849where
850    F: FnOnce() -> T,
851{
852    if TLV.is_set() {
853        Err(Error::from("rustc_public already running"))
854    } else {
855        let ptr: *const () = (&raw const interface) as _;
856        TLV.set(&Cell::new(ptr), || Ok(f()))
857    }
858}
859
860/// Execute the given function with access the [`CompilerInterface`].
861///
862/// I.e., This function will load the current interface and calls a function with it.
863/// Do not nest these, as that will ICE.
864pub(crate) fn with<R>(f: impl for<'tcx> FnOnce(&CompilerInterface<'tcx>) -> R) -> R {
865    assert!(TLV.is_set());
866    TLV.with(|tlv| {
867        let ptr = tlv.get();
868        assert!(!ptr.is_null());
869        f(unsafe { *(ptr as *const &CompilerInterface<'_>) })
870    })
871}
872
873fn smir_crate<'tcx>(
874    cx: &CompilerCtxt<'tcx, BridgeTys>,
875    crate_num: rustc_span::def_id::CrateNum,
876) -> Crate {
877    let name = cx.crate_name(crate_num);
878    let is_local = cx.crate_is_local(crate_num);
879    let id = CrateNum(cx.crate_num_id(crate_num), ThreadLocalIndex);
880    debug!(?name, ?crate_num, "smir_crate");
881    Crate { id, name, is_local }
882}