Skip to content

Commit 60ac68c

Browse files
committed
Use Model trait to map logical model with Rust types
1 parent 974109a commit 60ac68c

17 files changed

Lines changed: 396 additions & 87 deletions

src/analyze.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ mod crate_;
2929
mod did_cache;
3030
mod local_def;
3131

32+
// TODO: organize structure and remove cross dependency between refine
33+
pub use did_cache::DefIdCache;
34+
3235
pub fn local_of_function_param(idx: rty::FunctionParamIdx) -> Local {
3336
Local::from(idx.index() + 1)
3437
}
@@ -209,6 +212,11 @@ impl<'tcx> Analyzer<'tcx> {
209212
}
210213
}
211214

215+
pub fn def_ids(&self) -> did_cache::DefIdCache<'tcx> {
216+
// DefIdCache is backed by Rc
217+
self.def_ids.clone()
218+
}
219+
212220
pub fn add_clause(&mut self, clause: chc::Clause) {
213221
self.system.borrow_mut().push_clause(clause);
214222
}
@@ -234,7 +242,7 @@ impl<'tcx> Analyzer<'tcx> {
234242
.iter()
235243
.map(|field| {
236244
let field_ty = self.tcx.type_of(field.did).instantiate_identity();
237-
TypeBuilder::new(self.tcx, def_id).build(field_ty)
245+
TypeBuilder::new(self.tcx, self.def_ids(), def_id).build(field_ty)
238246
})
239247
.collect();
240248
rty::EnumVariantDef {
@@ -355,7 +363,7 @@ impl<'tcx> Analyzer<'tcx> {
355363
def_id: DefId,
356364
generic_args: mir_ty::GenericArgsRef<'tcx>,
357365
) -> Option<rty::RefinedType> {
358-
let type_builder = TypeBuilder::new(self.tcx, def_id);
366+
let type_builder = TypeBuilder::new(self.tcx, self.def_ids(), def_id);
359367

360368
let deferred_ty = match self.defs.get(&def_id)? {
361369
DefTy::Concrete(rty) => {

src/analyze/annot.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,46 @@ pub fn ignored_path() -> [Symbol; 2] {
4949
[Symbol::intern("thrust"), Symbol::intern("ignored")]
5050
}
5151

52+
pub fn model_ty_path() -> [Symbol; 3] {
53+
[
54+
Symbol::intern("thrust"),
55+
Symbol::intern("def"),
56+
Symbol::intern("model_ty"),
57+
]
58+
}
59+
60+
pub fn int_model_path() -> [Symbol; 3] {
61+
[
62+
Symbol::intern("thrust"),
63+
Symbol::intern("def"),
64+
Symbol::intern("int_model"),
65+
]
66+
}
67+
68+
pub fn mut_model_path() -> [Symbol; 3] {
69+
[
70+
Symbol::intern("thrust"),
71+
Symbol::intern("def"),
72+
Symbol::intern("mut_model"),
73+
]
74+
}
75+
76+
pub fn box_model_path() -> [Symbol; 3] {
77+
[
78+
Symbol::intern("thrust"),
79+
Symbol::intern("def"),
80+
Symbol::intern("box_model"),
81+
]
82+
}
83+
84+
pub fn array_model_path() -> [Symbol; 3] {
85+
[
86+
Symbol::intern("thrust"),
87+
Symbol::intern("def"),
88+
Symbol::intern("array_model"),
89+
]
90+
}
91+
5292
/// A [`annot::Resolver`] implementation for resolving function parameters.
5393
///
5494
/// The parameter names and their sorts needs to be configured via

src/analyze/basic_block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ impl<'tcx, 'ctx> Analyzer<'tcx, 'ctx> {
11161116
let env = ctx.new_env();
11171117
let local_decls = body.local_decls.clone();
11181118
let prophecy_vars = Default::default();
1119-
let type_builder = TypeBuilder::new(tcx, local_def_id.to_def_id());
1119+
let type_builder = TypeBuilder::new(tcx, ctx.def_ids(), local_def_id.to_def_id());
11201120
Self {
11211121
ctx,
11221122
tcx,

src/analyze/did_cache.rs

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
//! Retrieves and caches well-known [`DefId`]s.
22
33
use std::cell::OnceCell;
4+
use std::rc::Rc;
45

56
use rustc_middle::ty::TyCtxt;
67
use rustc_span::def_id::DefId;
8+
use rustc_span::symbol::Symbol;
79
use rustc_target::abi::FieldIdx;
810

911
#[derive(Debug, Clone, Default)]
1012
struct DefIds {
1113
unique: OnceCell<Option<DefId>>,
1214
nonnull: OnceCell<Option<DefId>>,
15+
16+
model_ty: OnceCell<Option<DefId>>,
17+
int_model: OnceCell<Option<DefId>>,
18+
mut_model: OnceCell<Option<DefId>>,
19+
box_model: OnceCell<Option<DefId>>,
20+
array_model: OnceCell<Option<DefId>>,
1321
}
1422

1523
/// Retrieves and caches well-known [`DefId`]s.
@@ -19,14 +27,14 @@ struct DefIds {
1927
#[derive(Clone)]
2028
pub struct DefIdCache<'tcx> {
2129
tcx: TyCtxt<'tcx>,
22-
def_ids: DefIds,
30+
def_ids: Rc<DefIds>,
2331
}
2432

2533
impl<'tcx> DefIdCache<'tcx> {
2634
pub fn new(tcx: TyCtxt<'tcx>) -> Self {
2735
Self {
2836
tcx,
29-
def_ids: DefIds::default(),
37+
def_ids: Rc::new(DefIds::default()),
3038
}
3139
}
3240

@@ -63,4 +71,60 @@ impl<'tcx> DefIdCache<'tcx> {
6371
Some(nonnull_def.did())
6472
})
6573
}
74+
75+
fn annotated_def(&self, path: &[Symbol]) -> Option<DefId> {
76+
let map = self.tcx.hir();
77+
for item_id in map.items() {
78+
let def_id = item_id.owner_id.to_def_id();
79+
if self.tcx.get_attrs_by_path(def_id, path).next().is_some() {
80+
return Some(def_id);
81+
}
82+
83+
let item = map.item(item_id);
84+
if let rustc_hir::ItemKind::Trait(_, _, _, _, trait_item_refs) = item.kind {
85+
for trait_item_ref in trait_item_refs {
86+
let def_id = trait_item_ref.id.owner_id.to_def_id();
87+
if self.tcx.get_attrs_by_path(def_id, path).next().is_some() {
88+
return Some(def_id);
89+
}
90+
}
91+
}
92+
}
93+
None
94+
}
95+
96+
pub fn model_ty(&self) -> Option<DefId> {
97+
*self
98+
.def_ids
99+
.model_ty
100+
.get_or_init(|| self.annotated_def(&crate::analyze::annot::model_ty_path()))
101+
}
102+
103+
pub fn int_model(&self) -> Option<DefId> {
104+
*self
105+
.def_ids
106+
.int_model
107+
.get_or_init(|| self.annotated_def(&crate::analyze::annot::int_model_path()))
108+
}
109+
110+
pub fn mut_model(&self) -> Option<DefId> {
111+
*self
112+
.def_ids
113+
.mut_model
114+
.get_or_init(|| self.annotated_def(&crate::analyze::annot::mut_model_path()))
115+
}
116+
117+
pub fn box_model(&self) -> Option<DefId> {
118+
*self
119+
.def_ids
120+
.box_model
121+
.get_or_init(|| self.annotated_def(&crate::analyze::annot::box_model_path()))
122+
}
123+
124+
pub fn array_model(&self) -> Option<DefId> {
125+
*self
126+
.def_ids
127+
.array_model
128+
.get_or_init(|| self.annotated_def(&crate::analyze::annot::array_model_path()))
129+
}
66130
}

src/analyze/local_def.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ impl<'tcx, 'ctx> Analyzer<'tcx, 'ctx> {
842842
let tcx = ctx.tcx;
843843
let body = tcx.optimized_mir(local_def_id.to_def_id()).clone();
844844
let drop_points = Default::default();
845-
let type_builder = TypeBuilder::new(tcx, local_def_id.to_def_id());
845+
let type_builder = TypeBuilder::new(tcx, ctx.def_ids(), local_def_id.to_def_id());
846846
Self {
847847
ctx,
848848
tcx,

0 commit comments

Comments
 (0)