]>
Commit | Line | Data |
---|---|---|
b7449926 XL |
1 | // Not in interpret to make sure we do not use private implementation details |
2 | ||
ba9703b0 XL |
3 | use std::convert::TryFrom; |
4 | ||
5 | use rustc_middle::mir; | |
6 | use rustc_middle::ty::{self, TyCtxt}; | |
dfeec247 | 7 | use rustc_span::{source_map::DUMMY_SP, symbol::Symbol}; |
ba9703b0 | 8 | use rustc_target::abi::VariantIdx; |
0bf4aa26 | 9 | |
dfeec247 | 10 | use crate::interpret::{intern_const_alloc_recursive, ConstValue, InternKind, InterpCx}; |
0bf4aa26 | 11 | |
dfeec247 XL |
12 | mod error; |
13 | mod eval_queries; | |
14 | mod fn_queries; | |
15 | mod machine; | |
0bf4aa26 | 16 | |
dfeec247 XL |
17 | pub use error::*; |
18 | pub use eval_queries::*; | |
19 | pub use fn_queries::*; | |
20 | pub use machine::*; | |
b7449926 | 21 | |
dc9dc135 | 22 | /// Extracts a field of a (variant of a) const. |
532ac7d7 XL |
23 | // this function uses `unwrap` copiously, because an already validated constant must have valid |
24 | // fields and can thus never fail outside of compiler bugs | |
dfeec247 | 25 | pub(crate) fn const_field<'tcx>( |
dc9dc135 | 26 | tcx: TyCtxt<'tcx>, |
b7449926 | 27 | param_env: ty::ParamEnv<'tcx>, |
a1dfa0c6 | 28 | variant: Option<VariantIdx>, |
b7449926 | 29 | field: mir::Field, |
dc9dc135 | 30 | value: &'tcx ty::Const<'tcx>, |
74b04a01 | 31 | ) -> ConstValue<'tcx> { |
9fa01778 | 32 | trace!("const_field: {:?}, {:?}", field, value); |
dfeec247 | 33 | let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false); |
532ac7d7 XL |
34 | // get the operand again |
35 | let op = ecx.eval_const_to_op(value, None).unwrap(); | |
36 | // downcast | |
37 | let down = match variant { | |
38 | None => op, | |
39 | Some(variant) => ecx.operand_downcast(op, variant).unwrap(), | |
40 | }; | |
41 | // then project | |
ba9703b0 | 42 | let field = ecx.operand_field(down, field.index()).unwrap(); |
532ac7d7 XL |
43 | // and finally move back to the const world, always normalizing because |
44 | // this is not called for statics. | |
45 | op_to_const(&ecx, field) | |
b7449926 XL |
46 | } |
47 | ||
ba9703b0 | 48 | pub(crate) fn const_caller_location( |
e74abb32 XL |
49 | tcx: TyCtxt<'tcx>, |
50 | (file, line, col): (Symbol, u32, u32), | |
74b04a01 | 51 | ) -> ConstValue<'tcx> { |
e74abb32 | 52 | trace!("const_caller_location: {}:{}:{}", file, line, col); |
dfeec247 | 53 | let mut ecx = mk_eval_cx(tcx, DUMMY_SP, ty::ParamEnv::reveal_all(), false); |
e74abb32 | 54 | |
60c5eb7d | 55 | let loc_place = ecx.alloc_caller_location(file, line, col); |
dfeec247 | 56 | intern_const_alloc_recursive(&mut ecx, InternKind::Constant, loc_place, false).unwrap(); |
74b04a01 | 57 | ConstValue::Scalar(loc_place.ptr) |
e74abb32 XL |
58 | } |
59 | ||
dfeec247 XL |
60 | // this function uses `unwrap` copiously, because an already validated constant |
61 | // must have valid fields and can thus never fail outside of compiler bugs | |
62 | pub(crate) fn destructure_const<'tcx>( | |
dc9dc135 | 63 | tcx: TyCtxt<'tcx>, |
b7449926 | 64 | param_env: ty::ParamEnv<'tcx>, |
dc9dc135 | 65 | val: &'tcx ty::Const<'tcx>, |
dfeec247 XL |
66 | ) -> mir::DestructuredConst<'tcx> { |
67 | trace!("destructure_const: {:?}", val); | |
68 | let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false); | |
532ac7d7 | 69 | let op = ecx.eval_const_to_op(val, None).unwrap(); |
a1dfa0c6 | 70 | |
dfeec247 | 71 | let variant = ecx.read_discriminant(op).unwrap().1; |
b7449926 | 72 | |
ba9703b0 | 73 | // We go to `usize` as we cannot allocate anything bigger anyway. |
dfeec247 | 74 | let field_count = match val.ty.kind { |
ba9703b0 XL |
75 | ty::Array(_, len) => usize::try_from(len.eval_usize(tcx, param_env)).unwrap(), |
76 | ty::Adt(def, _) => def.variants[variant].fields.len(), | |
77 | ty::Tuple(substs) => substs.len(), | |
dfeec247 XL |
78 | _ => bug!("cannot destructure constant {:?}", val), |
79 | }; | |
b7449926 | 80 | |
dfeec247 XL |
81 | let down = ecx.operand_downcast(op, variant).unwrap(); |
82 | let fields_iter = (0..field_count).map(|i| { | |
83 | let field_op = ecx.operand_field(down, i).unwrap(); | |
74b04a01 XL |
84 | let val = op_to_const(&ecx, field_op); |
85 | ty::Const::from_value(tcx, val, field_op.layout.ty) | |
dfeec247 XL |
86 | }); |
87 | let fields = tcx.arena.alloc_from_iter(fields_iter); | |
b7449926 | 88 | |
dfeec247 | 89 | mir::DestructuredConst { variant, fields } |
b7449926 | 90 | } |