1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! The MIR is built from some high-level abstract IR
12 //! (HAIR). This section defines the HAIR along with a trait for
13 //! accessing it. The intention is to allow MIR construction to be
14 //! unit-tested and separated from the Rust source and compiler data
17 use rustc::mir::{BinOp, BorrowKind, Field, UnOp};
18 use rustc::hir::def_id::DefId;
19 use rustc::middle::region;
20 use rustc::ty::subst::Substs;
21 use rustc::ty::{AdtDef, CanonicalTy, UpvarSubsts, Region, Ty, Const};
30 pub use self::pattern::{BindingMode, Pattern, PatternKind, FieldPattern};
32 #[derive(Copy, Clone, Debug)]
39 pub fn is_explicit(self) -> bool {
41 LintLevel::Inherited => false,
42 LintLevel::Explicit(_) => true
47 #[derive(Clone, Debug)]
48 pub struct Block<'tcx> {
49 pub targeted_by_break: bool,
50 pub region_scope: region::Scope,
51 pub opt_destruction_scope: Option<region::Scope>,
53 pub stmts: Vec<StmtRef<'tcx>>,
54 pub expr: Option<ExprRef<'tcx>>,
55 pub safety_mode: BlockSafety,
58 #[derive(Copy, Clone, Debug)]
59 pub enum BlockSafety {
61 ExplicitUnsafe(ast::NodeId),
66 #[derive(Clone, Debug)]
67 pub enum StmtRef<'tcx> {
68 Mirror(Box<Stmt<'tcx>>),
71 #[derive(Clone, Debug)]
72 pub struct Stmt<'tcx> {
73 pub kind: StmtKind<'tcx>,
74 pub opt_destruction_scope: Option<region::Scope>,
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 /// shortlived. 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>>,
156 }, // NOT overloaded!
161 }, // NOT overloaded!
166 }, // NOT overloaded!
167 // LogicalOp is distinct from BinaryOp because of lazy evaluation of the operands.
171 }, // NOT overloaded!
173 source: ExprRef<'tcx>,
176 source: ExprRef<'tcx>,
177 }, // Use a lexpr to get a vexpr.
179 source: ExprRef<'tcx>,
182 source: ExprRef<'tcx>,
185 source: ExprRef<'tcx>,
188 source: ExprRef<'tcx>,
191 source: ExprRef<'tcx>,
194 condition: ExprRef<'tcx>,
196 otherwise: Option<ExprRef<'tcx>>,
199 condition: Option<ExprRef<'tcx>>,
203 discriminant: ExprRef<'tcx>,
204 arms: Vec<Arm<'tcx>>,
207 body: &'tcx hir::Block,
224 index: ExprRef<'tcx>,
229 /// first argument, used for self in a closure
235 region: Region<'tcx>,
236 borrow_kind: BorrowKind,
240 label: region::Scope,
241 value: Option<ExprRef<'tcx>>,
244 label: region::Scope,
247 value: Option<ExprRef<'tcx>>,
250 value: ExprRef<'tcx>,
254 fields: Vec<ExprRef<'tcx>>,
257 fields: Vec<ExprRef<'tcx>>,
260 adt_def: &'tcx AdtDef,
261 variant_index: usize,
262 substs: &'tcx Substs<'tcx>,
264 /// Optional user-given substs: for something like `let x =
265 /// Bar::<T> { ... }`.
266 user_ty: Option<CanonicalTy<'tcx>>,
268 fields: Vec<FieldExprRef<'tcx>>,
269 base: Option<FruInfo<'tcx>>
271 PlaceTypeAscription {
272 source: ExprRef<'tcx>,
273 /// Type that the user gave to this expression
274 user_ty: CanonicalTy<'tcx>,
276 ValueTypeAscription {
277 source: ExprRef<'tcx>,
278 /// Type that the user gave to this expression
279 user_ty: CanonicalTy<'tcx>,
283 substs: UpvarSubsts<'tcx>,
284 upvars: Vec<ExprRef<'tcx>>,
285 movability: Option<hir::GeneratorMovability>,
288 literal: &'tcx Const<'tcx>,
290 /// Optional user-given type: for something like
291 /// `collect::<Vec<_>>`, this would be present and would
292 /// indicate that `Vec<_>` was explicitly specified.
294 /// Needed for NLL to impose user-given type constraints.
295 user_ty: Option<CanonicalTy<'tcx>>,
298 asm: &'tcx hir::InlineAsm,
299 outputs: Vec<ExprRef<'tcx>>,
300 inputs: Vec<ExprRef<'tcx>>
303 value: ExprRef<'tcx>,
307 #[derive(Clone, Debug)]
308 pub enum ExprRef<'tcx> {
309 Hair(&'tcx hir::Expr),
310 Mirror(Box<Expr<'tcx>>),
313 #[derive(Clone, Debug)]
314 pub struct FieldExprRef<'tcx> {
316 pub expr: ExprRef<'tcx>,
319 #[derive(Clone, Debug)]
320 pub struct FruInfo<'tcx> {
321 pub base: ExprRef<'tcx>,
322 pub field_types: Vec<Ty<'tcx>>
325 #[derive(Clone, Debug)]
326 pub struct Arm<'tcx> {
327 pub patterns: Vec<Pattern<'tcx>>,
328 pub guard: Option<Guard<'tcx>>,
329 pub body: ExprRef<'tcx>,
330 pub lint_level: LintLevel,
333 #[derive(Clone, Debug)]
334 pub enum Guard<'tcx> {
338 #[derive(Copy, Clone, Debug)]
344 impl<'tcx> ExprRef<'tcx> {
345 pub fn span(&self) -> Span {
347 ExprRef::Hair(expr) => expr.span,
348 ExprRef::Mirror(expr) => expr.span,
353 ///////////////////////////////////////////////////////////////////////////
356 /// "Mirroring" is the process of converting from a HIR type into one
357 /// of the HAIR types defined in this file. This is basically a "on
358 /// the fly" desugaring step that hides a lot of the messiness in the
359 /// tcx. For example, the mirror of a `&'tcx hir::Expr` is an
362 /// Mirroring is gradual: when you mirror an outer expression like `e1
363 /// + e2`, the references to the inner expressions `e1` and `e2` are
364 /// `ExprRef<'tcx>` instances, and they may or may not be eagerly
365 /// mirrored. This allows a single AST node from the compiler to
366 /// expand into one or more Hair nodes, which lets the Hair nodes be
368 pub trait Mirror<'tcx> {
371 fn make_mirror<'a, 'gcx>(self, cx: &mut Cx<'a, 'gcx, 'tcx>) -> Self::Output;
374 impl<'tcx> Mirror<'tcx> for Expr<'tcx> {
375 type Output = Expr<'tcx>;
377 fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Expr<'tcx> {
382 impl<'tcx> Mirror<'tcx> for ExprRef<'tcx> {
383 type Output = Expr<'tcx>;
385 fn make_mirror<'a, 'gcx>(self, hir: &mut Cx<'a, 'gcx, 'tcx>) -> Expr<'tcx> {
387 ExprRef::Hair(h) => h.make_mirror(hir),
388 ExprRef::Mirror(m) => *m,
393 impl<'tcx> Mirror<'tcx> for Stmt<'tcx> {
394 type Output = Stmt<'tcx>;
396 fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Stmt<'tcx> {
401 impl<'tcx> Mirror<'tcx> for StmtRef<'tcx> {
402 type Output = Stmt<'tcx>;
404 fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Stmt<'tcx> {
406 StmtRef::Mirror(m) => *m,
411 impl<'tcx> Mirror<'tcx> for Block<'tcx> {
412 type Output = Block<'tcx>;
414 fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Block<'tcx> {