]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/hair/mod.rs
Rollup merge of #60581 - hellow554:fix_60580, r=alexcrichton
[rust.git] / src / librustc_mir / hair / mod.rs
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
5 //! structures.
6
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;
15 use rustc::hir;
16 use syntax_pos::Span;
17 use self::cx::Cx;
18
19 pub mod cx;
20 mod constant;
21
22 pub mod pattern;
23 pub use self::pattern::{BindingMode, Pattern, PatternKind, PatternRange, FieldPattern};
24 pub(crate) use self::pattern::PatternTypeProjection;
25
26 mod util;
27
28 #[derive(Copy, Clone, Debug)]
29 pub enum LintLevel {
30     Inherited,
31     Explicit(hir::HirId)
32 }
33
34 impl LintLevel {
35     pub fn is_explicit(self) -> bool {
36         match self {
37             LintLevel::Inherited => false,
38             LintLevel::Explicit(_) => true
39         }
40     }
41 }
42
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>,
48     pub span: Span,
49     pub stmts: Vec<StmtRef<'tcx>>,
50     pub expr: Option<ExprRef<'tcx>>,
51     pub safety_mode: BlockSafety,
52 }
53
54 #[derive(Copy, Clone, Debug)]
55 pub enum BlockSafety {
56     Safe,
57     ExplicitUnsafe(hir::HirId),
58     PushUnsafe,
59     PopUnsafe
60 }
61
62 #[derive(Clone, Debug)]
63 pub enum StmtRef<'tcx> {
64     Mirror(Box<Stmt<'tcx>>),
65 }
66
67 #[derive(Clone, Debug)]
68 pub struct StatementSpan(pub Span);
69
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,
75 }
76
77 #[derive(Clone, Debug)]
78 pub enum StmtKind<'tcx> {
79     Expr {
80         /// scope for this statement; may be used as lifetime of temporaries
81         scope: region::Scope,
82
83         /// expression being evaluated in this statement
84         expr: ExprRef<'tcx>,
85     },
86
87     Let {
88         /// scope for variables bound in this let; covers this and
89         /// remaining statements in block
90         remainder_scope: region::Scope,
91
92         /// scope for the initialization itself; might be used as
93         /// lifetime of temporaries
94         init_scope: region::Scope,
95
96         /// `let <PAT> = ...`
97         ///
98         /// if a type is included, it is added as an ascription pattern
99         pattern: Pattern<'tcx>,
100
101         /// let pat: ty = <INIT> ...
102         initializer: Option<ExprRef<'tcx>>,
103
104         /// the lint level for this let-statement
105         lint_level: LintLevel,
106     },
107 }
108
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.
117 ///
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
126     pub ty: Ty<'tcx>,
127
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>,
131
132     /// span of the expression in the source
133     pub span: Span,
134
135     /// kind of expression
136     pub kind: ExprKind<'tcx>,
137 }
138
139 #[derive(Clone, Debug)]
140 pub enum ExprKind<'tcx> {
141     Scope {
142         region_scope: region::Scope,
143         lint_level: LintLevel,
144         value: ExprRef<'tcx>,
145     },
146     Box {
147         value: ExprRef<'tcx>,
148     },
149     Call {
150         ty: Ty<'tcx>,
151         fun: 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.
155         from_hir_call: bool,
156     },
157     Deref {
158         arg: ExprRef<'tcx>,
159     }, // NOT overloaded!
160     Binary {
161         op: BinOp,
162         lhs: ExprRef<'tcx>,
163         rhs: ExprRef<'tcx>,
164     }, // NOT overloaded!
165     LogicalOp {
166         op: LogicalOp,
167         lhs: ExprRef<'tcx>,
168         rhs: ExprRef<'tcx>,
169     }, // NOT overloaded!
170        // LogicalOp is distinct from BinaryOp because of lazy evaluation of the operands.
171     Unary {
172         op: UnOp,
173         arg: ExprRef<'tcx>,
174     }, // NOT overloaded!
175     Cast {
176         source: ExprRef<'tcx>,
177     },
178     Use {
179         source: ExprRef<'tcx>,
180     }, // Use a lexpr to get a vexpr.
181     NeverToAny {
182         source: ExprRef<'tcx>,
183     },
184     Pointer {
185         cast: PointerCast,
186         source: ExprRef<'tcx>,
187     },
188     Loop {
189         condition: Option<ExprRef<'tcx>>,
190         body: ExprRef<'tcx>,
191     },
192     Match {
193         scrutinee: ExprRef<'tcx>,
194         arms: Vec<Arm<'tcx>>,
195     },
196     Block {
197         body: &'tcx hir::Block,
198     },
199     Assign {
200         lhs: ExprRef<'tcx>,
201         rhs: ExprRef<'tcx>,
202     },
203     AssignOp {
204         op: BinOp,
205         lhs: ExprRef<'tcx>,
206         rhs: ExprRef<'tcx>,
207     },
208     Field {
209         lhs: ExprRef<'tcx>,
210         name: Field,
211     },
212     Index {
213         lhs: ExprRef<'tcx>,
214         index: ExprRef<'tcx>,
215     },
216     VarRef {
217         id: hir::HirId,
218     },
219     /// first argument, used for self in a closure
220     SelfRef,
221     StaticRef {
222         id: DefId,
223     },
224     Borrow {
225         borrow_kind: BorrowKind,
226         arg: ExprRef<'tcx>,
227     },
228     Break {
229         label: region::Scope,
230         value: Option<ExprRef<'tcx>>,
231     },
232     Continue {
233         label: region::Scope,
234     },
235     Return {
236         value: Option<ExprRef<'tcx>>,
237     },
238     Repeat {
239         value: ExprRef<'tcx>,
240         count: u64,
241     },
242     Array {
243         fields: Vec<ExprRef<'tcx>>,
244     },
245     Tuple {
246         fields: Vec<ExprRef<'tcx>>,
247     },
248     Adt {
249         adt_def: &'tcx AdtDef,
250         variant_index: VariantIdx,
251         substs: SubstsRef<'tcx>,
252
253         /// Optional user-given substs: for something like `let x =
254         /// Bar::<T> { ... }`.
255         user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
256
257         fields: Vec<FieldExprRef<'tcx>>,
258         base: Option<FruInfo<'tcx>>
259     },
260     PlaceTypeAscription {
261         source: ExprRef<'tcx>,
262         /// Type that the user gave to this expression
263         user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
264     },
265     ValueTypeAscription {
266         source: ExprRef<'tcx>,
267         /// Type that the user gave to this expression
268         user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
269     },
270     Closure {
271         closure_id: DefId,
272         substs: UpvarSubsts<'tcx>,
273         upvars: Vec<ExprRef<'tcx>>,
274         movability: Option<hir::GeneratorMovability>,
275     },
276     Literal {
277         literal: &'tcx Const<'tcx>,
278         user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
279     },
280     InlineAsm {
281         asm: &'tcx hir::InlineAsm,
282         outputs: Vec<ExprRef<'tcx>>,
283         inputs: Vec<ExprRef<'tcx>>
284     },
285     Yield {
286         value: ExprRef<'tcx>,
287     },
288 }
289
290 #[derive(Clone, Debug)]
291 pub enum ExprRef<'tcx> {
292     Hair(&'tcx hir::Expr),
293     Mirror(Box<Expr<'tcx>>),
294 }
295
296 #[derive(Clone, Debug)]
297 pub struct FieldExprRef<'tcx> {
298     pub name: Field,
299     pub expr: ExprRef<'tcx>,
300 }
301
302 #[derive(Clone, Debug)]
303 pub struct FruInfo<'tcx> {
304     pub base: ExprRef<'tcx>,
305     pub field_types: Vec<Ty<'tcx>>
306 }
307
308 #[derive(Clone, Debug)]
309 pub struct Arm<'tcx> {
310     pub patterns: Vec<Pattern<'tcx>>,
311     pub guard: Option<Guard<'tcx>>,
312     pub body: ExprRef<'tcx>,
313     pub lint_level: LintLevel,
314 }
315
316 #[derive(Clone, Debug)]
317 pub enum Guard<'tcx> {
318     If(ExprRef<'tcx>),
319 }
320
321 #[derive(Copy, Clone, Debug)]
322 pub enum LogicalOp {
323     And,
324     Or,
325 }
326
327 impl<'tcx> ExprRef<'tcx> {
328     pub fn span(&self) -> Span {
329         match self {
330             ExprRef::Hair(expr) => expr.span,
331             ExprRef::Mirror(expr) => expr.span,
332         }
333     }
334 }
335
336 ///////////////////////////////////////////////////////////////////////////
337 // The Mirror trait
338
339 /// "Mirroring" is the process of converting from a HIR type into one
340 /// of the HAIR types defined in this file. This is basically a "on
341 /// the fly" desugaring step that hides a lot of the messiness in the
342 /// tcx. For example, the mirror of a `&'tcx hir::Expr` is an
343 /// `Expr<'tcx>`.
344 ///
345 /// Mirroring is gradual: when you mirror an outer expression like `e1
346 /// + e2`, the references to the inner expressions `e1` and `e2` are
347 /// `ExprRef<'tcx>` instances, and they may or may not be eagerly
348 /// mirrored. This allows a single AST node from the compiler to
349 /// expand into one or more Hair nodes, which lets the Hair nodes be
350 /// simpler.
351 pub trait Mirror<'tcx> {
352     type Output;
353
354     fn make_mirror<'a, 'gcx>(self, cx: &mut Cx<'a, 'gcx, 'tcx>) -> Self::Output;
355 }
356
357 impl<'tcx> Mirror<'tcx> for Expr<'tcx> {
358     type Output = Expr<'tcx>;
359
360     fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Expr<'tcx> {
361         self
362     }
363 }
364
365 impl<'tcx> Mirror<'tcx> for ExprRef<'tcx> {
366     type Output = Expr<'tcx>;
367
368     fn make_mirror<'a, 'gcx>(self, hir: &mut Cx<'a, 'gcx, 'tcx>) -> Expr<'tcx> {
369         match self {
370             ExprRef::Hair(h) => h.make_mirror(hir),
371             ExprRef::Mirror(m) => *m,
372         }
373     }
374 }
375
376 impl<'tcx> Mirror<'tcx> for Stmt<'tcx> {
377     type Output = Stmt<'tcx>;
378
379     fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Stmt<'tcx> {
380         self
381     }
382 }
383
384 impl<'tcx> Mirror<'tcx> for StmtRef<'tcx> {
385     type Output = Stmt<'tcx>;
386
387     fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Stmt<'tcx> {
388         match self {
389             StmtRef::Mirror(m) => *m,
390         }
391     }
392 }
393
394 impl<'tcx> Mirror<'tcx> for Block<'tcx> {
395     type Output = Block<'tcx>;
396
397     fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Block<'tcx> {
398         self
399     }
400 }