]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir_build/hair/cx/mod.rs
nix rustc_target::abi::* reexport in ty::layout
[rust.git] / src / librustc_mir_build / hair / cx / mod.rs
1 //! This module contains the fcuntaiontliy to convert from the wacky tcx data
2 //! structures into the HAIR. The `builder` is generally ignorant of the tcx,
3 //! etc., and instead goes through the `Cx` for most of its work.
4
5 use crate::hair::util::UserAnnotatedTyHelpers;
6 use crate::hair::*;
7
8 use rustc_ast::ast;
9 use rustc_ast::attr;
10 use rustc_hir as hir;
11 use rustc_hir::def_id::DefId;
12 use rustc_hir::Node;
13 use rustc_index::vec::Idx;
14 use rustc_infer::infer::InferCtxt;
15 use rustc_middle::middle::region;
16 use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
17 use rustc_middle::ty::subst::Subst;
18 use rustc_middle::ty::subst::{GenericArg, InternalSubsts};
19 use rustc_middle::ty::{self, Ty, TyCtxt};
20 use rustc_span::symbol::{sym, Symbol};
21 use rustc_target::abi::VariantIdx;
22 use rustc_trait_selection::infer::InferCtxtExt;
23
24 #[derive(Clone)]
25 crate struct Cx<'a, 'tcx> {
26     tcx: TyCtxt<'tcx>,
27     infcx: &'a InferCtxt<'a, 'tcx>,
28
29     crate root_lint_level: hir::HirId,
30     crate param_env: ty::ParamEnv<'tcx>,
31
32     /// Identity `InternalSubsts` for use with const-evaluation.
33     crate identity_substs: &'tcx InternalSubsts<'tcx>,
34
35     crate region_scope_tree: &'tcx region::ScopeTree,
36     crate tables: &'a ty::TypeckTables<'tcx>,
37
38     /// This is `Constness::Const` if we are compiling a `static`,
39     /// `const`, or the body of a `const fn`.
40     constness: hir::Constness,
41
42     /// The `DefId` of the owner of this body.
43     body_owner: DefId,
44
45     /// What kind of body is being compiled.
46     crate body_owner_kind: hir::BodyOwnerKind,
47
48     /// Whether this constant/function needs overflow checks.
49     check_overflow: bool,
50
51     /// See field with the same name on `mir::Body`.
52     control_flow_destroyed: Vec<(Span, String)>,
53 }
54
55 impl<'a, 'tcx> Cx<'a, 'tcx> {
56     crate fn new(infcx: &'a InferCtxt<'a, 'tcx>, src_id: hir::HirId) -> Cx<'a, 'tcx> {
57         let tcx = infcx.tcx;
58         let src_def_id = tcx.hir().local_def_id(src_id);
59         let tables = tcx.typeck_tables_of(src_def_id);
60         let body_owner_kind = tcx.hir().body_owner_kind(src_id);
61
62         let constness = match body_owner_kind {
63             hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_) => hir::Constness::Const,
64             hir::BodyOwnerKind::Closure | hir::BodyOwnerKind::Fn => hir::Constness::NotConst,
65         };
66
67         let attrs = tcx.hir().attrs(src_id);
68
69         // Some functions always have overflow checks enabled,
70         // however, they may not get codegen'd, depending on
71         // the settings for the crate they are codegened in.
72         let mut check_overflow = attr::contains_name(attrs, sym::rustc_inherit_overflow_checks);
73
74         // Respect -C overflow-checks.
75         check_overflow |= tcx.sess.overflow_checks();
76
77         // Constants always need overflow checks.
78         check_overflow |= constness == hir::Constness::Const;
79
80         Cx {
81             tcx,
82             infcx,
83             root_lint_level: src_id,
84             param_env: tcx.param_env(src_def_id),
85             identity_substs: InternalSubsts::identity_for_item(tcx, src_def_id),
86             region_scope_tree: tcx.region_scope_tree(src_def_id),
87             tables,
88             constness,
89             body_owner: src_def_id,
90             body_owner_kind,
91             check_overflow,
92             control_flow_destroyed: Vec::new(),
93         }
94     }
95
96     crate fn control_flow_destroyed(self) -> Vec<(Span, String)> {
97         self.control_flow_destroyed
98     }
99 }
100
101 impl<'a, 'tcx> Cx<'a, 'tcx> {
102     /// Normalizes `ast` into the appropriate "mirror" type.
103     crate fn mirror<M: Mirror<'tcx>>(&mut self, ast: M) -> M::Output {
104         ast.make_mirror(self)
105     }
106
107     crate fn usize_ty(&mut self) -> Ty<'tcx> {
108         self.tcx.types.usize
109     }
110
111     crate fn usize_literal(&mut self, value: u64) -> &'tcx ty::Const<'tcx> {
112         ty::Const::from_usize(self.tcx, value)
113     }
114
115     crate fn bool_ty(&mut self) -> Ty<'tcx> {
116         self.tcx.types.bool
117     }
118
119     crate fn unit_ty(&mut self) -> Ty<'tcx> {
120         self.tcx.mk_unit()
121     }
122
123     crate fn true_literal(&mut self) -> &'tcx ty::Const<'tcx> {
124         ty::Const::from_bool(self.tcx, true)
125     }
126
127     crate fn false_literal(&mut self) -> &'tcx ty::Const<'tcx> {
128         ty::Const::from_bool(self.tcx, false)
129     }
130
131     crate fn const_eval_literal(
132         &mut self,
133         lit: &'tcx ast::LitKind,
134         ty: Ty<'tcx>,
135         sp: Span,
136         neg: bool,
137     ) -> &'tcx ty::Const<'tcx> {
138         trace!("const_eval_literal: {:#?}, {:?}, {:?}, {:?}", lit, ty, sp, neg);
139
140         match self.tcx.at(sp).lit_to_const(LitToConstInput { lit, ty, neg }) {
141             Ok(c) => c,
142             Err(LitToConstError::UnparseableFloat) => {
143                 // FIXME(#31407) this is only necessary because float parsing is buggy
144                 self.tcx.sess.span_err(sp, "could not evaluate float literal (see issue #31407)");
145                 // create a dummy value and continue compiling
146                 Const::from_bits(self.tcx, 0, self.param_env.and(ty))
147             }
148             Err(LitToConstError::Reported) => {
149                 // create a dummy value and continue compiling
150                 Const::from_bits(self.tcx, 0, self.param_env.and(ty))
151             }
152             Err(LitToConstError::TypeError) => bug!("const_eval_literal: had type error"),
153         }
154     }
155
156     crate fn pattern_from_hir(&mut self, p: &hir::Pat<'_>) -> Pat<'tcx> {
157         let p = match self.tcx.hir().get(p.hir_id) {
158             Node::Pat(p) | Node::Binding(p) => p,
159             node => bug!("pattern became {:?}", node),
160         };
161         Pat::from_hir(self.tcx, self.param_env, self.tables(), p)
162     }
163
164     crate fn trait_method(
165         &mut self,
166         trait_def_id: DefId,
167         method_name: Symbol,
168         self_ty: Ty<'tcx>,
169         params: &[GenericArg<'tcx>],
170     ) -> &'tcx ty::Const<'tcx> {
171         let substs = self.tcx.mk_substs_trait(self_ty, params);
172
173         // The unhygienic comparison here is acceptable because this is only
174         // used on known traits.
175         let item = self
176             .tcx
177             .associated_items(trait_def_id)
178             .filter_by_name_unhygienic(method_name)
179             .find(|item| item.kind == ty::AssocKind::Method)
180             .expect("trait method not found");
181
182         let method_ty = self.tcx.type_of(item.def_id);
183         let method_ty = method_ty.subst(self.tcx, substs);
184         ty::Const::zero_sized(self.tcx, method_ty)
185     }
186
187     crate fn all_fields(&mut self, adt_def: &ty::AdtDef, variant_index: VariantIdx) -> Vec<Field> {
188         (0..adt_def.variants[variant_index].fields.len()).map(Field::new).collect()
189     }
190
191     crate fn needs_drop(&mut self, ty: Ty<'tcx>) -> bool {
192         ty.needs_drop(self.tcx, self.param_env)
193     }
194
195     crate fn tcx(&self) -> TyCtxt<'tcx> {
196         self.tcx
197     }
198
199     crate fn tables(&self) -> &'a ty::TypeckTables<'tcx> {
200         self.tables
201     }
202
203     crate fn check_overflow(&self) -> bool {
204         self.check_overflow
205     }
206
207     crate fn type_is_copy_modulo_regions(&self, ty: Ty<'tcx>, span: Span) -> bool {
208         self.infcx.type_is_copy_modulo_regions(self.param_env, ty, span)
209     }
210 }
211
212 impl<'tcx> UserAnnotatedTyHelpers<'tcx> for Cx<'_, 'tcx> {
213     fn tcx(&self) -> TyCtxt<'tcx> {
214         self.tcx()
215     }
216
217     fn tables(&self) -> &ty::TypeckTables<'tcx> {
218         self.tables()
219     }
220 }
221
222 mod block;
223 mod expr;
224 mod to_ref;