]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/hair/mod.rs
Auto merge of #30680 - wesleywiser:rustdoc_image_max_width, r=steveklabnik
[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 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
15 //! structures.
16
17 use rustc::mir::repr::{BinOp, BorrowKind, Field, Literal, Mutability, UnOp, ItemKind};
18 use rustc::middle::const_eval::ConstVal;
19 use rustc::middle::def_id::DefId;
20 use rustc::middle::region::CodeExtent;
21 use rustc::middle::subst::Substs;
22 use rustc::middle::ty::{AdtDef, ClosureSubsts, Region, Ty};
23 use rustc_front::hir;
24 use syntax::ast;
25 use syntax::codemap::Span;
26 use self::cx::Cx;
27
28 pub mod cx;
29
30 #[derive(Clone, Debug)]
31 pub struct ItemRef<'tcx> {
32     pub ty: Ty<'tcx>,
33     pub kind: ItemKind,
34     pub def_id: DefId,
35     pub substs: &'tcx Substs<'tcx>,
36 }
37
38 #[derive(Clone, Debug)]
39 pub struct Block<'tcx> {
40     pub extent: CodeExtent,
41     pub span: Span,
42     pub stmts: Vec<StmtRef<'tcx>>,
43     pub expr: Option<ExprRef<'tcx>>,
44 }
45
46 #[derive(Clone, Debug)]
47 pub enum StmtRef<'tcx> {
48     Mirror(Box<Stmt<'tcx>>),
49 }
50
51 #[derive(Clone, Debug)]
52 pub struct Stmt<'tcx> {
53     pub span: Span,
54     pub kind: StmtKind<'tcx>,
55 }
56
57 #[derive(Clone, Debug)]
58 pub enum StmtKind<'tcx> {
59     Expr {
60         /// scope for this statement; may be used as lifetime of temporaries
61         scope: CodeExtent,
62
63         /// expression being evaluated in this statement
64         expr: ExprRef<'tcx>,
65     },
66
67     Let {
68         /// scope for variables bound in this let; covers this and
69         /// remaining statements in block
70         remainder_scope: CodeExtent,
71
72         /// scope for the initialization itself; might be used as
73         /// lifetime of temporaries
74         init_scope: CodeExtent,
75
76         /// let <PAT> = ...
77         pattern: Pattern<'tcx>,
78
79         /// let pat = <INIT> ...
80         initializer: Option<ExprRef<'tcx>>,
81
82         /// let pat = init; <STMTS>
83         stmts: Vec<StmtRef<'tcx>>,
84     },
85 }
86
87 // The Hair trait implementor translates their expressions (`&'tcx H::Expr`)
88 // into instances of this `Expr` enum. This translation can be done
89 // basically as lazilly or as eagerly as desired: every recursive
90 // reference to an expression in this enum is an `ExprRef<'tcx>`, which
91 // may in turn be another instance of this enum (boxed), or else an
92 // untranslated `&'tcx H::Expr`. Note that instances of `Expr` are very
93 // shortlived. They are created by `Hair::to_expr`, analyzed and
94 // converted into MIR, and then discarded.
95 //
96 // If you compare `Expr` to the full compiler AST, you will see it is
97 // a good bit simpler. In fact, a number of the more straight-forward
98 // MIR simplifications are already done in the impl of `Hair`. For
99 // example, method calls and overloaded operators are absent: they are
100 // expected to be converted into `Expr::Call` instances.
101 #[derive(Clone, Debug)]
102 pub struct Expr<'tcx> {
103     // type of this expression
104     pub ty: Ty<'tcx>,
105
106     // lifetime of this expression if it should be spilled into a
107     // temporary; should be None only if in a constant context
108     pub temp_lifetime: Option<CodeExtent>,
109
110     // span of the expression in the source
111     pub span: Span,
112
113     // kind of expression
114     pub kind: ExprKind<'tcx>,
115 }
116
117 #[derive(Clone, Debug)]
118 pub enum ExprKind<'tcx> {
119     Scope {
120         extent: CodeExtent,
121         value: ExprRef<'tcx>,
122     },
123     Box {
124         value: ExprRef<'tcx>,
125     },
126     Call {
127         fun: ExprRef<'tcx>,
128         args: Vec<ExprRef<'tcx>>,
129     },
130     Deref {
131         arg: ExprRef<'tcx>,
132     }, // NOT overloaded!
133     Binary {
134         op: BinOp,
135         lhs: ExprRef<'tcx>,
136         rhs: ExprRef<'tcx>,
137     }, // NOT overloaded!
138     LogicalOp {
139         op: LogicalOp,
140         lhs: ExprRef<'tcx>,
141         rhs: ExprRef<'tcx>,
142     },
143     Unary {
144         op: UnOp,
145         arg: ExprRef<'tcx>,
146     }, // NOT overloaded!
147     Cast {
148         source: ExprRef<'tcx>,
149     },
150     ReifyFnPointer {
151         source: ExprRef<'tcx>,
152     },
153     UnsafeFnPointer {
154         source: ExprRef<'tcx>,
155     },
156     Unsize {
157         source: ExprRef<'tcx>,
158     },
159     If {
160         condition: ExprRef<'tcx>,
161         then: ExprRef<'tcx>,
162         otherwise: Option<ExprRef<'tcx>>,
163     },
164     Loop {
165         condition: Option<ExprRef<'tcx>>,
166         body: ExprRef<'tcx>,
167     },
168     Match {
169         discriminant: ExprRef<'tcx>,
170         arms: Vec<Arm<'tcx>>,
171     },
172     Block {
173         body: &'tcx hir::Block,
174     },
175     Assign {
176         lhs: ExprRef<'tcx>,
177         rhs: ExprRef<'tcx>,
178     },
179     AssignOp {
180         op: BinOp,
181         lhs: ExprRef<'tcx>,
182         rhs: ExprRef<'tcx>,
183     },
184     Field {
185         lhs: ExprRef<'tcx>,
186         name: Field,
187     },
188     Index {
189         lhs: ExprRef<'tcx>,
190         index: ExprRef<'tcx>,
191     },
192     VarRef {
193         id: ast::NodeId,
194     },
195     SelfRef, // first argument, used for self in a closure
196     StaticRef {
197         id: DefId,
198     },
199     Borrow {
200         region: Region,
201         borrow_kind: BorrowKind,
202         arg: ExprRef<'tcx>,
203     },
204     Break {
205         label: Option<CodeExtent>,
206     },
207     Continue {
208         label: Option<CodeExtent>,
209     },
210     Return {
211         value: Option<ExprRef<'tcx>>,
212     },
213     Repeat {
214         value: ExprRef<'tcx>,
215         // FIXME(#29789): Add a separate hair::Constant<'tcx> so this could be more explicit about
216         // its contained data. Currently this should only contain expression of ExprKind::Literal
217         // kind.
218         count: ExprRef<'tcx>,
219     },
220     Vec {
221         fields: Vec<ExprRef<'tcx>>,
222     },
223     Tuple {
224         fields: Vec<ExprRef<'tcx>>,
225     },
226     Adt {
227         adt_def: AdtDef<'tcx>,
228         variant_index: usize,
229         substs: &'tcx Substs<'tcx>,
230         fields: Vec<FieldExprRef<'tcx>>,
231         base: Option<ExprRef<'tcx>>,
232     },
233     Closure {
234         closure_id: DefId,
235         substs: &'tcx ClosureSubsts<'tcx>,
236         upvars: Vec<ExprRef<'tcx>>,
237     },
238     Literal {
239         literal: Literal<'tcx>,
240     },
241     InlineAsm {
242         asm: &'tcx hir::InlineAsm,
243     },
244 }
245
246 #[derive(Clone, Debug)]
247 pub enum ExprRef<'tcx> {
248     Hair(&'tcx hir::Expr),
249     Mirror(Box<Expr<'tcx>>),
250 }
251
252 #[derive(Clone, Debug)]
253 pub struct FieldExprRef<'tcx> {
254     pub name: Field,
255     pub expr: ExprRef<'tcx>,
256 }
257
258 #[derive(Clone, Debug)]
259 pub struct Arm<'tcx> {
260     pub patterns: Vec<Pattern<'tcx>>,
261     pub guard: Option<ExprRef<'tcx>>,
262     pub body: ExprRef<'tcx>,
263 }
264
265 #[derive(Clone, Debug)]
266 pub struct Pattern<'tcx> {
267     pub ty: Ty<'tcx>,
268     pub span: Span,
269     pub kind: Box<PatternKind<'tcx>>,
270 }
271
272 #[derive(Copy, Clone, Debug)]
273 pub enum LogicalOp {
274     And,
275     Or,
276 }
277
278 #[derive(Clone, Debug)]
279 pub enum PatternKind<'tcx> {
280     Wild,
281
282     // x, ref x, x @ P, etc
283     Binding {
284         mutability: Mutability,
285         name: ast::Name,
286         mode: BindingMode,
287         var: ast::NodeId,
288         ty: Ty<'tcx>,
289         subpattern: Option<Pattern<'tcx>>,
290     },
291
292     // Foo(...) or Foo{...} or Foo, where `Foo` is a variant name from an adt with >1 variants
293     Variant {
294         adt_def: AdtDef<'tcx>,
295         variant_index: usize,
296         subpatterns: Vec<FieldPattern<'tcx>>,
297     },
298
299     // (...), Foo(...), Foo{...}, or Foo, where `Foo` is a variant name from an adt with 1 variant
300     Leaf {
301         subpatterns: Vec<FieldPattern<'tcx>>,
302     },
303
304     Deref {
305         subpattern: Pattern<'tcx>,
306     }, // box P, &P, &mut P, etc
307
308     Constant {
309         value: ConstVal,
310     },
311
312     Range {
313         lo: Literal<'tcx>,
314         hi: Literal<'tcx>,
315     },
316
317     // matches against a slice, checking the length and extracting elements
318     Slice {
319         prefix: Vec<Pattern<'tcx>>,
320         slice: Option<Pattern<'tcx>>,
321         suffix: Vec<Pattern<'tcx>>,
322     },
323
324     // fixed match against an array, irrefutable
325     Array {
326         prefix: Vec<Pattern<'tcx>>,
327         slice: Option<Pattern<'tcx>>,
328         suffix: Vec<Pattern<'tcx>>,
329     },
330 }
331
332 #[derive(Copy, Clone, Debug)]
333 pub enum BindingMode {
334     ByValue,
335     ByRef(Region, BorrowKind),
336 }
337
338 #[derive(Clone, Debug)]
339 pub struct FieldPattern<'tcx> {
340     pub field: Field,
341     pub pattern: Pattern<'tcx>,
342 }
343
344 ///////////////////////////////////////////////////////////////////////////
345 // The Mirror trait
346
347 /// "Mirroring" is the process of converting from a HIR type into one
348 /// of the HAIR types defined in this file. This is basically a "on
349 /// the fly" desugaring step that hides a lot of the messiness in the
350 /// tcx. For example, the mirror of a `&'tcx hir::Expr` is an
351 /// `Expr<'tcx>`.
352 ///
353 /// Mirroring is gradual: when you mirror an outer expression like `e1
354 /// + e2`, the references to the inner expressions `e1` and `e2` are
355 /// `ExprRef<'tcx>` instances, and they may or may not be eagerly
356 /// mirrored.  This allows a single AST node from the compiler to
357 /// expand into one or more Hair nodes, which lets the Hair nodes be
358 /// simpler.
359 pub trait Mirror<'tcx> {
360     type Output;
361
362     fn make_mirror<'a>(self, cx: &mut Cx<'a, 'tcx>) -> Self::Output;
363 }
364
365 impl<'tcx> Mirror<'tcx> for Expr<'tcx> {
366     type Output = Expr<'tcx>;
367
368     fn make_mirror<'a>(self, _: &mut Cx<'a, 'tcx>) -> Expr<'tcx> {
369         self
370     }
371 }
372
373 impl<'tcx> Mirror<'tcx> for ExprRef<'tcx> {
374     type Output = Expr<'tcx>;
375
376     fn make_mirror<'a>(self, hir: &mut Cx<'a, 'tcx>) -> Expr<'tcx> {
377         match self {
378             ExprRef::Hair(h) => h.make_mirror(hir),
379             ExprRef::Mirror(m) => *m,
380         }
381     }
382 }
383
384 impl<'tcx> Mirror<'tcx> for Stmt<'tcx> {
385     type Output = Stmt<'tcx>;
386
387     fn make_mirror<'a>(self, _: &mut Cx<'a, 'tcx>) -> Stmt<'tcx> {
388         self
389     }
390 }
391
392 impl<'tcx> Mirror<'tcx> for StmtRef<'tcx> {
393     type Output = Stmt<'tcx>;
394
395     fn make_mirror<'a>(self, _: &mut Cx<'a,'tcx>) -> Stmt<'tcx> {
396         match self {
397             StmtRef::Mirror(m) => *m,
398         }
399     }
400 }
401
402 impl<'tcx> Mirror<'tcx> for Block<'tcx> {
403     type Output = Block<'tcx>;
404
405     fn make_mirror<'a>(self, _: &mut Cx<'a, 'tcx>) -> Block<'tcx> {
406         self
407     }
408 }