1#![allow(rustc::usage_of_qualified_ty)]
4
5use std::iter;
6
7use rustc_abi::{Endian, Layout, ReprOptions};
8use rustc_hir::def::DefKind;
9use rustc_hir::{Attribute, LangItem};
10use rustc_middle::mir::interpret::{AllocId, ConstAllocation, ErrorHandled, GlobalAlloc, Scalar};
11use rustc_middle::mir::{BinOp, Body, Const as MirConst, ConstValue, UnOp};
12use rustc_middle::ty::layout::{FnAbiOf, LayoutOf};
13use rustc_middle::ty::print::{
14 with_forced_trimmed_paths, with_no_trimmed_paths, with_resolve_crate_name,
15};
16use rustc_middle::ty::util::Discr;
17use rustc_middle::ty::{
18 AdtDef, AdtKind, AssocItem, Binder, ClosureKind, CoroutineArgsExt, EarlyBinder,
19 ExistentialTraitRef, FnSig, GenericArgsRef, Instance, InstanceKind, IntrinsicDef, List,
20 PolyFnSig, ScalarInt, TraitDef, TraitRef, Ty, TyCtxt, TyKind, TypeVisitableExt, UintTy,
21 ValTree, VariantDef,
22};
23use rustc_middle::{mir, ty};
24use rustc_session::cstore::ForeignModule;
25use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
26use rustc_span::{FileNameDisplayPreference, Span, Symbol};
27use rustc_target::callconv::FnAbi;
28
29use super::{AllocRangeHelpers, CompilerCtxt, TyHelpers, TypingEnvHelpers};
30use crate::builder::BodyBuilder;
31use crate::{Bridge, Error, Tables, filter_def_ids};
32
33impl<'tcx, B: Bridge> TyHelpers<'tcx> for CompilerCtxt<'tcx, B> {
34 fn new_foreign(&self, def_id: DefId) -> ty::Ty<'tcx> {
35 ty::Ty::new_foreign(self.tcx, def_id)
36 }
37}
38
39impl<'tcx, B: Bridge> TypingEnvHelpers<'tcx> for CompilerCtxt<'tcx, B> {
40 fn fully_monomorphized(&self) -> ty::TypingEnv<'tcx> {
41 ty::TypingEnv::fully_monomorphized()
42 }
43}
44
45impl<'tcx, B: Bridge> AllocRangeHelpers<'tcx> for CompilerCtxt<'tcx, B> {
46 fn alloc_range(
47 &self,
48 offset: rustc_abi::Size,
49 size: rustc_abi::Size,
50 ) -> mir::interpret::AllocRange {
51 rustc_middle::mir::interpret::alloc_range(offset, size)
52 }
53}
54
55impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> {
56 pub fn lift<T: ty::Lift<TyCtxt<'tcx>>>(&self, value: T) -> Option<T::Lifted> {
57 self.tcx.lift(value)
58 }
59
60 pub fn adt_def(&self, def_id: DefId) -> AdtDef<'tcx> {
61 self.tcx.adt_def(def_id)
62 }
63
64 pub fn coroutine_movability(&self, def_id: DefId) -> ty::Movability {
65 self.tcx.coroutine_movability(def_id)
66 }
67
68 pub fn valtree_to_const_val(&self, key: ty::Value<'tcx>) -> ConstValue {
69 self.tcx.valtree_to_const_val(key)
70 }
71
72 pub(crate) fn instance_has_body(&self, instance: Instance<'tcx>) -> bool {
77 let def_id = instance.def_id();
78 self.item_has_body(def_id)
79 || !matches!(
80 instance.def,
81 ty::InstanceKind::Virtual(..)
82 | ty::InstanceKind::Intrinsic(..)
83 | ty::InstanceKind::Item(..)
84 )
85 }
86
87 pub(crate) fn item_has_body(&self, def_id: DefId) -> bool {
92 let must_override = if let Some(intrinsic) = self.tcx.intrinsic(def_id) {
93 intrinsic.must_be_overridden
94 } else {
95 false
96 };
97 !must_override && self.tcx.is_mir_available(def_id) && !self.tcx.is_trivial_const(def_id)
99 }
100
101 fn filter_fn_def(&self, def_id: DefId) -> Option<DefId> {
102 if matches!(self.tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) {
103 Some(def_id)
104 } else {
105 None
106 }
107 }
108
109 fn filter_static_def(&self, def_id: DefId) -> Option<DefId> {
110 matches!(self.tcx.def_kind(def_id), DefKind::Static { .. }).then(|| def_id)
111 }
112
113 pub fn target_endian(&self) -> Endian {
114 self.tcx.data_layout.endian
115 }
116
117 pub fn target_pointer_size(&self) -> usize {
118 self.tcx.data_layout.pointer_size().bits().try_into().unwrap()
119 }
120
121 pub fn entry_fn(&self) -> Option<DefId> {
122 Some(self.tcx.entry_fn(())?.0)
123 }
124
125 pub fn all_local_items(&self) -> Vec<DefId> {
127 self.tcx.mir_keys(()).iter().map(|item| item.to_def_id()).collect()
128 }
129
130 pub fn mir_body(&self, item: DefId) -> &'tcx Body<'tcx> {
133 self.tcx.instance_mir(InstanceKind::Item(item))
134 }
135
136 pub fn has_body(&self, def: DefId) -> bool {
138 self.item_has_body(def)
139 }
140
141 pub fn foreign_modules(&self, crate_num: CrateNum) -> Vec<DefId> {
142 self.tcx.foreign_modules(crate_num).keys().map(|mod_def_id| *mod_def_id).collect()
143 }
144
145 pub fn crate_functions(&self, crate_num: CrateNum) -> Vec<DefId> {
147 filter_def_ids(self.tcx, crate_num, |def_id| self.filter_fn_def(def_id))
148 }
149
150 pub fn crate_statics(&self, crate_num: CrateNum) -> Vec<DefId> {
152 filter_def_ids(self.tcx, crate_num, |def_id| self.filter_static_def(def_id))
153 }
154
155 pub fn foreign_module(&self, mod_def: DefId) -> &ForeignModule {
156 self.tcx.foreign_modules(mod_def.krate).get(&mod_def).unwrap()
157 }
158
159 pub fn foreign_items(&self, mod_def: DefId) -> Vec<DefId> {
160 self.tcx
161 .foreign_modules(mod_def.krate)
162 .get(&mod_def)
163 .unwrap()
164 .foreign_items
165 .iter()
166 .map(|item_def| *item_def)
167 .collect()
168 }
169
170 pub fn all_trait_decls(&self) -> impl Iterator<Item = DefId> {
171 self.tcx.all_traits_including_private()
172 }
173
174 pub fn trait_decls(&self, crate_num: CrateNum) -> Vec<DefId> {
175 self.tcx.traits(crate_num).iter().map(|trait_def_id| *trait_def_id).collect()
176 }
177
178 pub fn trait_decl(&self, trait_def: DefId) -> &'tcx TraitDef {
179 self.tcx.trait_def(trait_def)
180 }
181
182 pub fn all_trait_impls(&self) -> Vec<DefId> {
183 iter::once(LOCAL_CRATE)
184 .chain(self.tcx.crates(()).iter().copied())
185 .flat_map(|cnum| self.tcx.trait_impls_in_crate(cnum).iter())
186 .map(|impl_def_id| *impl_def_id)
187 .collect()
188 }
189
190 pub fn trait_impls(&self, crate_num: CrateNum) -> Vec<DefId> {
191 self.tcx.trait_impls_in_crate(crate_num).iter().map(|impl_def_id| *impl_def_id).collect()
192 }
193
194 pub fn trait_impl(&self, impl_def: DefId) -> EarlyBinder<'tcx, TraitRef<'tcx>> {
195 self.tcx.impl_trait_ref(impl_def)
196 }
197
198 pub fn generics_of(&self, def_id: DefId) -> &'tcx ty::Generics {
199 self.tcx.generics_of(def_id)
200 }
201
202 pub fn predicates_of(
203 &self,
204 def_id: DefId,
205 ) -> (Option<DefId>, Vec<(ty::PredicateKind<'tcx>, Span)>) {
206 let ty::GenericPredicates { parent, predicates } = self.tcx.predicates_of(def_id);
207 (
208 parent,
209 predicates
210 .iter()
211 .map(|(clause, span)| (clause.as_predicate().kind().skip_binder(), *span))
212 .collect(),
213 )
214 }
215
216 pub fn explicit_predicates_of(
217 &self,
218 def_id: DefId,
219 ) -> (Option<DefId>, Vec<(ty::PredicateKind<'tcx>, Span)>) {
220 let ty::GenericPredicates { parent, predicates } = self.tcx.explicit_predicates_of(def_id);
221 (
222 parent,
223 predicates
224 .iter()
225 .map(|(clause, span)| (clause.as_predicate().kind().skip_binder(), *span))
226 .collect(),
227 )
228 }
229
230 pub fn crate_name(&self, crate_num: CrateNum) -> String {
231 self.tcx.crate_name(crate_num).to_string()
232 }
233
234 pub fn crate_is_local(&self, crate_num: CrateNum) -> bool {
235 crate_num == LOCAL_CRATE
236 }
237
238 pub fn crate_num_id(&self, crate_num: CrateNum) -> usize {
239 crate_num.into()
240 }
241
242 pub fn local_crate_num(&self) -> CrateNum {
243 LOCAL_CRATE
244 }
245
246 pub fn external_crates(&self) -> Vec<CrateNum> {
248 self.tcx.crates(()).iter().map(|crate_num| *crate_num).collect()
249 }
250
251 pub fn find_crates(&self, name: &str) -> Vec<CrateNum> {
253 let crates: Vec<CrateNum> = [LOCAL_CRATE]
254 .iter()
255 .chain(self.tcx.crates(()).iter())
256 .filter_map(|crate_num| {
257 let crate_name = self.tcx.crate_name(*crate_num).to_string();
258 (name == crate_name).then(|| *crate_num)
259 })
260 .collect();
261 crates
262 }
263
264 pub fn def_name(&self, def_id: DefId, trimmed: bool) -> String {
266 if trimmed {
267 with_forced_trimmed_paths!(self.tcx.def_path_str(def_id))
268 } else {
269 with_resolve_crate_name!(with_no_trimmed_paths!(self.tcx.def_path_str(def_id)))
271 }
272 }
273
274 pub fn def_parent(&self, def_id: DefId) -> Option<DefId> {
276 self.tcx.opt_parent(def_id)
277 }
278
279 pub fn tool_attrs(&self, def_id: DefId, attr: &[String]) -> Vec<(String, Span)> {
287 let attr_name: Vec<_> = attr.iter().map(|seg| Symbol::intern(&seg)).collect();
288 self.tcx
289 .get_attrs_by_path(def_id, &attr_name)
290 .filter_map(|attribute| {
291 if let Attribute::Unparsed(u) = attribute {
292 let attr_str = rustc_hir_pretty::attribute_to_string(&self.tcx, attribute);
293 Some((attr_str, u.span))
294 } else {
295 None
296 }
297 })
298 .collect()
299 }
300
301 pub fn all_tool_attrs(&self, did: DefId) -> Vec<(String, Span)> {
303 let attrs_iter = if let Some(did) = did.as_local() {
304 self.tcx.hir_attrs(self.tcx.local_def_id_to_hir_id(did)).iter()
305 } else {
306 self.tcx.attrs_for_def(did).iter()
307 };
308 attrs_iter
309 .filter_map(|attribute| {
310 if let Attribute::Unparsed(u) = attribute {
311 let attr_str = rustc_hir_pretty::attribute_to_string(&self.tcx, attribute);
312 Some((attr_str, u.span))
313 } else {
314 None
315 }
316 })
317 .collect()
318 }
319
320 pub fn span_to_string(&self, span: Span) -> String {
322 self.tcx.sess.source_map().span_to_diagnostic_string(span)
323 }
324
325 pub fn get_filename(&self, span: Span) -> String {
327 self.tcx
328 .sess
329 .source_map()
330 .span_to_filename(span)
331 .display(FileNameDisplayPreference::Local)
332 .to_string()
333 }
334
335 pub fn get_lines(&self, span: Span) -> (usize, usize, usize, usize) {
337 let lines = &self.tcx.sess.source_map().span_to_location_info(span);
338 (lines.1, lines.2, lines.3, lines.4)
339 }
340
341 pub fn def_kind(&self, item: DefId) -> DefKind {
343 self.tcx.def_kind(item)
344 }
345
346 pub fn is_foreign_item(&self, item: DefId) -> bool {
348 self.tcx.is_foreign_item(item)
349 }
350
351 pub fn foreign_item_kind(&self, def_id: DefId) -> DefKind {
353 self.tcx.def_kind(def_id)
354 }
355
356 pub fn adt_kind(&self, def: AdtDef<'tcx>) -> AdtKind {
358 def.adt_kind()
359 }
360
361 pub fn adt_is_box(&self, def: AdtDef<'tcx>) -> bool {
363 def.is_box()
364 }
365
366 pub fn adt_is_simd(&self, def: AdtDef<'tcx>) -> bool {
368 def.repr().simd()
369 }
370
371 pub fn adt_is_cstr(&self, def_id: DefId) -> bool {
373 self.tcx.is_lang_item(def_id, LangItem::CStr)
374 }
375
376 pub fn adt_repr(&self, def: AdtDef<'tcx>) -> ReprOptions {
378 def.repr()
379 }
380
381 pub fn fn_sig(
383 &self,
384 def_id: DefId,
385 args_ref: GenericArgsRef<'tcx>,
386 ) -> Binder<'tcx, FnSig<'tcx>> {
387 let sig = self.tcx.fn_sig(def_id).instantiate(self.tcx, args_ref);
388 sig
389 }
390
391 pub fn intrinsic(&self, def_id: DefId) -> Option<IntrinsicDef> {
393 let intrinsic = self.tcx.intrinsic_raw(def_id);
394 intrinsic
395 }
396
397 pub fn intrinsic_name(&self, def_id: DefId) -> String {
399 self.tcx.intrinsic(def_id).unwrap().name.to_string()
400 }
401
402 pub fn closure_sig(&self, args_ref: GenericArgsRef<'tcx>) -> Binder<'tcx, FnSig<'tcx>> {
404 args_ref.as_closure().sig()
405 }
406
407 pub fn adt_variants_len(&self, def: AdtDef<'tcx>) -> usize {
409 def.variants().len()
410 }
411
412 pub fn adt_discr_for_variant(
414 &self,
415 adt: AdtDef<'tcx>,
416 variant: rustc_abi::VariantIdx,
417 ) -> Discr<'tcx> {
418 adt.discriminant_for_variant(self.tcx, variant)
419 }
420
421 pub fn coroutine_discr_for_variant(
423 &self,
424 coroutine: DefId,
425 args: GenericArgsRef<'tcx>,
426 variant: rustc_abi::VariantIdx,
427 ) -> Discr<'tcx> {
428 args.as_coroutine().discriminant_for_variant(coroutine, self.tcx, variant)
429 }
430
431 pub fn variant_name(&self, def: &'tcx VariantDef) -> String {
433 def.name.to_string()
434 }
435
436 pub fn eval_target_usize(&self, cnst: MirConst<'tcx>) -> Result<u64, B::Error> {
438 use crate::context::TypingEnvHelpers;
439 cnst.try_eval_target_usize(self.tcx, self.fully_monomorphized())
440 .ok_or_else(|| B::Error::new(format!("Const `{cnst:?}` cannot be encoded as u64")))
441 }
442
443 pub fn eval_target_usize_ty(&self, cnst: ty::Const<'tcx>) -> Result<u64, B::Error> {
444 cnst.try_to_target_usize(self.tcx)
445 .ok_or_else(|| B::Error::new(format!("Const `{cnst:?}` cannot be encoded as u64")))
446 }
447
448 pub fn try_new_const_zst(&self, ty_internal: Ty<'tcx>) -> Result<MirConst<'tcx>, B::Error> {
449 let size = self
450 .tcx
451 .layout_of(self.fully_monomorphized().as_query_input(ty_internal))
452 .map_err(|err| {
453 B::Error::new(format!(
454 "Cannot create a zero-sized constant for type `{ty_internal}`: {err}"
455 ))
456 })?
457 .size;
458 if size.bytes() != 0 {
459 return Err(B::Error::new(format!(
460 "Cannot create a zero-sized constant for type `{ty_internal}`: \
461 Type `{ty_internal}` has {} bytes",
462 size.bytes()
463 )));
464 }
465
466 Ok(MirConst::Ty(ty_internal, self.const_zero_sized(ty_internal)))
467 }
468
469 pub fn const_zero_sized(&self, ty_internal: Ty<'tcx>) -> ty::Const<'tcx> {
470 ty::Const::zero_sized(self.tcx, ty_internal)
471 }
472
473 pub fn new_const_str(&self, value: &str) -> MirConst<'tcx> {
475 let ty = Ty::new_static_str(self.tcx);
476 let bytes = value.as_bytes();
477 let valtree = ValTree::from_raw_bytes(self.tcx, bytes);
478 let cv = ty::Value { ty, valtree };
479 let val = self.tcx.valtree_to_const_val(cv);
480 MirConst::from_value(val, ty)
481 }
482
483 pub fn new_const_bool(&self, value: bool) -> MirConst<'tcx> {
485 MirConst::from_bool(self.tcx, value)
486 }
487
488 pub fn try_new_const_uint(
489 &self,
490 value: u128,
491 ty_internal: Ty<'tcx>,
492 ) -> Result<MirConst<'tcx>, B::Error> {
493 let size = self
494 .tcx
495 .layout_of(self.fully_monomorphized().as_query_input(ty_internal))
496 .unwrap()
497 .size;
498 let scalar = ScalarInt::try_from_uint(value, size).ok_or_else(|| {
499 B::Error::new(format!("Value overflow: cannot convert `{value}` to `{ty_internal}`."))
500 })?;
501 Ok(self.mir_const_from_scalar(Scalar::Int(scalar), ty_internal))
502 }
503
504 pub fn try_new_ty_const_uint(
505 &self,
506 value: u128,
507 ty_internal: Ty<'tcx>,
508 ) -> Result<ty::Const<'tcx>, B::Error> {
509 let size = self
510 .tcx
511 .layout_of(self.fully_monomorphized().as_query_input(ty_internal))
512 .unwrap()
513 .size;
514 let scalar = ScalarInt::try_from_uint(value, size).ok_or_else(|| {
515 B::Error::new(format!("Value overflow: cannot convert `{value}` to `{ty_internal}`."))
516 })?;
517
518 Ok(self.ty_const_new_value(ValTree::from_scalar_int(self.tcx, scalar), ty_internal))
519 }
520
521 pub fn ty_new_uint(&self, ty: UintTy) -> Ty<'tcx> {
522 Ty::new_uint(self.tcx, ty)
523 }
524
525 pub fn mir_const_from_scalar(&self, s: Scalar, ty: Ty<'tcx>) -> MirConst<'tcx> {
526 MirConst::from_scalar(self.tcx, s, ty)
527 }
528
529 pub fn ty_const_new_value(&self, valtree: ValTree<'tcx>, ty: Ty<'tcx>) -> ty::Const<'tcx> {
530 ty::Const::new_value(self.tcx, valtree, ty)
531 }
532
533 pub fn ty_valtree_from_scalar_int(&self, i: ScalarInt) -> ValTree<'tcx> {
534 ValTree::from_scalar_int(self.tcx, i)
535 }
536
537 pub fn new_rigid_ty(&self, internal_kind: TyKind<'tcx>) -> Ty<'tcx> {
539 self.tcx.mk_ty_from_kind(internal_kind)
540 }
541
542 pub fn new_box_ty(&self, ty: Ty<'tcx>) -> Ty<'tcx> {
544 ty::Ty::new_box(self.tcx, ty)
545 }
546
547 pub fn def_ty(&self, item: DefId) -> Ty<'tcx> {
549 self.tcx.type_of(item).instantiate_identity()
550 }
551
552 pub fn def_ty_with_args(&self, item: DefId, args_ref: GenericArgsRef<'tcx>) -> Ty<'tcx> {
554 let def_ty = self.tcx.type_of(item);
555 self.tcx.instantiate_and_normalize_erasing_regions(
556 args_ref,
557 self.fully_monomorphized(),
558 def_ty,
559 )
560 }
561
562 pub fn span_of_an_item(&self, def_id: DefId) -> Span {
564 self.tcx.def_span(def_id)
565 }
566
567 pub fn ty_const_pretty(&self, ct: ty::Const<'tcx>) -> String {
568 ct.to_string()
569 }
570
571 pub fn ty_pretty(&self, ty: Ty<'tcx>) -> String {
573 ty.to_string()
574 }
575
576 pub fn ty_kind(&self, ty: Ty<'tcx>) -> &'tcx TyKind<'tcx> {
578 ty.kind()
579 }
580
581 pub fn rigid_ty_discriminant_ty(&self, internal_kind: TyKind<'tcx>) -> Ty<'tcx> {
583 let internal_ty = self.tcx.mk_ty_from_kind(internal_kind);
584 internal_ty.discriminant_ty(self.tcx)
585 }
586
587 pub fn instance_body(&self, instance: ty::Instance<'tcx>) -> Option<Body<'tcx>> {
589 self.instance_has_body(instance).then(|| BodyBuilder::new(self.tcx, instance).build())
590 }
591
592 pub fn instance_ty(&self, instance: ty::Instance<'tcx>) -> Ty<'tcx> {
594 assert!(!instance.has_non_region_param(), "{instance:?} needs further instantiation");
595 instance.ty(self.tcx, self.fully_monomorphized())
596 }
597
598 pub fn instance_args(&self, instance: ty::Instance<'tcx>) -> GenericArgsRef<'tcx> {
600 instance.args
601 }
602
603 pub fn instance_abi(
605 &self,
606 instance: ty::Instance<'tcx>,
607 ) -> Result<&FnAbi<'tcx, Ty<'tcx>>, B::Error> {
608 Ok(self.fn_abi_of_instance(instance, List::empty())?)
609 }
610
611 pub fn fn_ptr_abi(&self, sig: PolyFnSig<'tcx>) -> Result<&FnAbi<'tcx, Ty<'tcx>>, B::Error> {
613 Ok(self.fn_abi_of_fn_ptr(sig, List::empty())?)
614 }
615
616 pub fn instance_def_id(
618 &self,
619 instances: ty::Instance<'tcx>,
620 tables: &mut Tables<'_, B>,
621 ) -> B::DefId {
622 let def_id = instances.def_id();
623 tables.create_def_id(def_id)
624 }
625
626 pub fn instance_mangled_name(&self, instance: ty::Instance<'tcx>) -> String {
628 self.tcx.symbol_name(instance).name.to_string()
629 }
630
631 pub fn is_empty_drop_shim(&self, instance: ty::Instance<'tcx>) -> bool {
633 matches!(instance.def, ty::InstanceKind::DropGlue(_, None))
634 }
635
636 pub fn mono_instance(&self, def_id: DefId) -> Instance<'tcx> {
639 Instance::mono(self.tcx, def_id)
640 }
641
642 pub fn requires_monomorphization(&self, def_id: DefId) -> bool {
644 let generics = self.tcx.generics_of(def_id);
645 let result = generics.requires_monomorphization(self.tcx);
646 result
647 }
648
649 pub fn resolve_instance(
651 &self,
652 def_id: DefId,
653 args_ref: GenericArgsRef<'tcx>,
654 ) -> Option<Instance<'tcx>> {
655 match Instance::try_resolve(self.tcx, self.fully_monomorphized(), def_id, args_ref) {
656 Ok(Some(instance)) => Some(instance),
657 Ok(None) | Err(_) => None,
658 }
659 }
660
661 pub fn resolve_drop_in_place(&self, internal_ty: Ty<'tcx>) -> Instance<'tcx> {
663 let instance = Instance::resolve_drop_in_place(self.tcx, internal_ty);
664 instance
665 }
666
667 pub fn resolve_for_fn_ptr(
669 &self,
670 def_id: DefId,
671 args_ref: GenericArgsRef<'tcx>,
672 ) -> Option<Instance<'tcx>> {
673 Instance::resolve_for_fn_ptr(self.tcx, self.fully_monomorphized(), def_id, args_ref)
674 }
675
676 pub fn resolve_closure(
678 &self,
679 def_id: DefId,
680 args_ref: GenericArgsRef<'tcx>,
681 closure_kind: ClosureKind,
682 ) -> Option<Instance<'tcx>> {
683 Some(Instance::resolve_closure(self.tcx, def_id, args_ref, closure_kind))
684 }
685
686 pub fn eval_instance(&self, instance: ty::Instance<'tcx>) -> Result<ConstValue, ErrorHandled> {
688 self.tcx.const_eval_instance(
689 self.fully_monomorphized(),
690 instance,
691 self.tcx.def_span(instance.def_id()),
692 )
693 }
694
695 pub fn eval_static_initializer(
697 &self,
698 def_id: DefId,
699 ) -> Result<ConstAllocation<'tcx>, ErrorHandled> {
700 self.tcx.eval_static_initializer(def_id)
701 }
702
703 pub fn global_alloc(&self, alloc_id: AllocId) -> GlobalAlloc<'tcx> {
705 self.tcx.global_alloc(alloc_id)
706 }
707
708 pub fn vtable_allocation(
710 &self,
711 ty: Ty<'tcx>,
712 trait_ref: Option<Binder<'tcx, ExistentialTraitRef<'tcx>>>,
713 ) -> AllocId {
714 let alloc_id = self.tcx.vtable_allocation((
715 ty,
716 trait_ref.map(|principal| self.tcx.instantiate_bound_regions_with_erased(principal)),
717 ));
718 alloc_id
719 }
720
721 pub fn instance_name(&self, instance: ty::Instance<'tcx>, trimmed: bool) -> String {
725 if trimmed {
726 with_forced_trimmed_paths!(
727 self.tcx.def_path_str_with_args(instance.def_id(), instance.args)
728 )
729 } else {
730 with_resolve_crate_name!(with_no_trimmed_paths!(
731 self.tcx.def_path_str_with_args(instance.def_id(), instance.args)
732 ))
733 }
734 }
735
736 pub fn ty_layout(&self, ty: Ty<'tcx>) -> Result<Layout<'tcx>, B::Error> {
738 let layout = self.layout_of(ty)?.layout;
739 Ok(layout)
740 }
741
742 pub fn binop_ty(&self, bin_op: BinOp, rhs: Ty<'tcx>, lhs: Ty<'tcx>) -> Ty<'tcx> {
744 bin_op.ty(self.tcx, rhs, lhs)
745 }
746
747 pub fn unop_ty(&self, un_op: UnOp, arg: Ty<'tcx>) -> Ty<'tcx> {
749 un_op.ty(self.tcx, arg)
750 }
751
752 pub fn associated_items(&self, def_id: DefId) -> Vec<AssocItem> {
754 let assoc_items = if self.tcx.is_trait_alias(def_id) {
755 Vec::new()
756 } else {
757 self.tcx
758 .associated_item_def_ids(def_id)
759 .iter()
760 .map(|did| self.tcx.associated_item(*did))
761 .collect()
762 };
763 assoc_items
764 }
765}