]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/hair/mod.rs
Auto merge of #53517 - phungleson:fix-impl-from-for-error, r=frewsxcv
[rust.git] / src / librustc_mir / hair / mod.rs
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.
4 //
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.
10
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
15 //! structures.
16
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};
22 use rustc::hir;
23 use syntax::ast;
24 use syntax_pos::Span;
25 use self::cx::Cx;
26
27 pub mod cx;
28
29 pub mod pattern;
30 pub use self::pattern::{BindingMode, Pattern, PatternKind, FieldPattern};
31
32 #[derive(Copy, Clone, Debug)]
33 pub enum LintLevel {
34     Inherited,
35     Explicit(ast::NodeId)
36 }
37
38 impl LintLevel {
39     pub fn is_explicit(self) -> bool {
40         match self {
41             LintLevel::Inherited => false,
42             LintLevel::Explicit(_) => true
43         }
44     }
45 }
46
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>,
52     pub span: Span,
53     pub stmts: Vec<StmtRef<'tcx>>,
54     pub expr: Option<ExprRef<'tcx>>,
55     pub safety_mode: BlockSafety,
56 }
57
58 #[derive(Copy, Clone, Debug)]
59 pub enum BlockSafety {
60     Safe,
61     ExplicitUnsafe(ast::NodeId),
62     PushUnsafe,
63     PopUnsafe
64 }
65
66 #[derive(Clone, Debug)]
67 pub enum StmtRef<'tcx> {
68     Mirror(Box<Stmt<'tcx>>),
69 }
70
71 #[derive(Clone, Debug)]
72 pub struct Stmt<'tcx> {
73     pub kind: StmtKind<'tcx>,
74     pub opt_destruction_scope: Option<region::Scope>,
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 /// shortlived. 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     ReifyFnPointer {
185         source: ExprRef<'tcx>,
186     },
187     ClosureFnPointer {
188         source: ExprRef<'tcx>,
189     },
190     UnsafeFnPointer {
191         source: ExprRef<'tcx>,
192     },
193     Unsize {
194         source: ExprRef<'tcx>,
195     },
196     If {
197         condition: ExprRef<'tcx>,
198         then: ExprRef<'tcx>,
199         otherwise: Option<ExprRef<'tcx>>,
200     },
201     Loop {
202         condition: Option<ExprRef<'tcx>>,
203         body: ExprRef<'tcx>,
204     },
205     Match {
206         discriminant: ExprRef<'tcx>,
207         arms: Vec<Arm<'tcx>>,
208     },
209     Block {
210         body: &'tcx hir::Block,
211     },
212     Assign {
213         lhs: ExprRef<'tcx>,
214         rhs: ExprRef<'tcx>,
215     },
216     AssignOp {
217         op: BinOp,
218         lhs: ExprRef<'tcx>,
219         rhs: ExprRef<'tcx>,
220     },
221     Field {
222         lhs: ExprRef<'tcx>,
223         name: Field,
224     },
225     Index {
226         lhs: ExprRef<'tcx>,
227         index: ExprRef<'tcx>,
228     },
229     VarRef {
230         id: ast::NodeId,
231     },
232     /// first argument, used for self in a closure
233     SelfRef,
234     StaticRef {
235         id: DefId,
236     },
237     Borrow {
238         region: Region<'tcx>,
239         borrow_kind: BorrowKind,
240         arg: ExprRef<'tcx>,
241     },
242     Break {
243         label: region::Scope,
244         value: Option<ExprRef<'tcx>>,
245     },
246     Continue {
247         label: region::Scope,
248     },
249     Return {
250         value: Option<ExprRef<'tcx>>,
251     },
252     Repeat {
253         value: ExprRef<'tcx>,
254         count: u64,
255     },
256     Array {
257         fields: Vec<ExprRef<'tcx>>,
258     },
259     Tuple {
260         fields: Vec<ExprRef<'tcx>>,
261     },
262     Adt {
263         adt_def: &'tcx AdtDef,
264         variant_index: usize,
265         substs: &'tcx Substs<'tcx>,
266
267         /// Optional user-given substs: for something like `let x =
268         /// Bar::<T> { ... }`.
269         user_ty: Option<CanonicalTy<'tcx>>,
270
271         fields: Vec<FieldExprRef<'tcx>>,
272         base: Option<FruInfo<'tcx>>
273     },
274     PlaceTypeAscription {
275         source: ExprRef<'tcx>,
276         /// Type that the user gave to this expression
277         user_ty: CanonicalTy<'tcx>,
278     },
279     ValueTypeAscription {
280         source: ExprRef<'tcx>,
281         /// Type that the user gave to this expression
282         user_ty: CanonicalTy<'tcx>,
283     },
284     Closure {
285         closure_id: DefId,
286         substs: UpvarSubsts<'tcx>,
287         upvars: Vec<ExprRef<'tcx>>,
288         movability: Option<hir::GeneratorMovability>,
289     },
290     Literal {
291         literal: &'tcx Const<'tcx>,
292
293         /// Optional user-given type: for something like
294         /// `collect::<Vec<_>>`, this would be present and would
295         /// indicate that `Vec<_>` was explicitly specified.
296         ///
297         /// Needed for NLL to impose user-given type constraints.
298         user_ty: Option<CanonicalTy<'tcx>>,
299     },
300     InlineAsm {
301         asm: &'tcx hir::InlineAsm,
302         outputs: Vec<ExprRef<'tcx>>,
303         inputs: Vec<ExprRef<'tcx>>
304     },
305     Yield {
306         value: ExprRef<'tcx>,
307     },
308 }
309
310 #[derive(Clone, Debug)]
311 pub enum ExprRef<'tcx> {
312     Hair(&'tcx hir::Expr),
313     Mirror(Box<Expr<'tcx>>),
314 }
315
316 #[derive(Clone, Debug)]
317 pub struct FieldExprRef<'tcx> {
318     pub name: Field,
319     pub expr: ExprRef<'tcx>,
320 }
321
322 #[derive(Clone, Debug)]
323 pub struct FruInfo<'tcx> {
324     pub base: ExprRef<'tcx>,
325     pub field_types: Vec<Ty<'tcx>>
326 }
327
328 #[derive(Clone, Debug)]
329 pub struct Arm<'tcx> {
330     pub patterns: Vec<Pattern<'tcx>>,
331     pub guard: Option<Guard<'tcx>>,
332     pub body: ExprRef<'tcx>,
333     pub lint_level: LintLevel,
334 }
335
336 #[derive(Clone, Debug)]
337 pub enum Guard<'tcx> {
338     If(ExprRef<'tcx>),
339 }
340
341 #[derive(Copy, Clone, Debug)]
342 pub enum LogicalOp {
343     And,
344     Or,
345 }
346
347 impl<'tcx> ExprRef<'tcx> {
348     pub fn span(&self) -> Span {
349         match self {
350             ExprRef::Hair(expr) => expr.span,
351             ExprRef::Mirror(expr) => expr.span,
352         }
353     }
354 }
355
356 ///////////////////////////////////////////////////////////////////////////
357 // The Mirror trait
358
359 /// "Mirroring" is the process of converting from a HIR type into one
360 /// of the HAIR types defined in this file. This is basically a "on
361 /// the fly" desugaring step that hides a lot of the messiness in the
362 /// tcx. For example, the mirror of a `&'tcx hir::Expr` is an
363 /// `Expr<'tcx>`.
364 ///
365 /// Mirroring is gradual: when you mirror an outer expression like `e1
366 /// + e2`, the references to the inner expressions `e1` and `e2` are
367 /// `ExprRef<'tcx>` instances, and they may or may not be eagerly
368 /// mirrored.  This allows a single AST node from the compiler to
369 /// expand into one or more Hair nodes, which lets the Hair nodes be
370 /// simpler.
371 pub trait Mirror<'tcx> {
372     type Output;
373
374     fn make_mirror<'a, 'gcx>(self, cx: &mut Cx<'a, 'gcx, 'tcx>) -> Self::Output;
375 }
376
377 impl<'tcx> Mirror<'tcx> for Expr<'tcx> {
378     type Output = Expr<'tcx>;
379
380     fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Expr<'tcx> {
381         self
382     }
383 }
384
385 impl<'tcx> Mirror<'tcx> for ExprRef<'tcx> {
386     type Output = Expr<'tcx>;
387
388     fn make_mirror<'a, 'gcx>(self, hir: &mut Cx<'a, 'gcx, 'tcx>) -> Expr<'tcx> {
389         match self {
390             ExprRef::Hair(h) => h.make_mirror(hir),
391             ExprRef::Mirror(m) => *m,
392         }
393     }
394 }
395
396 impl<'tcx> Mirror<'tcx> for Stmt<'tcx> {
397     type Output = Stmt<'tcx>;
398
399     fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Stmt<'tcx> {
400         self
401     }
402 }
403
404 impl<'tcx> Mirror<'tcx> for StmtRef<'tcx> {
405     type Output = Stmt<'tcx>;
406
407     fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Stmt<'tcx> {
408         match self {
409             StmtRef::Mirror(m) => *m,
410         }
411     }
412 }
413
414 impl<'tcx> Mirror<'tcx> for Block<'tcx> {
415     type Output = Block<'tcx>;
416
417     fn make_mirror<'a, 'gcx>(self, _: &mut Cx<'a, 'gcx, 'tcx>) -> Block<'tcx> {
418         self
419     }
420 }