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 fn with_cx<R>(
83 &self,
84 f: impl FnOnce(&mut Tables<'tcx, BridgeTys>, &CompilerCtxt<'tcx, BridgeTys>) -> R,
85 ) -> R {
86 let mut tables = self.tables.borrow_mut();
87 let cx = self.cx.borrow();
88 f(&mut *tables, &*cx)
89 }
90
91 pub(crate) fn entry_fn(&self) -> Option<CrateItem> {
92 self.with_cx(|tables, cx| {
93 let did = cx.entry_fn();
94 Some(tables.crate_item(did?))
95 })
96 }
97
98 pub(crate) fn all_local_items(&self) -> CrateItems {
100 self.with_cx(|tables, cx| {
101 cx.all_local_items().iter().map(|did| tables.crate_item(*did)).collect()
102 })
103 }
104
105 pub(crate) fn mir_body(&self, item: DefId) -> mir::Body {
108 self.with_cx(|tables, cx| {
109 let did = tables[item];
110 cx.mir_body(did).stable(tables, cx)
111 })
112 }
113
114 pub(crate) fn has_body(&self, item: DefId) -> bool {
116 self.with_cx(|tables, cx| {
117 let def = item.internal(tables, cx.tcx);
118 cx.has_body(def)
119 })
120 }
121
122 pub(crate) fn foreign_modules(&self, crate_num: CrateNum) -> Vec<ForeignModuleDef> {
123 self.with_cx(|tables, cx| {
124 cx.foreign_modules(crate_num.internal(tables, cx.tcx))
125 .iter()
126 .map(|did| tables.foreign_module_def(*did))
127 .collect()
128 })
129 }
130
131 pub(crate) fn crate_functions(&self, crate_num: CrateNum) -> Vec<FnDef> {
133 self.with_cx(|tables, cx| {
134 let krate = crate_num.internal(tables, cx.tcx);
135 cx.crate_functions(krate).iter().map(|did| tables.fn_def(*did)).collect()
136 })
137 }
138
139 pub(crate) fn crate_adts(&self, crate_num: CrateNum) -> Vec<AdtDef> {
140 self.with_cx(|tables, cx| {
141 let krate = crate_num.internal(tables, cx.tcx);
142 cx.crate_adts(krate).iter().map(|did| tables.adt_def(*did)).collect()
143 })
144 }
145
146 pub(crate) fn crate_statics(&self, crate_num: CrateNum) -> Vec<StaticDef> {
148 self.with_cx(|tables, cx| {
149 let krate = crate_num.internal(tables, cx.tcx);
150 cx.crate_statics(krate).iter().map(|did| tables.static_def(*did)).collect()
151 })
152 }
153
154 pub(crate) fn foreign_module(&self, mod_def: ForeignModuleDef) -> ForeignModule {
155 self.with_cx(|tables, cx| {
156 let did = tables[mod_def.def_id()];
157 cx.foreign_module(did).stable(tables, cx)
158 })
159 }
160
161 pub(crate) fn foreign_items(&self, mod_def: ForeignModuleDef) -> Vec<ForeignDef> {
162 self.with_cx(|tables, cx| {
163 let did = tables[mod_def.def_id()];
164 cx.foreign_items(did).iter().map(|did| tables.foreign_def(*did)).collect()
165 })
166 }
167
168 pub(crate) fn all_trait_decls(&self) -> TraitDecls {
169 self.with_cx(|tables, cx| cx.all_trait_decls().map(|did| tables.trait_def(did)).collect())
170 }
171
172 pub(crate) fn trait_decls(&self, crate_num: CrateNum) -> TraitDecls {
173 self.with_cx(|tables, cx| {
174 let krate = crate_num.internal(tables, cx.tcx);
175 cx.trait_decls(krate).iter().map(|did| tables.trait_def(*did)).collect()
176 })
177 }
178
179 pub(crate) fn trait_decl(&self, trait_def: &TraitDef) -> TraitDecl {
180 self.with_cx(|tables, cx| {
181 let did = tables[trait_def.0];
182 cx.trait_decl(did).stable(tables, cx)
183 })
184 }
185
186 pub(crate) fn all_trait_impls(&self) -> ImplTraitDecls {
187 self.with_cx(|tables, cx| {
188 cx.all_trait_impls().iter().map(|did| tables.impl_def(*did)).collect()
189 })
190 }
191
192 pub(crate) fn trait_impls(&self, crate_num: CrateNum) -> ImplTraitDecls {
193 self.with_cx(|tables, cx| {
194 let krate = crate_num.internal(tables, cx.tcx);
195 cx.trait_impls(krate).iter().map(|did| tables.impl_def(*did)).collect()
196 })
197 }
198
199 pub(crate) fn trait_impl(&self, trait_impl: &ImplDef) -> ImplTrait {
200 self.with_cx(|tables, cx| {
201 let did = tables[trait_impl.0];
202 cx.trait_impl(did).stable(tables, cx)
203 })
204 }
205
206 pub(crate) fn generics_of(&self, def_id: DefId) -> Generics {
207 self.with_cx(|tables, cx| {
208 let did = tables[def_id];
209 cx.generics_of(did).stable(tables, cx)
210 })
211 }
212
213 pub(crate) fn predicates_of(&self, def_id: DefId) -> GenericPredicates {
214 self.with_cx(|tables, cx| {
215 let did = tables[def_id];
216 let (parent, kinds) = cx.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(tables, cx), span.stable(tables, cx)))
222 .collect(),
223 }
224 })
225 }
226
227 pub(crate) fn explicit_predicates_of(&self, def_id: DefId) -> GenericPredicates {
228 self.with_cx(|tables, cx| {
229 let did = tables[def_id];
230 let (parent, kinds) = cx.explicit_predicates_of(did);
231 crate::ty::GenericPredicates {
232 parent: parent.map(|did| tables.trait_def(did)),
233 predicates: kinds
234 .iter()
235 .map(|(kind, span)| (kind.stable(tables, cx), span.stable(tables, cx)))
236 .collect(),
237 }
238 })
239 }
240
241 pub(crate) fn local_crate(&self) -> Crate {
243 self.with_cx(|_, cx| smir_crate(cx, cx.local_crate_num()))
244 }
245
246 pub(crate) fn external_crates(&self) -> Vec<Crate> {
248 self.with_cx(|_, cx| {
249 cx.external_crates().iter().map(|crate_num| smir_crate(cx, *crate_num)).collect()
250 })
251 }
252
253 pub(crate) fn find_crates(&self, name: &str) -> Vec<Crate> {
255 self.with_cx(|_, cx| {
256 cx.find_crates(name).iter().map(|crate_num| smir_crate(cx, *crate_num)).collect()
257 })
258 }
259
260 pub(crate) fn def_name(&self, def_id: DefId, trimmed: bool) -> Symbol {
262 self.with_cx(|tables, cx| {
263 let did = tables[def_id];
264 cx.def_name(did, trimmed)
265 })
266 }
267
268 pub(crate) fn def_parent(&self, def_id: DefId) -> Option<DefId> {
270 self.with_cx(|tables, cx| {
271 let did = tables[def_id];
272 cx.def_parent(did).map(|did| tables.create_def_id(did))
273 })
274 }
275
276 pub(crate) fn tool_attrs(&self, def_id: DefId, attr: &[Symbol]) -> Vec<Attribute> {
284 self.with_cx(|tables, cx| {
285 let did = tables[def_id];
286 cx.tool_attrs(did, attr)
287 .into_iter()
288 .map(|(attr_str, span)| Attribute::new(attr_str, span.stable(tables, cx)))
289 .collect()
290 })
291 }
292
293 pub(crate) fn all_tool_attrs(&self, def_id: DefId) -> Vec<Attribute> {
295 self.with_cx(|tables, cx| {
296 let did = tables[def_id];
297 cx.all_tool_attrs(did)
298 .into_iter()
299 .map(|(attr_str, span)| Attribute::new(attr_str, span.stable(tables, cx)))
300 .collect()
301 })
302 }
303
304 pub(crate) fn span_to_string(&self, span: Span) -> String {
306 self.with_cx(|tables, cx| {
307 let sp = tables.spans[span];
308 cx.span_to_string(sp)
309 })
310 }
311
312 pub(crate) fn get_filename(&self, span: &Span) -> Filename {
314 self.with_cx(|tables, cx| {
315 let sp = tables.spans[*span];
316 cx.get_filename(sp)
317 })
318 }
319
320 pub(crate) fn get_lines(&self, span: &Span) -> LineInfo {
322 self.with_cx(|tables, cx| {
323 let sp = tables.spans[*span];
324 let lines = cx.get_lines(sp);
325 LineInfo::from(lines)
326 })
327 }
328
329 pub(crate) fn item_kind(&self, item: CrateItem) -> ItemKind {
331 self.with_cx(|tables, cx| {
332 let did = tables[item.0];
333 new_item_kind(cx.def_kind(did))
334 })
335 }
336
337 pub(crate) fn is_foreign_item(&self, item: DefId) -> bool {
339 self.with_cx(|tables, cx| {
340 let did = tables[item];
341 cx.is_foreign_item(did)
342 })
343 }
344
345 pub(crate) fn foreign_item_kind(&self, def: ForeignDef) -> ForeignItemKind {
347 self.with_cx(|tables, cx| {
348 let def_id = tables[def.def_id()];
349 let def_kind = cx.foreign_item_kind(def_id);
350 match def_kind {
351 DefKind::Fn => ForeignItemKind::Fn(tables.fn_def(def_id)),
352 DefKind::Static { .. } => ForeignItemKind::Static(tables.static_def(def_id)),
353 DefKind::ForeignTy => {
354 use rustc_public_bridge::context::TyHelpers;
355 ForeignItemKind::Type(tables.intern_ty(cx.new_foreign(def_id)))
356 }
357 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),
358 }
359 })
360 }
361
362 pub(crate) fn adt_kind(&self, def: AdtDef) -> AdtKind {
364 self.with_cx(|tables, cx| cx.adt_kind(def.internal(tables, cx.tcx)).stable(tables, cx))
365 }
366
367 pub(crate) fn adt_is_box(&self, def: AdtDef) -> bool {
369 self.with_cx(|tables, cx| cx.adt_is_box(def.internal(tables, cx.tcx)))
370 }
371
372 pub(crate) fn adt_is_simd(&self, def: AdtDef) -> bool {
374 self.with_cx(|tables, cx| cx.adt_is_simd(def.internal(tables, cx.tcx)))
375 }
376
377 pub(crate) fn adt_is_cstr(&self, def: AdtDef) -> bool {
379 self.with_cx(|tables, cx| cx.adt_is_cstr(def.0.internal(tables, cx.tcx)))
380 }
381
382 pub(crate) fn adt_repr(&self, def: AdtDef) -> ReprOptions {
384 self.with_cx(|tables, cx| cx.adt_repr(def.internal(tables, cx.tcx)).stable(tables, cx))
385 }
386
387 pub(crate) fn fn_sig(&self, def: FnDef, args: &GenericArgs) -> PolyFnSig {
389 self.with_cx(|tables, cx| {
390 let def_id = def.0.internal(tables, cx.tcx);
391 let args_ref = args.internal(tables, cx.tcx);
392 cx.fn_sig(def_id, args_ref).stable(tables, cx)
393 })
394 }
395
396 pub(crate) fn constness(&self, def: FnDef) -> Constness {
398 self.with_cx(|tables, cx| {
399 let def_id = def.0.internal(tables, cx.tcx);
400 cx.constness(def_id).stable(tables, cx)
401 })
402 }
403
404 pub(crate) fn asyncness(&self, def: FnDef) -> Asyncness {
406 self.with_cx(|tables, cx| {
407 let def_id = def.0.internal(tables, cx.tcx);
408 cx.asyncness(def_id).stable(tables, cx)
409 })
410 }
411
412 pub(crate) fn intrinsic(&self, item: DefId) -> Option<IntrinsicDef> {
414 self.with_cx(|tables, cx| {
415 let def_id = item.internal(tables, cx.tcx);
416 cx.intrinsic(def_id).map(|_| IntrinsicDef(item))
417 })
418 }
419
420 pub(crate) fn intrinsic_name(&self, def: IntrinsicDef) -> Symbol {
422 self.with_cx(|tables, cx| {
423 let def_id = def.0.internal(tables, cx.tcx);
424 cx.intrinsic_name(def_id)
425 })
426 }
427
428 pub(crate) fn closure_sig(&self, args: &GenericArgs) -> PolyFnSig {
430 self.with_cx(|tables, cx| {
431 let args_ref = args.internal(tables, cx.tcx);
432 cx.closure_sig(args_ref).stable(tables, cx)
433 })
434 }
435
436 pub(crate) fn adt_variants_len(&self, def: AdtDef) -> usize {
438 self.with_cx(|tables, cx| cx.adt_variants_len(def.internal(tables, cx.tcx)))
439 }
440
441 pub(crate) fn adt_discr_for_variant(&self, adt: AdtDef, variant: VariantIdx) -> Discr {
443 self.with_cx(|tables, cx| {
444 cx.adt_discr_for_variant(adt.internal(tables, cx.tcx), variant.internal(tables, cx.tcx))
445 .stable(tables, cx)
446 })
447 }
448
449 pub(crate) fn coroutine_discr_for_variant(
451 &self,
452 coroutine: CoroutineDef,
453 args: &GenericArgs,
454 variant: VariantIdx,
455 ) -> Discr {
456 self.with_cx(|tables, cx| {
457 let tcx = cx.tcx;
458 let def = coroutine.def_id().internal(tables, tcx);
459 let args_ref = args.internal(tables, tcx);
460 cx.coroutine_discr_for_variant(def, args_ref, variant.internal(tables, tcx))
461 .stable(tables, cx)
462 })
463 }
464
465 pub(crate) fn variant_name(&self, def: VariantDef) -> Symbol {
467 self.with_cx(|tables, cx| cx.variant_name(def.internal(tables, cx.tcx)))
468 }
469
470 pub(crate) fn variant_fields(&self, def: VariantDef) -> Vec<FieldDef> {
471 self.with_cx(|tables, cx| {
472 def.internal(tables, cx.tcx).fields.iter().map(|f| f.stable(tables, cx)).collect()
473 })
474 }
475
476 pub(crate) fn eval_target_usize(&self, mir_const: &MirConst) -> Result<u64, Error> {
478 self.with_cx(|tables, cx| {
479 let cnst = mir_const.internal(tables, cx.tcx);
480 cx.eval_target_usize(cnst)
481 })
482 }
483
484 pub(crate) fn eval_target_usize_ty(&self, ty_const: &TyConst) -> Result<u64, Error> {
485 self.with_cx(|tables, cx| {
486 let cnst = ty_const.internal(tables, cx.tcx);
487 cx.eval_target_usize_ty(cnst)
488 })
489 }
490
491 pub(crate) fn try_new_const_zst(&self, ty: Ty) -> Result<MirConst, Error> {
493 self.with_cx(|tables, cx| {
494 let ty_internal = ty.internal(tables, cx.tcx);
495 cx.try_new_const_zst(ty_internal).map(|cnst| cnst.stable(tables, cx))
496 })
497 }
498
499 pub(crate) fn new_const_str(&self, value: &str) -> MirConst {
501 self.with_cx(|tables, cx| cx.new_const_str(value).stable(tables, cx))
502 }
503
504 pub(crate) fn new_const_bool(&self, value: bool) -> MirConst {
506 self.with_cx(|tables, cx| cx.new_const_bool(value).stable(tables, cx))
507 }
508
509 pub(crate) fn try_new_const_uint(
511 &self,
512 value: u128,
513 uint_ty: UintTy,
514 ) -> Result<MirConst, Error> {
515 self.with_cx(|tables, cx| {
516 let ty = cx.ty_new_uint(uint_ty.internal(tables, cx.tcx));
517 cx.try_new_const_uint(value, ty).map(|cnst| cnst.stable(tables, cx))
518 })
519 }
520
521 pub(crate) fn try_new_ty_const_uint(
522 &self,
523 value: u128,
524 uint_ty: UintTy,
525 ) -> Result<TyConst, Error> {
526 self.with_cx(|tables, cx| {
527 let ty = cx.ty_new_uint(uint_ty.internal(tables, cx.tcx));
528 cx.try_new_ty_const_uint(value, ty).map(|cnst| cnst.stable(tables, cx))
529 })
530 }
531
532 pub(crate) fn new_rigid_ty(&self, kind: RigidTy) -> Ty {
534 self.with_cx(|tables, cx| {
535 let internal_kind = kind.internal(tables, cx.tcx);
536 cx.new_rigid_ty(internal_kind).stable(tables, cx)
537 })
538 }
539
540 pub(crate) fn new_box_ty(&self, ty: Ty) -> Ty {
542 self.with_cx(|tables, cx| {
543 let inner = ty.internal(tables, cx.tcx);
544 cx.new_box_ty(inner).stable(tables, cx)
545 })
546 }
547
548 pub(crate) fn def_ty(&self, item: DefId) -> Ty {
550 self.with_cx(|tables, cx| {
551 let inner = item.internal(tables, cx.tcx);
552 cx.def_ty(inner).stable(tables, cx)
553 })
554 }
555
556 pub(crate) fn def_ty_with_args(&self, item: DefId, args: &GenericArgs) -> Ty {
558 self.with_cx(|tables, cx| {
559 let inner = item.internal(tables, cx.tcx);
560 let args_ref = args.internal(tables, cx.tcx);
561 cx.def_ty_with_args(inner, args_ref).stable(tables, cx)
562 })
563 }
564
565 pub(crate) fn mir_const_pretty(&self, cnst: &MirConst) -> String {
567 self.with_cx(|tables, cx| cnst.internal(tables, cx.tcx).to_string())
568 }
569
570 pub(crate) fn span_of_a_def(&self, def_id: DefId) -> Span {
572 self.with_cx(|tables, cx| {
573 let did = tables[def_id];
574 cx.span_of_a_def(did).stable(tables, cx)
575 })
576 }
577
578 pub(crate) fn ty_const_pretty(&self, ct: TyConstId) -> String {
579 self.with_cx(|tables, cx| cx.ty_const_pretty(tables.ty_consts[ct]))
580 }
581
582 pub(crate) fn ty_pretty(&self, ty: Ty) -> String {
584 self.with_cx(|tables, cx| cx.ty_pretty(tables.types[ty]))
585 }
586
587 pub(crate) fn ty_kind(&self, ty: Ty) -> TyKind {
589 self.with_cx(|tables, cx| cx.ty_kind(tables.types[ty]).stable(tables, cx))
590 }
591
592 pub(crate) fn rigid_ty_discriminant_ty(&self, ty: &RigidTy) -> Ty {
594 self.with_cx(|tables, cx| {
595 let internal_kind = ty.internal(tables, cx.tcx);
596 cx.rigid_ty_discriminant_ty(internal_kind).stable(tables, cx)
597 })
598 }
599
600 pub(crate) fn instance_body(&self, instance: InstanceDef) -> Option<Body> {
602 self.with_cx(|tables, cx| {
603 let instance = tables.instances[instance];
604 cx.instance_body(instance).map(|body| body.stable(tables, cx))
605 })
606 }
607
608 pub(crate) fn instance_ty(&self, instance: InstanceDef) -> Ty {
610 self.with_cx(|tables, cx| {
611 let instance = tables.instances[instance];
612 cx.instance_ty(instance).stable(tables, cx)
613 })
614 }
615
616 pub(crate) fn instance_args(&self, def: InstanceDef) -> GenericArgs {
618 self.with_cx(|tables, cx| {
619 let instance = tables.instances[def];
620 cx.instance_args(instance).stable(tables, cx)
621 })
622 }
623
624 pub(crate) fn instance_def_id(&self, instance: InstanceDef) -> DefId {
626 self.with_cx(|tables, cx| {
627 let instance = tables.instances[instance];
628 cx.instance_def_id(instance, tables)
629 })
630 }
631
632 pub(crate) fn instance_mangled_name(&self, instance: InstanceDef) -> Symbol {
634 self.with_cx(|tables, cx| {
635 let instance = tables.instances[instance];
636 cx.instance_mangled_name(instance)
637 })
638 }
639
640 pub(crate) fn is_empty_drop_shim(&self, def: InstanceDef) -> bool {
642 self.with_cx(|tables, cx| {
643 let instance = tables.instances[def];
644 cx.is_empty_drop_shim(instance)
645 })
646 }
647
648 pub(crate) fn mono_instance(&self, def_id: DefId) -> Instance {
651 self.with_cx(|tables, cx| {
652 let did = tables[def_id];
653 cx.mono_instance(did).stable(tables, cx)
654 })
655 }
656
657 pub(crate) fn requires_monomorphization(&self, def_id: DefId) -> bool {
659 self.with_cx(|tables, cx| {
660 let did = tables[def_id];
661 cx.requires_monomorphization(did)
662 })
663 }
664
665 pub(crate) fn resolve_instance(&self, def: FnDef, args: &GenericArgs) -> Option<Instance> {
667 self.with_cx(|tables, cx| {
668 let def_id = def.0.internal(tables, cx.tcx);
669 let args_ref = args.internal(tables, cx.tcx);
670 cx.resolve_instance(def_id, args_ref).map(|inst| inst.stable(tables, cx))
671 })
672 }
673
674 pub(crate) fn resolve_drop_in_place(&self, ty: Ty) -> Instance {
676 self.with_cx(|tables, cx| {
677 let internal_ty = ty.internal(tables, cx.tcx);
678
679 cx.resolve_drop_in_place(internal_ty).stable(tables, cx)
680 })
681 }
682
683 pub(crate) fn resolve_for_fn_ptr(&self, def: FnDef, args: &GenericArgs) -> Option<Instance> {
685 self.with_cx(|tables, cx| {
686 let def_id = def.0.internal(tables, cx.tcx);
687 let args_ref = args.internal(tables, cx.tcx);
688 cx.resolve_for_fn_ptr(def_id, args_ref).stable(tables, cx)
689 })
690 }
691
692 pub(crate) fn resolve_closure(
694 &self,
695 def: ClosureDef,
696 args: &GenericArgs,
697 kind: ClosureKind,
698 ) -> Option<Instance> {
699 self.with_cx(|tables, cx| {
700 let def_id = def.0.internal(tables, cx.tcx);
701 let args_ref = args.internal(tables, cx.tcx);
702 let closure_kind = kind.internal(tables, cx.tcx);
703 cx.resolve_closure(def_id, args_ref, closure_kind).map(|inst| inst.stable(tables, cx))
704 })
705 }
706
707 pub(crate) fn eval_static_initializer(&self, def: StaticDef) -> Result<Allocation, Error> {
709 self.with_cx(|tables, cx| {
710 let def_id = def.0.internal(tables, cx.tcx);
711
712 cx.eval_static_initializer(def_id).stable(tables, cx)
713 })
714 }
715
716 pub(crate) fn eval_instance(
718 &self,
719 def: InstanceDef,
720 const_ty: Ty,
721 ) -> Result<Allocation, Error> {
722 self.with_cx(|tables, cx| {
723 let instance = tables.instances[def];
724 let const_ty = const_ty.internal(tables, cx.tcx);
725 cx.eval_instance(instance)
726 .map(|const_val| alloc::try_new_allocation(const_ty, const_val, tables, cx))
727 .map_err(|e| e.stable(tables, cx))?
728 })
729 }
730
731 pub(crate) fn global_alloc(&self, id: AllocId) -> GlobalAlloc {
733 self.with_cx(|tables, cx| {
734 let alloc_id = id.internal(tables, cx.tcx);
735 cx.global_alloc(alloc_id).stable(tables, cx)
736 })
737 }
738
739 pub(crate) fn vtable_allocation(&self, global_alloc: &GlobalAlloc) -> Option<AllocId> {
741 self.with_cx(|tables, cx| {
742 let GlobalAlloc::VTable(ty, trait_ref) = global_alloc else {
743 return None;
744 };
745 let ty = ty.internal(tables, cx.tcx);
746 let trait_ref = trait_ref.internal(tables, cx.tcx);
747 let alloc_id = cx.vtable_allocation(ty, trait_ref);
748 Some(alloc_id.stable(tables, cx))
749 })
750 }
751
752 pub(crate) fn krate(&self, def_id: DefId) -> Crate {
753 self.with_cx(|tables, cx| smir_crate(cx, tables[def_id].krate))
754 }
755
756 pub(crate) fn instance_name(&self, def: InstanceDef, trimmed: bool) -> Symbol {
757 self.with_cx(|tables, cx| {
758 let instance = tables.instances[def];
759 cx.instance_name(instance, trimmed)
760 })
761 }
762
763 pub(crate) fn target_info(&self) -> MachineInfo {
765 self.with_cx(|tables, cx| MachineInfo {
766 endian: cx.target_endian().stable(tables, cx),
767 pointer_width: MachineSize::from_bits(cx.target_pointer_size()),
768 })
769 }
770
771 pub(crate) fn instance_abi(&self, def: InstanceDef) -> Result<FnAbi, Error> {
773 self.with_cx(|tables, cx| {
774 let instance = tables.instances[def];
775 cx.instance_abi(instance).map(|fn_abi| fn_abi.stable(tables, cx))
776 })
777 }
778
779 pub(crate) fn fn_ptr_abi(&self, fn_ptr: PolyFnSig) -> Result<FnAbi, Error> {
781 self.with_cx(|tables, cx| {
782 let sig = fn_ptr.internal(tables, cx.tcx);
783 cx.fn_ptr_abi(sig).map(|fn_abi| fn_abi.stable(tables, cx))
784 })
785 }
786
787 pub(crate) fn ty_layout(&self, ty: Ty) -> Result<Layout, Error> {
789 self.with_cx(|tables, cx| {
790 let internal_ty = ty.internal(tables, cx.tcx);
791 cx.ty_layout(internal_ty).map(|layout| layout.stable(tables, cx))
792 })
793 }
794
795 pub(crate) fn layout_shape(&self, id: Layout) -> LayoutShape {
797 self.with_cx(|tables, cx| id.internal(tables, cx.tcx).0.stable(tables, cx))
798 }
799
800 pub(crate) fn place_pretty(&self, place: &Place) -> String {
802 self.with_cx(|tables, cx| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}",
place.internal(tables, cx.tcx)))
})format!("{:?}", place.internal(tables, cx.tcx)))
803 }
804
805 pub(crate) fn binop_ty(&self, bin_op: BinOp, rhs: Ty, lhs: Ty) -> Ty {
807 self.with_cx(|tables, cx| {
808 let rhs_internal = rhs.internal(tables, cx.tcx);
809 let lhs_internal = lhs.internal(tables, cx.tcx);
810 let bin_op_internal = bin_op.internal(tables, cx.tcx);
811 cx.binop_ty(bin_op_internal, rhs_internal, lhs_internal).stable(tables, cx)
812 })
813 }
814
815 pub(crate) fn unop_ty(&self, un_op: UnOp, arg: Ty) -> Ty {
817 self.with_cx(|tables, cx| {
818 let un_op = un_op.internal(tables, cx.tcx);
819 let arg = arg.internal(tables, cx.tcx);
820 cx.unop_ty(un_op, arg).stable(tables, cx)
821 })
822 }
823
824 pub(crate) fn associated_items(&self, def_id: DefId) -> AssocItems {
826 self.with_cx(|tables, cx| {
827 let did = tables[def_id];
828 cx.associated_items(did).iter().map(|assoc| assoc.stable(tables, cx)).collect()
829 })
830 }
831
832 pub(crate) fn vtable_entries(&self, trait_ref: &TraitRef) -> Vec<VtblEntry> {
834 self.with_cx(|tables, cx| {
835 cx.vtable_entries(trait_ref.internal(tables, cx.tcx))
836 .iter()
837 .map(|v| v.stable(tables, cx))
838 .collect()
839 })
840 }
841
842 pub(crate) fn vtable_entry(&self, trait_ref: &TraitRef, idx: usize) -> Option<VtblEntry> {
846 self.with_cx(|tables, cx| {
847 cx.vtable_entry(trait_ref.internal(tables, cx.tcx), idx).stable(tables, cx)
848 })
849 }
850}
851
852static 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 ()>);
854
855#[cfg(feature = "rustc_internal")]
857pub(crate) fn run<'tcx, F, T>(interface: &CompilerInterface<'tcx>, f: F) -> Result<T, Error>
858where
859 F: FnOnce() -> T,
860{
861 if TLV.is_set() {
862 Err(Error::from("rustc_public already running"))
863 } else {
864 let ptr: *const () = (&raw const interface) as _;
865 TLV.set(&Cell::new(ptr), || Ok(f()))
866 }
867}
868
869pub(crate) fn with<R>(f: impl for<'tcx> FnOnce(&CompilerInterface<'tcx>) -> R) -> R {
874 if !TLV.is_set() {
::core::panicking::panic("assertion failed: TLV.is_set()")
};assert!(TLV.is_set());
875 TLV.with(|tlv| {
876 let ptr = tlv.get();
877 if !!ptr.is_null() {
::core::panicking::panic("assertion failed: !ptr.is_null()")
};assert!(!ptr.is_null());
878 f(unsafe { *(ptr as *const &CompilerInterface<'_>) })
879 })
880}
881
882fn smir_crate<'tcx>(
883 cx: &CompilerCtxt<'tcx, BridgeTys>,
884 crate_num: rustc_span::def_id::CrateNum,
885) -> Crate {
886 let name = cx.crate_name(crate_num);
887 let is_local = cx.crate_is_local(crate_num);
888 let id = CrateNum(cx.crate_num_id(crate_num), ThreadLocalIndex);
889 {
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:889",
"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(889u32),
::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");
890 Crate { id, name, is_local }
891}