-
Notifications
You must be signed in to change notification settings - Fork 155
/
setup_tracked_fn.rs
294 lines (247 loc) · 11.2 KB
/
setup_tracked_fn.rs
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/// Macro for setting up a function that must intern its arguments.
#[macro_export]
macro_rules! setup_tracked_fn {
(
// Attributes on the function
attrs: [$(#[$attr:meta]),*],
// Visibility of the function
vis: $vis:vis,
// Name of the function
fn_name: $fn_name:ident,
// Name of the `'db` lifetime that the user gave; if they didn't, then defaults to `'db`
db_lt: $db_lt:lifetime,
// Path to the database trait that the user's database parameter used
Db: $Db:path,
// Name of the database parameter given by the user.
db: $db:ident,
// An identifier for each function argument EXCEPT the database.
// We prefer to use the identifier the user gave, but if the user gave a pattern
// (e.g., `(a, b): (u32, u32)`) we will synthesize an identifier.
input_ids: [$($input_id:ident),*],
// Types of the function arguments (may reference `$generics`).
input_tys: [$($input_ty:ty),*],
// Return type of the function (may reference `$generics`).
output_ty: $output_ty:ty,
// Function body, may reference identifiers defined in `$input_pats` and the generics from `$generics`
inner_fn: $inner_fn:item,
// Path to the cycle recovery function to use.
cycle_recovery_fn: ($($cycle_recovery_fn:tt)*),
// Name of cycle recovery strategy variant to use.
cycle_recovery_strategy: $cycle_recovery_strategy:ident,
// If true, this is specifiable.
is_specifiable: $is_specifiable:tt,
// If true, don't backdate the value when the new value compares equal to the old value.
no_eq: $no_eq:tt,
// If true, the input needs an interner (because it has >1 argument).
needs_interner: $needs_interner:tt,
// LRU capacity (a literal, maybe 0)
lru: $lru:tt,
// True if we `return_ref` flag was given to the function
return_ref: $return_ref:tt,
// Annoyingly macro-rules hygiene does not extend to items defined in the macro.
// We have the procedural macro generate names for those items that are
// not used elsewhere in the user's code.
unused_names: [
$zalsa:ident,
$Configuration:ident,
$InternedData:ident,
$FN_CACHE:ident,
$INTERN_CACHE:ident,
$inner:ident,
]
) => {
// Suppress this clippy lint because we sometimes require `'db` where the ordinary Rust rules would not.
#[allow(clippy::needless_lifetimes)]
$(#[$attr])*
$vis fn $fn_name<$db_lt>(
$db: &$db_lt dyn $Db,
$($input_id: $input_ty,)*
) -> salsa::plumbing::macro_if! {
if $return_ref {
&$db_lt $output_ty
} else {
$output_ty
}
} {
use salsa::plumbing as $zalsa;
struct $Configuration;
static $FN_CACHE: $zalsa::IngredientCache<$zalsa::function::IngredientImpl<$Configuration>> =
$zalsa::IngredientCache::new();
$zalsa::macro_if! {
if $needs_interner {
#[derive(Copy, Clone)]
struct $InternedData<$db_lt>(
salsa::Id,
std::marker::PhantomData<&$db_lt $zalsa::interned::Value<$Configuration>>,
);
static $INTERN_CACHE: $zalsa::IngredientCache<$zalsa::interned::IngredientImpl<$Configuration>> =
$zalsa::IngredientCache::new();
impl $zalsa::SalsaStructInDb for $InternedData<'_> {
}
impl $zalsa::interned::Configuration for $Configuration {
const DEBUG_NAME: &'static str = "Configuration";
type Data<$db_lt> = ($($input_ty),*);
type Struct<$db_lt> = $InternedData<$db_lt>;
fn struct_from_id<$db_lt>(
id: salsa::Id,
) -> Self::Struct<$db_lt> {
$InternedData(id, std::marker::PhantomData)
}
fn deref_struct(s: Self::Struct<'_>) -> salsa::Id {
s.0
}
}
} else {
type $InternedData<$db_lt> = ($($input_ty),*);
}
}
impl $Configuration {
fn fn_ingredient(db: &dyn $Db) -> &$zalsa::function::IngredientImpl<$Configuration> {
$FN_CACHE.get_or_create(db.as_dyn_database(), || {
<dyn $Db as $Db>::zalsa_db(db);
db.zalsa().add_or_lookup_jar_by_type(&$Configuration)
})
}
$zalsa::macro_if! { $needs_interner =>
fn intern_ingredient(
db: &dyn $Db,
) -> &$zalsa::interned::IngredientImpl<$Configuration> {
$INTERN_CACHE.get_or_create(db.as_dyn_database(), || {
db.zalsa().add_or_lookup_jar_by_type(&$Configuration).successor(0)
})
}
}
}
impl $zalsa::function::Configuration for $Configuration {
const DEBUG_NAME: &'static str = stringify!($fn_name);
type DbView = dyn $Db;
type SalsaStruct<$db_lt> = $InternedData<$db_lt>;
type Input<$db_lt> = ($($input_ty),*);
type Output<$db_lt> = $output_ty;
const CYCLE_STRATEGY: $zalsa::CycleRecoveryStrategy = $zalsa::CycleRecoveryStrategy::$cycle_recovery_strategy;
fn should_backdate_value(
old_value: &Self::Output<'_>,
new_value: &Self::Output<'_>,
) -> bool {
$zalsa::macro_if! {
if $no_eq {
false
} else {
$zalsa::should_backdate_value(old_value, new_value)
}
}
}
fn execute<$db_lt>($db: &$db_lt Self::DbView, ($($input_id),*): ($($input_ty),*)) -> Self::Output<$db_lt> {
$inner_fn
$inner($db, $($input_id),*)
}
fn recover_from_cycle<$db_lt>(
db: &$db_lt dyn $Db,
cycle: &$zalsa::Cycle,
($($input_id),*): ($($input_ty),*)
) -> Self::Output<$db_lt> {
$($cycle_recovery_fn)*(db, cycle, $($input_id),*)
}
fn id_to_input<$db_lt>(db: &$db_lt Self::DbView, key: salsa::Id) -> Self::Input<$db_lt> {
$zalsa::macro_if! {
if $needs_interner {
$Configuration::intern_ingredient(db).data(db.as_dyn_database(), key).clone()
} else {
$zalsa::FromId::from_id(key)
}
}
}
}
impl $zalsa::Jar for $Configuration {
fn create_ingredients(
&self,
aux: &dyn $zalsa::JarAux,
first_index: $zalsa::IngredientIndex,
) -> Vec<Box<dyn $zalsa::Ingredient>> {
let fn_ingredient = <$zalsa::function::IngredientImpl<$Configuration>>::new(
first_index,
aux,
);
fn_ingredient.set_capacity($lru);
$zalsa::macro_if! {
if $needs_interner {
vec![
Box::new(fn_ingredient),
Box::new(<$zalsa::interned::IngredientImpl<$Configuration>>::new(
first_index.successor(0)
)),
]
} else {
vec![
Box::new(fn_ingredient),
]
}
}
}
}
#[allow(non_local_definitions)]
impl $fn_name {
pub fn accumulated<$db_lt, A: salsa::Accumulator>(
$db: &$db_lt dyn $Db,
$($input_id: $input_ty,)*
) -> Vec<A> {
use salsa::plumbing as $zalsa;
let key = $zalsa::macro_if! {
if $needs_interner {
$Configuration::intern_ingredient($db).intern_id($db.as_dyn_database(), ($($input_id),*))
} else {
$zalsa::AsId::as_id(&($($input_id),*))
}
};
$Configuration::fn_ingredient($db).accumulated_by::<A>($db, key)
}
$zalsa::macro_if! { $is_specifiable =>
pub fn specify<$db_lt>(
$db: &$db_lt dyn $Db,
$($input_id: $input_ty,)*
value: $output_ty,
) {
let key = $zalsa::AsId::as_id(&($($input_id),*));
$Configuration::fn_ingredient($db).specify_and_record(
$db,
key,
value,
)
}
}
$zalsa::macro_if! { if0 $lru { } else {
#[allow(dead_code)]
fn set_lru_capacity(db: &dyn $Db, value: usize) {
$Configuration::fn_ingredient(db).set_capacity(value);
}
} }
}
$zalsa::attach($db, || {
let result = $zalsa::macro_if! {
if $needs_interner {
{
let key = $Configuration::intern_ingredient($db).intern_id($db.as_dyn_database(), ($($input_id),*));
$Configuration::fn_ingredient($db).fetch($db, key)
}
} else {
$Configuration::fn_ingredient($db).fetch($db, $zalsa::AsId::as_id(&($($input_id),*)))
}
};
$zalsa::macro_if! {
if $return_ref {
result
} else {
<$output_ty as std::clone::Clone>::clone(result)
}
}
})
}
// The struct needs be last in the macro expansion in order to make the tracked
// function's ident be identified as a function, not a struct, during semantic highlighting.
// for more details, see https://github.com/salsa-rs/salsa/pull/612.
#[allow(non_camel_case_types)]
$vis struct $fn_name {
_priv: std::convert::Infallible,
}
};
}