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, UserTypeAnnotation, 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, UpvarSubsts, Region, Ty, Const};
22 use rustc::ty::layout::VariantIdx;
31 pub use self::pattern::{BindingMode, Pattern, PatternKind, FieldPattern};
32 pub(crate) use self::pattern::{PatternTypeProjection, PatternTypeProjections};
36 #[derive(Copy, Clone, Debug)]
43 pub fn is_explicit(self) -> bool {
45 LintLevel::Inherited => false,
46 LintLevel::Explicit(_) => true
51 #[derive(Clone, Debug)]
52 pub struct Block<'tcx> {
53 pub targeted_by_break: bool,
54 pub region_scope: region::Scope,
55 pub opt_destruction_scope: Option<region::Scope>,
57 pub stmts: Vec<StmtRef<'tcx>>,
58 pub expr: Option<ExprRef<'tcx>>,
59 pub safety_mode: BlockSafety,
62 #[derive(Copy, Clone, Debug)]
63 pub enum BlockSafety {
65 ExplicitUnsafe(ast::NodeId),
70 #[derive(Clone, Debug)]
71 pub enum StmtRef<'tcx> {
72 Mirror(Box<Stmt<'tcx>>),
75 #[derive(Clone, Debug)]
76 pub struct StatementSpan(pub Span);
78 #[derive(Clone, Debug)]
79 pub struct Stmt<'tcx> {
80 pub kind: StmtKind<'tcx>,
81 pub opt_destruction_scope: Option<region::Scope>,
82 pub span: StatementSpan,
85 #[derive(Clone, Debug)]
86 pub enum StmtKind<'tcx> {
88 /// scope for this statement; may be used as lifetime of temporaries
91 /// expression being evaluated in this statement
96 /// scope for variables bound in this let; covers this and
97 /// remaining statements in block
98 remainder_scope: region::Scope,
100 /// scope for the initialization itself; might be used as
101 /// lifetime of temporaries
102 init_scope: region::Scope,
104 /// `let <PAT> = ...`
106 /// if a type is included, it is added as an ascription pattern
107 pattern: Pattern<'tcx>,
109 /// let pat: ty = <INIT> ...
110 initializer: Option<ExprRef<'tcx>>,
112 /// the lint level for this let-statement
113 lint_level: LintLevel,
117 /// The Hair trait implementor lowers their expressions (`&'tcx H::Expr`)
118 /// into instances of this `Expr` enum. This lowering can be done
119 /// basically as lazily or as eagerly as desired: every recursive
120 /// reference to an expression in this enum is an `ExprRef<'tcx>`, which
121 /// may in turn be another instance of this enum (boxed), or else an
122 /// unlowered `&'tcx H::Expr`. Note that instances of `Expr` are very
123 /// short-lived. They are created by `Hair::to_expr`, analyzed and
124 /// converted into MIR, and then discarded.
126 /// If you compare `Expr` to the full compiler AST, you will see it is
127 /// a good bit simpler. In fact, a number of the more straight-forward
128 /// MIR simplifications are already done in the impl of `Hair`. For
129 /// example, method calls and overloaded operators are absent: they are
130 /// expected to be converted into `Expr::Call` instances.
131 #[derive(Clone, Debug)]
132 pub struct Expr<'tcx> {
133 /// type of this expression
136 /// lifetime of this expression if it should be spilled into a
137 /// temporary; should be None only if in a constant context
138 pub temp_lifetime: Option<region::Scope>,
140 /// span of the expression in the source
143 /// kind of expression
144 pub kind: ExprKind<'tcx>,
147 #[derive(Clone, Debug)]
148 pub enum ExprKind<'tcx> {
150 region_scope: region::Scope,
151 lint_level: LintLevel,
152 value: ExprRef<'tcx>,
155 value: ExprRef<'tcx>,
160 args: Vec<ExprRef<'tcx>>,
161 // Whether this is from a call in HIR, rather than from an overloaded
162 // operator. True for overloaded function call.
167 }, // NOT overloaded!
172 }, // NOT overloaded!
177 }, // NOT overloaded!
178 // LogicalOp is distinct from BinaryOp because of lazy evaluation of the operands.
182 }, // NOT overloaded!
184 source: ExprRef<'tcx>,
187 source: ExprRef<'tcx>,
188 }, // Use a lexpr to get a vexpr.
190 source: ExprRef<'tcx>,
193 source: ExprRef<'tcx>,
196 source: ExprRef<'tcx>,
199 source: ExprRef<'tcx>,
202 source: ExprRef<'tcx>,
205 condition: ExprRef<'tcx>,
207 otherwise: Option<ExprRef<'tcx>>,
210 condition: Option<ExprRef<'tcx>>,
214 discriminant: ExprRef<'tcx>,
215 arms: Vec<Arm<'tcx>>,
218 body: &'tcx hir::Block,
235 index: ExprRef<'tcx>,
240 /// first argument, used for self in a closure
246 region: Region<'tcx>,
247 borrow_kind: BorrowKind,
251 label: region::Scope,
252 value: Option<ExprRef<'tcx>>,
255 label: region::Scope,
258 value: Option<ExprRef<'tcx>>,
261 value: ExprRef<'tcx>,
265 fields: Vec<ExprRef<'tcx>>,
268 fields: Vec<ExprRef<'tcx>>,
271 adt_def: &'tcx AdtDef,
272 variant_index: VariantIdx,
273 substs: &'tcx Substs<'tcx>,
275 /// Optional user-given substs: for something like `let x =
276 /// Bar::<T> { ... }`.
277 user_ty: Option<UserTypeAnnotation<'tcx>>,
279 fields: Vec<FieldExprRef<'tcx>>,
280 base: Option<FruInfo<'tcx>>
282 PlaceTypeAscription {
283 source: ExprRef<'tcx>,
284 /// Type that the user gave to this expression
285 user_ty: Option<UserTypeAnnotation<'tcx>>,
287 ValueTypeAscription {
288 source: ExprRef<'tcx>,
289 /// Type that the user gave to this expression
290 user_ty: Option<UserTypeAnnotation<'tcx>>,
294 substs: UpvarSubsts<'tcx>,
295 upvars: Vec<ExprRef<'tcx>>,
296 movability: Option<hir::GeneratorMovability>,
299 literal: &'tcx Const<'tcx>,
300 user_ty: Option<UserTypeAnnotation<'tcx>>,
303 asm: &'tcx hir::InlineAsm,
304 outputs: Vec<ExprRef<'tcx>>,
305 inputs: Vec<ExprRef<'tcx>>
308 value: ExprRef<'tcx>,
312 #[derive(Clone, Debug)]
313 pub enum ExprRef<'tcx> {
314 Hair(&'tcx hir::Expr),
315 Mirror(Box<Expr<'tcx>>),
318 #[derive(Clone, Debug)]
319 pub struct FieldExprRef<'tcx> {
321 pub expr: ExprRef<'tcx>,
324 #[derive(Clone, Debug)]
325 pub struct FruInfo<'tcx> {
326 pub base: ExprRef<'tcx>,
327 pub field_types: Vec<Ty<'tcx>>
330 #[derive(Clone, Debug)]
331 pub struct Arm<'tcx> {
332 pub patterns: Vec<Pattern<'tcx>>,
333 pub guard: Option<Guard<'tcx>>,
334 pub body: ExprRef<'tcx>,
335 pub lint_level: LintLevel,
338 #[derive(Clone, Debug)]
339 pub enum Guard<'tcx> {
343 #[derive(Copy, Clone, Debug)]
349 impl<'tcx> ExprRef<'tcx> {
350 pub fn span(&self) -> Span {
352 ExprRef::Hair(expr) => expr.span,
353 ExprRef::Mirror(expr) => expr.span,
358 ///////////////////////////////////////////////////////////////////////////
361 /// "Mirroring" is the process of converting from a HIR type into one
362 /// of the HAIR types defined in this file. This is basically a "on
363 /// the fly" desugaring step that hides a lot of the messiness in the
364 /// tcx. For example, the mirror of a `&'tcx hir::Expr` is an
367 /// Mirroring is gradual: when you mirror an outer expression like `e1
368 /// + e2`, the references to the inner expressions `e1` and `e2` are
369 /// `ExprRef<'tcx>` instances, and they may or may not be eagerly
370 /// mirrored. This allows a single AST node from the compiler to
371 /// expand into one or more Hair nodes, which lets the Hair nodes be
373 pub trait Mirror<'tcx> {
376 fn make_mirror<'a, 'gcx>(self, cx: &mut Cx<'a, 'gcx, 'tcx>) -> Self::Output;
379 impl<'tcx> Mirror<'tcx> for Expr<'tcx> {
380 type Output = Expr<'tcx>;
382 fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Expr<'tcx> {
387 impl<'tcx> Mirror<'tcx> for ExprRef<'tcx> {
388 type Output = Expr<'tcx>;
390 fn make_mirror<'a, 'gcx>(self, hir: &mut Cx<'a, 'gcx, 'tcx>) -> Expr<'tcx> {
392 ExprRef::Hair(h) => h.make_mirror(hir),
393 ExprRef::Mirror(m) => *m,
398 impl<'tcx> Mirror<'tcx> for Stmt<'tcx> {
399 type Output = Stmt<'tcx>;
401 fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Stmt<'tcx> {
406 impl<'tcx> Mirror<'tcx> for StmtRef<'tcx> {
407 type Output = Stmt<'tcx>;
409 fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Stmt<'tcx> {
411 StmtRef::Mirror(m) => *m,
416 impl<'tcx> Mirror<'tcx> for Block<'tcx> {
417 type Output = Block<'tcx>;
419 fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Block<'tcx> {