1use crate::ast::*;
2use derive_generic_visitor::{Drive, DriveMut, DriveTwo};
3use enum_as_inner::EnumAsInner;
4use serde_state::DeserializeState;
5use serde_state::SerializeState;
6
7#[derive(Debug, Clone)]
9#[derive(SerializeState, DeserializeState, Drive, DriveMut, DriveTwo)]
10pub struct FunDecl {
11 pub def_id: FunDeclId,
12 pub item_meta: ItemMeta,
14 pub generics: GenericParams,
15 pub signature: Box<FunSig>,
17 pub src: FunSource,
19 pub body: Body,
21}
22
23#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
25#[derive(SerializeState, DeserializeState, Drive, DriveMut, DriveTwo)]
26pub struct FunSig {
27 pub is_unsafe: bool,
29 pub abi: Abi,
31 pub is_variadic: bool,
33 pub inputs: Vec<Ty>,
34 pub output: Ty,
35}
36
37#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
38#[derive(EnumAsInner)]
39#[derive(SerializeState, DeserializeState, Drive, DriveMut, DriveTwo)]
40#[serde_state(stateless)]
41#[cfg_attr(feature = "charon_on_charon", charon::variants_prefix("Abi"))]
42pub enum Abi {
43 Rust,
44 C,
45 Other(ustr::Ustr),
47}
48
49#[derive(Debug, Clone)]
51#[derive(SerializeState, DeserializeState, Drive, DriveMut, DriveTwo)]
52#[cfg_attr(feature = "charon_on_charon", charon::variants_suffix("Fun"))]
53pub enum FunSource {
54 Normal,
56 AdtConstructor,
58 InherentImpl {
60 self_ty: Ty,
62 },
63 TraitDefault {
65 trait_ref: TraitDeclRef,
67 item_id: TraitMethodId,
70 },
71 TraitImpl {
73 impl_ref: TraitImplRef,
75 trait_ref: TraitDeclRef,
77 item_id: TraitMethodId,
80 reuses_default: bool,
83 },
84 VTableShim,
88 GlobalInitializer(GlobalDeclRef),
90 TargetDependent { dispatcher: FunDeclRef },
93}
94
95impl FunDecl {
96 pub fn substitute_params(self, subst: Binder<GenericArgs>) -> Self {
98 let FunDecl {
99 def_id,
100 item_meta,
101 generics: _,
102 signature,
103 src,
104 body,
105 } = self;
106 let signature = signature.substitute(&subst.skip_binder);
107 let src = src.substitute(&subst.skip_binder);
108 let body = body.substitute(&subst.skip_binder);
109 FunDecl {
110 def_id,
111 item_meta,
112 generics: subst.params,
113 signature,
114 src,
115 body,
116 }
117 }
118
119 pub fn is_unsafe_to_call(&self, _krate: &TranslatedCrate) -> bool {
123 self.signature.is_unsafe
124 }
125
126 pub fn is_unsafe_to_declare(&self, _krate: &TranslatedCrate) -> bool {
128 !matches!(self.src, FunSource::GlobalInitializer(_))
130 && self.item_meta.is_unsafe_to_declare()
131 }
132
133 pub fn as_fn_ptr(&self) -> RegionBinder<FunSig> {
135 struct RebindLateRegions<'a> {
136 late_regions: &'a IndexVec<RegionId, Option<RegionId>>,
137 }
138
139 impl VarsVisitor for RebindLateRegions<'_> {
140 fn visit_region_var(&mut self, var: RegionDbVar) -> Option<Region> {
141 let DeBruijnVar::Bound(depth, id) = var else {
142 return None;
143 };
144 if depth == DeBruijnId::one() {
145 self.late_regions[id].map(|id| Region::Var(DeBruijnVar::new_at_zero(id)))
146 } else {
147 None
148 }
149 }
150 }
151
152 let mut regions = IndexVec::new();
153 let late_regions = self.generics.regions.map_ref(|param| {
154 if param.late_bound {
155 Some(regions.push_with(|index| RegionParam {
156 index,
157 ..param.clone()
158 }))
159 } else {
160 None
161 }
162 });
163 let mut signature = self.signature.as_ref().clone().move_under_binder();
164 signature.visit_vars(&mut RebindLateRegions {
165 late_regions: &late_regions,
166 });
167 RegionBinder {
168 regions,
169 outlives: Vec::new(),
172 skip_binder: signature,
173 }
174 }
175}
176
177impl Abi {
178 pub fn rust() -> Self {
179 Self::Rust
180 }
181
182 pub fn rust_name(&self) -> &str {
183 match self {
184 Self::Rust => "Rust",
185 Self::C => "C",
186 Self::Other(name) => name.as_str(),
187 }
188 }
189}