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