]> git.proxmox.com Git - rustc.git/blob - src/librustc_mir/hair/cx/mod.rs
Imported Upstream version 1.7.0+dfsg1
[rustc.git] / src / librustc_mir / hair / cx / 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 /*!
12 * This module contains the code to convert from the wacky tcx data
13 * structures into the hair. The `builder` is generally ignorant of
14 * the tcx etc, and instead goes through the `Cx` for most of its
15 * work.
16 */
17
18 use hair::*;
19 use rustc::mir::repr::*;
20
21 use rustc::middle::const_eval::{self, ConstVal};
22 use rustc::middle::infer::InferCtxt;
23 use rustc::middle::ty::{self, Ty};
24 use syntax::codemap::Span;
25 use syntax::parse::token;
26 use rustc_front::hir;
27
28 #[derive(Copy, Clone)]
29 pub struct Cx<'a, 'tcx: 'a> {
30 tcx: &'a ty::ctxt<'tcx>,
31 infcx: &'a InferCtxt<'a, 'tcx>,
32 }
33
34 impl<'a,'tcx> Cx<'a,'tcx> {
35 pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Cx<'a, 'tcx> {
36 Cx {
37 tcx: infcx.tcx,
38 infcx: infcx,
39 }
40 }
41 }
42
43 impl<'a,'tcx:'a> Cx<'a, 'tcx> {
44 /// Normalizes `ast` into the appropriate `mirror` type.
45 pub fn mirror<M: Mirror<'tcx>>(&mut self, ast: M) -> M::Output {
46 ast.make_mirror(self)
47 }
48
49 pub fn usize_ty(&mut self) -> Ty<'tcx> {
50 self.tcx.types.usize
51 }
52
53 pub fn usize_literal(&mut self, value: usize) -> Literal<'tcx> {
54 Literal::Value { value: ConstVal::Uint(value as u64) }
55 }
56
57 pub fn bool_ty(&mut self) -> Ty<'tcx> {
58 self.tcx.types.bool
59 }
60
61 pub fn str_literal(&mut self, value: token::InternedString) -> Literal<'tcx> {
62 Literal::Value { value: ConstVal::Str(value) }
63 }
64
65 pub fn true_literal(&mut self) -> Literal<'tcx> {
66 Literal::Value { value: ConstVal::Bool(true) }
67 }
68
69 pub fn false_literal(&mut self) -> Literal<'tcx> {
70 Literal::Value { value: ConstVal::Bool(false) }
71 }
72
73 pub fn const_eval_literal(&mut self, e: &hir::Expr) -> Literal<'tcx> {
74 Literal::Value { value: const_eval::eval_const_expr(self.tcx, e) }
75 }
76
77 pub fn try_const_eval_literal(&mut self, e: &hir::Expr) -> Option<Literal<'tcx>> {
78 let hint = const_eval::EvalHint::ExprTypeChecked;
79 const_eval::eval_const_expr_partial(self.tcx, e, hint, None)
80 .ok()
81 .map(|v| Literal::Value { value: v })
82 }
83
84 pub fn num_variants(&mut self, adt_def: ty::AdtDef<'tcx>) -> usize {
85 adt_def.variants.len()
86 }
87
88 pub fn all_fields(&mut self, adt_def: ty::AdtDef<'tcx>, variant_index: usize) -> Vec<Field> {
89 (0..adt_def.variants[variant_index].fields.len())
90 .map(Field::new)
91 .collect()
92 }
93
94 pub fn needs_drop(&mut self, ty: Ty<'tcx>) -> bool {
95 self.tcx.type_needs_drop_given_env(ty, &self.infcx.parameter_environment)
96 }
97
98 pub fn span_bug(&mut self, span: Span, message: &str) -> ! {
99 self.tcx.sess.span_bug(span, message)
100 }
101
102 pub fn tcx(&self) -> &'a ty::ctxt<'tcx> {
103 self.tcx
104 }
105 }
106
107 mod block;
108 mod expr;
109 mod pattern;
110 mod to_ref;