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, Asyncness, ClosureDef, ClosureKind, Constness, CoroutineDef,
21 Discr, FieldDef, FnDef, ForeignDef, ForeignItemKind, ForeignModule, ForeignModuleDef,
22 GenericArgs, GenericPredicates, Generics, ImplDef, ImplTrait, IntrinsicDef, LineInfo, MirConst,
23 PolyFnSig, RigidTy, Span, TraitDecl, TraitDef, TraitRef, Ty, TyConst, TyConstId, TyKind,
24 UintTy, VariantDef, VariantIdx, 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 constness(&self, def: FnDef) -> Constness {
393 let mut tables = self.tables.borrow_mut();
394 let cx = &*self.cx.borrow();
395 let def_id = def.0.internal(&mut *tables, cx.tcx);
396 cx.constness(def_id).stable(&mut *tables, cx)
397 }
398
399 pub(crate) fn asyncness(&self, def: FnDef) -> Asyncness {
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.asyncness(def_id).stable(&mut *tables, cx)
405 }
406
407 pub(crate) fn intrinsic(&self, item: DefId) -> Option<IntrinsicDef> {
409 let mut tables = self.tables.borrow_mut();
410 let cx = &*self.cx.borrow();
411 let def_id = item.internal(&mut *tables, cx.tcx);
412 cx.intrinsic(def_id).map(|_| IntrinsicDef(item))
413 }
414
415 pub(crate) fn intrinsic_name(&self, def: IntrinsicDef) -> Symbol {
417 let mut tables = self.tables.borrow_mut();
418 let cx = &*self.cx.borrow();
419 let def_id = def.0.internal(&mut *tables, cx.tcx);
420 cx.intrinsic_name(def_id)
421 }
422
423 pub(crate) fn closure_sig(&self, args: &GenericArgs) -> PolyFnSig {
425 let mut tables = self.tables.borrow_mut();
426 let cx = &*self.cx.borrow();
427 let args_ref = args.internal(&mut *tables, cx.tcx);
428 cx.closure_sig(args_ref).stable(&mut *tables, cx)
429 }
430
431 pub(crate) fn adt_variants_len(&self, def: AdtDef) -> usize {
433 let mut tables = self.tables.borrow_mut();
434 let cx = &*self.cx.borrow();
435 cx.adt_variants_len(def.internal(&mut *tables, cx.tcx))
436 }
437
438 pub(crate) fn adt_discr_for_variant(&self, adt: AdtDef, variant: VariantIdx) -> Discr {
440 let mut tables = self.tables.borrow_mut();
441 let cx = &*self.cx.borrow();
442 cx.adt_discr_for_variant(
443 adt.internal(&mut *tables, cx.tcx),
444 variant.internal(&mut *tables, cx.tcx),
445 )
446 .stable(&mut *tables, cx)
447 }
448
449 pub(crate) fn coroutine_discr_for_variant(
451 &self,
452 coroutine: CoroutineDef,
453 args: &GenericArgs,
454 variant: VariantIdx,
455 ) -> Discr {
456 let mut tables = self.tables.borrow_mut();
457 let cx = &*self.cx.borrow();
458 let tcx = cx.tcx;
459 let def = coroutine.def_id().internal(&mut *tables, tcx);
460 let args_ref = args.internal(&mut *tables, tcx);
461 cx.coroutine_discr_for_variant(def, args_ref, variant.internal(&mut *tables, tcx))
462 .stable(&mut *tables, cx)
463 }
464
465 pub(crate) fn variant_name(&self, def: VariantDef) -> Symbol {
467 let mut tables = self.tables.borrow_mut();
468 let cx = &*self.cx.borrow();
469 cx.variant_name(def.internal(&mut *tables, cx.tcx))
470 }
471
472 pub(crate) fn variant_fields(&self, def: VariantDef) -> Vec<FieldDef> {
473 let mut tables = self.tables.borrow_mut();
474 let cx = &*self.cx.borrow();
475 def.internal(&mut *tables, cx.tcx)
476 .fields
477 .iter()
478 .map(|f| f.stable(&mut *tables, cx))
479 .collect()
480 }
481
482 pub(crate) fn eval_target_usize(&self, mir_const: &MirConst) -> Result<u64, Error> {
484 let mut tables = self.tables.borrow_mut();
485 let cx = &*self.cx.borrow();
486 let cnst = mir_const.internal(&mut *tables, cx.tcx);
487 cx.eval_target_usize(cnst)
488 }
489
490 pub(crate) fn eval_target_usize_ty(&self, ty_const: &TyConst) -> Result<u64, Error> {
491 let mut tables = self.tables.borrow_mut();
492 let cx = &*self.cx.borrow();
493 let cnst = ty_const.internal(&mut *tables, cx.tcx);
494 cx.eval_target_usize_ty(cnst)
495 }
496
497 pub(crate) fn try_new_const_zst(&self, ty: Ty) -> Result<MirConst, Error> {
499 let mut tables = self.tables.borrow_mut();
500 let cx = &*self.cx.borrow();
501 let ty_internal = ty.internal(&mut *tables, cx.tcx);
502 cx.try_new_const_zst(ty_internal).map(|cnst| cnst.stable(&mut *tables, cx))
503 }
504
505 pub(crate) fn new_const_str(&self, value: &str) -> MirConst {
507 let mut tables = self.tables.borrow_mut();
508 let cx = &*self.cx.borrow();
509 cx.new_const_str(value).stable(&mut *tables, cx)
510 }
511
512 pub(crate) fn new_const_bool(&self, value: bool) -> MirConst {
514 let mut tables = self.tables.borrow_mut();
515 let cx = &*self.cx.borrow();
516 cx.new_const_bool(value).stable(&mut *tables, cx)
517 }
518
519 pub(crate) fn try_new_const_uint(
521 &self,
522 value: u128,
523 uint_ty: UintTy,
524 ) -> Result<MirConst, Error> {
525 let mut tables = self.tables.borrow_mut();
526 let cx = &*self.cx.borrow();
527 let ty = cx.ty_new_uint(uint_ty.internal(&mut *tables, cx.tcx));
528 cx.try_new_const_uint(value, ty).map(|cnst| cnst.stable(&mut *tables, cx))
529 }
530
531 pub(crate) fn try_new_ty_const_uint(
532 &self,
533 value: u128,
534 uint_ty: UintTy,
535 ) -> Result<TyConst, Error> {
536 let mut tables = self.tables.borrow_mut();
537 let cx = &*self.cx.borrow();
538 let ty = cx.ty_new_uint(uint_ty.internal(&mut *tables, cx.tcx));
539 cx.try_new_ty_const_uint(value, ty).map(|cnst| cnst.stable(&mut *tables, cx))
540 }
541
542 pub(crate) fn new_rigid_ty(&self, kind: RigidTy) -> Ty {
544 let mut tables = self.tables.borrow_mut();
545 let cx = &*self.cx.borrow();
546 let internal_kind = kind.internal(&mut *tables, cx.tcx);
547 cx.new_rigid_ty(internal_kind).stable(&mut *tables, cx)
548 }
549
550 pub(crate) fn new_box_ty(&self, ty: Ty) -> Ty {
552 let mut tables = self.tables.borrow_mut();
553 let cx = &*self.cx.borrow();
554 let inner = ty.internal(&mut *tables, cx.tcx);
555 cx.new_box_ty(inner).stable(&mut *tables, cx)
556 }
557
558 pub(crate) fn def_ty(&self, item: DefId) -> Ty {
560 let mut tables = self.tables.borrow_mut();
561 let cx = &*self.cx.borrow();
562 let inner = item.internal(&mut *tables, cx.tcx);
563 cx.def_ty(inner).stable(&mut *tables, cx)
564 }
565
566 pub(crate) fn def_ty_with_args(&self, item: DefId, args: &GenericArgs) -> Ty {
568 let mut tables = self.tables.borrow_mut();
569 let cx = &*self.cx.borrow();
570 let inner = item.internal(&mut *tables, cx.tcx);
571 let args_ref = args.internal(&mut *tables, cx.tcx);
572 cx.def_ty_with_args(inner, args_ref).stable(&mut *tables, cx)
573 }
574
575 pub(crate) fn mir_const_pretty(&self, cnst: &MirConst) -> String {
577 let mut tables = self.tables.borrow_mut();
578 let cx = &*self.cx.borrow();
579 cnst.internal(&mut *tables, cx.tcx).to_string()
580 }
581
582 pub(crate) fn span_of_a_def(&self, def_id: DefId) -> Span {
584 let mut tables = self.tables.borrow_mut();
585 let cx = &*self.cx.borrow();
586 let did = tables[def_id];
587 cx.span_of_a_def(did).stable(&mut *tables, cx)
588 }
589
590 pub(crate) fn ty_const_pretty(&self, ct: TyConstId) -> String {
591 let tables = self.tables.borrow_mut();
592 let cx = &*self.cx.borrow();
593 cx.ty_const_pretty(tables.ty_consts[ct])
594 }
595
596 pub(crate) fn ty_pretty(&self, ty: Ty) -> String {
598 let tables = self.tables.borrow_mut();
599 let cx = &*self.cx.borrow();
600 cx.ty_pretty(tables.types[ty])
601 }
602
603 pub(crate) fn ty_kind(&self, ty: Ty) -> TyKind {
605 let mut tables = self.tables.borrow_mut();
606 let cx = &*self.cx.borrow();
607 cx.ty_kind(tables.types[ty]).stable(&mut *tables, cx)
608 }
609
610 pub(crate) fn rigid_ty_discriminant_ty(&self, ty: &RigidTy) -> Ty {
612 let mut tables = self.tables.borrow_mut();
613 let cx = &*self.cx.borrow();
614 let internal_kind = ty.internal(&mut *tables, cx.tcx);
615 cx.rigid_ty_discriminant_ty(internal_kind).stable(&mut *tables, cx)
616 }
617
618 pub(crate) fn instance_body(&self, instance: InstanceDef) -> Option<Body> {
620 let mut tables = self.tables.borrow_mut();
621 let cx = &*self.cx.borrow();
622 let instance = tables.instances[instance];
623 cx.instance_body(instance).map(|body| body.stable(&mut *tables, cx))
624 }
625
626 pub(crate) fn instance_ty(&self, instance: InstanceDef) -> Ty {
628 let mut tables = self.tables.borrow_mut();
629 let cx = &*self.cx.borrow();
630 let instance = tables.instances[instance];
631 cx.instance_ty(instance).stable(&mut *tables, cx)
632 }
633
634 pub(crate) fn instance_args(&self, def: InstanceDef) -> GenericArgs {
636 let mut tables = self.tables.borrow_mut();
637 let cx = &*self.cx.borrow();
638 let instance = tables.instances[def];
639 cx.instance_args(instance).stable(&mut *tables, cx)
640 }
641
642 pub(crate) fn instance_def_id(&self, instance: InstanceDef) -> DefId {
644 let mut tables = self.tables.borrow_mut();
645 let cx = &*self.cx.borrow();
646 let instance = tables.instances[instance];
647 cx.instance_def_id(instance, &mut *tables)
648 }
649
650 pub(crate) fn instance_mangled_name(&self, instance: InstanceDef) -> Symbol {
652 let tables = self.tables.borrow_mut();
653 let cx = &*self.cx.borrow();
654 let instance = tables.instances[instance];
655 cx.instance_mangled_name(instance)
656 }
657
658 pub(crate) fn is_empty_drop_shim(&self, def: InstanceDef) -> bool {
660 let tables = self.tables.borrow_mut();
661 let cx = &*self.cx.borrow();
662 let instance = tables.instances[def];
663 cx.is_empty_drop_shim(instance)
664 }
665
666 pub(crate) fn mono_instance(&self, def_id: DefId) -> Instance {
669 let mut tables = self.tables.borrow_mut();
670 let cx = &*self.cx.borrow();
671 let did = tables[def_id];
672 cx.mono_instance(did).stable(&mut *tables, cx)
673 }
674
675 pub(crate) fn requires_monomorphization(&self, def_id: DefId) -> bool {
677 let tables = self.tables.borrow();
678 let cx = &*self.cx.borrow();
679 let did = tables[def_id];
680 cx.requires_monomorphization(did)
681 }
682
683 pub(crate) fn resolve_instance(&self, def: FnDef, args: &GenericArgs) -> Option<Instance> {
685 let mut tables = self.tables.borrow_mut();
686 let cx = &*self.cx.borrow();
687 let def_id = def.0.internal(&mut *tables, cx.tcx);
688 let args_ref = args.internal(&mut *tables, cx.tcx);
689 cx.resolve_instance(def_id, args_ref).map(|inst| inst.stable(&mut *tables, cx))
690 }
691
692 pub(crate) fn resolve_drop_in_place(&self, ty: Ty) -> Instance {
694 let mut tables = self.tables.borrow_mut();
695 let cx = &*self.cx.borrow();
696 let internal_ty = ty.internal(&mut *tables, cx.tcx);
697
698 cx.resolve_drop_in_place(internal_ty).stable(&mut *tables, cx)
699 }
700
701 pub(crate) fn resolve_for_fn_ptr(&self, def: FnDef, args: &GenericArgs) -> Option<Instance> {
703 let mut tables = self.tables.borrow_mut();
704 let cx = &*self.cx.borrow();
705 let def_id = def.0.internal(&mut *tables, cx.tcx);
706 let args_ref = args.internal(&mut *tables, cx.tcx);
707 cx.resolve_for_fn_ptr(def_id, args_ref).stable(&mut *tables, cx)
708 }
709
710 pub(crate) fn resolve_closure(
712 &self,
713 def: ClosureDef,
714 args: &GenericArgs,
715 kind: ClosureKind,
716 ) -> Option<Instance> {
717 let mut tables = self.tables.borrow_mut();
718 let cx = &*self.cx.borrow();
719 let def_id = def.0.internal(&mut *tables, cx.tcx);
720 let args_ref = args.internal(&mut *tables, cx.tcx);
721 let closure_kind = kind.internal(&mut *tables, cx.tcx);
722 cx.resolve_closure(def_id, args_ref, closure_kind).map(|inst| inst.stable(&mut *tables, cx))
723 }
724
725 pub(crate) fn eval_static_initializer(&self, def: StaticDef) -> Result<Allocation, Error> {
727 let mut tables = self.tables.borrow_mut();
728 let cx = &*self.cx.borrow();
729 let def_id = def.0.internal(&mut *tables, cx.tcx);
730
731 cx.eval_static_initializer(def_id).stable(&mut *tables, cx)
732 }
733
734 pub(crate) fn eval_instance(
736 &self,
737 def: InstanceDef,
738 const_ty: Ty,
739 ) -> Result<Allocation, Error> {
740 let mut tables = self.tables.borrow_mut();
741 let instance = tables.instances[def];
742 let cx = &*self.cx.borrow();
743 let const_ty = const_ty.internal(&mut *tables, cx.tcx);
744 cx.eval_instance(instance)
745 .map(|const_val| alloc::try_new_allocation(const_ty, const_val, &mut *tables, cx))
746 .map_err(|e| e.stable(&mut *tables, cx))?
747 }
748
749 pub(crate) fn global_alloc(&self, id: AllocId) -> GlobalAlloc {
751 let mut tables = self.tables.borrow_mut();
752 let cx = &*self.cx.borrow();
753 let alloc_id = id.internal(&mut *tables, cx.tcx);
754 cx.global_alloc(alloc_id).stable(&mut *tables, cx)
755 }
756
757 pub(crate) fn vtable_allocation(&self, global_alloc: &GlobalAlloc) -> Option<AllocId> {
759 let mut tables = self.tables.borrow_mut();
760 let GlobalAlloc::VTable(ty, trait_ref) = global_alloc else {
761 return None;
762 };
763 let cx = &*self.cx.borrow();
764 let ty = ty.internal(&mut *tables, cx.tcx);
765 let trait_ref = trait_ref.internal(&mut *tables, cx.tcx);
766 let alloc_id = cx.vtable_allocation(ty, trait_ref);
767 Some(alloc_id.stable(&mut *tables, cx))
768 }
769
770 pub(crate) fn krate(&self, def_id: DefId) -> Crate {
771 let tables = self.tables.borrow();
772 let cx = &*self.cx.borrow();
773 smir_crate(cx, tables[def_id].krate)
774 }
775
776 pub(crate) fn instance_name(&self, def: InstanceDef, trimmed: bool) -> Symbol {
777 let tables = self.tables.borrow_mut();
778 let cx = &*self.cx.borrow();
779 let instance = tables.instances[def];
780 cx.instance_name(instance, trimmed)
781 }
782
783 pub(crate) fn target_info(&self) -> MachineInfo {
785 let mut tables = self.tables.borrow_mut();
786 let cx = &*self.cx.borrow();
787 MachineInfo {
788 endian: cx.target_endian().stable(&mut *tables, cx),
789 pointer_width: MachineSize::from_bits(cx.target_pointer_size()),
790 }
791 }
792
793 pub(crate) fn instance_abi(&self, def: InstanceDef) -> Result<FnAbi, Error> {
795 let mut tables = self.tables.borrow_mut();
796 let cx = &*self.cx.borrow();
797 let instance = tables.instances[def];
798 cx.instance_abi(instance).map(|fn_abi| fn_abi.stable(&mut *tables, cx))
799 }
800
801 pub(crate) fn fn_ptr_abi(&self, fn_ptr: PolyFnSig) -> Result<FnAbi, Error> {
803 let mut tables = self.tables.borrow_mut();
804 let cx = &*self.cx.borrow();
805 let sig = fn_ptr.internal(&mut *tables, cx.tcx);
806 cx.fn_ptr_abi(sig).map(|fn_abi| fn_abi.stable(&mut *tables, cx))
807 }
808
809 pub(crate) fn ty_layout(&self, ty: Ty) -> Result<Layout, Error> {
811 let mut tables = self.tables.borrow_mut();
812 let cx = &*self.cx.borrow();
813 let internal_ty = ty.internal(&mut *tables, cx.tcx);
814 cx.ty_layout(internal_ty).map(|layout| layout.stable(&mut *tables, cx))
815 }
816
817 pub(crate) fn layout_shape(&self, id: Layout) -> LayoutShape {
819 let mut tables = self.tables.borrow_mut();
820 let cx = &*self.cx.borrow();
821 id.internal(&mut *tables, cx.tcx).0.stable(&mut *tables, cx)
822 }
823
824 pub(crate) fn place_pretty(&self, place: &Place) -> String {
826 let mut tables = self.tables.borrow_mut();
827 let cx = &*self.cx.borrow();
828
829 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}",
place.internal(&mut *tables, cx.tcx)))
})format!("{:?}", place.internal(&mut *tables, cx.tcx))
830 }
831
832 pub(crate) fn binop_ty(&self, bin_op: BinOp, rhs: Ty, lhs: Ty) -> Ty {
834 let mut tables = self.tables.borrow_mut();
835 let cx = &*self.cx.borrow();
836 let rhs_internal = rhs.internal(&mut *tables, cx.tcx);
837 let lhs_internal = lhs.internal(&mut *tables, cx.tcx);
838 let bin_op_internal = bin_op.internal(&mut *tables, cx.tcx);
839 cx.binop_ty(bin_op_internal, rhs_internal, lhs_internal).stable(&mut *tables, cx)
840 }
841
842 pub(crate) fn unop_ty(&self, un_op: UnOp, arg: Ty) -> Ty {
844 let mut tables = self.tables.borrow_mut();
845 let cx = &*self.cx.borrow();
846 let un_op = un_op.internal(&mut *tables, cx.tcx);
847 let arg = arg.internal(&mut *tables, cx.tcx);
848 cx.unop_ty(un_op, arg).stable(&mut *tables, cx)
849 }
850
851 pub(crate) fn associated_items(&self, def_id: DefId) -> AssocItems {
853 let mut tables = self.tables.borrow_mut();
854 let cx = &*self.cx.borrow();
855 let did = tables[def_id];
856 cx.associated_items(did).iter().map(|assoc| assoc.stable(&mut *tables, cx)).collect()
857 }
858
859 pub(crate) fn vtable_entries(&self, trait_ref: &TraitRef) -> Vec<VtblEntry> {
861 let mut tables = self.tables.borrow_mut();
862 let cx = &*self.cx.borrow();
863 cx.vtable_entries(trait_ref.internal(&mut *tables, cx.tcx))
864 .iter()
865 .map(|v| v.stable(&mut *tables, cx))
866 .collect()
867 }
868
869 pub(crate) fn vtable_entry(&self, trait_ref: &TraitRef, idx: usize) -> Option<VtblEntry> {
873 let mut tables = self.tables.borrow_mut();
874 let cx = &*self.cx.borrow();
875 cx.vtable_entry(trait_ref.internal(&mut *tables, cx.tcx), idx).stable(&mut *tables, cx)
876 }
877}
878
879static 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 ()>);
881
882#[cfg(feature = "rustc_internal")]
884pub(crate) fn run<'tcx, F, T>(interface: &CompilerInterface<'tcx>, f: F) -> Result<T, Error>
885where
886 F: FnOnce() -> T,
887{
888 if TLV.is_set() {
889 Err(Error::from("rustc_public already running"))
890 } else {
891 let ptr: *const () = (&raw const interface) as _;
892 TLV.set(&Cell::new(ptr), || Ok(f()))
893 }
894}
895
896pub(crate) fn with<R>(f: impl for<'tcx> FnOnce(&CompilerInterface<'tcx>) -> R) -> R {
901 if !TLV.is_set() {
::core::panicking::panic("assertion failed: TLV.is_set()")
};assert!(TLV.is_set());
902 TLV.with(|tlv| {
903 let ptr = tlv.get();
904 if !!ptr.is_null() {
::core::panicking::panic("assertion failed: !ptr.is_null()")
};assert!(!ptr.is_null());
905 f(unsafe { *(ptr as *const &CompilerInterface<'_>) })
906 })
907}
908
909fn smir_crate<'tcx>(
910 cx: &CompilerCtxt<'tcx, BridgeTys>,
911 crate_num: rustc_span::def_id::CrateNum,
912) -> Crate {
913 let name = cx.crate_name(crate_num);
914 let is_local = cx.crate_is_local(crate_num);
915 let id = CrateNum(cx.crate_num_id(crate_num), ThreadLocalIndex);
916 {
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:916",
"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(916u32),
::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");
917 Crate { id, name, is_local }
918}