forked from model-checking/kani
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathty_stable.rs
More file actions
141 lines (117 loc) · 4.79 KB
/
Copy pathty_stable.rs
File metadata and controls
141 lines (117 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT
//! Stable functions involving type manipulation.
//!
//! This may for now invoke functions that use internal Rust compiler APIs.
//! While we migrate to stable APIs, this module will contain stable versions of functions from
//! `typ.rs`.
use crate::codegen_cprover_gotoc::GotocCtx;
use crate::kani_middle::abi::LayoutOf;
use cbmc::goto_program::Type;
use rustc_middle::ty::layout::{LayoutOf as _, TyAndLayout};
use rustc_public::mir::mono::Instance;
use rustc_public::mir::{Local, Operand, Place, Rvalue};
use rustc_public::rustc_internal;
use rustc_public::ty::{FnSig, RigidTy, Ty, TyKind};
impl<'tcx> GotocCtx<'tcx> {
pub fn place_ty_stable(&self, place: &Place) -> Ty {
place.ty(self.current_fn().locals()).unwrap()
}
pub fn codegen_ty_stable(&mut self, ty: Ty) -> Type {
self.codegen_ty(rustc_internal::internal(self.tcx, ty))
}
pub fn codegen_ty_ref_stable(&mut self, ty: Ty) -> Type {
self.codegen_ty_ref(rustc_internal::internal(self.tcx, ty))
}
pub fn local_ty_stable(&self, local: Local) -> Ty {
self.current_fn().locals()[local].ty
}
pub fn operand_ty_stable(&self, operand: &Operand) -> Ty {
operand.ty(self.current_fn().locals()).unwrap()
}
pub fn is_zst_stable(&self, ty: Ty) -> bool {
LayoutOf::new(ty).is_zst()
}
pub fn layout_of_stable(&self, ty: Ty) -> TyAndLayout<'tcx> {
self.layout_of(rustc_internal::internal(self.tcx, ty))
}
pub fn codegen_fndef_type_stable(&mut self, instance: Instance) -> Type {
let func = instance.mangled_name();
self.ensure_struct(
format!("{func}::FnDefStruct"),
format!("{}::FnDefStruct", instance.name()),
|_, _| vec![],
)
}
pub fn use_fat_pointer_stable(&self, pointer_ty: Ty) -> bool {
self.use_fat_pointer(rustc_internal::internal(self.tcx, pointer_ty))
}
pub fn use_thin_pointer_stable(&self, pointer_ty: Ty) -> bool {
self.use_thin_pointer(rustc_internal::internal(self.tcx, pointer_ty))
}
pub fn is_fat_pointer_stable(&self, pointer_ty: Ty) -> bool {
self.is_fat_pointer(rustc_internal::internal(self.tcx, pointer_ty))
}
pub fn is_vtable_fat_pointer_stable(&self, pointer_ty: Ty) -> bool {
self.is_vtable_fat_pointer(rustc_internal::internal(self.tcx, pointer_ty))
}
pub fn use_vtable_fat_pointer_stable(&self, pointer_ty: Ty) -> bool {
self.use_vtable_fat_pointer(rustc_internal::internal(self.tcx, pointer_ty))
}
pub fn vtable_name_stable(&self, ty: Ty) -> String {
self.vtable_name(rustc_internal::internal(self.tcx, ty))
}
pub fn rvalue_ty_stable(&self, rvalue: &Rvalue) -> Ty {
rvalue.ty(self.current_fn().locals()).unwrap()
}
pub fn simd_size_and_type(&self, ty: Ty) -> (u64, Ty) {
let (sz, ty) = rustc_internal::internal(self.tcx, ty).simd_size_and_type(self.tcx);
(sz, rustc_internal::stable(ty))
}
pub fn codegen_enum_discr_typ_stable(&self, ty: Ty) -> Ty {
rustc_internal::stable(self.codegen_enum_discr_typ(rustc_internal::internal(self.tcx, ty)))
}
pub fn codegen_function_sig_stable(&mut self, sig: FnSig) -> Type {
let params = sig
.inputs()
.iter()
.filter_map(|ty| {
if self.is_zst_stable(*ty) { None } else { Some(self.codegen_ty_stable(*ty)) }
})
.collect();
if sig.c_variadic {
Type::variadic_code_with_unnamed_parameters(
params,
self.codegen_ty_stable(sig.output()),
)
} else {
Type::code_with_unnamed_parameters(params, self.codegen_ty_stable(sig.output()))
}
}
/// Convert a type into a user readable type representation.
///
/// This should be replaced by StableMIR `pretty_ty()` after
/// <https://github.com/rust-lang/rust/pull/118364> is merged.
pub fn pretty_ty(&self, ty: Ty) -> String {
ty.to_string()
}
pub fn requires_caller_location(&self, instance: Instance) -> bool {
let instance_internal = rustc_internal::internal(self.tcx, instance);
instance_internal.def.requires_caller_location(self.tcx)
}
}
/// If given type is a Ref / Raw ref, return the pointee type.
pub fn pointee_type(mir_type: Ty) -> Option<Ty> {
match mir_type.kind() {
TyKind::RigidTy(RigidTy::Ref(_, pointee_type, _)) => Some(pointee_type),
TyKind::RigidTy(RigidTy::RawPtr(ty, ..)) => Some(ty),
_ => None,
}
}
pub fn pointee_type_stable(ty: Ty) -> Option<Ty> {
match ty.kind() {
TyKind::RigidTy(RigidTy::Ref(_, pointee_ty, _))
| TyKind::RigidTy(RigidTy::RawPtr(pointee_ty, ..)) => Some(pointee_ty),
_ => None,
}
}