]> git.proxmox.com Git - rustc.git/blob - src/librustc_mir/hair/cx/mod.rs
New upstream version 1.17.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 //! This module contains the code to convert from the wacky tcx data
12 //! structures into the hair. The `builder` is generally ignorant of
13 //! the tcx etc, and instead goes through the `Cx` for most of its
14 //! work.
15 //!
16
17 use hair::*;
18 use rustc::mir::transform::MirSource;
19
20 use rustc::middle::const_val::ConstVal;
21 use rustc_const_eval::{ConstContext, fatal_const_eval_err};
22 use rustc_data_structures::indexed_vec::Idx;
23 use rustc::hir::def_id::DefId;
24 use rustc::hir::map::blocks::FnLikeNode;
25 use rustc::infer::InferCtxt;
26 use rustc::ty::subst::Subst;
27 use rustc::ty::{self, Ty, TyCtxt};
28 use syntax::symbol::{Symbol, InternedString};
29 use rustc::hir;
30 use rustc_const_math::{ConstInt, ConstUsize};
31
32 #[derive(Copy, Clone)]
33 pub struct Cx<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
34 tcx: TyCtxt<'a, 'gcx, 'tcx>,
35 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
36 constness: hir::Constness,
37
38 /// True if this constant/function needs overflow checks.
39 check_overflow: bool,
40 }
41
42 impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
43 pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>, src: MirSource) -> Cx<'a, 'gcx, 'tcx> {
44 let constness = match src {
45 MirSource::Const(_) |
46 MirSource::Static(..) => hir::Constness::Const,
47 MirSource::Fn(id) => {
48 let fn_like = FnLikeNode::from_node(infcx.tcx.hir.get(id));
49 fn_like.map_or(hir::Constness::NotConst, |f| f.constness())
50 }
51 MirSource::Promoted(..) => bug!(),
52 };
53
54 let attrs = infcx.tcx.hir.attrs(src.item_id());
55
56 // Some functions always have overflow checks enabled,
57 // however, they may not get codegen'd, depending on
58 // the settings for the crate they are translated in.
59 let mut check_overflow = attrs.iter()
60 .any(|item| item.check_name("rustc_inherit_overflow_checks"));
61
62 // Respect -C overflow-checks.
63 check_overflow |= infcx.tcx.sess.overflow_checks();
64
65 // Constants and const fn's always need overflow checks.
66 check_overflow |= constness == hir::Constness::Const;
67
68 Cx {
69 tcx: infcx.tcx,
70 infcx: infcx,
71 constness: constness,
72 check_overflow: check_overflow,
73 }
74 }
75 }
76
77 impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
78 /// Normalizes `ast` into the appropriate `mirror` type.
79 pub fn mirror<M: Mirror<'tcx>>(&mut self, ast: M) -> M::Output {
80 ast.make_mirror(self)
81 }
82
83 pub fn usize_ty(&mut self) -> Ty<'tcx> {
84 self.tcx.types.usize
85 }
86
87 pub fn usize_literal(&mut self, value: u64) -> Literal<'tcx> {
88 match ConstUsize::new(value, self.tcx.sess.target.uint_type) {
89 Ok(val) => Literal::Value { value: ConstVal::Integral(ConstInt::Usize(val)) },
90 Err(_) => bug!("usize literal out of range for target"),
91 }
92 }
93
94 pub fn bool_ty(&mut self) -> Ty<'tcx> {
95 self.tcx.types.bool
96 }
97
98 pub fn unit_ty(&mut self) -> Ty<'tcx> {
99 self.tcx.mk_nil()
100 }
101
102 pub fn str_literal(&mut self, value: InternedString) -> Literal<'tcx> {
103 Literal::Value { value: ConstVal::Str(value) }
104 }
105
106 pub fn true_literal(&mut self) -> Literal<'tcx> {
107 Literal::Value { value: ConstVal::Bool(true) }
108 }
109
110 pub fn false_literal(&mut self) -> Literal<'tcx> {
111 Literal::Value { value: ConstVal::Bool(false) }
112 }
113
114 pub fn const_eval_literal(&mut self, e: &hir::Expr) -> Literal<'tcx> {
115 let tcx = self.tcx.global_tcx();
116 match ConstContext::with_tables(tcx, self.tables()).eval(e) {
117 Ok(value) => Literal::Value { value: value },
118 Err(s) => fatal_const_eval_err(tcx, &s, e.span, "expression")
119 }
120 }
121
122 pub fn trait_method(&mut self,
123 trait_def_id: DefId,
124 method_name: &str,
125 self_ty: Ty<'tcx>,
126 params: &[Ty<'tcx>])
127 -> (Ty<'tcx>, Literal<'tcx>) {
128 let method_name = Symbol::intern(method_name);
129 let substs = self.tcx.mk_substs_trait(self_ty, params);
130 for item in self.tcx.associated_items(trait_def_id) {
131 if item.kind == ty::AssociatedKind::Method && item.name == method_name {
132 let method_ty = self.tcx.item_type(item.def_id);
133 let method_ty = method_ty.subst(self.tcx, substs);
134 return (method_ty,
135 Literal::Item {
136 def_id: item.def_id,
137 substs: substs,
138 });
139 }
140 }
141
142 bug!("found no method `{}` in `{:?}`", method_name, trait_def_id);
143 }
144
145 pub fn num_variants(&mut self, adt_def: &ty::AdtDef) -> usize {
146 adt_def.variants.len()
147 }
148
149 pub fn all_fields(&mut self, adt_def: &ty::AdtDef, variant_index: usize) -> Vec<Field> {
150 (0..adt_def.variants[variant_index].fields.len())
151 .map(Field::new)
152 .collect()
153 }
154
155 pub fn needs_drop(&mut self, ty: Ty<'tcx>) -> bool {
156 let ty = self.tcx.lift_to_global(&ty).unwrap_or_else(|| {
157 bug!("MIR: Cx::needs_drop({}) got \
158 type with inference types/regions",
159 ty);
160 });
161 self.tcx.type_needs_drop_given_env(ty, &self.infcx.parameter_environment)
162 }
163
164 pub fn tcx(&self) -> TyCtxt<'a, 'gcx, 'tcx> {
165 self.tcx
166 }
167
168 pub fn tables(&self) -> &'a ty::TypeckTables<'gcx> {
169 self.infcx.tables.expect_interned()
170 }
171
172 pub fn check_overflow(&self) -> bool {
173 self.check_overflow
174 }
175 }
176
177 mod block;
178 mod expr;
179 mod to_ref;