]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_mir/src/shim.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / compiler / rustc_mir / src / shim.rs
1 use rustc_hir as hir;
2 use rustc_hir::def_id::DefId;
3 use rustc_hir::lang_items::LangItem;
4 use rustc_middle::mir::*;
5 use rustc_middle::ty::query::Providers;
6 use rustc_middle::ty::subst::{InternalSubsts, Subst};
7 use rustc_middle::ty::{self, Ty, TyCtxt};
8 use rustc_target::abi::VariantIdx;
9
10 use rustc_index::vec::{Idx, IndexVec};
11
12 use rustc_span::Span;
13 use rustc_target::spec::abi::Abi;
14
15 use std::fmt;
16 use std::iter;
17
18 use crate::transform::{
19 add_call_guards, add_moves_for_packed_drops, no_landing_pads, remove_noop_landing_pads,
20 run_passes, simplify,
21 };
22 use crate::util::elaborate_drops::{self, DropElaborator, DropFlagMode, DropStyle};
23 use crate::util::expand_aggregate;
24 use crate::util::patch::MirPatch;
25
26 pub fn provide(providers: &mut Providers) {
27 providers.mir_shims = make_shim;
28 }
29
30 fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> Body<'tcx> {
31 debug!("make_shim({:?})", instance);
32
33 let mut result = match instance {
34 ty::InstanceDef::Item(..) => bug!("item {:?} passed to make_shim", instance),
35 ty::InstanceDef::VtableShim(def_id) => {
36 build_call_shim(tcx, instance, Some(Adjustment::Deref), CallKind::Direct(def_id))
37 }
38 ty::InstanceDef::FnPtrShim(def_id, ty) => {
39 let trait_ = tcx.trait_of_item(def_id).unwrap();
40 let adjustment = match tcx.fn_trait_kind_from_lang_item(trait_) {
41 Some(ty::ClosureKind::FnOnce) => Adjustment::Identity,
42 Some(ty::ClosureKind::FnMut | ty::ClosureKind::Fn) => Adjustment::Deref,
43 None => bug!("fn pointer {:?} is not an fn", ty),
44 };
45
46 build_call_shim(tcx, instance, Some(adjustment), CallKind::Indirect(ty))
47 }
48 // We are generating a call back to our def-id, which the
49 // codegen backend knows to turn to an actual call, be it
50 // a virtual call, or a direct call to a function for which
51 // indirect calls must be codegen'd differently than direct ones
52 // (such as `#[track_caller]`).
53 ty::InstanceDef::ReifyShim(def_id) => {
54 build_call_shim(tcx, instance, None, CallKind::Direct(def_id))
55 }
56 ty::InstanceDef::ClosureOnceShim { call_once: _ } => {
57 let fn_mut = tcx.require_lang_item(LangItem::FnMut, None);
58 let call_mut = tcx
59 .associated_items(fn_mut)
60 .in_definition_order()
61 .find(|it| it.kind == ty::AssocKind::Fn)
62 .unwrap()
63 .def_id;
64
65 build_call_shim(tcx, instance, Some(Adjustment::RefMut), CallKind::Direct(call_mut))
66 }
67 ty::InstanceDef::DropGlue(def_id, ty) => build_drop_shim(tcx, def_id, ty),
68 ty::InstanceDef::CloneShim(def_id, ty) => build_clone_shim(tcx, def_id, ty),
69 ty::InstanceDef::Virtual(..) => {
70 bug!("InstanceDef::Virtual ({:?}) is for direct calls only", instance)
71 }
72 ty::InstanceDef::Intrinsic(_) => {
73 bug!("creating shims from intrinsics ({:?}) is unsupported", instance)
74 }
75 };
76 debug!("make_shim({:?}) = untransformed {:?}", instance, result);
77
78 run_passes(
79 tcx,
80 &mut result,
81 instance,
82 None,
83 MirPhase::Const,
84 &[&[
85 &add_moves_for_packed_drops::AddMovesForPackedDrops,
86 &no_landing_pads::NoLandingPads::new(tcx),
87 &remove_noop_landing_pads::RemoveNoopLandingPads,
88 &simplify::SimplifyCfg::new("make_shim"),
89 &add_call_guards::CriticalCallEdges,
90 ]],
91 );
92
93 debug!("make_shim({:?}) = {:?}", instance, result);
94
95 result
96 }
97
98 #[derive(Copy, Clone, Debug, PartialEq)]
99 enum Adjustment {
100 /// Pass the receiver as-is.
101 Identity,
102
103 /// We get passed `&[mut] self` and call the target with `*self`.
104 ///
105 /// This either copies `self` (if `Self: Copy`, eg. for function items), or moves out of it
106 /// (for `VtableShim`, which effectively is passed `&own Self`).
107 Deref,
108
109 /// We get passed `self: Self` and call the target with `&mut self`.
110 ///
111 /// In this case we need to ensure that the `Self` is dropped after the call, as the callee
112 /// won't do it for us.
113 RefMut,
114 }
115
116 #[derive(Copy, Clone, Debug, PartialEq)]
117 enum CallKind<'tcx> {
118 /// Call the `FnPtr` that was passed as the receiver.
119 Indirect(Ty<'tcx>),
120
121 /// Call a known `FnDef`.
122 Direct(DefId),
123 }
124
125 fn local_decls_for_sig<'tcx>(
126 sig: &ty::FnSig<'tcx>,
127 span: Span,
128 ) -> IndexVec<Local, LocalDecl<'tcx>> {
129 iter::once(LocalDecl::new(sig.output(), span))
130 .chain(sig.inputs().iter().map(|ity| LocalDecl::new(ity, span).immutable()))
131 .collect()
132 }
133
134 fn build_drop_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, ty: Option<Ty<'tcx>>) -> Body<'tcx> {
135 debug!("build_drop_shim(def_id={:?}, ty={:?})", def_id, ty);
136
137 // Check if this is a generator, if so, return the drop glue for it
138 if let Some(&ty::Generator(gen_def_id, substs, _)) = ty.map(|ty| ty.kind()) {
139 let body = &**tcx.optimized_mir(gen_def_id).generator_drop.as_ref().unwrap();
140 return body.subst(tcx, substs);
141 }
142
143 let substs = if let Some(ty) = ty {
144 tcx.intern_substs(&[ty.into()])
145 } else {
146 InternalSubsts::identity_for_item(tcx, def_id)
147 };
148 let sig = tcx.fn_sig(def_id).subst(tcx, substs);
149 let sig = tcx.erase_late_bound_regions(&sig);
150 let span = tcx.def_span(def_id);
151
152 let source_info = SourceInfo::outermost(span);
153
154 let return_block = BasicBlock::new(1);
155 let mut blocks = IndexVec::with_capacity(2);
156 let block = |blocks: &mut IndexVec<_, _>, kind| {
157 blocks.push(BasicBlockData {
158 statements: vec![],
159 terminator: Some(Terminator { source_info, kind }),
160 is_cleanup: false,
161 })
162 };
163 block(&mut blocks, TerminatorKind::Goto { target: return_block });
164 block(&mut blocks, TerminatorKind::Return);
165
166 let mut body = new_body(blocks, local_decls_for_sig(&sig, span), sig.inputs().len(), span);
167
168 if let Some(..) = ty {
169 // The first argument (index 0), but add 1 for the return value.
170 let dropee_ptr = Place::from(Local::new(1 + 0));
171 if tcx.sess.opts.debugging_opts.mir_emit_retag {
172 // Function arguments should be retagged, and we make this one raw.
173 body.basic_blocks_mut()[START_BLOCK].statements.insert(
174 0,
175 Statement {
176 source_info,
177 kind: StatementKind::Retag(RetagKind::Raw, box (dropee_ptr)),
178 },
179 );
180 }
181 let patch = {
182 let param_env = tcx.param_env_reveal_all_normalized(def_id);
183 let mut elaborator =
184 DropShimElaborator { body: &body, patch: MirPatch::new(&body), tcx, param_env };
185 let dropee = tcx.mk_place_deref(dropee_ptr);
186 let resume_block = elaborator.patch.resume_block();
187 elaborate_drops::elaborate_drop(
188 &mut elaborator,
189 source_info,
190 dropee,
191 (),
192 return_block,
193 elaborate_drops::Unwind::To(resume_block),
194 START_BLOCK,
195 );
196 elaborator.patch
197 };
198 patch.apply(&mut body);
199 }
200
201 body
202 }
203
204 fn new_body<'tcx>(
205 basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
206 local_decls: IndexVec<Local, LocalDecl<'tcx>>,
207 arg_count: usize,
208 span: Span,
209 ) -> Body<'tcx> {
210 Body::new(
211 basic_blocks,
212 IndexVec::from_elem_n(
213 SourceScopeData { span, parent_scope: None, local_data: ClearCrossCrate::Clear },
214 1,
215 ),
216 local_decls,
217 IndexVec::new(),
218 arg_count,
219 vec![],
220 span,
221 None,
222 )
223 }
224
225 pub struct DropShimElaborator<'a, 'tcx> {
226 pub body: &'a Body<'tcx>,
227 pub patch: MirPatch<'tcx>,
228 pub tcx: TyCtxt<'tcx>,
229 pub param_env: ty::ParamEnv<'tcx>,
230 }
231
232 impl<'a, 'tcx> fmt::Debug for DropShimElaborator<'a, 'tcx> {
233 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
234 Ok(())
235 }
236 }
237
238 impl<'a, 'tcx> DropElaborator<'a, 'tcx> for DropShimElaborator<'a, 'tcx> {
239 type Path = ();
240
241 fn patch(&mut self) -> &mut MirPatch<'tcx> {
242 &mut self.patch
243 }
244 fn body(&self) -> &'a Body<'tcx> {
245 self.body
246 }
247 fn tcx(&self) -> TyCtxt<'tcx> {
248 self.tcx
249 }
250 fn param_env(&self) -> ty::ParamEnv<'tcx> {
251 self.param_env
252 }
253
254 fn drop_style(&self, _path: Self::Path, mode: DropFlagMode) -> DropStyle {
255 match mode {
256 DropFlagMode::Shallow => {
257 // Drops for the contained fields are "shallow" and "static" - they will simply call
258 // the field's own drop glue.
259 DropStyle::Static
260 }
261 DropFlagMode::Deep => {
262 // The top-level drop is "deep" and "open" - it will be elaborated to a drop ladder
263 // dropping each field contained in the value.
264 DropStyle::Open
265 }
266 }
267 }
268
269 fn get_drop_flag(&mut self, _path: Self::Path) -> Option<Operand<'tcx>> {
270 None
271 }
272
273 fn clear_drop_flag(&mut self, _location: Location, _path: Self::Path, _mode: DropFlagMode) {}
274
275 fn field_subpath(&self, _path: Self::Path, _field: Field) -> Option<Self::Path> {
276 None
277 }
278 fn deref_subpath(&self, _path: Self::Path) -> Option<Self::Path> {
279 None
280 }
281 fn downcast_subpath(&self, _path: Self::Path, _variant: VariantIdx) -> Option<Self::Path> {
282 Some(())
283 }
284 fn array_subpath(&self, _path: Self::Path, _index: u64, _size: u64) -> Option<Self::Path> {
285 None
286 }
287 }
288
289 /// Builds a `Clone::clone` shim for `self_ty`. Here, `def_id` is `Clone::clone`.
290 fn build_clone_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, self_ty: Ty<'tcx>) -> Body<'tcx> {
291 debug!("build_clone_shim(def_id={:?})", def_id);
292
293 let param_env = tcx.param_env(def_id);
294
295 let mut builder = CloneShimBuilder::new(tcx, def_id, self_ty);
296 let is_copy = self_ty.is_copy_modulo_regions(tcx.at(builder.span), param_env);
297
298 let dest = Place::return_place();
299 let src = tcx.mk_place_deref(Place::from(Local::new(1 + 0)));
300
301 match self_ty.kind() {
302 _ if is_copy => builder.copy_shim(),
303 ty::Array(ty, len) => {
304 let len = len.eval_usize(tcx, param_env);
305 builder.array_shim(dest, src, ty, len)
306 }
307 ty::Closure(_, substs) => {
308 builder.tuple_like_shim(dest, src, substs.as_closure().upvar_tys())
309 }
310 ty::Tuple(..) => builder.tuple_like_shim(dest, src, self_ty.tuple_fields()),
311 _ => bug!("clone shim for `{:?}` which is not `Copy` and is not an aggregate", self_ty),
312 };
313
314 builder.into_mir()
315 }
316
317 struct CloneShimBuilder<'tcx> {
318 tcx: TyCtxt<'tcx>,
319 def_id: DefId,
320 local_decls: IndexVec<Local, LocalDecl<'tcx>>,
321 blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
322 span: Span,
323 sig: ty::FnSig<'tcx>,
324 }
325
326 impl CloneShimBuilder<'tcx> {
327 fn new(tcx: TyCtxt<'tcx>, def_id: DefId, self_ty: Ty<'tcx>) -> Self {
328 // we must subst the self_ty because it's
329 // otherwise going to be TySelf and we can't index
330 // or access fields of a Place of type TySelf.
331 let substs = tcx.mk_substs_trait(self_ty, &[]);
332 let sig = tcx.fn_sig(def_id).subst(tcx, substs);
333 let sig = tcx.erase_late_bound_regions(&sig);
334 let span = tcx.def_span(def_id);
335
336 CloneShimBuilder {
337 tcx,
338 def_id,
339 local_decls: local_decls_for_sig(&sig, span),
340 blocks: IndexVec::new(),
341 span,
342 sig,
343 }
344 }
345
346 fn into_mir(self) -> Body<'tcx> {
347 new_body(self.blocks, self.local_decls, self.sig.inputs().len(), self.span)
348 }
349
350 fn source_info(&self) -> SourceInfo {
351 SourceInfo::outermost(self.span)
352 }
353
354 fn block(
355 &mut self,
356 statements: Vec<Statement<'tcx>>,
357 kind: TerminatorKind<'tcx>,
358 is_cleanup: bool,
359 ) -> BasicBlock {
360 let source_info = self.source_info();
361 self.blocks.push(BasicBlockData {
362 statements,
363 terminator: Some(Terminator { source_info, kind }),
364 is_cleanup,
365 })
366 }
367
368 /// Gives the index of an upcoming BasicBlock, with an offset.
369 /// offset=0 will give you the index of the next BasicBlock,
370 /// offset=1 will give the index of the next-to-next block,
371 /// offset=-1 will give you the index of the last-created block
372 fn block_index_offset(&mut self, offset: usize) -> BasicBlock {
373 BasicBlock::new(self.blocks.len() + offset)
374 }
375
376 fn make_statement(&self, kind: StatementKind<'tcx>) -> Statement<'tcx> {
377 Statement { source_info: self.source_info(), kind }
378 }
379
380 fn copy_shim(&mut self) {
381 let rcvr = self.tcx.mk_place_deref(Place::from(Local::new(1 + 0)));
382 let ret_statement = self.make_statement(StatementKind::Assign(box (
383 Place::return_place(),
384 Rvalue::Use(Operand::Copy(rcvr)),
385 )));
386 self.block(vec![ret_statement], TerminatorKind::Return, false);
387 }
388
389 fn make_place(&mut self, mutability: Mutability, ty: Ty<'tcx>) -> Place<'tcx> {
390 let span = self.span;
391 let mut local = LocalDecl::new(ty, span);
392 if mutability == Mutability::Not {
393 local = local.immutable();
394 }
395 Place::from(self.local_decls.push(local))
396 }
397
398 fn make_clone_call(
399 &mut self,
400 dest: Place<'tcx>,
401 src: Place<'tcx>,
402 ty: Ty<'tcx>,
403 next: BasicBlock,
404 cleanup: BasicBlock,
405 ) {
406 let tcx = self.tcx;
407
408 let substs = tcx.mk_substs_trait(ty, &[]);
409
410 // `func == Clone::clone(&ty) -> ty`
411 let func_ty = tcx.mk_fn_def(self.def_id, substs);
412 let func = Operand::Constant(box Constant {
413 span: self.span,
414 user_ty: None,
415 literal: ty::Const::zero_sized(tcx, func_ty),
416 });
417
418 let ref_loc = self.make_place(
419 Mutability::Not,
420 tcx.mk_ref(tcx.lifetimes.re_erased, ty::TypeAndMut { ty, mutbl: hir::Mutability::Not }),
421 );
422
423 // `let ref_loc: &ty = &src;`
424 let statement = self.make_statement(StatementKind::Assign(box (
425 ref_loc,
426 Rvalue::Ref(tcx.lifetimes.re_erased, BorrowKind::Shared, src),
427 )));
428
429 // `let loc = Clone::clone(ref_loc);`
430 self.block(
431 vec![statement],
432 TerminatorKind::Call {
433 func,
434 args: vec![Operand::Move(ref_loc)],
435 destination: Some((dest, next)),
436 cleanup: Some(cleanup),
437 from_hir_call: true,
438 fn_span: self.span,
439 },
440 false,
441 );
442 }
443
444 fn loop_header(
445 &mut self,
446 beg: Place<'tcx>,
447 end: Place<'tcx>,
448 loop_body: BasicBlock,
449 loop_end: BasicBlock,
450 is_cleanup: bool,
451 ) {
452 let tcx = self.tcx;
453
454 let cond = self.make_place(Mutability::Mut, tcx.types.bool);
455 let compute_cond = self.make_statement(StatementKind::Assign(box (
456 cond,
457 Rvalue::BinaryOp(BinOp::Ne, Operand::Copy(end), Operand::Copy(beg)),
458 )));
459
460 // `if end != beg { goto loop_body; } else { goto loop_end; }`
461 self.block(
462 vec![compute_cond],
463 TerminatorKind::if_(tcx, Operand::Move(cond), loop_body, loop_end),
464 is_cleanup,
465 );
466 }
467
468 fn make_usize(&self, value: u64) -> Box<Constant<'tcx>> {
469 box Constant {
470 span: self.span,
471 user_ty: None,
472 literal: ty::Const::from_usize(self.tcx, value),
473 }
474 }
475
476 fn array_shim(&mut self, dest: Place<'tcx>, src: Place<'tcx>, ty: Ty<'tcx>, len: u64) {
477 let tcx = self.tcx;
478 let span = self.span;
479
480 let beg = self.local_decls.push(LocalDecl::new(tcx.types.usize, span));
481 let end = self.make_place(Mutability::Not, tcx.types.usize);
482
483 // BB #0
484 // `let mut beg = 0;`
485 // `let end = len;`
486 // `goto #1;`
487 let inits = vec![
488 self.make_statement(StatementKind::Assign(box (
489 Place::from(beg),
490 Rvalue::Use(Operand::Constant(self.make_usize(0))),
491 ))),
492 self.make_statement(StatementKind::Assign(box (
493 end,
494 Rvalue::Use(Operand::Constant(self.make_usize(len))),
495 ))),
496 ];
497 self.block(inits, TerminatorKind::Goto { target: BasicBlock::new(1) }, false);
498
499 // BB #1: loop {
500 // BB #2;
501 // BB #3;
502 // }
503 // BB #4;
504 self.loop_header(Place::from(beg), end, BasicBlock::new(2), BasicBlock::new(4), false);
505
506 // BB #2
507 // `dest[i] = Clone::clone(src[beg])`;
508 // Goto #3 if ok, #5 if unwinding happens.
509 let dest_field = self.tcx.mk_place_index(dest, beg);
510 let src_field = self.tcx.mk_place_index(src, beg);
511 self.make_clone_call(dest_field, src_field, ty, BasicBlock::new(3), BasicBlock::new(5));
512
513 // BB #3
514 // `beg = beg + 1;`
515 // `goto #1`;
516 let statements = vec![self.make_statement(StatementKind::Assign(box (
517 Place::from(beg),
518 Rvalue::BinaryOp(
519 BinOp::Add,
520 Operand::Copy(Place::from(beg)),
521 Operand::Constant(self.make_usize(1)),
522 ),
523 )))];
524 self.block(statements, TerminatorKind::Goto { target: BasicBlock::new(1) }, false);
525
526 // BB #4
527 // `return dest;`
528 self.block(vec![], TerminatorKind::Return, false);
529
530 // BB #5 (cleanup)
531 // `let end = beg;`
532 // `let mut beg = 0;`
533 // goto #6;
534 let end = beg;
535 let beg = self.local_decls.push(LocalDecl::new(tcx.types.usize, span));
536 let init = self.make_statement(StatementKind::Assign(box (
537 Place::from(beg),
538 Rvalue::Use(Operand::Constant(self.make_usize(0))),
539 )));
540 self.block(vec![init], TerminatorKind::Goto { target: BasicBlock::new(6) }, true);
541
542 // BB #6 (cleanup): loop {
543 // BB #7;
544 // BB #8;
545 // }
546 // BB #9;
547 self.loop_header(
548 Place::from(beg),
549 Place::from(end),
550 BasicBlock::new(7),
551 BasicBlock::new(9),
552 true,
553 );
554
555 // BB #7 (cleanup)
556 // `drop(dest[beg])`;
557 self.block(
558 vec![],
559 TerminatorKind::Drop {
560 place: self.tcx.mk_place_index(dest, beg),
561 target: BasicBlock::new(8),
562 unwind: None,
563 },
564 true,
565 );
566
567 // BB #8 (cleanup)
568 // `beg = beg + 1;`
569 // `goto #6;`
570 let statement = self.make_statement(StatementKind::Assign(box (
571 Place::from(beg),
572 Rvalue::BinaryOp(
573 BinOp::Add,
574 Operand::Copy(Place::from(beg)),
575 Operand::Constant(self.make_usize(1)),
576 ),
577 )));
578 self.block(vec![statement], TerminatorKind::Goto { target: BasicBlock::new(6) }, true);
579
580 // BB #9 (resume)
581 self.block(vec![], TerminatorKind::Resume, true);
582 }
583
584 fn tuple_like_shim<I>(&mut self, dest: Place<'tcx>, src: Place<'tcx>, tys: I)
585 where
586 I: Iterator<Item = Ty<'tcx>>,
587 {
588 let mut previous_field = None;
589 for (i, ity) in tys.enumerate() {
590 let field = Field::new(i);
591 let src_field = self.tcx.mk_place_field(src, field, ity);
592
593 let dest_field = self.tcx.mk_place_field(dest, field, ity);
594
595 // #(2i + 1) is the cleanup block for the previous clone operation
596 let cleanup_block = self.block_index_offset(1);
597 // #(2i + 2) is the next cloning block
598 // (or the Return terminator if this is the last block)
599 let next_block = self.block_index_offset(2);
600
601 // BB #(2i)
602 // `dest.i = Clone::clone(&src.i);`
603 // Goto #(2i + 2) if ok, #(2i + 1) if unwinding happens.
604 self.make_clone_call(dest_field, src_field, ity, next_block, cleanup_block);
605
606 // BB #(2i + 1) (cleanup)
607 if let Some((previous_field, previous_cleanup)) = previous_field.take() {
608 // Drop previous field and goto previous cleanup block.
609 self.block(
610 vec![],
611 TerminatorKind::Drop {
612 place: previous_field,
613 target: previous_cleanup,
614 unwind: None,
615 },
616 true,
617 );
618 } else {
619 // Nothing to drop, just resume.
620 self.block(vec![], TerminatorKind::Resume, true);
621 }
622
623 previous_field = Some((dest_field, cleanup_block));
624 }
625
626 self.block(vec![], TerminatorKind::Return, false);
627 }
628 }
629
630 /// Builds a "call" shim for `instance`. The shim calls the function specified by `call_kind`,
631 /// first adjusting its first argument according to `rcvr_adjustment`.
632 fn build_call_shim<'tcx>(
633 tcx: TyCtxt<'tcx>,
634 instance: ty::InstanceDef<'tcx>,
635 rcvr_adjustment: Option<Adjustment>,
636 call_kind: CallKind<'tcx>,
637 ) -> Body<'tcx> {
638 debug!(
639 "build_call_shim(instance={:?}, rcvr_adjustment={:?}, call_kind={:?})",
640 instance, rcvr_adjustment, call_kind
641 );
642
643 // `FnPtrShim` contains the fn pointer type that a call shim is being built for - this is used
644 // to substitute into the signature of the shim. It is not necessary for users of this
645 // MIR body to perform further substitutions (see `InstanceDef::has_polymorphic_mir_body`).
646 let (sig_substs, untuple_args) = if let ty::InstanceDef::FnPtrShim(_, ty) = instance {
647 let sig = tcx.erase_late_bound_regions(&ty.fn_sig(tcx));
648
649 let untuple_args = sig.inputs();
650
651 // Create substitutions for the `Self` and `Args` generic parameters of the shim body.
652 let arg_tup = tcx.mk_tup(untuple_args.iter());
653 let sig_substs = tcx.mk_substs_trait(ty, &[ty::subst::GenericArg::from(arg_tup)]);
654
655 (Some(sig_substs), Some(untuple_args))
656 } else {
657 (None, None)
658 };
659
660 let def_id = instance.def_id();
661 let sig = tcx.fn_sig(def_id);
662 let mut sig = tcx.erase_late_bound_regions(&sig);
663
664 assert_eq!(sig_substs.is_some(), !instance.has_polymorphic_mir_body());
665 if let Some(sig_substs) = sig_substs {
666 sig = sig.subst(tcx, sig_substs);
667 }
668
669 if let CallKind::Indirect(fnty) = call_kind {
670 // `sig` determines our local decls, and thus the callee type in the `Call` terminator. This
671 // can only be an `FnDef` or `FnPtr`, but currently will be `Self` since the types come from
672 // the implemented `FnX` trait.
673
674 // Apply the opposite adjustment to the MIR input.
675 let mut inputs_and_output = sig.inputs_and_output.to_vec();
676
677 // Initial signature is `fn(&? Self, Args) -> Self::Output` where `Args` is a tuple of the
678 // fn arguments. `Self` may be passed via (im)mutable reference or by-value.
679 assert_eq!(inputs_and_output.len(), 3);
680
681 // `Self` is always the original fn type `ty`. The MIR call terminator is only defined for
682 // `FnDef` and `FnPtr` callees, not the `Self` type param.
683 let self_arg = &mut inputs_and_output[0];
684 *self_arg = match rcvr_adjustment.unwrap() {
685 Adjustment::Identity => fnty,
686 Adjustment::Deref => tcx.mk_imm_ptr(fnty),
687 Adjustment::RefMut => tcx.mk_mut_ptr(fnty),
688 };
689 sig.inputs_and_output = tcx.intern_type_list(&inputs_and_output);
690 }
691
692 // FIXME(eddyb) avoid having this snippet both here and in
693 // `Instance::fn_sig` (introduce `InstanceDef::fn_sig`?).
694 if let ty::InstanceDef::VtableShim(..) = instance {
695 // Modify fn(self, ...) to fn(self: *mut Self, ...)
696 let mut inputs_and_output = sig.inputs_and_output.to_vec();
697 let self_arg = &mut inputs_and_output[0];
698 debug_assert!(tcx.generics_of(def_id).has_self && *self_arg == tcx.types.self_param);
699 *self_arg = tcx.mk_mut_ptr(*self_arg);
700 sig.inputs_and_output = tcx.intern_type_list(&inputs_and_output);
701 }
702
703 let span = tcx.def_span(def_id);
704
705 debug!("build_call_shim: sig={:?}", sig);
706
707 let mut local_decls = local_decls_for_sig(&sig, span);
708 let source_info = SourceInfo::outermost(span);
709
710 let rcvr_place = || {
711 assert!(rcvr_adjustment.is_some());
712 Place::from(Local::new(1 + 0))
713 };
714 let mut statements = vec![];
715
716 let rcvr = rcvr_adjustment.map(|rcvr_adjustment| match rcvr_adjustment {
717 Adjustment::Identity => Operand::Move(rcvr_place()),
718 Adjustment::Deref => Operand::Move(tcx.mk_place_deref(rcvr_place())),
719 Adjustment::RefMut => {
720 // let rcvr = &mut rcvr;
721 let ref_rcvr = local_decls.push(
722 LocalDecl::new(
723 tcx.mk_ref(
724 tcx.lifetimes.re_erased,
725 ty::TypeAndMut { ty: sig.inputs()[0], mutbl: hir::Mutability::Mut },
726 ),
727 span,
728 )
729 .immutable(),
730 );
731 let borrow_kind = BorrowKind::Mut { allow_two_phase_borrow: false };
732 statements.push(Statement {
733 source_info,
734 kind: StatementKind::Assign(box (
735 Place::from(ref_rcvr),
736 Rvalue::Ref(tcx.lifetimes.re_erased, borrow_kind, rcvr_place()),
737 )),
738 });
739 Operand::Move(Place::from(ref_rcvr))
740 }
741 });
742
743 let (callee, mut args) = match call_kind {
744 // `FnPtr` call has no receiver. Args are untupled below.
745 CallKind::Indirect(_) => (rcvr.unwrap(), vec![]),
746
747 // `FnDef` call with optional receiver.
748 CallKind::Direct(def_id) => {
749 let ty = tcx.type_of(def_id);
750 (
751 Operand::Constant(box Constant {
752 span,
753 user_ty: None,
754 literal: ty::Const::zero_sized(tcx, ty),
755 }),
756 rcvr.into_iter().collect::<Vec<_>>(),
757 )
758 }
759 };
760
761 let mut arg_range = 0..sig.inputs().len();
762
763 // Take the `self` ("receiver") argument out of the range (it's adjusted above).
764 if rcvr_adjustment.is_some() {
765 arg_range.start += 1;
766 }
767
768 // Take the last argument, if we need to untuple it (handled below).
769 if untuple_args.is_some() {
770 arg_range.end -= 1;
771 }
772
773 // Pass all of the non-special arguments directly.
774 args.extend(arg_range.map(|i| Operand::Move(Place::from(Local::new(1 + i)))));
775
776 // Untuple the last argument, if we have to.
777 if let Some(untuple_args) = untuple_args {
778 let tuple_arg = Local::new(1 + (sig.inputs().len() - 1));
779 args.extend(untuple_args.iter().enumerate().map(|(i, ity)| {
780 Operand::Move(tcx.mk_place_field(Place::from(tuple_arg), Field::new(i), *ity))
781 }));
782 }
783
784 let n_blocks = if let Some(Adjustment::RefMut) = rcvr_adjustment { 5 } else { 2 };
785 let mut blocks = IndexVec::with_capacity(n_blocks);
786 let block = |blocks: &mut IndexVec<_, _>, statements, kind, is_cleanup| {
787 blocks.push(BasicBlockData {
788 statements,
789 terminator: Some(Terminator { source_info, kind }),
790 is_cleanup,
791 })
792 };
793
794 // BB #0
795 block(
796 &mut blocks,
797 statements,
798 TerminatorKind::Call {
799 func: callee,
800 args,
801 destination: Some((Place::return_place(), BasicBlock::new(1))),
802 cleanup: if let Some(Adjustment::RefMut) = rcvr_adjustment {
803 Some(BasicBlock::new(3))
804 } else {
805 None
806 },
807 from_hir_call: true,
808 fn_span: span,
809 },
810 false,
811 );
812
813 if let Some(Adjustment::RefMut) = rcvr_adjustment {
814 // BB #1 - drop for Self
815 block(
816 &mut blocks,
817 vec![],
818 TerminatorKind::Drop { place: rcvr_place(), target: BasicBlock::new(2), unwind: None },
819 false,
820 );
821 }
822 // BB #1/#2 - return
823 block(&mut blocks, vec![], TerminatorKind::Return, false);
824 if let Some(Adjustment::RefMut) = rcvr_adjustment {
825 // BB #3 - drop if closure panics
826 block(
827 &mut blocks,
828 vec![],
829 TerminatorKind::Drop { place: rcvr_place(), target: BasicBlock::new(4), unwind: None },
830 true,
831 );
832
833 // BB #4 - resume
834 block(&mut blocks, vec![], TerminatorKind::Resume, true);
835 }
836
837 let mut body = new_body(blocks, local_decls, sig.inputs().len(), span);
838
839 if let Abi::RustCall = sig.abi {
840 body.spread_arg = Some(Local::new(sig.inputs().len()));
841 }
842
843 body
844 }
845
846 pub fn build_adt_ctor(tcx: TyCtxt<'_>, ctor_id: DefId) -> Body<'_> {
847 debug_assert!(tcx.is_constructor(ctor_id));
848
849 let span =
850 tcx.hir().span_if_local(ctor_id).unwrap_or_else(|| bug!("no span for ctor {:?}", ctor_id));
851
852 let param_env = tcx.param_env(ctor_id);
853
854 // Normalize the sig.
855 let sig = tcx.fn_sig(ctor_id).no_bound_vars().expect("LBR in ADT constructor signature");
856 let sig = tcx.normalize_erasing_regions(param_env, sig);
857
858 let (adt_def, substs) = match sig.output().kind() {
859 ty::Adt(adt_def, substs) => (adt_def, substs),
860 _ => bug!("unexpected type for ADT ctor {:?}", sig.output()),
861 };
862
863 debug!("build_ctor: ctor_id={:?} sig={:?}", ctor_id, sig);
864
865 let local_decls = local_decls_for_sig(&sig, span);
866
867 let source_info = SourceInfo::outermost(span);
868
869 let variant_index = if adt_def.is_enum() {
870 adt_def.variant_index_with_ctor_id(ctor_id)
871 } else {
872 VariantIdx::new(0)
873 };
874
875 // Generate the following MIR:
876 //
877 // (return as Variant).field0 = arg0;
878 // (return as Variant).field1 = arg1;
879 //
880 // return;
881 debug!("build_ctor: variant_index={:?}", variant_index);
882
883 let statements = expand_aggregate(
884 Place::return_place(),
885 adt_def.variants[variant_index].fields.iter().enumerate().map(|(idx, field_def)| {
886 (Operand::Move(Place::from(Local::new(idx + 1))), field_def.ty(tcx, substs))
887 }),
888 AggregateKind::Adt(adt_def, variant_index, substs, None, None),
889 source_info,
890 tcx,
891 )
892 .collect();
893
894 let start_block = BasicBlockData {
895 statements,
896 terminator: Some(Terminator { source_info, kind: TerminatorKind::Return }),
897 is_cleanup: false,
898 };
899
900 let body =
901 new_body(IndexVec::from_elem_n(start_block, 1), local_decls, sig.inputs().len(), span);
902
903 crate::util::dump_mir(
904 tcx,
905 None,
906 "mir_map",
907 &0,
908 crate::transform::MirSource::item(ctor_id),
909 &body,
910 |_, _| Ok(()),
911 );
912
913 body
914 }