|
1 | | -use rustc_middle::mir::interpret::{alloc_range, AllocRange, ConstValue, Pointer}; |
2 | | - |
3 | 1 | use super::{mir::Mutability, mir::Safety, with, DefId}; |
4 | | -use crate::{ |
5 | | - rustc_internal::{opaque, Opaque}, |
6 | | - rustc_smir::{Stable, Tables}, |
7 | | -}; |
| 2 | +use crate::rustc_internal::Opaque; |
8 | 3 |
|
9 | 4 | #[derive(Copy, Clone, Debug)] |
10 | 5 | pub struct Ty(pub usize); |
@@ -286,124 +281,6 @@ pub struct Allocation { |
286 | 281 | pub mutability: Mutability, |
287 | 282 | } |
288 | 283 |
|
289 | | -impl Allocation { |
290 | | - /// Creates new empty `Allocation` from given `Align`. |
291 | | - fn new_empty_allocation(align: rustc_target::abi::Align) -> Allocation { |
292 | | - Allocation { |
293 | | - bytes: Vec::new(), |
294 | | - provenance: ProvenanceMap { ptrs: Vec::new() }, |
295 | | - align: align.bytes(), |
296 | | - mutability: Mutability::Not, |
297 | | - } |
298 | | - } |
299 | | -} |
300 | | - |
301 | | -// We need this method instead of a Stable implementation |
302 | | -// because we need to get `Ty` of the const we are trying to create, to do that |
303 | | -// we need to have access to `ConstantKind` but we can't access that inside Stable impl. |
304 | | -#[allow(rustc::usage_of_qualified_ty)] |
305 | | -pub fn new_allocation<'tcx>( |
306 | | - ty: rustc_middle::ty::Ty<'tcx>, |
307 | | - const_value: ConstValue<'tcx>, |
308 | | - tables: &mut Tables<'tcx>, |
309 | | -) -> Allocation { |
310 | | - match const_value { |
311 | | - ConstValue::Scalar(scalar) => { |
312 | | - let size = scalar.size(); |
313 | | - let align = tables |
314 | | - .tcx |
315 | | - .layout_of(rustc_middle::ty::ParamEnv::reveal_all().and(ty)) |
316 | | - .unwrap() |
317 | | - .align; |
318 | | - let mut allocation = rustc_middle::mir::interpret::Allocation::uninit(size, align.abi); |
319 | | - allocation |
320 | | - .write_scalar(&tables.tcx, alloc_range(rustc_target::abi::Size::ZERO, size), scalar) |
321 | | - .unwrap(); |
322 | | - allocation.stable(tables) |
323 | | - } |
324 | | - ConstValue::ZeroSized => { |
325 | | - let align = |
326 | | - tables.tcx.layout_of(rustc_middle::ty::ParamEnv::empty().and(ty)).unwrap().align; |
327 | | - Allocation::new_empty_allocation(align.abi) |
328 | | - } |
329 | | - ConstValue::Slice { data, start, end } => { |
330 | | - let alloc_id = tables.tcx.create_memory_alloc(data); |
331 | | - let ptr = Pointer::new(alloc_id, rustc_target::abi::Size::from_bytes(start)); |
332 | | - let scalar_ptr = rustc_middle::mir::interpret::Scalar::from_pointer(ptr, &tables.tcx); |
333 | | - let scalar_len = rustc_middle::mir::interpret::Scalar::from_target_usize( |
334 | | - (end - start) as u64, |
335 | | - &tables.tcx, |
336 | | - ); |
337 | | - let layout = |
338 | | - tables.tcx.layout_of(rustc_middle::ty::ParamEnv::reveal_all().and(ty)).unwrap(); |
339 | | - let mut allocation = |
340 | | - rustc_middle::mir::interpret::Allocation::uninit(layout.size, layout.align.abi); |
341 | | - allocation |
342 | | - .write_scalar( |
343 | | - &tables.tcx, |
344 | | - alloc_range(rustc_target::abi::Size::ZERO, tables.tcx.data_layout.pointer_size), |
345 | | - scalar_ptr, |
346 | | - ) |
347 | | - .unwrap(); |
348 | | - allocation |
349 | | - .write_scalar( |
350 | | - &tables.tcx, |
351 | | - alloc_range(tables.tcx.data_layout.pointer_size, scalar_len.size()), |
352 | | - scalar_len, |
353 | | - ) |
354 | | - .unwrap(); |
355 | | - allocation.stable(tables) |
356 | | - } |
357 | | - ConstValue::ByRef { alloc, offset } => { |
358 | | - let ty_size = tables |
359 | | - .tcx |
360 | | - .layout_of(rustc_middle::ty::ParamEnv::reveal_all().and(ty)) |
361 | | - .unwrap() |
362 | | - .size; |
363 | | - allocation_filter(&alloc.0, alloc_range(offset, ty_size), tables) |
364 | | - } |
365 | | - } |
366 | | -} |
367 | | - |
368 | | -/// Creates an `Allocation` only from information within the `AllocRange`. |
369 | | -pub fn allocation_filter<'tcx>( |
370 | | - alloc: &rustc_middle::mir::interpret::Allocation, |
371 | | - alloc_range: AllocRange, |
372 | | - tables: &mut Tables<'tcx>, |
373 | | -) -> Allocation { |
374 | | - let mut bytes: Vec<Option<u8>> = alloc |
375 | | - .inspect_with_uninit_and_ptr_outside_interpreter( |
376 | | - alloc_range.start.bytes_usize()..alloc_range.end().bytes_usize(), |
377 | | - ) |
378 | | - .iter() |
379 | | - .copied() |
380 | | - .map(Some) |
381 | | - .collect(); |
382 | | - for (i, b) in bytes.iter_mut().enumerate() { |
383 | | - if !alloc |
384 | | - .init_mask() |
385 | | - .get(rustc_target::abi::Size::from_bytes(i + alloc_range.start.bytes_usize())) |
386 | | - { |
387 | | - *b = None; |
388 | | - } |
389 | | - } |
390 | | - let mut ptrs = Vec::new(); |
391 | | - for (offset, prov) in alloc |
392 | | - .provenance() |
393 | | - .ptrs() |
394 | | - .iter() |
395 | | - .filter(|a| a.0 >= alloc_range.start && a.0 <= alloc_range.end()) |
396 | | - { |
397 | | - ptrs.push((offset.bytes_usize() - alloc_range.start.bytes_usize(), opaque(prov))); |
398 | | - } |
399 | | - Allocation { |
400 | | - bytes: bytes, |
401 | | - provenance: ProvenanceMap { ptrs }, |
402 | | - align: alloc.align.bytes(), |
403 | | - mutability: alloc.mutability.stable(tables), |
404 | | - } |
405 | | -} |
406 | | - |
407 | 284 | #[derive(Clone, Debug)] |
408 | 285 | pub enum ConstantKind { |
409 | 286 | Allocated(Allocation), |
|
0 commit comments