2018-08-24 14:39:25 +00:00
|
|
|
// Not in interpret to make sure we do not use private implementation details
|
|
|
|
|
2018-01-02 23:22:09 +00:00
|
|
|
use rustc::mir;
|
2019-12-25 00:15:26 +00:00
|
|
|
use rustc::ty::layout::VariantIdx;
|
2019-12-25 00:09:47 +00:00
|
|
|
use rustc::ty::{self, TyCtxt};
|
2020-01-01 18:25:28 +00:00
|
|
|
use rustc_span::{source_map::DUMMY_SP, symbol::Symbol};
|
2017-12-12 16:14:49 +00:00
|
|
|
|
2019-12-25 12:58:02 +00:00
|
|
|
use crate::interpret::{intern_const_alloc_recursive, ConstValue, InternKind, InterpCx};
|
2017-12-12 16:14:49 +00:00
|
|
|
|
2019-12-25 00:04:32 +00:00
|
|
|
mod error;
|
2019-12-23 11:55:16 +00:00
|
|
|
mod eval_queries;
|
2020-01-01 17:06:00 +00:00
|
|
|
mod fn_queries;
|
2019-12-25 00:28:30 +00:00
|
|
|
mod machine;
|
2019-12-25 00:04:32 +00:00
|
|
|
|
|
|
|
pub use error::*;
|
2019-12-23 11:55:16 +00:00
|
|
|
pub use eval_queries::*;
|
2020-01-01 17:06:00 +00:00
|
|
|
pub use fn_queries::*;
|
2019-12-25 00:28:30 +00:00
|
|
|
pub use machine::*;
|
2018-09-20 09:57:45 +00:00
|
|
|
|
2019-05-24 19:58:37 +00:00
|
|
|
/// Extracts a field of a (variant of a) const.
|
2019-02-16 19:51:43 +00:00
|
|
|
// this function uses `unwrap` copiously, because an already validated constant must have valid
|
|
|
|
// fields and can thus never fail outside of compiler bugs
|
2019-12-15 16:50:37 +00:00
|
|
|
pub(crate) fn const_field<'tcx>(
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2017-12-27 20:32:01 +00:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2018-11-01 18:03:38 +00:00
|
|
|
variant: Option<VariantIdx>,
|
2017-12-27 20:32:01 +00:00
|
|
|
field: mir::Field,
|
2019-04-03 13:29:31 +00:00
|
|
|
value: &'tcx ty::Const<'tcx>,
|
2020-02-14 23:57:46 +00:00
|
|
|
) -> ConstValue<'tcx> {
|
2019-01-14 16:54:00 +00:00
|
|
|
trace!("const_field: {:?}, {:?}", field, value);
|
2019-12-16 14:23:42 +00:00
|
|
|
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
|
2019-02-16 19:51:43 +00:00
|
|
|
// get the operand again
|
2019-03-14 09:19:31 +00:00
|
|
|
let op = ecx.eval_const_to_op(value, None).unwrap();
|
2019-02-16 19:51:43 +00:00
|
|
|
// downcast
|
|
|
|
let down = match variant {
|
|
|
|
None => op,
|
|
|
|
Some(variant) => ecx.operand_downcast(op, variant).unwrap(),
|
|
|
|
};
|
|
|
|
// then project
|
|
|
|
let field = ecx.operand_field(down, field.index() as u64).unwrap();
|
|
|
|
// and finally move back to the const world, always normalizing because
|
|
|
|
// this is not called for statics.
|
2020-02-14 23:57:46 +00:00
|
|
|
op_to_const(&ecx, field)
|
2018-01-16 08:24:38 +00:00
|
|
|
}
|
|
|
|
|
2019-12-15 16:50:37 +00:00
|
|
|
pub(crate) fn const_caller_location<'tcx>(
|
2019-10-09 15:25:41 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
(file, line, col): (Symbol, u32, u32),
|
2020-02-14 23:57:46 +00:00
|
|
|
) -> ConstValue<'tcx> {
|
2019-10-09 15:25:41 +00:00
|
|
|
trace!("const_caller_location: {}:{}:{}", file, line, col);
|
2019-12-16 14:23:42 +00:00
|
|
|
let mut ecx = mk_eval_cx(tcx, DUMMY_SP, ty::ParamEnv::reveal_all(), false);
|
2019-10-09 15:25:41 +00:00
|
|
|
|
2019-11-29 10:29:30 +00:00
|
|
|
let loc_place = ecx.alloc_caller_location(file, line, col);
|
2019-12-25 12:58:02 +00:00
|
|
|
intern_const_alloc_recursive(&mut ecx, InternKind::Constant, loc_place, false).unwrap();
|
2020-02-14 23:57:46 +00:00
|
|
|
ConstValue::Scalar(loc_place.ptr.into())
|
2019-10-09 15:25:41 +00:00
|
|
|
}
|
|
|
|
|
2020-01-05 15:46:44 +00:00
|
|
|
// this function uses `unwrap` copiously, because an already validated constant
|
|
|
|
// must have valid fields and can thus never fail outside of compiler bugs
|
|
|
|
pub(crate) fn destructure_const<'tcx>(
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2017-12-27 20:32:01 +00:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2019-04-03 13:29:31 +00:00
|
|
|
val: &'tcx ty::Const<'tcx>,
|
2020-01-05 15:46:44 +00:00
|
|
|
) -> mir::DestructuredConst<'tcx> {
|
|
|
|
trace!("destructure_const: {:?}", val);
|
2019-12-16 14:23:42 +00:00
|
|
|
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
|
2019-03-14 09:19:31 +00:00
|
|
|
let op = ecx.eval_const_to_op(val, None).unwrap();
|
2020-01-05 15:46:44 +00:00
|
|
|
|
|
|
|
let variant = ecx.read_discriminant(op).unwrap().1;
|
|
|
|
|
|
|
|
let field_count = match val.ty.kind {
|
|
|
|
ty::Array(_, len) => len.eval_usize(tcx, param_env),
|
|
|
|
ty::Adt(def, _) => def.variants[variant].fields.len() as u64,
|
|
|
|
ty::Tuple(substs) => substs.len() as u64,
|
|
|
|
_ => bug!("cannot destructure constant {:?}", val),
|
|
|
|
};
|
|
|
|
|
|
|
|
let down = ecx.operand_downcast(op, variant).unwrap();
|
|
|
|
let fields_iter = (0..field_count).map(|i| {
|
|
|
|
let field_op = ecx.operand_field(down, i).unwrap();
|
2020-02-14 22:56:23 +00:00
|
|
|
let val = op_to_const(&ecx, field_op);
|
2020-02-15 20:59:01 +00:00
|
|
|
ty::Const::from_value(tcx, val, field_op.layout.ty)
|
2020-01-05 15:46:44 +00:00
|
|
|
});
|
|
|
|
let fields = tcx.arena.alloc_from_iter(fields_iter);
|
|
|
|
|
|
|
|
mir::DestructuredConst { variant, fields }
|
2018-01-16 08:24:38 +00:00
|
|
|
}
|