1use 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, TraitRef, Ty, TyConst, TyConstId, TyKind, UintTy, VariantDef, VariantIdx,
24 VtblEntry,
25};
26use crate::unstable::{RustcInternal, Stable, new_item_kind};
27use crate::{
28 AssocItems, Crate, CrateDef, CrateItem, CrateItems, CrateNum, DefId, Error, Filename,
29 ImplTraitDecls, ItemKind, Symbol, ThreadLocalIndex, TraitDecls, alloc, mir,
30};
31
32pub struct BridgeTys;
33
34impl Bridge for BridgeTys {
35 type DefId = crate::DefId;
36 type AllocId = crate::mir::alloc::AllocId;
37 type Span = crate::ty::Span;
38 type Ty = crate::ty::Ty;
39 type InstanceDef = crate::mir::mono::InstanceDef;
40 type TyConstId = crate::ty::TyConstId;
41 type MirConstId = crate::ty::MirConstId;
42 type Layout = crate::abi::Layout;
43
44 type Error = crate::Error;
45 type CrateItem = crate::CrateItem;
46 type AdtDef = crate::ty::AdtDef;
47 type ForeignModuleDef = crate::ty::ForeignModuleDef;
48 type ForeignDef = crate::ty::ForeignDef;
49 type FnDef = crate::ty::FnDef;
50 type ClosureDef = crate::ty::ClosureDef;
51 type CoroutineDef = crate::ty::CoroutineDef;
52 type CoroutineClosureDef = crate::ty::CoroutineClosureDef;
53 type AliasDef = crate::ty::AliasDef;
54 type ParamDef = crate::ty::ParamDef;
55 type BrNamedDef = crate::ty::BrNamedDef;
56 type TraitDef = crate::ty::TraitDef;
57 type GenericDef = crate::ty::GenericDef;
58 type ConstDef = crate::ty::ConstDef;
59 type ImplDef = crate::ty::ImplDef;
60 type RegionDef = crate::ty::RegionDef;
61 type CoroutineWitnessDef = crate::ty::CoroutineWitnessDef;
62 type AssocDef = crate::ty::AssocDef;
63 type OpaqueDef = crate::ty::OpaqueDef;
64 type Prov = crate::ty::Prov;
65 type StaticDef = crate::mir::mono::StaticDef;
66
67 type Allocation = crate::ty::Allocation;
68}
69
70pub(crate) struct CompilerInterface<'tcx> {
77 pub tables: RefCell<Tables<'tcx, BridgeTys>>,
78 pub cx: RefCell<CompilerCtxt<'tcx, BridgeTys>>,
79}
80
81impl<'tcx> CompilerInterface<'tcx> {
82 pub(crate) fn entry_fn(&self) -> Option<CrateItem> {
83 let mut tables = self.tables.borrow_mut();
84 let cx = &*self.cx.borrow();
85 let did = cx.entry_fn();
86 Some(tables.crate_item(did?))
87 }
88
89 pub(crate) fn all_local_items(&self) -> CrateItems {
91 let mut tables = self.tables.borrow_mut();
92 let cx = &*self.cx.borrow();
93 cx.all_local_items().iter().map(|did| tables.crate_item(*did)).collect()
94 }
95
96 pub(crate) fn mir_body(&self, item: DefId) -> mir::Body {
99 let mut tables = self.tables.borrow_mut();
100 let cx = &*self.cx.borrow();
101 let did = tables[item];
102 cx.mir_body(did).stable(&mut *tables, cx)
103 }
104
105 pub(crate) fn has_body(&self, item: DefId) -> bool {
107 let mut tables = self.tables.borrow_mut();
108 let cx = &*self.cx.borrow();
109 let def = item.internal(&mut *tables, cx.tcx);
110 cx.has_body(def)
111 }
112
113 pub(crate) fn foreign_modules(&self, crate_num: CrateNum) -> Vec<ForeignModuleDef> {
114 let mut tables = self.tables.borrow_mut();
115 let cx = &*self.cx.borrow();
116 cx.foreign_modules(crate_num.internal(&mut *tables, cx.tcx))
117 .iter()
118 .map(|did| tables.foreign_module_def(*did))
119 .collect()
120 }
121
122 pub(crate) fn crate_functions(&self, crate_num: CrateNum) -> Vec<FnDef> {
124 let mut tables = self.tables.borrow_mut();
125 let cx = &*self.cx.borrow();
126 let krate = crate_num.internal(&mut *tables, cx.tcx);
127 cx.crate_functions(krate).iter().map(|did| tables.fn_def(*did)).collect()
128 }
129
130 pub(crate) fn crate_statics(&self, crate_num: CrateNum) -> Vec<StaticDef> {
132 let mut tables = self.tables.borrow_mut();
133 let cx = &*self.cx.borrow();
134 let krate = crate_num.internal(&mut *tables, cx.tcx);
135 cx.crate_statics(krate).iter().map(|did| tables.static_def(*did)).collect()
136 }
137
138 pub(crate) fn foreign_module(&self, mod_def: ForeignModuleDef) -> ForeignModule {
139 let mut tables = self.tables.borrow_mut();
140 let cx = &*self.cx.borrow();
141 let did = tables[mod_def.def_id()];
142 cx.foreign_module(did).stable(&mut *tables, cx)
143 }
144
145 pub(crate) fn foreign_items(&self, mod_def: ForeignModuleDef) -> Vec<ForeignDef> {
146 let mut tables = self.tables.borrow_mut();
147 let cx = &*self.cx.borrow();
148 let did = tables[mod_def.def_id()];
149 cx.foreign_items(did).iter().map(|did| tables.foreign_def(*did)).collect()
150 }
151
152 pub(crate) fn all_trait_decls(&self) -> TraitDecls {
153 let mut tables = self.tables.borrow_mut();
154 let cx = &*self.cx.borrow();
155 cx.all_trait_decls().map(|did| tables.trait_def(did)).collect()
156 }
157
158 pub(crate) fn trait_decls(&self, crate_num: CrateNum) -> TraitDecls {
159 let mut tables = self.tables.borrow_mut();
160 let cx = &*self.cx.borrow();
161 let krate = crate_num.internal(&mut *tables, cx.tcx);
162 cx.trait_decls(krate).iter().map(|did| tables.trait_def(*did)).collect()
163 }
164
165 pub(crate) fn trait_decl(&self, trait_def: &TraitDef) -> TraitDecl {
166 let mut tables = self.tables.borrow_mut();
167 let cx = &*self.cx.borrow();
168 let did = tables[trait_def.0];
169 cx.trait_decl(did).stable(&mut *tables, cx)
170 }
171
172 pub(crate) fn all_trait_impls(&self) -> ImplTraitDecls {
173 let mut tables = self.tables.borrow_mut();
174 let cx = &*self.cx.borrow();
175 cx.all_trait_impls().iter().map(|did| tables.impl_def(*did)).collect()
176 }
177
178 pub(crate) fn trait_impls(&self, crate_num: CrateNum) -> ImplTraitDecls {
179 let mut tables = self.tables.borrow_mut();
180 let cx = &*self.cx.borrow();
181 let krate = crate_num.internal(&mut *tables, cx.tcx);
182 cx.trait_impls(krate).iter().map(|did| tables.impl_def(*did)).collect()
183 }
184
185 pub(crate) fn trait_impl(&self, trait_impl: &ImplDef) -> ImplTrait {
186 let mut tables = self.tables.borrow_mut();
187 let cx = &*self.cx.borrow();
188 let did = tables[trait_impl.0];
189 cx.trait_impl(did).stable(&mut *tables, cx)
190 }
191
192 pub(crate) fn generics_of(&self, def_id: DefId) -> Generics {
193 let mut tables = self.tables.borrow_mut();
194 let cx = &*self.cx.borrow();
195 let did = tables[def_id];
196 cx.generics_of(did).stable(&mut *tables, cx)
197 }
198
199 pub(crate) fn predicates_of(&self, def_id: DefId) -> GenericPredicates {
200 let mut tables = self.tables.borrow_mut();
201 let cx = &*self.cx.borrow();
202 let did = tables[def_id];
203 let (parent, kinds) = cx.predicates_of(did);
204 crate::ty::GenericPredicates {
205 parent: parent.map(|did| tables.trait_def(did)),
206 predicates: kinds
207 .iter()
208 .map(|(kind, span)| (kind.stable(&mut *tables, cx), span.stable(&mut *tables, cx)))
209 .collect(),
210 }
211 }
212
213 pub(crate) fn explicit_predicates_of(&self, def_id: DefId) -> GenericPredicates {
214 let mut tables = self.tables.borrow_mut();
215 let cx = &*self.cx.borrow();
216 let did = tables[def_id];
217 let (parent, kinds) = cx.explicit_predicates_of(did);
218 crate::ty::GenericPredicates {
219 parent: parent.map(|did| tables.trait_def(did)),
220 predicates: kinds
221 .iter()
222 .map(|(kind, span)| (kind.stable(&mut *tables, cx), span.stable(&mut *tables, cx)))
223 .collect(),
224 }
225 }
226
227 pub(crate) fn local_crate(&self) -> Crate {
229 let cx = &*self.cx.borrow();
230 smir_crate(cx, cx.local_crate_num())
231 }
232
233 pub(crate) fn external_crates(&self) -> Vec<Crate> {
235 let cx = &*self.cx.borrow();
236 cx.external_crates().iter().map(|crate_num| smir_crate(cx, *crate_num)).collect()
237 }
238
239 pub(crate) fn find_crates(&self, name: &str) -> Vec<Crate> {
241 let cx = &*self.cx.borrow();
242 cx.find_crates(name).iter().map(|crate_num| smir_crate(cx, *crate_num)).collect()
243 }
244
245 pub(crate) fn def_name(&self, def_id: DefId, trimmed: bool) -> Symbol {
247 let tables = self.tables.borrow();
248 let cx = &*self.cx.borrow();
249 let did = tables[def_id];
250 cx.def_name(did, trimmed)
251 }
252
253 pub(crate) fn def_parent(&self, def_id: DefId) -> Option<DefId> {
255 let mut tables = self.tables.borrow_mut();
256 let cx = &*self.cx.borrow();
257 let did = tables[def_id];
258 cx.def_parent(did).map(|did| tables.create_def_id(did))
259 }
260
261 pub(crate) fn tool_attrs(&self, def_id: DefId, attr: &[Symbol]) -> Vec<Attribute> {
269 let mut tables = self.tables.borrow_mut();
270 let cx = &*self.cx.borrow();
271 let did = tables[def_id];
272 cx.tool_attrs(did, attr)
273 .into_iter()
274 .map(|(attr_str, span)| Attribute::new(attr_str, span.stable(&mut *tables, cx)))
275 .collect()
276 }
277
278 pub(crate) fn all_tool_attrs(&self, def_id: DefId) -> Vec<Attribute> {
280 let mut tables = self.tables.borrow_mut();
281 let cx = &*self.cx.borrow();
282 let did = tables[def_id];
283 cx.all_tool_attrs(did)
284 .into_iter()
285 .map(|(attr_str, span)| Attribute::new(attr_str, span.stable(&mut *tables, cx)))
286 .collect()
287 }
288
289 pub(crate) fn span_to_string(&self, span: Span) -> String {
291 let tables = self.tables.borrow_mut();
292 let cx = &*self.cx.borrow();
293 let sp = tables.spans[span];
294 cx.span_to_string(sp)
295 }
296
297 pub(crate) fn get_filename(&self, span: &Span) -> Filename {
299 let tables = self.tables.borrow_mut();
300 let cx = &*self.cx.borrow();
301 let sp = tables.spans[*span];
302 cx.get_filename(sp)
303 }
304
305 pub(crate) fn get_lines(&self, span: &Span) -> LineInfo {
307 let tables = self.tables.borrow_mut();
308 let cx = &*self.cx.borrow();
309 let sp = tables.spans[*span];
310 let lines = cx.get_lines(sp);
311 LineInfo::from(lines)
312 }
313
314 pub(crate) fn item_kind(&self, item: CrateItem) -> ItemKind {
316 let tables = self.tables.borrow();
317 let cx = &*self.cx.borrow();
318 let did = tables[item.0];
319 new_item_kind(cx.def_kind(did))
320 }
321
322 pub(crate) fn is_foreign_item(&self, item: DefId) -> bool {
324 let tables = self.tables.borrow();
325 let cx = &*self.cx.borrow();
326 let did = tables[item];
327 cx.is_foreign_item(did)
328 }
329
330 pub(crate) fn foreign_item_kind(&self, def: ForeignDef) -> ForeignItemKind {
332 let mut tables = self.tables.borrow_mut();
333 let cx = &*self.cx.borrow();
334 let def_id = tables[def.def_id()];
335 let def_kind = cx.foreign_item_kind(def_id);
336 match def_kind {
337 DefKind::Fn => ForeignItemKind::Fn(tables.fn_def(def_id)),
338 DefKind::Static { .. } => ForeignItemKind::Static(tables.static_def(def_id)),
339 DefKind::ForeignTy => {
340 use rustc_public_bridge::context::TyHelpers;
341 ForeignItemKind::Type(tables.intern_ty(cx.new_foreign(def_id)))
342 }
343 def_kind => {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("Unexpected kind for a foreign item: {0:?}",
def_kind)));
}unreachable!("Unexpected kind for a foreign item: {:?}", def_kind),
344 }
345 }
346
347 pub(crate) fn adt_kind(&self, def: AdtDef) -> AdtKind {
349 let mut tables = self.tables.borrow_mut();
350 let cx = &*self.cx.borrow();
351 cx.adt_kind(def.internal(&mut *tables, cx.tcx)).stable(&mut *tables, cx)
352 }
353
354 pub(crate) fn adt_is_box(&self, def: AdtDef) -> bool {
356 let mut tables = self.tables.borrow_mut();
357 let cx = &*self.cx.borrow();
358 cx.adt_is_box(def.internal(&mut *tables, cx.tcx))
359 }
360
361 pub(crate) fn adt_is_simd(&self, def: AdtDef) -> bool {
363 let mut tables = self.tables.borrow_mut();
364 let cx = &*self.cx.borrow();
365 cx.adt_is_simd(def.internal(&mut *tables, cx.tcx))
366 }
367
368 pub(crate) fn adt_is_cstr(&self, def: AdtDef) -> bool {
370 let mut tables = self.tables.borrow_mut();
371 let cx = &*self.cx.borrow();
372 cx.adt_is_cstr(def.0.internal(&mut *tables, cx.tcx))
373 }
374
375 pub(crate) fn adt_repr(&self, def: AdtDef) -> ReprOptions {
377 let mut tables = self.tables.borrow_mut();
378 let cx = &*self.cx.borrow();
379 cx.adt_repr(def.internal(&mut *tables, cx.tcx)).stable(&mut *tables, cx)
380 }
381
382 pub(crate) fn fn_sig(&self, def: FnDef, args: &GenericArgs) -> PolyFnSig {
384 let mut tables = self.tables.borrow_mut();
385 let cx = &*self.cx.borrow();
386 let def_id = def.0.internal(&mut *tables, cx.tcx);
387 let args_ref = args.internal(&mut *tables, cx.tcx);
388 cx.fn_sig(def_id, args_ref).stable(&mut *tables, cx)
389 }
390
391 pub(crate) fn intrinsic(&self, item: DefId) -> Option<IntrinsicDef> {
393 let mut tables = self.tables.borrow_mut();
394 let cx = &*self.cx.borrow();
395 let def_id = item.internal(&mut *tables, cx.tcx);
396 cx.intrinsic(def_id).map(|_| IntrinsicDef(item))
397 }
398
399 pub(crate) fn intrinsic_name(&self, def: IntrinsicDef) -> Symbol {
401 let mut tables = self.tables.borrow_mut();
402 let cx = &*self.cx.borrow();
403 let def_id = def.0.internal(&mut *tables, cx.tcx);
404 cx.intrinsic_name(def_id)
405 }
406
407 pub(crate) fn closure_sig(&self, args: &GenericArgs) -> PolyFnSig {
409 let mut tables = self.tables.borrow_mut();
410 let cx = &*self.cx.borrow();
411 let args_ref = args.internal(&mut *tables, cx.tcx);
412 cx.closure_sig(args_ref).stable(&mut *tables, cx)
413 }
414
415 pub(crate) fn adt_variants_len(&self, def: AdtDef) -> usize {
417 let mut tables = self.tables.borrow_mut();
418 let cx = &*self.cx.borrow();
419 cx.adt_variants_len(def.internal(&mut *tables, cx.tcx))
420 }
421
422 pub(crate) fn adt_discr_for_variant(&self, adt: AdtDef, variant: VariantIdx) -> Discr {
424 let mut tables = self.tables.borrow_mut();
425 let cx = &*self.cx.borrow();
426 cx.adt_discr_for_variant(
427 adt.internal(&mut *tables, cx.tcx),
428 variant.internal(&mut *tables, cx.tcx),
429 )
430 .stable(&mut *tables, cx)
431 }
432
433 pub(crate) fn coroutine_discr_for_variant(
435 &self,
436 coroutine: CoroutineDef,
437 args: &GenericArgs,
438 variant: VariantIdx,
439 ) -> Discr {
440 let mut tables = self.tables.borrow_mut();
441 let cx = &*self.cx.borrow();
442 let tcx = cx.tcx;
443 let def = coroutine.def_id().internal(&mut *tables, tcx);
444 let args_ref = args.internal(&mut *tables, tcx);
445 cx.coroutine_discr_for_variant(def, args_ref, variant.internal(&mut *tables, tcx))
446 .stable(&mut *tables, cx)
447 }
448
449 pub(crate) fn variant_name(&self, def: VariantDef) -> Symbol {
451 let mut tables = self.tables.borrow_mut();
452 let cx = &*self.cx.borrow();
453 cx.variant_name(def.internal(&mut *tables, cx.tcx))
454 }
455
456 pub(crate) fn variant_fields(&self, def: VariantDef) -> Vec<FieldDef> {
457 let mut tables = self.tables.borrow_mut();
458 let cx = &*self.cx.borrow();
459 def.internal(&mut *tables, cx.tcx)
460 .fields
461 .iter()
462 .map(|f| f.stable(&mut *tables, cx))
463 .collect()
464 }
465
466 pub(crate) fn eval_target_usize(&self, mir_const: &MirConst) -> Result<u64, Error> {
468 let mut tables = self.tables.borrow_mut();
469 let cx = &*self.cx.borrow();
470 let cnst = mir_const.internal(&mut *tables, cx.tcx);
471 cx.eval_target_usize(cnst)
472 }
473
474 pub(crate) fn eval_target_usize_ty(&self, ty_const: &TyConst) -> Result<u64, Error> {
475 let mut tables = self.tables.borrow_mut();
476 let cx = &*self.cx.borrow();
477 let cnst = ty_const.internal(&mut *tables, cx.tcx);
478 cx.eval_target_usize_ty(cnst)
479 }
480
481 pub(crate) fn try_new_const_zst(&self, ty: Ty) -> Result<MirConst, Error> {
483 let mut tables = self.tables.borrow_mut();
484 let cx = &*self.cx.borrow();
485 let ty_internal = ty.internal(&mut *tables, cx.tcx);
486 cx.try_new_const_zst(ty_internal).map(|cnst| cnst.stable(&mut *tables, cx))
487 }
488
489 pub(crate) fn new_const_str(&self, value: &str) -> MirConst {
491 let mut tables = self.tables.borrow_mut();
492 let cx = &*self.cx.borrow();
493 cx.new_const_str(value).stable(&mut *tables, cx)
494 }
495
496 pub(crate) fn new_const_bool(&self, value: bool) -> MirConst {
498 let mut tables = self.tables.borrow_mut();
499 let cx = &*self.cx.borrow();
500 cx.new_const_bool(value).stable(&mut *tables, cx)
501 }
502
503 pub(crate) fn try_new_const_uint(
505 &self,
506 value: u128,
507 uint_ty: UintTy,
508 ) -> Result<MirConst, Error> {
509 let mut tables = self.tables.borrow_mut();
510 let cx = &*self.cx.borrow();
511 let ty = cx.ty_new_uint(uint_ty.internal(&mut *tables, cx.tcx));
512 cx.try_new_const_uint(value, ty).map(|cnst| cnst.stable(&mut *tables, cx))
513 }
514
515 pub(crate) fn try_new_ty_const_uint(
516 &self,
517 value: u128,
518 uint_ty: UintTy,
519 ) -> Result<TyConst, Error> {
520 let mut tables = self.tables.borrow_mut();
521 let cx = &*self.cx.borrow();
522 let ty = cx.ty_new_uint(uint_ty.internal(&mut *tables, cx.tcx));
523 cx.try_new_ty_const_uint(value, ty).map(|cnst| cnst.stable(&mut *tables, cx))
524 }
525
526 pub(crate) fn new_rigid_ty(&self, kind: RigidTy) -> Ty {
528 let mut tables = self.tables.borrow_mut();
529 let cx = &*self.cx.borrow();
530 let internal_kind = kind.internal(&mut *tables, cx.tcx);
531 cx.new_rigid_ty(internal_kind).stable(&mut *tables, cx)
532 }
533
534 pub(crate) fn new_box_ty(&self, ty: Ty) -> Ty {
536 let mut tables = self.tables.borrow_mut();
537 let cx = &*self.cx.borrow();
538 let inner = ty.internal(&mut *tables, cx.tcx);
539 cx.new_box_ty(inner).stable(&mut *tables, cx)
540 }
541
542 pub(crate) fn def_ty(&self, item: DefId) -> Ty {
544 let mut tables = self.tables.borrow_mut();
545 let cx = &*self.cx.borrow();
546 let inner = item.internal(&mut *tables, cx.tcx);
547 cx.def_ty(inner).stable(&mut *tables, cx)
548 }
549
550 pub(crate) fn def_ty_with_args(&self, item: DefId, args: &GenericArgs) -> Ty {
552 let mut tables = self.tables.borrow_mut();
553 let cx = &*self.cx.borrow();
554 let inner = item.internal(&mut *tables, cx.tcx);
555 let args_ref = args.internal(&mut *tables, cx.tcx);
556 cx.def_ty_with_args(inner, args_ref).stable(&mut *tables, cx)
557 }
558
559 pub(crate) fn mir_const_pretty(&self, cnst: &MirConst) -> String {
561 let mut tables = self.tables.borrow_mut();
562 let cx = &*self.cx.borrow();
563 cnst.internal(&mut *tables, cx.tcx).to_string()
564 }
565
566 pub(crate) fn span_of_a_def(&self, def_id: DefId) -> Span {
568 let mut tables = self.tables.borrow_mut();
569 let cx = &*self.cx.borrow();
570 let did = tables[def_id];
571 cx.span_of_a_def(did).stable(&mut *tables, cx)
572 }
573
574 pub(crate) fn ty_const_pretty(&self, ct: TyConstId) -> String {
575 let tables = self.tables.borrow_mut();
576 let cx = &*self.cx.borrow();
577 cx.ty_const_pretty(tables.ty_consts[ct])
578 }
579
580 pub(crate) fn ty_pretty(&self, ty: Ty) -> String {
582 let tables = self.tables.borrow_mut();
583 let cx = &*self.cx.borrow();
584 cx.ty_pretty(tables.types[ty])
585 }
586
587 pub(crate) fn ty_kind(&self, ty: Ty) -> TyKind {
589 let mut tables = self.tables.borrow_mut();
590 let cx = &*self.cx.borrow();
591 cx.ty_kind(tables.types[ty]).stable(&mut *tables, cx)
592 }
593
594 pub(crate) fn rigid_ty_discriminant_ty(&self, ty: &RigidTy) -> Ty {
596 let mut tables = self.tables.borrow_mut();
597 let cx = &*self.cx.borrow();
598 let internal_kind = ty.internal(&mut *tables, cx.tcx);
599 cx.rigid_ty_discriminant_ty(internal_kind).stable(&mut *tables, cx)
600 }
601
602 pub(crate) fn instance_body(&self, instance: InstanceDef) -> Option<Body> {
604 let mut tables = self.tables.borrow_mut();
605 let cx = &*self.cx.borrow();
606 let instance = tables.instances[instance];
607 cx.instance_body(instance).map(|body| body.stable(&mut *tables, cx))
608 }
609
610 pub(crate) fn instance_ty(&self, instance: InstanceDef) -> Ty {
612 let mut tables = self.tables.borrow_mut();
613 let cx = &*self.cx.borrow();
614 let instance = tables.instances[instance];
615 cx.instance_ty(instance).stable(&mut *tables, cx)
616 }
617
618 pub(crate) fn instance_args(&self, def: InstanceDef) -> GenericArgs {
620 let mut tables = self.tables.borrow_mut();
621 let cx = &*self.cx.borrow();
622 let instance = tables.instances[def];
623 cx.instance_args(instance).stable(&mut *tables, cx)
624 }
625
626 pub(crate) fn instance_def_id(&self, instance: InstanceDef) -> DefId {
628 let mut tables = self.tables.borrow_mut();
629 let cx = &*self.cx.borrow();
630 let instance = tables.instances[instance];
631 cx.instance_def_id(instance, &mut *tables)
632 }
633
634 pub(crate) fn instance_mangled_name(&self, instance: InstanceDef) -> Symbol {
636 let tables = self.tables.borrow_mut();
637 let cx = &*self.cx.borrow();
638 let instance = tables.instances[instance];
639 cx.instance_mangled_name(instance)
640 }
641
642 pub(crate) fn is_empty_drop_shim(&self, def: InstanceDef) -> bool {
644 let tables = self.tables.borrow_mut();
645 let cx = &*self.cx.borrow();
646 let instance = tables.instances[def];
647 cx.is_empty_drop_shim(instance)
648 }
649
650 pub(crate) fn mono_instance(&self, def_id: DefId) -> Instance {
653 let mut tables = self.tables.borrow_mut();
654 let cx = &*self.cx.borrow();
655 let did = tables[def_id];
656 cx.mono_instance(did).stable(&mut *tables, cx)
657 }
658
659 pub(crate) fn requires_monomorphization(&self, def_id: DefId) -> bool {
661 let tables = self.tables.borrow();
662 let cx = &*self.cx.borrow();
663 let did = tables[def_id];
664 cx.requires_monomorphization(did)
665 }
666
667 pub(crate) fn resolve_instance(&self, def: FnDef, args: &GenericArgs) -> Option<Instance> {
669 let mut tables = self.tables.borrow_mut();
670 let cx = &*self.cx.borrow();
671 let def_id = def.0.internal(&mut *tables, cx.tcx);
672 let args_ref = args.internal(&mut *tables, cx.tcx);
673 cx.resolve_instance(def_id, args_ref).map(|inst| inst.stable(&mut *tables, cx))
674 }
675
676 pub(crate) fn resolve_drop_in_place(&self, ty: Ty) -> Instance {
678 let mut tables = self.tables.borrow_mut();
679 let cx = &*self.cx.borrow();
680 let internal_ty = ty.internal(&mut *tables, cx.tcx);
681
682 cx.resolve_drop_in_place(internal_ty).stable(&mut *tables, cx)
683 }
684
685 pub(crate) fn resolve_for_fn_ptr(&self, def: FnDef, args: &GenericArgs) -> Option<Instance> {
687 let mut tables = self.tables.borrow_mut();
688 let cx = &*self.cx.borrow();
689 let def_id = def.0.internal(&mut *tables, cx.tcx);
690 let args_ref = args.internal(&mut *tables, cx.tcx);
691 cx.resolve_for_fn_ptr(def_id, args_ref).stable(&mut *tables, cx)
692 }
693
694 pub(crate) fn resolve_closure(
696 &self,
697 def: ClosureDef,
698 args: &GenericArgs,
699 kind: ClosureKind,
700 ) -> Option<Instance> {
701 let mut tables = self.tables.borrow_mut();
702 let cx = &*self.cx.borrow();
703 let def_id = def.0.internal(&mut *tables, cx.tcx);
704 let args_ref = args.internal(&mut *tables, cx.tcx);
705 let closure_kind = kind.internal(&mut *tables, cx.tcx);
706 cx.resolve_closure(def_id, args_ref, closure_kind).map(|inst| inst.stable(&mut *tables, cx))
707 }
708
709 pub(crate) fn eval_static_initializer(&self, def: StaticDef) -> Result<Allocation, Error> {
711 let mut tables = self.tables.borrow_mut();
712 let cx = &*self.cx.borrow();
713 let def_id = def.0.internal(&mut *tables, cx.tcx);
714
715 cx.eval_static_initializer(def_id).stable(&mut *tables, cx)
716 }
717
718 pub(crate) fn eval_instance(
720 &self,
721 def: InstanceDef,
722 const_ty: Ty,
723 ) -> Result<Allocation, Error> {
724 let mut tables = self.tables.borrow_mut();
725 let instance = tables.instances[def];
726 let cx = &*self.cx.borrow();
727 let const_ty = const_ty.internal(&mut *tables, cx.tcx);
728 cx.eval_instance(instance)
729 .map(|const_val| alloc::try_new_allocation(const_ty, const_val, &mut *tables, cx))
730 .map_err(|e| e.stable(&mut *tables, cx))?
731 }
732
733 pub(crate) fn global_alloc(&self, id: AllocId) -> GlobalAlloc {
735 let mut tables = self.tables.borrow_mut();
736 let cx = &*self.cx.borrow();
737 let alloc_id = id.internal(&mut *tables, cx.tcx);
738 cx.global_alloc(alloc_id).stable(&mut *tables, cx)
739 }
740
741 pub(crate) fn vtable_allocation(&self, global_alloc: &GlobalAlloc) -> Option<AllocId> {
743 let mut tables = self.tables.borrow_mut();
744 let GlobalAlloc::VTable(ty, trait_ref) = global_alloc else {
745 return None;
746 };
747 let cx = &*self.cx.borrow();
748 let ty = ty.internal(&mut *tables, cx.tcx);
749 let trait_ref = trait_ref.internal(&mut *tables, cx.tcx);
750 let alloc_id = cx.vtable_allocation(ty, trait_ref);
751 Some(alloc_id.stable(&mut *tables, cx))
752 }
753
754 pub(crate) fn krate(&self, def_id: DefId) -> Crate {
755 let tables = self.tables.borrow();
756 let cx = &*self.cx.borrow();
757 smir_crate(cx, tables[def_id].krate)
758 }
759
760 pub(crate) fn instance_name(&self, def: InstanceDef, trimmed: bool) -> Symbol {
761 let tables = self.tables.borrow_mut();
762 let cx = &*self.cx.borrow();
763 let instance = tables.instances[def];
764 cx.instance_name(instance, trimmed)
765 }
766
767 pub(crate) fn target_info(&self) -> MachineInfo {
769 let mut tables = self.tables.borrow_mut();
770 let cx = &*self.cx.borrow();
771 MachineInfo {
772 endian: cx.target_endian().stable(&mut *tables, cx),
773 pointer_width: MachineSize::from_bits(cx.target_pointer_size()),
774 }
775 }
776
777 pub(crate) fn instance_abi(&self, def: InstanceDef) -> Result<FnAbi, Error> {
779 let mut tables = self.tables.borrow_mut();
780 let cx = &*self.cx.borrow();
781 let instance = tables.instances[def];
782 cx.instance_abi(instance).map(|fn_abi| fn_abi.stable(&mut *tables, cx))
783 }
784
785 pub(crate) fn fn_ptr_abi(&self, fn_ptr: PolyFnSig) -> Result<FnAbi, Error> {
787 let mut tables = self.tables.borrow_mut();
788 let cx = &*self.cx.borrow();
789 let sig = fn_ptr.internal(&mut *tables, cx.tcx);
790 cx.fn_ptr_abi(sig).map(|fn_abi| fn_abi.stable(&mut *tables, cx))
791 }
792
793 pub(crate) fn ty_layout(&self, ty: Ty) -> Result<Layout, Error> {
795 let mut tables = self.tables.borrow_mut();
796 let cx = &*self.cx.borrow();
797 let internal_ty = ty.internal(&mut *tables, cx.tcx);
798 cx.ty_layout(internal_ty).map(|layout| layout.stable(&mut *tables, cx))
799 }
800
801 pub(crate) fn layout_shape(&self, id: Layout) -> LayoutShape {
803 let mut tables = self.tables.borrow_mut();
804 let cx = &*self.cx.borrow();
805 id.internal(&mut *tables, cx.tcx).0.stable(&mut *tables, cx)
806 }
807
808 pub(crate) fn place_pretty(&self, place: &Place) -> String {
810 let mut tables = self.tables.borrow_mut();
811 let cx = &*self.cx.borrow();
812
813 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}",
place.internal(&mut *tables, cx.tcx)))
})format!("{:?}", place.internal(&mut *tables, cx.tcx))
814 }
815
816 pub(crate) fn binop_ty(&self, bin_op: BinOp, rhs: Ty, lhs: Ty) -> Ty {
818 let mut tables = self.tables.borrow_mut();
819 let cx = &*self.cx.borrow();
820 let rhs_internal = rhs.internal(&mut *tables, cx.tcx);
821 let lhs_internal = lhs.internal(&mut *tables, cx.tcx);
822 let bin_op_internal = bin_op.internal(&mut *tables, cx.tcx);
823 cx.binop_ty(bin_op_internal, rhs_internal, lhs_internal).stable(&mut *tables, cx)
824 }
825
826 pub(crate) fn unop_ty(&self, un_op: UnOp, arg: Ty) -> Ty {
828 let mut tables = self.tables.borrow_mut();
829 let cx = &*self.cx.borrow();
830 let un_op = un_op.internal(&mut *tables, cx.tcx);
831 let arg = arg.internal(&mut *tables, cx.tcx);
832 cx.unop_ty(un_op, arg).stable(&mut *tables, cx)
833 }
834
835 pub(crate) fn associated_items(&self, def_id: DefId) -> AssocItems {
837 let mut tables = self.tables.borrow_mut();
838 let cx = &*self.cx.borrow();
839 let did = tables[def_id];
840 cx.associated_items(did).iter().map(|assoc| assoc.stable(&mut *tables, cx)).collect()
841 }
842
843 pub(crate) fn vtable_entries(&self, trait_ref: &TraitRef) -> Vec<VtblEntry> {
845 let mut tables = self.tables.borrow_mut();
846 let cx = &*self.cx.borrow();
847 cx.vtable_entries(trait_ref.internal(&mut *tables, cx.tcx))
848 .iter()
849 .map(|v| v.stable(&mut *tables, cx))
850 .collect()
851 }
852
853 pub(crate) fn vtable_entry(&self, trait_ref: &TraitRef, idx: usize) -> Option<VtblEntry> {
857 let mut tables = self.tables.borrow_mut();
858 let cx = &*self.cx.borrow();
859 cx.vtable_entry(trait_ref.internal(&mut *tables, cx.tcx), idx).stable(&mut *tables, cx)
860 }
861}
862
863static TLV: ::scoped_tls::ScopedKey<Cell<*const ()>> =
::scoped_tls::ScopedKey {
inner: {
const FOO: ::std::thread::LocalKey<::std::cell::Cell<*const ()>> =
{
const __RUST_STD_INTERNAL_INIT: ::std::cell::Cell<*const ()>
=
{ ::std::cell::Cell::new(::std::ptr::null()) };
unsafe {
::std::thread::LocalKey::new(const {
if ::std::mem::needs_drop::<::std::cell::Cell<*const ()>>()
{
|_|
{
#[thread_local]
static __RUST_STD_INTERNAL_VAL:
::std::thread::local_impl::EagerStorage<::std::cell::Cell<*const ()>>
=
::std::thread::local_impl::EagerStorage::new(__RUST_STD_INTERNAL_INIT);
__RUST_STD_INTERNAL_VAL.get()
}
} else {
|_|
{
#[thread_local]
static __RUST_STD_INTERNAL_VAL: ::std::cell::Cell<*const ()>
=
__RUST_STD_INTERNAL_INIT;
&__RUST_STD_INTERNAL_VAL
}
}
})
}
};
&FOO
},
_marker: ::std::marker::PhantomData,
};scoped_tls::scoped_thread_local!(static TLV: Cell<*const ()>);
865
866#[cfg(feature = "rustc_internal")]
868pub(crate) fn run<'tcx, F, T>(interface: &CompilerInterface<'tcx>, f: F) -> Result<T, Error>
869where
870 F: FnOnce() -> T,
871{
872 if TLV.is_set() {
873 Err(Error::from("rustc_public already running"))
874 } else {
875 let ptr: *const () = (&raw const interface) as _;
876 TLV.set(&Cell::new(ptr), || Ok(f()))
877 }
878}
879
880pub(crate) fn with<R>(f: impl for<'tcx> FnOnce(&CompilerInterface<'tcx>) -> R) -> R {
885 if !TLV.is_set() {
::core::panicking::panic("assertion failed: TLV.is_set()")
};assert!(TLV.is_set());
886 TLV.with(|tlv| {
887 let ptr = tlv.get();
888 if !!ptr.is_null() {
::core::panicking::panic("assertion failed: !ptr.is_null()")
};assert!(!ptr.is_null());
889 f(unsafe { *(ptr as *const &CompilerInterface<'_>) })
890 })
891}
892
893fn smir_crate<'tcx>(
894 cx: &CompilerCtxt<'tcx, BridgeTys>,
895 crate_num: rustc_span::def_id::CrateNum,
896) -> Crate {
897 let name = cx.crate_name(crate_num);
898 let is_local = cx.crate_is_local(crate_num);
899 let id = CrateNum(cx.crate_num_id(crate_num), ThreadLocalIndex);
900 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_public/src/compiler_interface.rs:900",
"rustc_public::compiler_interface", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_public/src/compiler_interface.rs"),
::tracing_core::__macro_support::Option::Some(900u32),
::tracing_core::__macro_support::Option::Some("rustc_public::compiler_interface"),
::tracing_core::field::FieldSet::new(&["message", "name",
"crate_num"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("smir_crate")
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&name) as
&dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&crate_num)
as &dyn Value))])
});
} else { ; }
};debug!(?name, ?crate_num, "smir_crate");
901 Crate { id, name, is_local }
902}