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 translated 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_const_math::ConstUsize;
18 use rustc::mir::{BinOp, BorrowKind, Field, Literal, UnOp};
19 use rustc::hir::def_id::DefId;
20 use rustc::middle::region::CodeExtent;
21 use rustc::ty::subst::Substs;
22 use rustc::ty::{self, AdtDef, ClosureSubsts, Region, Ty};
30 pub use rustc_const_eval::pattern::{BindingMode, Pattern, PatternKind, FieldPattern};
32 #[derive(Clone, Debug)]
33 pub struct Block<'tcx> {
34 pub targeted_by_break: bool,
35 pub extent: CodeExtent,
37 pub stmts: Vec<StmtRef<'tcx>>,
38 pub expr: Option<ExprRef<'tcx>>,
41 #[derive(Clone, Debug)]
42 pub enum StmtRef<'tcx> {
43 Mirror(Box<Stmt<'tcx>>),
46 #[derive(Clone, Debug)]
47 pub struct Stmt<'tcx> {
49 pub kind: StmtKind<'tcx>,
52 #[derive(Clone, Debug)]
53 pub enum StmtKind<'tcx> {
55 /// scope for this statement; may be used as lifetime of temporaries
58 /// expression being evaluated in this statement
63 /// scope for variables bound in this let; covers this and
64 /// remaining statements in block
65 remainder_scope: CodeExtent,
67 /// scope for the initialization itself; might be used as
68 /// lifetime of temporaries
69 init_scope: CodeExtent,
72 pattern: Pattern<'tcx>,
74 /// let pat = <INIT> ...
75 initializer: Option<ExprRef<'tcx>>
79 /// The Hair trait implementor translates their expressions (`&'tcx H::Expr`)
80 /// into instances of this `Expr` enum. This translation can be done
81 /// basically as lazilly or as eagerly as desired: every recursive
82 /// reference to an expression in this enum is an `ExprRef<'tcx>`, which
83 /// may in turn be another instance of this enum (boxed), or else an
84 /// untranslated `&'tcx H::Expr`. Note that instances of `Expr` are very
85 /// shortlived. They are created by `Hair::to_expr`, analyzed and
86 /// converted into MIR, and then discarded.
88 /// If you compare `Expr` to the full compiler AST, you will see it is
89 /// a good bit simpler. In fact, a number of the more straight-forward
90 /// MIR simplifications are already done in the impl of `Hair`. For
91 /// example, method calls and overloaded operators are absent: they are
92 /// expected to be converted into `Expr::Call` instances.
93 #[derive(Clone, Debug)]
94 pub struct Expr<'tcx> {
95 /// type of this expression
98 /// lifetime of this expression if it should be spilled into a
99 /// temporary; should be None only if in a constant context
100 pub temp_lifetime: Option<CodeExtent>,
102 /// span of the expression in the source
105 /// kind of expression
106 pub kind: ExprKind<'tcx>,
109 #[derive(Clone, Debug)]
110 pub enum ExprKind<'tcx> {
113 value: ExprRef<'tcx>,
116 value: ExprRef<'tcx>,
117 value_extents: CodeExtent,
122 args: Vec<ExprRef<'tcx>>,
126 }, // NOT overloaded!
131 }, // NOT overloaded!
136 }, // NOT overloaded!
137 // LogicalOp is distinct from BinaryOp because of lazy evaluation of the operands.
141 }, // NOT overloaded!
143 source: ExprRef<'tcx>,
146 source: ExprRef<'tcx>,
147 }, // Use a lexpr to get a vexpr.
149 source: ExprRef<'tcx>,
152 source: ExprRef<'tcx>,
155 source: ExprRef<'tcx>,
158 source: ExprRef<'tcx>,
161 source: ExprRef<'tcx>,
164 condition: ExprRef<'tcx>,
166 otherwise: Option<ExprRef<'tcx>>,
169 condition: Option<ExprRef<'tcx>>,
173 discriminant: ExprRef<'tcx>,
174 arms: Vec<Arm<'tcx>>,
177 body: &'tcx hir::Block,
194 index: ExprRef<'tcx>,
199 /// first argument, used for self in a closure
205 region: Region<'tcx>,
206 borrow_kind: BorrowKind,
211 value: Option<ExprRef<'tcx>>,
217 value: Option<ExprRef<'tcx>>,
220 value: ExprRef<'tcx>,
224 fields: Vec<ExprRef<'tcx>>,
227 fields: Vec<ExprRef<'tcx>>,
230 adt_def: &'tcx AdtDef,
231 variant_index: usize,
232 substs: &'tcx Substs<'tcx>,
233 fields: Vec<FieldExprRef<'tcx>>,
234 base: Option<FruInfo<'tcx>>
238 substs: ClosureSubsts<'tcx>,
239 upvars: Vec<ExprRef<'tcx>>,
242 literal: Literal<'tcx>,
245 asm: &'tcx hir::InlineAsm,
246 outputs: Vec<ExprRef<'tcx>>,
247 inputs: Vec<ExprRef<'tcx>>
251 #[derive(Clone, Debug)]
252 pub enum ExprRef<'tcx> {
253 Hair(&'tcx hir::Expr),
254 Mirror(Box<Expr<'tcx>>),
257 #[derive(Clone, Debug)]
258 pub struct FieldExprRef<'tcx> {
260 pub expr: ExprRef<'tcx>,
263 #[derive(Clone, Debug)]
264 pub struct FruInfo<'tcx> {
265 pub base: ExprRef<'tcx>,
266 pub field_types: Vec<Ty<'tcx>>
269 #[derive(Clone, Debug)]
270 pub struct Arm<'tcx> {
271 pub patterns: Vec<Pattern<'tcx>>,
272 pub guard: Option<ExprRef<'tcx>>,
273 pub body: ExprRef<'tcx>,
276 #[derive(Copy, Clone, Debug)]
282 ///////////////////////////////////////////////////////////////////////////
285 /// "Mirroring" is the process of converting from a HIR type into one
286 /// of the HAIR types defined in this file. This is basically a "on
287 /// the fly" desugaring step that hides a lot of the messiness in the
288 /// tcx. For example, the mirror of a `&'tcx hir::Expr` is an
291 /// Mirroring is gradual: when you mirror an outer expression like `e1
292 /// + e2`, the references to the inner expressions `e1` and `e2` are
293 /// `ExprRef<'tcx>` instances, and they may or may not be eagerly
294 /// mirrored. This allows a single AST node from the compiler to
295 /// expand into one or more Hair nodes, which lets the Hair nodes be
297 pub trait Mirror<'tcx> {
300 fn make_mirror<'a, 'gcx>(self, cx: &mut Cx<'a, 'gcx, 'tcx>) -> Self::Output;
303 impl<'tcx> Mirror<'tcx> for Expr<'tcx> {
304 type Output = Expr<'tcx>;
306 fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Expr<'tcx> {
311 impl<'tcx> Mirror<'tcx> for ExprRef<'tcx> {
312 type Output = Expr<'tcx>;
314 fn make_mirror<'a, 'gcx>(self, hir: &mut Cx<'a, 'gcx, 'tcx>) -> Expr<'tcx> {
316 ExprRef::Hair(h) => h.make_mirror(hir),
317 ExprRef::Mirror(m) => *m,
322 impl<'tcx> Mirror<'tcx> for Stmt<'tcx> {
323 type Output = Stmt<'tcx>;
325 fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Stmt<'tcx> {
330 impl<'tcx> Mirror<'tcx> for StmtRef<'tcx> {
331 type Output = Stmt<'tcx>;
333 fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Stmt<'tcx> {
335 StmtRef::Mirror(m) => *m,
340 impl<'tcx> Mirror<'tcx> for Block<'tcx> {
341 type Output = Block<'tcx>;
343 fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Block<'tcx> {