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::mir::repr::{BinOp, BorrowKind, Field, Literal, Mutability, UnOp};
18 use rustc::middle::def_id::DefId;
19 use rustc::middle::region::CodeExtent;
20 use rustc::middle::subst::Substs;
21 use rustc::middle::ty::{AdtDef, ClosureSubsts, Region, Ty};
24 use syntax::codemap::Span;
29 #[derive(Clone, Debug)]
30 pub struct ItemRef<'tcx> {
33 pub substs: &'tcx Substs<'tcx>,
36 #[derive(Clone, Debug)]
37 pub struct Block<'tcx> {
38 pub extent: CodeExtent,
40 pub stmts: Vec<StmtRef<'tcx>>,
41 pub expr: Option<ExprRef<'tcx>>,
44 #[derive(Clone, Debug)]
45 pub enum StmtRef<'tcx> {
46 Mirror(Box<Stmt<'tcx>>),
49 #[derive(Clone, Debug)]
50 pub struct Stmt<'tcx> {
52 pub kind: StmtKind<'tcx>,
55 #[derive(Clone, Debug)]
56 pub enum StmtKind<'tcx> {
58 /// scope for this statement; may be used as lifetime of temporaries
61 /// expression being evaluated in this statement
66 /// scope for variables bound in this let; covers this and
67 /// remaining statements in block
68 remainder_scope: CodeExtent,
70 /// scope for the initialization itself; might be used as
71 /// lifetime of temporaries
72 init_scope: CodeExtent,
75 pattern: Pattern<'tcx>,
77 /// let pat = <INIT> ...
78 initializer: Option<ExprRef<'tcx>>,
80 /// let pat = init; <STMTS>
81 stmts: Vec<StmtRef<'tcx>>,
85 // The Hair trait implementor translates their expressions (`&'tcx H::Expr`)
86 // into instances of this `Expr` enum. This translation can be done
87 // basically as lazilly or as eagerly as desired: every recursive
88 // reference to an expression in this enum is an `ExprRef<'tcx>`, which
89 // may in turn be another instance of this enum (boxed), or else an
90 // untranslated `&'tcx H::Expr`. Note that instances of `Expr` are very
91 // shortlived. They are created by `Hair::to_expr`, analyzed and
92 // converted into MIR, and then discarded.
94 // If you compare `Expr` to the full compiler AST, you will see it is
95 // a good bit simpler. In fact, a number of the more straight-forward
96 // MIR simplifications are already done in the impl of `Hair`. For
97 // example, method calls and overloaded operators are absent: they are
98 // expected to be converted into `Expr::Call` instances.
99 #[derive(Clone, Debug)]
100 pub struct Expr<'tcx> {
101 // type of this expression
104 // lifetime of this expression if it should be spilled into a
105 // temporary; should be None only if in a constant context
106 pub temp_lifetime: Option<CodeExtent>,
108 // span of the expression in the source
111 // kind of expression
112 pub kind: ExprKind<'tcx>,
115 #[derive(Clone, Debug)]
116 pub enum ExprKind<'tcx> {
119 value: ExprRef<'tcx>,
122 value: ExprRef<'tcx>,
126 args: Vec<ExprRef<'tcx>>,
130 }, // NOT overloaded!
135 }, // NOT overloaded!
144 }, // NOT overloaded!
146 source: ExprRef<'tcx>,
149 source: ExprRef<'tcx>,
152 source: ExprRef<'tcx>,
155 source: ExprRef<'tcx>,
158 condition: ExprRef<'tcx>,
160 otherwise: Option<ExprRef<'tcx>>,
163 condition: Option<ExprRef<'tcx>>,
167 discriminant: ExprRef<'tcx>,
168 arms: Vec<Arm<'tcx>>,
171 body: &'tcx hir::Block,
188 index: ExprRef<'tcx>,
193 SelfRef, // first argument, used for self in a closure
199 borrow_kind: BorrowKind,
203 label: Option<CodeExtent>,
206 label: Option<CodeExtent>,
209 value: Option<ExprRef<'tcx>>,
212 value: ExprRef<'tcx>,
213 // FIXME(#29789): Add a separate hair::Constant<'tcx> so this could be more explicit about
214 // its contained data. Currently this should only contain expression of ExprKind::Literal
216 count: ExprRef<'tcx>,
219 fields: Vec<ExprRef<'tcx>>,
222 fields: Vec<ExprRef<'tcx>>,
225 adt_def: AdtDef<'tcx>,
226 variant_index: usize,
227 substs: &'tcx Substs<'tcx>,
228 fields: Vec<FieldExprRef<'tcx>>,
229 base: Option<ExprRef<'tcx>>,
233 substs: &'tcx ClosureSubsts<'tcx>,
234 upvars: Vec<ExprRef<'tcx>>,
237 literal: Literal<'tcx>,
240 asm: &'tcx hir::InlineAsm,
244 #[derive(Clone, Debug)]
245 pub enum ExprRef<'tcx> {
246 Hair(&'tcx hir::Expr),
247 Mirror(Box<Expr<'tcx>>),
250 #[derive(Clone, Debug)]
251 pub struct FieldExprRef<'tcx> {
253 pub expr: ExprRef<'tcx>,
256 #[derive(Clone, Debug)]
257 pub struct Arm<'tcx> {
258 pub patterns: Vec<Pattern<'tcx>>,
259 pub guard: Option<ExprRef<'tcx>>,
260 pub body: ExprRef<'tcx>,
263 #[derive(Clone, Debug)]
264 pub struct Pattern<'tcx> {
267 pub kind: Box<PatternKind<'tcx>>,
270 #[derive(Copy, Clone, Debug)]
276 #[derive(Clone, Debug)]
277 pub enum PatternKind<'tcx> {
280 // x, ref x, x @ P, etc
282 mutability: Mutability,
287 subpattern: Option<Pattern<'tcx>>,
290 // Foo(...) or Foo{...} or Foo, where `Foo` is a variant name from an adt with >1 variants
292 adt_def: AdtDef<'tcx>,
293 variant_index: usize,
294 subpatterns: Vec<FieldPattern<'tcx>>,
297 // (...), Foo(...), Foo{...}, or Foo, where `Foo` is a variant name from an adt with 1 variant
299 subpatterns: Vec<FieldPattern<'tcx>>,
303 subpattern: Pattern<'tcx>,
304 }, // box P, &P, &mut P, etc
307 value: Literal<'tcx>,
315 // matches against a slice, checking the length and extracting elements
317 prefix: Vec<Pattern<'tcx>>,
318 slice: Option<Pattern<'tcx>>,
319 suffix: Vec<Pattern<'tcx>>,
322 // fixed match against an array, irrefutable
324 prefix: Vec<Pattern<'tcx>>,
325 slice: Option<Pattern<'tcx>>,
326 suffix: Vec<Pattern<'tcx>>,
330 #[derive(Copy, Clone, Debug)]
331 pub enum BindingMode {
333 ByRef(Region, BorrowKind),
336 #[derive(Clone, Debug)]
337 pub struct FieldPattern<'tcx> {
339 pub pattern: Pattern<'tcx>,
342 ///////////////////////////////////////////////////////////////////////////
345 /// "Mirroring" is the process of converting from a HIR type into one
346 /// of the HAIR types defined in this file. This is basically a "on
347 /// the fly" desugaring step that hides a lot of the messiness in the
348 /// tcx. For example, the mirror of a `&'tcx hir::Expr` is an
351 /// Mirroring is gradual: when you mirror an outer expression like `e1
352 /// + e2`, the references to the inner expressions `e1` and `e2` are
353 /// `ExprRef<'tcx>` instances, and they may or may not be eagerly
354 /// mirrored. This allows a single AST node from the compiler to
355 /// expand into one or more Hair nodes, which lets the Hair nodes be
357 pub trait Mirror<'tcx> {
360 fn make_mirror<'a>(self, cx: &mut Cx<'a, 'tcx>) -> Self::Output;
363 impl<'tcx> Mirror<'tcx> for Expr<'tcx> {
364 type Output = Expr<'tcx>;
366 fn make_mirror<'a>(self, _: &mut Cx<'a, 'tcx>) -> Expr<'tcx> {
371 impl<'tcx> Mirror<'tcx> for ExprRef<'tcx> {
372 type Output = Expr<'tcx>;
374 fn make_mirror<'a>(self, hir: &mut Cx<'a, 'tcx>) -> Expr<'tcx> {
376 ExprRef::Hair(h) => h.make_mirror(hir),
377 ExprRef::Mirror(m) => *m,
382 impl<'tcx> Mirror<'tcx> for Stmt<'tcx> {
383 type Output = Stmt<'tcx>;
385 fn make_mirror<'a>(self, _: &mut Cx<'a, 'tcx>) -> Stmt<'tcx> {
390 impl<'tcx> Mirror<'tcx> for StmtRef<'tcx> {
391 type Output = Stmt<'tcx>;
393 fn make_mirror<'a>(self, _: &mut Cx<'a,'tcx>) -> Stmt<'tcx> {
395 StmtRef::Mirror(m) => *m,
400 impl<'tcx> Mirror<'tcx> for Block<'tcx> {
401 type Output = Block<'tcx>;
403 fn make_mirror<'a>(self, _: &mut Cx<'a, 'tcx>) -> Block<'tcx> {