2018-08-24 14:39:25 +00:00
|
|
|
// Not in interpret to make sure we do not use private implementation details
|
|
|
|
|
2020-03-21 16:17:01 +00:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::mir;
|
|
|
|
use rustc_middle::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
|
|
|
|
2020-03-06 23:56:32 +00:00
|
|
|
pub(crate) fn const_caller_location(
|
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);
|
2020-04-29 10:34:51 +00:00
|
|
|
intern_const_alloc_recursive(&mut ecx, InternKind::Constant, loc_place, false);
|
2020-02-25 17:10:34 +00:00
|
|
|
ConstValue::Scalar(loc_place.ptr)
|
2019-10-09 15:25:41 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 09:37:59 +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. However, it is
|
|
|
|
/// invoked from the pretty printer, where it can receive enums with no variants and e.g.
|
|
|
|
/// `read_discriminant` needs to be able to handle that.
|
2020-01-05 15:46:44 +00:00
|
|
|
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);
|
2020-07-27 11:01:01 +00:00
|
|
|
let op = ecx.const_to_op(val, None).unwrap();
|
2020-01-05 15:46:44 +00:00
|
|
|
|
2020-03-21 16:17:01 +00:00
|
|
|
// We go to `usize` as we cannot allocate anything bigger anyway.
|
2020-06-18 09:37:59 +00:00
|
|
|
let (field_count, variant, down) = match val.ty.kind {
|
|
|
|
ty::Array(_, len) => (usize::try_from(len.eval_usize(tcx, param_env)).unwrap(), None, op),
|
|
|
|
ty::Adt(def, _) if def.variants.is_empty() => {
|
|
|
|
return mir::DestructuredConst { variant: None, fields: tcx.arena.alloc_slice(&[]) };
|
|
|
|
}
|
|
|
|
ty::Adt(def, _) => {
|
|
|
|
let variant = ecx.read_discriminant(op).unwrap().1;
|
|
|
|
let down = ecx.operand_downcast(op, variant).unwrap();
|
|
|
|
(def.variants[variant].fields.len(), Some(variant), down)
|
|
|
|
}
|
|
|
|
ty::Tuple(substs) => (substs.len(), None, op),
|
2020-01-05 15:46:44 +00:00
|
|
|
_ => bug!("cannot destructure constant {:?}", val),
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
}
|