Skip to content

Commit 6692dc2

Browse files
authored
Merge pull request #63 from coord-e/coord-e/model-traits
Add Model trait to map logical models with Rust types
2 parents fd015c6 + d628b83 commit 6692dc2

27 files changed

Lines changed: 532 additions & 89 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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,54 @@ 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+
92+
pub fn closure_model_path() -> [Symbol; 3] {
93+
[
94+
Symbol::intern("thrust"),
95+
Symbol::intern("def"),
96+
Symbol::intern("closure_model"),
97+
]
98+
}
99+
52100
/// A [`annot::Resolver`] implementation for resolving function parameters.
53101
///
54102
/// 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: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
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>>,
21+
closure_model: OnceCell<Option<DefId>>,
1322
}
1423

1524
/// Retrieves and caches well-known [`DefId`]s.
@@ -19,14 +28,14 @@ struct DefIds {
1928
#[derive(Clone)]
2029
pub struct DefIdCache<'tcx> {
2130
tcx: TyCtxt<'tcx>,
22-
def_ids: DefIds,
31+
def_ids: Rc<DefIds>,
2332
}
2433

2534
impl<'tcx> DefIdCache<'tcx> {
2635
pub fn new(tcx: TyCtxt<'tcx>) -> Self {
2736
Self {
2837
tcx,
29-
def_ids: DefIds::default(),
38+
def_ids: Rc::new(DefIds::default()),
3039
}
3140
}
3241

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

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)