1 //! The MIR is built from some high-level abstract IR
2 //! (HAIR). This section defines the HAIR along with a trait for
3 //! accessing it. The intention is to allow MIR construction to be
4 //! unit-tested and separated from the Rust source and compiler data
7 use rustc::mir::{BinOp, BorrowKind, Field, UnOp};
8 use rustc::hir::def_id::DefId;
9 use rustc::infer::canonical::Canonical;
10 use rustc::middle::region;
11 use rustc::ty::subst::SubstsRef;
12 use rustc::ty::{AdtDef, UpvarSubsts, Ty, Const, UserType};
13 use rustc::ty::adjustment::{PointerCast};
14 use rustc::ty::layout::VariantIdx;
23 pub use self::pattern::{BindingMode, Pattern, PatternKind, PatternRange, FieldPattern};
24 pub(crate) use self::pattern::PatternTypeProjection;
28 #[derive(Copy, Clone, Debug)]
35 pub fn is_explicit(self) -> bool {
37 LintLevel::Inherited => false,
38 LintLevel::Explicit(_) => true
43 #[derive(Clone, Debug)]
44 pub struct Block<'tcx> {
45 pub targeted_by_break: bool,
46 pub region_scope: region::Scope,
47 pub opt_destruction_scope: Option<region::Scope>,
49 pub stmts: Vec<StmtRef<'tcx>>,
50 pub expr: Option<ExprRef<'tcx>>,
51 pub safety_mode: BlockSafety,
54 #[derive(Copy, Clone, Debug)]
55 pub enum BlockSafety {
57 ExplicitUnsafe(hir::HirId),
62 #[derive(Clone, Debug)]
63 pub enum StmtRef<'tcx> {
64 Mirror(Box<Stmt<'tcx>>),
67 #[derive(Clone, Debug)]
68 pub struct StatementSpan(pub Span);
70 #[derive(Clone, Debug)]
71 pub struct Stmt<'tcx> {
72 pub kind: StmtKind<'tcx>,
73 pub opt_destruction_scope: Option<region::Scope>,
74 pub span: StatementSpan,
77 #[derive(Clone, Debug)]
78 pub enum StmtKind<'tcx> {
80 /// scope for this statement; may be used as lifetime of temporaries
83 /// expression being evaluated in this statement
88 /// scope for variables bound in this let; covers this and
89 /// remaining statements in block
90 remainder_scope: region::Scope,
92 /// scope for the initialization itself; might be used as
93 /// lifetime of temporaries
94 init_scope: region::Scope,
98 /// if a type is included, it is added as an ascription pattern
99 pattern: Pattern<'tcx>,
101 /// let pat: ty = <INIT> ...
102 initializer: Option<ExprRef<'tcx>>,
104 /// the lint level for this let-statement
105 lint_level: LintLevel,
109 /// The Hair trait implementor lowers their expressions (`&'tcx H::Expr`)
110 /// into instances of this `Expr` enum. This lowering can be done
111 /// basically as lazily or as eagerly as desired: every recursive
112 /// reference to an expression in this enum is an `ExprRef<'tcx>`, which
113 /// may in turn be another instance of this enum (boxed), or else an
114 /// unlowered `&'tcx H::Expr`. Note that instances of `Expr` are very
115 /// short-lived. They are created by `Hair::to_expr`, analyzed and
116 /// converted into MIR, and then discarded.
118 /// If you compare `Expr` to the full compiler AST, you will see it is
119 /// a good bit simpler. In fact, a number of the more straight-forward
120 /// MIR simplifications are already done in the impl of `Hair`. For
121 /// example, method calls and overloaded operators are absent: they are
122 /// expected to be converted into `Expr::Call` instances.
123 #[derive(Clone, Debug)]
124 pub struct Expr<'tcx> {
125 /// type of this expression
128 /// lifetime of this expression if it should be spilled into a
129 /// temporary; should be None only if in a constant context
130 pub temp_lifetime: Option<region::Scope>,
132 /// span of the expression in the source
135 /// kind of expression
136 pub kind: ExprKind<'tcx>,
139 #[derive(Clone, Debug)]
140 pub enum ExprKind<'tcx> {
142 region_scope: region::Scope,
143 lint_level: LintLevel,
144 value: ExprRef<'tcx>,
147 value: ExprRef<'tcx>,
152 args: Vec<ExprRef<'tcx>>,
153 // Whether this is from a call in HIR, rather than from an overloaded
154 // operator. True for overloaded function call.
159 }, // NOT overloaded!
164 }, // NOT overloaded!
169 }, // NOT overloaded!
170 // LogicalOp is distinct from BinaryOp because of lazy evaluation of the operands.
174 }, // NOT overloaded!
176 source: ExprRef<'tcx>,
179 source: ExprRef<'tcx>,
180 }, // Use a lexpr to get a vexpr.
182 source: ExprRef<'tcx>,
186 source: ExprRef<'tcx>,
189 condition: ExprRef<'tcx>,
191 otherwise: Option<ExprRef<'tcx>>,
194 condition: Option<ExprRef<'tcx>>,
198 scrutinee: ExprRef<'tcx>,
199 arms: Vec<Arm<'tcx>>,
202 body: &'tcx hir::Block,
219 index: ExprRef<'tcx>,
224 /// first argument, used for self in a closure
230 borrow_kind: BorrowKind,
234 label: region::Scope,
235 value: Option<ExprRef<'tcx>>,
238 label: region::Scope,
241 value: Option<ExprRef<'tcx>>,
244 value: ExprRef<'tcx>,
248 fields: Vec<ExprRef<'tcx>>,
251 fields: Vec<ExprRef<'tcx>>,
254 adt_def: &'tcx AdtDef,
255 variant_index: VariantIdx,
256 substs: SubstsRef<'tcx>,
258 /// Optional user-given substs: for something like `let x =
259 /// Bar::<T> { ... }`.
260 user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
262 fields: Vec<FieldExprRef<'tcx>>,
263 base: Option<FruInfo<'tcx>>
265 PlaceTypeAscription {
266 source: ExprRef<'tcx>,
267 /// Type that the user gave to this expression
268 user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
270 ValueTypeAscription {
271 source: ExprRef<'tcx>,
272 /// Type that the user gave to this expression
273 user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
277 substs: UpvarSubsts<'tcx>,
278 upvars: Vec<ExprRef<'tcx>>,
279 movability: Option<hir::GeneratorMovability>,
282 literal: &'tcx Const<'tcx>,
283 user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
286 asm: &'tcx hir::InlineAsm,
287 outputs: Vec<ExprRef<'tcx>>,
288 inputs: Vec<ExprRef<'tcx>>
291 value: ExprRef<'tcx>,
295 #[derive(Clone, Debug)]
296 pub enum ExprRef<'tcx> {
297 Hair(&'tcx hir::Expr),
298 Mirror(Box<Expr<'tcx>>),
301 #[derive(Clone, Debug)]
302 pub struct FieldExprRef<'tcx> {
304 pub expr: ExprRef<'tcx>,
307 #[derive(Clone, Debug)]
308 pub struct FruInfo<'tcx> {
309 pub base: ExprRef<'tcx>,
310 pub field_types: Vec<Ty<'tcx>>
313 #[derive(Clone, Debug)]
314 pub struct Arm<'tcx> {
315 pub patterns: Vec<Pattern<'tcx>>,
316 pub guard: Option<Guard<'tcx>>,
317 pub body: ExprRef<'tcx>,
318 pub lint_level: LintLevel,
321 #[derive(Clone, Debug)]
322 pub enum Guard<'tcx> {
326 #[derive(Copy, Clone, Debug)]
332 impl<'tcx> ExprRef<'tcx> {
333 pub fn span(&self) -> Span {
335 ExprRef::Hair(expr) => expr.span,
336 ExprRef::Mirror(expr) => expr.span,
341 ///////////////////////////////////////////////////////////////////////////
344 /// "Mirroring" is the process of converting from a HIR type into one
345 /// of the HAIR types defined in this file. This is basically a "on
346 /// the fly" desugaring step that hides a lot of the messiness in the
347 /// tcx. For example, the mirror of a `&'tcx hir::Expr` is an
350 /// Mirroring is gradual: when you mirror an outer expression like `e1
351 /// + e2`, the references to the inner expressions `e1` and `e2` are
352 /// `ExprRef<'tcx>` instances, and they may or may not be eagerly
353 /// mirrored. This allows a single AST node from the compiler to
354 /// expand into one or more Hair nodes, which lets the Hair nodes be
356 pub trait Mirror<'tcx> {
359 fn make_mirror<'a, 'gcx>(self, cx: &mut Cx<'a, 'gcx, 'tcx>) -> Self::Output;
362 impl<'tcx> Mirror<'tcx> for Expr<'tcx> {
363 type Output = Expr<'tcx>;
365 fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Expr<'tcx> {
370 impl<'tcx> Mirror<'tcx> for ExprRef<'tcx> {
371 type Output = Expr<'tcx>;
373 fn make_mirror<'a, 'gcx>(self, hir: &mut Cx<'a, 'gcx, 'tcx>) -> Expr<'tcx> {
375 ExprRef::Hair(h) => h.make_mirror(hir),
376 ExprRef::Mirror(m) => *m,
381 impl<'tcx> Mirror<'tcx> for Stmt<'tcx> {
382 type Output = Stmt<'tcx>;
384 fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Stmt<'tcx> {
389 impl<'tcx> Mirror<'tcx> for StmtRef<'tcx> {
390 type Output = Stmt<'tcx>;
392 fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Stmt<'tcx> {
394 StmtRef::Mirror(m) => *m,
399 impl<'tcx> Mirror<'tcx> for Block<'tcx> {
400 type Output = Block<'tcx>;
402 fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Block<'tcx> {