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_statics(&self, crate_num: CrateNum) -> Vec<StaticDef> {
141 self.with_cx(|tables, cx| {
142 let krate = crate_num.internal(tables, cx.tcx);
143 cx.crate_statics(krate).iter().map(|did| tables.static_def(*did)).collect()
144 })
145 }
146
147 pub(crate) fn foreign_module(&self, mod_def: ForeignModuleDef) -> ForeignModule {
148 self.with_cx(|tables, cx| {
149 let did = tables[mod_def.def_id()];
150 cx.foreign_module(did).stable(tables, cx)
151 })
152 }
153
154 pub(crate) fn foreign_items(&self, mod_def: ForeignModuleDef) -> Vec<ForeignDef> {
155 self.with_cx(|tables, cx| {
156 let did = tables[mod_def.def_id()];
157 cx.foreign_items(did).iter().map(|did| tables.foreign_def(*did)).collect()
158 })
159 }
160
161 pub(crate) fn all_trait_decls(&self) -> TraitDecls {
162 self.with_cx(|tables, cx| cx.all_trait_decls().map(|did| tables.trait_def(did)).collect())
163 }
164
165 pub(crate) fn trait_decls(&self, crate_num: CrateNum) -> TraitDecls {
166 self.with_cx(|tables, cx| {
167 let krate = crate_num.internal(tables, cx.tcx);
168 cx.trait_decls(krate).iter().map(|did| tables.trait_def(*did)).collect()
169 })
170 }
171
172 pub(crate) fn trait_decl(&self, trait_def: &TraitDef) -> TraitDecl {
173 self.with_cx(|tables, cx| {
174 let did = tables[trait_def.0];
175 cx.trait_decl(did).stable(tables, cx)
176 })
177 }
178
179 pub(crate) fn all_trait_impls(&self) -> ImplTraitDecls {
180 self.with_cx(|tables, cx| {
181 cx.all_trait_impls().iter().map(|did| tables.impl_def(*did)).collect()
182 })
183 }
184
185 pub(crate) fn trait_impls(&self, crate_num: CrateNum) -> ImplTraitDecls {
186 self.with_cx(|tables, cx| {
187 let krate = crate_num.internal(tables, cx.tcx);
188 cx.trait_impls(krate).iter().map(|did| tables.impl_def(*did)).collect()
189 })
190 }
191
192 pub(crate) fn trait_impl(&self, trait_impl: &ImplDef) -> ImplTrait {
193 self.with_cx(|tables, cx| {
194 let did = tables[trait_impl.0];
195 cx.trait_impl(did).stable(tables, cx)
196 })
197 }
198
199 pub(crate) fn generics_of(&self, def_id: DefId) -> Generics {
200 self.with_cx(|tables, cx| {
201 let did = tables[def_id];
202 cx.generics_of(did).stable(tables, cx)
203 })
204 }
205
206 pub(crate) fn predicates_of(&self, def_id: DefId) -> GenericPredicates {
207 self.with_cx(|tables, cx| {
208 let did = tables[def_id];
209 let (parent, kinds) = cx.predicates_of(did);
210 crate::ty::GenericPredicates {
211 parent: parent.map(|did| tables.trait_def(did)),
212 predicates: kinds
213 .iter()
214 .map(|(kind, span)| (kind.stable(tables, cx), span.stable(tables, cx)))
215 .collect(),
216 }
217 })
218 }
219
220 pub(crate) fn explicit_predicates_of(&self, def_id: DefId) -> GenericPredicates {
221 self.with_cx(|tables, cx| {
222 let did = tables[def_id];
223 let (parent, kinds) = cx.explicit_predicates_of(did);
224 crate::ty::GenericPredicates {
225 parent: parent.map(|did| tables.trait_def(did)),
226 predicates: kinds
227 .iter()
228 .map(|(kind, span)| (kind.stable(tables, cx), span.stable(tables, cx)))
229 .collect(),
230 }
231 })
232 }
233
234 pub(crate) fn local_crate(&self) -> Crate {
236 self.with_cx(|_, cx| smir_crate(cx, cx.local_crate_num()))
237 }
238
239 pub(crate) fn external_crates(&self) -> Vec<Crate> {
241 self.with_cx(|_, cx| {
242 cx.external_crates().iter().map(|crate_num| smir_crate(cx, *crate_num)).collect()
243 })
244 }
245
246 pub(crate) fn find_crates(&self, name: &str) -> Vec<Crate> {
248 self.with_cx(|_, cx| {
249 cx.find_crates(name).iter().map(|crate_num| smir_crate(cx, *crate_num)).collect()
250 })
251 }
252
253 pub(crate) fn def_name(&self, def_id: DefId, trimmed: bool) -> Symbol {
255 self.with_cx(|tables, cx| {
256 let did = tables[def_id];
257 cx.def_name(did, trimmed)
258 })
259 }
260
261 pub(crate) fn def_parent(&self, def_id: DefId) -> Option<DefId> {
263 self.with_cx(|tables, cx| {
264 let did = tables[def_id];
265 cx.def_parent(did).map(|did| tables.create_def_id(did))
266 })
267 }
268
269 pub(crate) fn tool_attrs(&self, def_id: DefId, attr: &[Symbol]) -> Vec<Attribute> {
277 self.with_cx(|tables, cx| {
278 let did = tables[def_id];
279 cx.tool_attrs(did, attr)
280 .into_iter()
281 .map(|(attr_str, span)| Attribute::new(attr_str, span.stable(tables, cx)))
282 .collect()
283 })
284 }
285
286 pub(crate) fn all_tool_attrs(&self, def_id: DefId) -> Vec<Attribute> {
288 self.with_cx(|tables, cx| {
289 let did = tables[def_id];
290 cx.all_tool_attrs(did)
291 .into_iter()
292 .map(|(attr_str, span)| Attribute::new(attr_str, span.stable(tables, cx)))
293 .collect()
294 })
295 }
296
297 pub(crate) fn span_to_string(&self, span: Span) -> String {
299 self.with_cx(|tables, cx| {
300 let sp = tables.spans[span];
301 cx.span_to_string(sp)
302 })
303 }
304
305 pub(crate) fn get_filename(&self, span: &Span) -> Filename {
307 self.with_cx(|tables, cx| {
308 let sp = tables.spans[*span];
309 cx.get_filename(sp)
310 })
311 }
312
313 pub(crate) fn get_lines(&self, span: &Span) -> LineInfo {
315 self.with_cx(|tables, cx| {
316 let sp = tables.spans[*span];
317 let lines = cx.get_lines(sp);
318 LineInfo::from(lines)
319 })
320 }
321
322 pub(crate) fn item_kind(&self, item: CrateItem) -> ItemKind {
324 self.with_cx(|tables, cx| {
325 let did = tables[item.0];
326 new_item_kind(cx.def_kind(did))
327 })
328 }
329
330 pub(crate) fn is_foreign_item(&self, item: DefId) -> bool {
332 self.with_cx(|tables, cx| {
333 let did = tables[item];
334 cx.is_foreign_item(did)
335 })
336 }
337
338 pub(crate) fn foreign_item_kind(&self, def: ForeignDef) -> ForeignItemKind {
340 self.with_cx(|tables, cx| {
341 let def_id = tables[def.def_id()];
342 let def_kind = cx.foreign_item_kind(def_id);
343 match def_kind {
344 DefKind::Fn => ForeignItemKind::Fn(tables.fn_def(def_id)),
345 DefKind::Static { .. } => ForeignItemKind::Static(tables.static_def(def_id)),
346 DefKind::ForeignTy => {
347 use rustc_public_bridge::context::TyHelpers;
348 ForeignItemKind::Type(tables.intern_ty(cx.new_foreign(def_id)))
349 }
350 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),
351 }
352 })
353 }
354
355 pub(crate) fn adt_kind(&self, def: AdtDef) -> AdtKind {
357 self.with_cx(|tables, cx| cx.adt_kind(def.internal(tables, cx.tcx)).stable(tables, cx))
358 }
359
360 pub(crate) fn adt_is_box(&self, def: AdtDef) -> bool {
362 self.with_cx(|tables, cx| cx.adt_is_box(def.internal(tables, cx.tcx)))
363 }
364
365 pub(crate) fn adt_is_simd(&self, def: AdtDef) -> bool {
367 self.with_cx(|tables, cx| cx.adt_is_simd(def.internal(tables, cx.tcx)))
368 }
369
370 pub(crate) fn adt_is_cstr(&self, def: AdtDef) -> bool {
372 self.with_cx(|tables, cx| cx.adt_is_cstr(def.0.internal(tables, cx.tcx)))
373 }
374
375 pub(crate) fn adt_repr(&self, def: AdtDef) -> ReprOptions {
377 self.with_cx(|tables, cx| cx.adt_repr(def.internal(tables, cx.tcx)).stable(tables, cx))
378 }
379
380 pub(crate) fn fn_sig(&self, def: FnDef, args: &GenericArgs) -> PolyFnSig {
382 self.with_cx(|tables, cx| {
383 let def_id = def.0.internal(tables, cx.tcx);
384 let args_ref = args.internal(tables, cx.tcx);
385 cx.fn_sig(def_id, args_ref).stable(tables, cx)
386 })
387 }
388
389 pub(crate) fn constness(&self, def: FnDef) -> Constness {
391 self.with_cx(|tables, cx| {
392 let def_id = def.0.internal(tables, cx.tcx);
393 cx.constness(def_id).stable(tables, cx)
394 })
395 }
396
397 pub(crate) fn asyncness(&self, def: FnDef) -> Asyncness {
399 self.with_cx(|tables, cx| {
400 let def_id = def.0.internal(tables, cx.tcx);
401 cx.asyncness(def_id).stable(tables, cx)
402 })
403 }
404
405 pub(crate) fn intrinsic(&self, item: DefId) -> Option<IntrinsicDef> {
407 self.with_cx(|tables, cx| {
408 let def_id = item.internal(tables, cx.tcx);
409 cx.intrinsic(def_id).map(|_| IntrinsicDef(item))
410 })
411 }
412
413 pub(crate) fn intrinsic_name(&self, def: IntrinsicDef) -> Symbol {
415 self.with_cx(|tables, cx| {
416 let def_id = def.0.internal(tables, cx.tcx);
417 cx.intrinsic_name(def_id)
418 })
419 }
420
421 pub(crate) fn closure_sig(&self, args: &GenericArgs) -> PolyFnSig {
423 self.with_cx(|tables, cx| {
424 let args_ref = args.internal(tables, cx.tcx);
425 cx.closure_sig(args_ref).stable(tables, cx)
426 })
427 }
428
429 pub(crate) fn adt_variants_len(&self, def: AdtDef) -> usize {
431 self.with_cx(|tables, cx| cx.adt_variants_len(def.internal(tables, cx.tcx)))
432 }
433
434 pub(crate) fn adt_discr_for_variant(&self, adt: AdtDef, variant: VariantIdx) -> Discr {
436 self.with_cx(|tables, cx| {
437 cx.adt_discr_for_variant(adt.internal(tables, cx.tcx), variant.internal(tables, cx.tcx))
438 .stable(tables, cx)
439 })
440 }
441
442 pub(crate) fn coroutine_discr_for_variant(
444 &self,
445 coroutine: CoroutineDef,
446 args: &GenericArgs,
447 variant: VariantIdx,
448 ) -> Discr {
449 self.with_cx(|tables, cx| {
450 let tcx = cx.tcx;
451 let def = coroutine.def_id().internal(tables, tcx);
452 let args_ref = args.internal(tables, tcx);
453 cx.coroutine_discr_for_variant(def, args_ref, variant.internal(tables, tcx))
454 .stable(tables, cx)
455 })
456 }
457
458 pub(crate) fn variant_name(&self, def: VariantDef) -> Symbol {
460 self.with_cx(|tables, cx| cx.variant_name(def.internal(tables, cx.tcx)))
461 }
462
463 pub(crate) fn variant_fields(&self, def: VariantDef) -> Vec<FieldDef> {
464 self.with_cx(|tables, cx| {
465 def.internal(tables, cx.tcx).fields.iter().map(|f| f.stable(tables, cx)).collect()
466 })
467 }
468
469 pub(crate) fn eval_target_usize(&self, mir_const: &MirConst) -> Result<u64, Error> {
471 self.with_cx(|tables, cx| {
472 let cnst = mir_const.internal(tables, cx.tcx);
473 cx.eval_target_usize(cnst)
474 })
475 }
476
477 pub(crate) fn eval_target_usize_ty(&self, ty_const: &TyConst) -> Result<u64, Error> {
478 self.with_cx(|tables, cx| {
479 let cnst = ty_const.internal(tables, cx.tcx);
480 cx.eval_target_usize_ty(cnst)
481 })
482 }
483
484 pub(crate) fn try_new_const_zst(&self, ty: Ty) -> Result<MirConst, Error> {
486 self.with_cx(|tables, cx| {
487 let ty_internal = ty.internal(tables, cx.tcx);
488 cx.try_new_const_zst(ty_internal).map(|cnst| cnst.stable(tables, cx))
489 })
490 }
491
492 pub(crate) fn new_const_str(&self, value: &str) -> MirConst {
494 self.with_cx(|tables, cx| cx.new_const_str(value).stable(tables, cx))
495 }
496
497 pub(crate) fn new_const_bool(&self, value: bool) -> MirConst {
499 self.with_cx(|tables, cx| cx.new_const_bool(value).stable(tables, cx))
500 }
501
502 pub(crate) fn try_new_const_uint(
504 &self,
505 value: u128,
506 uint_ty: UintTy,
507 ) -> Result<MirConst, Error> {
508 self.with_cx(|tables, cx| {
509 let ty = cx.ty_new_uint(uint_ty.internal(tables, cx.tcx));
510 cx.try_new_const_uint(value, ty).map(|cnst| cnst.stable(tables, cx))
511 })
512 }
513
514 pub(crate) fn try_new_ty_const_uint(
515 &self,
516 value: u128,
517 uint_ty: UintTy,
518 ) -> Result<TyConst, Error> {
519 self.with_cx(|tables, cx| {
520 let ty = cx.ty_new_uint(uint_ty.internal(tables, cx.tcx));
521 cx.try_new_ty_const_uint(value, ty).map(|cnst| cnst.stable(tables, cx))
522 })
523 }
524
525 pub(crate) fn new_rigid_ty(&self, kind: RigidTy) -> Ty {
527 self.with_cx(|tables, cx| {
528 let internal_kind = kind.internal(tables, cx.tcx);
529 cx.new_rigid_ty(internal_kind).stable(tables, cx)
530 })
531 }
532
533 pub(crate) fn new_box_ty(&self, ty: Ty) -> Ty {
535 self.with_cx(|tables, cx| {
536 let inner = ty.internal(tables, cx.tcx);
537 cx.new_box_ty(inner).stable(tables, cx)
538 })
539 }
540
541 pub(crate) fn def_ty(&self, item: DefId) -> Ty {
543 self.with_cx(|tables, cx| {
544 let inner = item.internal(tables, cx.tcx);
545 cx.def_ty(inner).stable(tables, cx)
546 })
547 }
548
549 pub(crate) fn def_ty_with_args(&self, item: DefId, args: &GenericArgs) -> Ty {
551 self.with_cx(|tables, cx| {
552 let inner = item.internal(tables, cx.tcx);
553 let args_ref = args.internal(tables, cx.tcx);
554 cx.def_ty_with_args(inner, args_ref).stable(tables, cx)
555 })
556 }
557
558 pub(crate) fn mir_const_pretty(&self, cnst: &MirConst) -> String {
560 self.with_cx(|tables, cx| cnst.internal(tables, cx.tcx).to_string())
561 }
562
563 pub(crate) fn span_of_a_def(&self, def_id: DefId) -> Span {
565 self.with_cx(|tables, cx| {
566 let did = tables[def_id];
567 cx.span_of_a_def(did).stable(tables, cx)
568 })
569 }
570
571 pub(crate) fn ty_const_pretty(&self, ct: TyConstId) -> String {
572 self.with_cx(|tables, cx| cx.ty_const_pretty(tables.ty_consts[ct]))
573 }
574
575 pub(crate) fn ty_pretty(&self, ty: Ty) -> String {
577 self.with_cx(|tables, cx| cx.ty_pretty(tables.types[ty]))
578 }
579
580 pub(crate) fn ty_kind(&self, ty: Ty) -> TyKind {
582 self.with_cx(|tables, cx| cx.ty_kind(tables.types[ty]).stable(tables, cx))
583 }
584
585 pub(crate) fn rigid_ty_discriminant_ty(&self, ty: &RigidTy) -> Ty {
587 self.with_cx(|tables, cx| {
588 let internal_kind = ty.internal(tables, cx.tcx);
589 cx.rigid_ty_discriminant_ty(internal_kind).stable(tables, cx)
590 })
591 }
592
593 pub(crate) fn instance_body(&self, instance: InstanceDef) -> Option<Body> {
595 self.with_cx(|tables, cx| {
596 let instance = tables.instances[instance];
597 cx.instance_body(instance).map(|body| body.stable(tables, cx))
598 })
599 }
600
601 pub(crate) fn instance_ty(&self, instance: InstanceDef) -> Ty {
603 self.with_cx(|tables, cx| {
604 let instance = tables.instances[instance];
605 cx.instance_ty(instance).stable(tables, cx)
606 })
607 }
608
609 pub(crate) fn instance_args(&self, def: InstanceDef) -> GenericArgs {
611 self.with_cx(|tables, cx| {
612 let instance = tables.instances[def];
613 cx.instance_args(instance).stable(tables, cx)
614 })
615 }
616
617 pub(crate) fn instance_def_id(&self, instance: InstanceDef) -> DefId {
619 self.with_cx(|tables, cx| {
620 let instance = tables.instances[instance];
621 cx.instance_def_id(instance, tables)
622 })
623 }
624
625 pub(crate) fn instance_mangled_name(&self, instance: InstanceDef) -> Symbol {
627 self.with_cx(|tables, cx| {
628 let instance = tables.instances[instance];
629 cx.instance_mangled_name(instance)
630 })
631 }
632
633 pub(crate) fn is_empty_drop_shim(&self, def: InstanceDef) -> bool {
635 self.with_cx(|tables, cx| {
636 let instance = tables.instances[def];
637 cx.is_empty_drop_shim(instance)
638 })
639 }
640
641 pub(crate) fn mono_instance(&self, def_id: DefId) -> Instance {
644 self.with_cx(|tables, cx| {
645 let did = tables[def_id];
646 cx.mono_instance(did).stable(tables, cx)
647 })
648 }
649
650 pub(crate) fn requires_monomorphization(&self, def_id: DefId) -> bool {
652 self.with_cx(|tables, cx| {
653 let did = tables[def_id];
654 cx.requires_monomorphization(did)
655 })
656 }
657
658 pub(crate) fn resolve_instance(&self, def: FnDef, args: &GenericArgs) -> Option<Instance> {
660 self.with_cx(|tables, cx| {
661 let def_id = def.0.internal(tables, cx.tcx);
662 let args_ref = args.internal(tables, cx.tcx);
663 cx.resolve_instance(def_id, args_ref).map(|inst| inst.stable(tables, cx))
664 })
665 }
666
667 pub(crate) fn resolve_drop_in_place(&self, ty: Ty) -> Instance {
669 self.with_cx(|tables, cx| {
670 let internal_ty = ty.internal(tables, cx.tcx);
671
672 cx.resolve_drop_in_place(internal_ty).stable(tables, cx)
673 })
674 }
675
676 pub(crate) fn resolve_for_fn_ptr(&self, def: FnDef, args: &GenericArgs) -> Option<Instance> {
678 self.with_cx(|tables, cx| {
679 let def_id = def.0.internal(tables, cx.tcx);
680 let args_ref = args.internal(tables, cx.tcx);
681 cx.resolve_for_fn_ptr(def_id, args_ref).stable(tables, cx)
682 })
683 }
684
685 pub(crate) fn resolve_closure(
687 &self,
688 def: ClosureDef,
689 args: &GenericArgs,
690 kind: ClosureKind,
691 ) -> Option<Instance> {
692 self.with_cx(|tables, cx| {
693 let def_id = def.0.internal(tables, cx.tcx);
694 let args_ref = args.internal(tables, cx.tcx);
695 let closure_kind = kind.internal(tables, cx.tcx);
696 cx.resolve_closure(def_id, args_ref, closure_kind).map(|inst| inst.stable(tables, cx))
697 })
698 }
699
700 pub(crate) fn eval_static_initializer(&self, def: StaticDef) -> Result<Allocation, Error> {
702 self.with_cx(|tables, cx| {
703 let def_id = def.0.internal(tables, cx.tcx);
704
705 cx.eval_static_initializer(def_id).stable(tables, cx)
706 })
707 }
708
709 pub(crate) fn eval_instance(
711 &self,
712 def: InstanceDef,
713 const_ty: Ty,
714 ) -> Result<Allocation, Error> {
715 self.with_cx(|tables, cx| {
716 let instance = tables.instances[def];
717 let const_ty = const_ty.internal(tables, cx.tcx);
718 cx.eval_instance(instance)
719 .map(|const_val| alloc::try_new_allocation(const_ty, const_val, tables, cx))
720 .map_err(|e| e.stable(tables, cx))?
721 })
722 }
723
724 pub(crate) fn global_alloc(&self, id: AllocId) -> GlobalAlloc {
726 self.with_cx(|tables, cx| {
727 let alloc_id = id.internal(tables, cx.tcx);
728 cx.global_alloc(alloc_id).stable(tables, cx)
729 })
730 }
731
732 pub(crate) fn vtable_allocation(&self, global_alloc: &GlobalAlloc) -> Option<AllocId> {
734 self.with_cx(|tables, cx| {
735 let GlobalAlloc::VTable(ty, trait_ref) = global_alloc else {
736 return None;
737 };
738 let ty = ty.internal(tables, cx.tcx);
739 let trait_ref = trait_ref.internal(tables, cx.tcx);
740 let alloc_id = cx.vtable_allocation(ty, trait_ref);
741 Some(alloc_id.stable(tables, cx))
742 })
743 }
744
745 pub(crate) fn krate(&self, def_id: DefId) -> Crate {
746 self.with_cx(|tables, cx| smir_crate(cx, tables[def_id].krate))
747 }
748
749 pub(crate) fn instance_name(&self, def: InstanceDef, trimmed: bool) -> Symbol {
750 self.with_cx(|tables, cx| {
751 let instance = tables.instances[def];
752 cx.instance_name(instance, trimmed)
753 })
754 }
755
756 pub(crate) fn target_info(&self) -> MachineInfo {
758 self.with_cx(|tables, cx| MachineInfo {
759 endian: cx.target_endian().stable(tables, cx),
760 pointer_width: MachineSize::from_bits(cx.target_pointer_size()),
761 })
762 }
763
764 pub(crate) fn instance_abi(&self, def: InstanceDef) -> Result<FnAbi, Error> {
766 self.with_cx(|tables, cx| {
767 let instance = tables.instances[def];
768 cx.instance_abi(instance).map(|fn_abi| fn_abi.stable(tables, cx))
769 })
770 }
771
772 pub(crate) fn fn_ptr_abi(&self, fn_ptr: PolyFnSig) -> Result<FnAbi, Error> {
774 self.with_cx(|tables, cx| {
775 let sig = fn_ptr.internal(tables, cx.tcx);
776 cx.fn_ptr_abi(sig).map(|fn_abi| fn_abi.stable(tables, cx))
777 })
778 }
779
780 pub(crate) fn ty_layout(&self, ty: Ty) -> Result<Layout, Error> {
782 self.with_cx(|tables, cx| {
783 let internal_ty = ty.internal(tables, cx.tcx);
784 cx.ty_layout(internal_ty).map(|layout| layout.stable(tables, cx))
785 })
786 }
787
788 pub(crate) fn layout_shape(&self, id: Layout) -> LayoutShape {
790 self.with_cx(|tables, cx| id.internal(tables, cx.tcx).0.stable(tables, cx))
791 }
792
793 pub(crate) fn place_pretty(&self, place: &Place) -> String {
795 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)))
796 }
797
798 pub(crate) fn binop_ty(&self, bin_op: BinOp, rhs: Ty, lhs: Ty) -> Ty {
800 self.with_cx(|tables, cx| {
801 let rhs_internal = rhs.internal(tables, cx.tcx);
802 let lhs_internal = lhs.internal(tables, cx.tcx);
803 let bin_op_internal = bin_op.internal(tables, cx.tcx);
804 cx.binop_ty(bin_op_internal, rhs_internal, lhs_internal).stable(tables, cx)
805 })
806 }
807
808 pub(crate) fn unop_ty(&self, un_op: UnOp, arg: Ty) -> Ty {
810 self.with_cx(|tables, cx| {
811 let un_op = un_op.internal(tables, cx.tcx);
812 let arg = arg.internal(tables, cx.tcx);
813 cx.unop_ty(un_op, arg).stable(tables, cx)
814 })
815 }
816
817 pub(crate) fn associated_items(&self, def_id: DefId) -> AssocItems {
819 self.with_cx(|tables, cx| {
820 let did = tables[def_id];
821 cx.associated_items(did).iter().map(|assoc| assoc.stable(tables, cx)).collect()
822 })
823 }
824
825 pub(crate) fn vtable_entries(&self, trait_ref: &TraitRef) -> Vec<VtblEntry> {
827 self.with_cx(|tables, cx| {
828 cx.vtable_entries(trait_ref.internal(tables, cx.tcx))
829 .iter()
830 .map(|v| v.stable(tables, cx))
831 .collect()
832 })
833 }
834
835 pub(crate) fn vtable_entry(&self, trait_ref: &TraitRef, idx: usize) -> Option<VtblEntry> {
839 self.with_cx(|tables, cx| {
840 cx.vtable_entry(trait_ref.internal(tables, cx.tcx), idx).stable(tables, cx)
841 })
842 }
843}
844
845static 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 ()>);
847
848#[cfg(feature = "rustc_internal")]
850pub(crate) fn run<'tcx, F, T>(interface: &CompilerInterface<'tcx>, f: F) -> Result<T, Error>
851where
852 F: FnOnce() -> T,
853{
854 if TLV.is_set() {
855 Err(Error::from("rustc_public already running"))
856 } else {
857 let ptr: *const () = (&raw const interface) as _;
858 TLV.set(&Cell::new(ptr), || Ok(f()))
859 }
860}
861
862pub(crate) fn with<R>(f: impl for<'tcx> FnOnce(&CompilerInterface<'tcx>) -> R) -> R {
867 if !TLV.is_set() {
::core::panicking::panic("assertion failed: TLV.is_set()")
};assert!(TLV.is_set());
868 TLV.with(|tlv| {
869 let ptr = tlv.get();
870 if !!ptr.is_null() {
::core::panicking::panic("assertion failed: !ptr.is_null()")
};assert!(!ptr.is_null());
871 f(unsafe { *(ptr as *const &CompilerInterface<'_>) })
872 })
873}
874
875fn smir_crate<'tcx>(
876 cx: &CompilerCtxt<'tcx, BridgeTys>,
877 crate_num: rustc_span::def_id::CrateNum,
878) -> Crate {
879 let name = cx.crate_name(crate_num);
880 let is_local = cx.crate_is_local(crate_num);
881 let id = CrateNum(cx.crate_num_id(crate_num), ThreadLocalIndex);
882 {
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:882",
"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(882u32),
::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");
883 Crate { id, name, is_local }
884}