1 use crate::mir::interpret::Scalar;
2 use crate::ty::{self, Ty, TyCtxt};
3 use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
6 AssertMessage, BasicBlock, InlineAsmOperand, Operand, Place, SourceInfo, Successors,
9 pub use rustc_ast::Mutability;
10 use rustc_macros::HashStable;
13 use std::fmt::{self, Debug, Formatter, Write};
17 pub use super::query::*;
19 #[derive(Clone, TyEncodable, TyDecodable, HashStable, PartialEq)]
20 pub enum TerminatorKind<'tcx> {
21 /// Block should have one successor in the graph; we jump there.
22 Goto { target: BasicBlock },
24 /// Operand evaluates to an integer; jump depending on its value
25 /// to one of the targets, and otherwise fallback to `otherwise`.
27 /// The discriminant value being tested.
30 /// The type of value being tested.
31 /// This is always the same as the type of `discr`.
32 /// FIXME: remove this redundant information. Currently, it is relied on by pretty-printing.
35 /// Possible values. The locations to branch to in each case
36 /// are found in the corresponding indices from the `targets` vector.
37 values: Cow<'tcx, [u128]>,
39 /// Possible branch sites. The last element of this vector is used
40 /// for the otherwise branch, so targets.len() == values.len() + 1
43 // This invariant is quite non-obvious and also could be improved.
44 // One way to make this invariant is to have something like this instead:
46 // branches: Vec<(ConstInt, BasicBlock)>,
47 // otherwise: Option<BasicBlock> // exhaustive if None
49 // However we’ve decided to keep this as-is until we figure a case
50 // where some other approach seems to be strictly better than other.
51 targets: Vec<BasicBlock>,
54 /// Indicates that the landing pad is finished and unwinding should
55 /// continue. Emitted by `build::scope::diverge_cleanup`.
58 /// Indicates that the landing pad is finished and that the process
59 /// should abort. Used to prevent unwinding for foreign items.
62 /// Indicates a normal return. The return place should have
63 /// been filled in before this executes. This can occur multiple times
64 /// in different basic blocks.
67 /// Indicates a terminator that can never be reached.
71 Drop { place: Place<'tcx>, target: BasicBlock, unwind: Option<BasicBlock> },
73 /// Drop the `Place` and assign the new value over it. This ensures
74 /// that the assignment to `P` occurs *even if* the destructor for
75 /// place unwinds. Its semantics are best explained by the
80 /// DropAndReplace(P <- V, goto BB1, unwind BB2)
88 /// Drop(P, goto BB1, unwind BB2)
91 /// // P is now uninitialized
95 /// // P is now uninitialized -- its dtor panicked
101 value: Operand<'tcx>,
103 unwind: Option<BasicBlock>,
106 /// Block ends with a call of a function.
108 /// The function that’s being called.
110 /// Arguments the function is called with.
111 /// These are owned by the callee, which is free to modify them.
112 /// This allows the memory occupied by "by-value" arguments to be
113 /// reused across function calls without duplicating the contents.
114 args: Vec<Operand<'tcx>>,
115 /// Destination for the return value. If some, the call is converging.
116 destination: Option<(Place<'tcx>, BasicBlock)>,
117 /// Cleanups to be done if the call unwinds.
118 cleanup: Option<BasicBlock>,
119 /// `true` if this is from a call in HIR rather than from an overloaded
120 /// operator. True for overloaded function call.
122 /// This `Span` is the span of the function, without the dot and receiver
123 /// (e.g. `foo(a, b)` in `x.foo(a, b)`
127 /// Jump to the target if the condition has the expected value,
128 /// otherwise panic with a message and a cleanup target.
132 msg: AssertMessage<'tcx>,
134 cleanup: Option<BasicBlock>,
139 /// The value to return.
140 value: Operand<'tcx>,
141 /// Where to resume to.
143 /// The place to store the resume argument in.
144 resume_arg: Place<'tcx>,
145 /// Cleanup to be done if the generator is dropped at this suspend point.
146 drop: Option<BasicBlock>,
149 /// Indicates the end of the dropping of a generator.
152 /// A block where control flow only ever takes one real path, but borrowck
153 /// needs to be more conservative.
155 /// The target normal control flow will take.
156 real_target: BasicBlock,
157 /// A block control flow could conceptually jump to, but won't in
159 imaginary_target: BasicBlock,
161 /// A terminator for blocks that only take one path in reality, but where we
162 /// reserve the right to unwind in borrowck, even if it won't happen in practice.
163 /// This can arise in infinite loops with no function calls for example.
165 /// The target normal control flow will take.
166 real_target: BasicBlock,
167 /// The imaginary cleanup block link. This particular path will never be taken
168 /// in practice, but in order to avoid fragility we want to always
169 /// consider it in borrowck. We don't want to accept programs which
170 /// pass borrowck only when `panic=abort` or some assertions are disabled
171 /// due to release vs. debug mode builds. This needs to be an `Option` because
172 /// of the `remove_noop_landing_pads` and `no_landing_pads` passes.
173 unwind: Option<BasicBlock>,
176 /// Block ends with an inline assembly block. This is a terminator since
177 /// inline assembly is allowed to diverge.
179 /// The template for the inline assembly, with placeholders.
180 template: &'tcx [InlineAsmTemplatePiece],
182 /// The operands for the inline assembly, as `Operand`s or `Place`s.
183 operands: Vec<InlineAsmOperand<'tcx>>,
185 /// Miscellaneous options for the inline assembly.
186 options: InlineAsmOptions,
188 /// Source spans for each line of the inline assembly code. These are
189 /// used to map assembler errors back to the line in the source code.
190 line_spans: &'tcx [Span],
192 /// Destination block after the inline assembly returns, unless it is
193 /// diverging (InlineAsmOptions::NORETURN).
194 destination: Option<BasicBlock>,
197 #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
198 pub struct Terminator<'tcx> {
199 pub source_info: SourceInfo,
200 pub kind: TerminatorKind<'tcx>,
203 impl<'tcx> Terminator<'tcx> {
204 pub fn successors(&self) -> Successors<'_> {
205 self.kind.successors()
208 pub fn successors_mut(&mut self) -> SuccessorsMut<'_> {
209 self.kind.successors_mut()
212 pub fn unwind(&self) -> Option<&Option<BasicBlock>> {
216 pub fn unwind_mut(&mut self) -> Option<&mut Option<BasicBlock>> {
217 self.kind.unwind_mut()
221 impl<'tcx> TerminatorKind<'tcx> {
227 ) -> TerminatorKind<'tcx> {
228 static BOOL_SWITCH_FALSE: &[u128] = &[0];
229 TerminatorKind::SwitchInt {
231 switch_ty: tcx.types.bool,
232 values: From::from(BOOL_SWITCH_FALSE),
237 pub fn successors(&self) -> Successors<'_> {
238 use self::TerminatorKind::*;
245 | Call { destination: None, cleanup: None, .. }
246 | InlineAsm { destination: None, .. } => None.into_iter().chain(&[]),
247 Goto { target: ref t }
248 | Call { destination: None, cleanup: Some(ref t), .. }
249 | Call { destination: Some((_, ref t)), cleanup: None, .. }
250 | Yield { resume: ref t, drop: None, .. }
251 | DropAndReplace { target: ref t, unwind: None, .. }
252 | Drop { target: ref t, unwind: None, .. }
253 | Assert { target: ref t, cleanup: None, .. }
254 | FalseUnwind { real_target: ref t, unwind: None }
255 | InlineAsm { destination: Some(ref t), .. } => Some(t).into_iter().chain(&[]),
256 Call { destination: Some((_, ref t)), cleanup: Some(ref u), .. }
257 | Yield { resume: ref t, drop: Some(ref u), .. }
258 | DropAndReplace { target: ref t, unwind: Some(ref u), .. }
259 | Drop { target: ref t, unwind: Some(ref u), .. }
260 | Assert { target: ref t, cleanup: Some(ref u), .. }
261 | FalseUnwind { real_target: ref t, unwind: Some(ref u) } => {
262 Some(t).into_iter().chain(slice::from_ref(u))
264 SwitchInt { ref targets, .. } => None.into_iter().chain(&targets[..]),
265 FalseEdge { ref real_target, ref imaginary_target } => {
266 Some(real_target).into_iter().chain(slice::from_ref(imaginary_target))
271 pub fn successors_mut(&mut self) -> SuccessorsMut<'_> {
272 use self::TerminatorKind::*;
279 | Call { destination: None, cleanup: None, .. }
280 | InlineAsm { destination: None, .. } => None.into_iter().chain(&mut []),
281 Goto { target: ref mut t }
282 | Call { destination: None, cleanup: Some(ref mut t), .. }
283 | Call { destination: Some((_, ref mut t)), cleanup: None, .. }
284 | Yield { resume: ref mut t, drop: None, .. }
285 | DropAndReplace { target: ref mut t, unwind: None, .. }
286 | Drop { target: ref mut t, unwind: None, .. }
287 | Assert { target: ref mut t, cleanup: None, .. }
288 | FalseUnwind { real_target: ref mut t, unwind: None }
289 | InlineAsm { destination: Some(ref mut t), .. } => Some(t).into_iter().chain(&mut []),
290 Call { destination: Some((_, ref mut t)), cleanup: Some(ref mut u), .. }
291 | Yield { resume: ref mut t, drop: Some(ref mut u), .. }
292 | DropAndReplace { target: ref mut t, unwind: Some(ref mut u), .. }
293 | Drop { target: ref mut t, unwind: Some(ref mut u), .. }
294 | Assert { target: ref mut t, cleanup: Some(ref mut u), .. }
295 | FalseUnwind { real_target: ref mut t, unwind: Some(ref mut u) } => {
296 Some(t).into_iter().chain(slice::from_mut(u))
298 SwitchInt { ref mut targets, .. } => None.into_iter().chain(&mut targets[..]),
299 FalseEdge { ref mut real_target, ref mut imaginary_target } => {
300 Some(real_target).into_iter().chain(slice::from_mut(imaginary_target))
305 pub fn unwind(&self) -> Option<&Option<BasicBlock>> {
307 TerminatorKind::Goto { .. }
308 | TerminatorKind::Resume
309 | TerminatorKind::Abort
310 | TerminatorKind::Return
311 | TerminatorKind::Unreachable
312 | TerminatorKind::GeneratorDrop
313 | TerminatorKind::Yield { .. }
314 | TerminatorKind::SwitchInt { .. }
315 | TerminatorKind::FalseEdge { .. }
316 | TerminatorKind::InlineAsm { .. } => None,
317 TerminatorKind::Call { cleanup: ref unwind, .. }
318 | TerminatorKind::Assert { cleanup: ref unwind, .. }
319 | TerminatorKind::DropAndReplace { ref unwind, .. }
320 | TerminatorKind::Drop { ref unwind, .. }
321 | TerminatorKind::FalseUnwind { ref unwind, .. } => Some(unwind),
325 pub fn unwind_mut(&mut self) -> Option<&mut Option<BasicBlock>> {
327 TerminatorKind::Goto { .. }
328 | TerminatorKind::Resume
329 | TerminatorKind::Abort
330 | TerminatorKind::Return
331 | TerminatorKind::Unreachable
332 | TerminatorKind::GeneratorDrop
333 | TerminatorKind::Yield { .. }
334 | TerminatorKind::SwitchInt { .. }
335 | TerminatorKind::FalseEdge { .. }
336 | TerminatorKind::InlineAsm { .. } => None,
337 TerminatorKind::Call { cleanup: ref mut unwind, .. }
338 | TerminatorKind::Assert { cleanup: ref mut unwind, .. }
339 | TerminatorKind::DropAndReplace { ref mut unwind, .. }
340 | TerminatorKind::Drop { ref mut unwind, .. }
341 | TerminatorKind::FalseUnwind { ref mut unwind, .. } => Some(unwind),
346 impl<'tcx> Debug for TerminatorKind<'tcx> {
347 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
349 let successor_count = self.successors().count();
350 let labels = self.fmt_successor_labels();
351 assert_eq!(successor_count, labels.len());
353 match successor_count {
356 1 => write!(fmt, " -> {:?}", self.successors().next().unwrap()),
359 write!(fmt, " -> [")?;
360 for (i, target) in self.successors().enumerate() {
364 write!(fmt, "{}: {:?}", labels[i], target)?;
372 impl<'tcx> TerminatorKind<'tcx> {
373 /// Writes the "head" part of the terminator; that is, its name and the data it uses to pick the
374 /// successor basic block, if any. The only information not included is the list of possible
375 /// successors, which may be rendered differently between the text and the graphviz format.
376 pub fn fmt_head<W: Write>(&self, fmt: &mut W) -> fmt::Result {
377 use self::TerminatorKind::*;
379 Goto { .. } => write!(fmt, "goto"),
380 SwitchInt { discr, .. } => write!(fmt, "switchInt({:?})", discr),
381 Return => write!(fmt, "return"),
382 GeneratorDrop => write!(fmt, "generator_drop"),
383 Resume => write!(fmt, "resume"),
384 Abort => write!(fmt, "abort"),
385 Yield { value, resume_arg, .. } => write!(fmt, "{:?} = yield({:?})", resume_arg, value),
386 Unreachable => write!(fmt, "unreachable"),
387 Drop { place, .. } => write!(fmt, "drop({:?})", place),
388 DropAndReplace { place, value, .. } => {
389 write!(fmt, "replace({:?} <- {:?})", place, value)
391 Call { func, args, destination, .. } => {
392 if let Some((destination, _)) = destination {
393 write!(fmt, "{:?} = ", destination)?;
395 write!(fmt, "{:?}(", func)?;
396 for (index, arg) in args.iter().enumerate() {
400 write!(fmt, "{:?}", arg)?;
404 Assert { cond, expected, msg, .. } => {
405 write!(fmt, "assert(")?;
409 write!(fmt, "{:?}, ", cond)?;
410 msg.fmt_assert_args(fmt)?;
413 FalseEdge { .. } => write!(fmt, "falseEdge"),
414 FalseUnwind { .. } => write!(fmt, "falseUnwind"),
415 InlineAsm { template, ref operands, options, .. } => {
416 write!(fmt, "asm!(\"{}\"", InlineAsmTemplatePiece::to_string(template))?;
419 let print_late = |&late| if late { "late" } else { "" };
421 InlineAsmOperand::In { reg, value } => {
422 write!(fmt, "in({}) {:?}", reg, value)?;
424 InlineAsmOperand::Out { reg, late, place: Some(place) } => {
425 write!(fmt, "{}out({}) {:?}", print_late(late), reg, place)?;
427 InlineAsmOperand::Out { reg, late, place: None } => {
428 write!(fmt, "{}out({}) _", print_late(late), reg)?;
430 InlineAsmOperand::InOut {
434 out_place: Some(out_place),
438 "in{}out({}) {:?} => {:?}",
445 InlineAsmOperand::InOut { reg, late, in_value, out_place: None } => {
446 write!(fmt, "in{}out({}) {:?} => _", print_late(late), reg, in_value)?;
448 InlineAsmOperand::Const { value } => {
449 write!(fmt, "const {:?}", value)?;
451 InlineAsmOperand::SymFn { value } => {
452 write!(fmt, "sym_fn {:?}", value)?;
454 InlineAsmOperand::SymStatic { def_id } => {
455 write!(fmt, "sym_static {:?}", def_id)?;
459 write!(fmt, ", options({:?}))", options)
464 /// Returns the list of labels for the edges to the successor basic blocks.
465 pub fn fmt_successor_labels(&self) -> Vec<Cow<'static, str>> {
466 use self::TerminatorKind::*;
468 Return | Resume | Abort | Unreachable | GeneratorDrop => vec![],
469 Goto { .. } => vec!["".into()],
470 SwitchInt { ref values, switch_ty, .. } => ty::tls::with(|tcx| {
471 let param_env = ty::ParamEnv::empty();
472 let switch_ty = tcx.lift(&switch_ty).unwrap();
473 let size = tcx.layout_of(param_env.and(switch_ty)).unwrap().size;
477 ty::Const::from_scalar(tcx, Scalar::from_uint(u, size), switch_ty)
481 .chain(iter::once("otherwise".into()))
484 Call { destination: Some(_), cleanup: Some(_), .. } => {
485 vec!["return".into(), "unwind".into()]
487 Call { destination: Some(_), cleanup: None, .. } => vec!["return".into()],
488 Call { destination: None, cleanup: Some(_), .. } => vec!["unwind".into()],
489 Call { destination: None, cleanup: None, .. } => vec![],
490 Yield { drop: Some(_), .. } => vec!["resume".into(), "drop".into()],
491 Yield { drop: None, .. } => vec!["resume".into()],
492 DropAndReplace { unwind: None, .. } | Drop { unwind: None, .. } => {
493 vec!["return".into()]
495 DropAndReplace { unwind: Some(_), .. } | Drop { unwind: Some(_), .. } => {
496 vec!["return".into(), "unwind".into()]
498 Assert { cleanup: None, .. } => vec!["".into()],
499 Assert { .. } => vec!["success".into(), "unwind".into()],
500 FalseEdge { .. } => vec!["real".into(), "imaginary".into()],
501 FalseUnwind { unwind: Some(_), .. } => vec!["real".into(), "cleanup".into()],
502 FalseUnwind { unwind: None, .. } => vec!["real".into()],
503 InlineAsm { destination: Some(_), .. } => vec!["".into()],
504 InlineAsm { destination: None, .. } => vec![],