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.
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.
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
18 use hair::util::UserAnnotatedTyHelpers;
20 use rustc_data_structures::indexed_vec::Idx;
21 use rustc::hir::def_id::{DefId, LOCAL_CRATE};
23 use rustc::middle::region;
24 use rustc::infer::InferCtxt;
25 use rustc::ty::subst::Subst;
26 use rustc::ty::{self, Ty, TyCtxt};
27 use rustc::ty::subst::{Kind, Substs};
28 use rustc::ty::layout::VariantIdx;
31 use syntax::symbol::Symbol;
33 use rustc_data_structures::sync::Lrc;
34 use hair::pattern::{lit_to_const, LitToConstError};
37 pub struct Cx<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
38 tcx: TyCtxt<'a, 'gcx, 'tcx>,
39 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
41 pub root_lint_level: ast::NodeId,
42 pub param_env: ty::ParamEnv<'gcx>,
44 /// Identity `Substs` for use with const-evaluation.
45 pub identity_substs: &'gcx Substs<'gcx>,
47 pub region_scope_tree: Lrc<region::ScopeTree>,
48 pub tables: &'a ty::TypeckTables<'gcx>,
50 /// This is `Constness::Const` if we are compiling a `static`,
51 /// `const`, or the body of a `const fn`.
52 constness: hir::Constness,
54 /// What kind of body is being compiled.
55 pub body_owner_kind: hir::BodyOwnerKind,
57 /// True if this constant/function needs overflow checks.
61 impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
62 pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
63 src_id: ast::NodeId) -> Cx<'a, 'gcx, 'tcx> {
65 let src_def_id = tcx.hir.local_def_id(src_id);
66 let body_owner_kind = tcx.hir.body_owner_kind(src_id);
68 let constness = match body_owner_kind {
69 hir::BodyOwnerKind::Const |
70 hir::BodyOwnerKind::Static(_) => hir::Constness::Const,
71 hir::BodyOwnerKind::Fn => hir::Constness::NotConst,
74 let attrs = tcx.hir.attrs(src_id);
76 // Some functions always have overflow checks enabled,
77 // however, they may not get codegen'd, depending on
78 // the settings for the crate they are codegened in.
79 let mut check_overflow = attr::contains_name(attrs, "rustc_inherit_overflow_checks");
81 // Respect -C overflow-checks.
82 check_overflow |= tcx.sess.overflow_checks();
84 // Constants always need overflow checks.
85 check_overflow |= constness == hir::Constness::Const;
87 let lint_level = lint_level_for_hir_id(tcx, src_id);
91 root_lint_level: lint_level,
92 param_env: tcx.param_env(src_def_id),
93 identity_substs: Substs::identity_for_item(tcx.global_tcx(), src_def_id),
94 region_scope_tree: tcx.region_scope_tree(src_def_id),
95 tables: tcx.typeck_tables_of(src_def_id),
104 impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
105 /// Normalizes `ast` into the appropriate `mirror` type.
106 pub fn mirror<M: Mirror<'tcx>>(&mut self, ast: M) -> M::Output {
107 ast.make_mirror(self)
110 pub fn usize_ty(&mut self) -> Ty<'tcx> {
114 pub fn usize_literal(&mut self, value: u64) -> &'tcx ty::Const<'tcx> {
115 ty::Const::from_usize(self.tcx, value)
118 pub fn bool_ty(&mut self) -> Ty<'tcx> {
122 pub fn unit_ty(&mut self) -> Ty<'tcx> {
126 pub fn true_literal(&mut self) -> &'tcx ty::Const<'tcx> {
127 ty::Const::from_bool(self.tcx, true)
130 pub fn false_literal(&mut self) -> &'tcx ty::Const<'tcx> {
131 ty::Const::from_bool(self.tcx, false)
134 pub fn const_eval_literal(
136 lit: &'tcx ast::LitKind,
140 ) -> &'tcx ty::Const<'tcx> {
141 trace!("const_eval_literal: {:#?}, {:?}, {:?}, {:?}", lit, ty, sp, neg);
143 match lit_to_const(lit, self.tcx, ty, neg) {
145 Err(LitToConstError::UnparseableFloat) => {
146 // FIXME(#31407) this is only necessary because float parsing is buggy
147 self.tcx.sess.span_err(sp, "could not evaluate float literal (see issue #31407)");
148 // create a dummy value and continue compiling
149 Const::from_bits(self.tcx, 0, self.param_env.and(ty))
151 Err(LitToConstError::Reported) => {
152 // create a dummy value and continue compiling
153 Const::from_bits(self.tcx, 0, self.param_env.and(ty))
158 pub fn pattern_from_hir(&mut self, p: &hir::Pat) -> Pattern<'tcx> {
159 let tcx = self.tcx.global_tcx();
160 let p = match tcx.hir.get(p.id) {
161 Node::Pat(p) | Node::Binding(p) => p,
162 node => bug!("pattern became {:?}", node)
164 Pattern::from_hir(tcx,
165 self.param_env.and(self.identity_substs),
170 pub fn trait_method(&mut self,
174 params: &[Kind<'tcx>])
175 -> (Ty<'tcx>, &'tcx ty::Const<'tcx>) {
176 let method_name = Symbol::intern(method_name);
177 let substs = self.tcx.mk_substs_trait(self_ty, params);
178 for item in self.tcx.associated_items(trait_def_id) {
179 if item.kind == ty::AssociatedKind::Method && item.ident.name == method_name {
180 let method_ty = self.tcx.type_of(item.def_id);
181 let method_ty = method_ty.subst(self.tcx, substs);
182 return (method_ty, ty::Const::zero_sized(self.tcx, method_ty));
186 bug!("found no method `{}` in `{:?}`", method_name, trait_def_id);
189 pub fn all_fields(&mut self, adt_def: &ty::AdtDef, variant_index: VariantIdx) -> Vec<Field> {
190 (0..adt_def.variants[variant_index].fields.len())
195 pub fn needs_drop(&mut self, ty: Ty<'tcx>) -> bool {
196 let (ty, param_env) = self.tcx.lift_to_global(&(ty, self.param_env)).unwrap_or_else(|| {
197 bug!("MIR: Cx::needs_drop({:?}, {:?}) got \
198 type with inference types/regions",
201 ty.needs_drop(self.tcx.global_tcx(), param_env)
204 fn lint_level_of(&self, node_id: ast::NodeId) -> LintLevel {
205 let hir_id = self.tcx.hir.definitions().node_to_hir_id(node_id);
206 let has_lint_level = self.tcx.dep_graph.with_ignore(|| {
207 self.tcx.lint_levels(LOCAL_CRATE).lint_level_set(hir_id).is_some()
211 LintLevel::Explicit(node_id)
217 pub fn tcx(&self) -> TyCtxt<'a, 'gcx, 'tcx> {
221 pub fn tables(&self) -> &'a ty::TypeckTables<'gcx> {
225 pub fn check_overflow(&self) -> bool {
229 pub fn type_moves_by_default(&self, ty: Ty<'tcx>, span: Span) -> bool {
230 self.infcx.type_moves_by_default(self.param_env, ty, span)
234 impl UserAnnotatedTyHelpers<'gcx, 'tcx> for Cx<'_, 'gcx, 'tcx> {
235 fn tcx(&self) -> TyCtxt<'_, 'gcx, 'tcx> {
239 fn tables(&self) -> &ty::TypeckTables<'tcx> {
244 fn lint_level_for_hir_id(tcx: TyCtxt, mut id: ast::NodeId) -> ast::NodeId {
245 // Right now we insert a `with_ignore` node in the dep graph here to
246 // ignore the fact that `lint_levels` below depends on the entire crate.
247 // For now this'll prevent false positives of recompiling too much when
250 // Once red/green incremental compilation lands we should be able to
251 // remove this because while the crate changes often the lint level map
252 // will change rarely.
253 tcx.dep_graph.with_ignore(|| {
254 let sets = tcx.lint_levels(LOCAL_CRATE);
256 let hir_id = tcx.hir.definitions().node_to_hir_id(id);
257 if sets.lint_level_set(hir_id).is_some() {
260 let next = tcx.hir.get_parent_node(id);
262 bug!("lint traversal reached the root of the crate");