Skip to content

Commit 13b922a

Browse files
committed
Implement proper Queries.Any.hash
1 parent a7ce040 commit 13b922a

File tree

1 file changed

+73
-43
lines changed

1 file changed

+73
-43
lines changed

src/domains/queries.ml

Lines changed: 73 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ module MustBool = BoolDomain.MustBool
5656
module Unit = Lattice.Unit
5757

5858
(* Helper definitions for deriving complex parts of Any.compare below. *)
59-
type maybepublic = {global: CilType.Varinfo.t; write: bool} [@@deriving ord]
60-
type maybepublicwithout = {global: CilType.Varinfo.t; write: bool; without_mutex: PreValueDomain.Addr.t} [@@deriving ord]
61-
type mustbeprotectedby = {mutex: PreValueDomain.Addr.t; global: CilType.Varinfo.t; write: bool} [@@deriving ord]
62-
type partaccess = {exp: CilType.Exp.t; var_opt: CilType.Varinfo.t option; write: bool} [@@deriving ord]
59+
type maybepublic = {global: CilType.Varinfo.t; write: bool} [@@deriving ord, hash]
60+
type maybepublicwithout = {global: CilType.Varinfo.t; write: bool; without_mutex: PreValueDomain.Addr.t} [@@deriving ord, hash]
61+
type mustbeprotectedby = {mutex: PreValueDomain.Addr.t; global: CilType.Varinfo.t; write: bool} [@@deriving ord, hash]
62+
type partaccess = {exp: CilType.Exp.t; var_opt: CilType.Varinfo.t option; write: bool} [@@deriving ord, hash]
6363

6464
(** GADT for queries with specific result type. *)
6565
type _ t =
@@ -213,45 +213,45 @@ struct
213213
type t = any_query
214214

215215
(* deriving ord doesn't work for GADTs (t and any_query) so this must be done manually... *)
216+
let order = function
217+
| Any (EqualSet _) -> 0
218+
| Any (MayPointTo _) -> 1
219+
| Any (ReachableFrom _) -> 2
220+
| Any (ReachableUkTypes _) -> 3
221+
| Any (Regions _) -> 4
222+
| Any (MayEscape _) -> 5
223+
| Any (Priority _) -> 6
224+
| Any (MayBePublic _) -> 7
225+
| Any (MayBePublicWithout _) -> 8
226+
| Any (MustBeProtectedBy _) -> 9
227+
| Any CurrentLockset -> 10
228+
| Any MustBeAtomic -> 11
229+
| Any MustBeSingleThreaded -> 12
230+
| Any MustBeUniqueThread -> 13
231+
| Any CurrentThreadId -> 14
232+
| Any MayBeThreadReturn -> 15
233+
| Any (EvalFunvar _) -> 16
234+
| Any (EvalInt _) -> 17
235+
| Any (EvalStr _) -> 18
236+
| Any (EvalLength _) -> 19
237+
| Any (BlobSize _) -> 20
238+
| Any PrintFullState -> 21
239+
| Any (CondVars _) -> 22
240+
| Any (PartAccess _) -> 23
241+
| Any (IterPrevVars _) -> 24
242+
| Any (IterVars _) -> 25
243+
| Any (MustBeEqual _) -> 26
244+
| Any (MayBeEqual _) -> 27
245+
| Any (MayBeLess _) -> 28
246+
| Any HeapVar -> 29
247+
| Any (IsHeapVar _) -> 30
248+
| Any (IsMultiple _) -> 31
249+
| Any (EvalThread _) -> 32
250+
| Any CreatedThreads -> 33
251+
| Any MustJoinedThreads -> 34
252+
| Any (WarnGlobal _) -> 35
253+
216254
let compare a b =
217-
let order = function
218-
| Any (EqualSet _) -> 0
219-
| Any (MayPointTo _) -> 1
220-
| Any (ReachableFrom _) -> 2
221-
| Any (ReachableUkTypes _) -> 3
222-
| Any (Regions _) -> 4
223-
| Any (MayEscape _) -> 5
224-
| Any (Priority _) -> 6
225-
| Any (MayBePublic _) -> 7
226-
| Any (MayBePublicWithout _) -> 8
227-
| Any (MustBeProtectedBy _) -> 9
228-
| Any CurrentLockset -> 10
229-
| Any MustBeAtomic -> 11
230-
| Any MustBeSingleThreaded -> 12
231-
| Any MustBeUniqueThread -> 13
232-
| Any CurrentThreadId -> 14
233-
| Any MayBeThreadReturn -> 15
234-
| Any (EvalFunvar _) -> 16
235-
| Any (EvalInt _) -> 17
236-
| Any (EvalStr _) -> 18
237-
| Any (EvalLength _) -> 19
238-
| Any (BlobSize _) -> 20
239-
| Any PrintFullState -> 21
240-
| Any (CondVars _) -> 22
241-
| Any (PartAccess _) -> 23
242-
| Any (IterPrevVars _) -> 24
243-
| Any (IterVars _) -> 25
244-
| Any (MustBeEqual _) -> 26
245-
| Any (MayBeEqual _) -> 27
246-
| Any (MayBeLess _) -> 28
247-
| Any HeapVar -> 29
248-
| Any (IsHeapVar _) -> 30
249-
| Any (IsMultiple _) -> 31
250-
| Any (EvalThread _) -> 32
251-
| Any CreatedThreads -> 33
252-
| Any MustJoinedThreads -> 34
253-
| Any (WarnGlobal _) -> 35
254-
in
255255
let r = Stdlib.compare (order a) (order b) in
256256
if r <> 0 then
257257
r
@@ -291,5 +291,35 @@ struct
291291

292292
let equal x y = compare x y = 0
293293

294-
let hash x = 0
294+
let hash_arg = function
295+
| Any (EqualSet e) -> CilType.Exp.hash e
296+
| Any (MayPointTo e) -> CilType.Exp.hash e
297+
| Any (ReachableFrom e) -> CilType.Exp.hash e
298+
| Any (ReachableUkTypes e) -> CilType.Exp.hash e
299+
| Any (Regions e) -> CilType.Exp.hash e
300+
| Any (MayEscape vi) -> CilType.Varinfo.hash vi
301+
| Any (Priority s) -> Hashtbl.hash s
302+
| Any (MayBePublic x) -> hash_maybepublic x
303+
| Any (MayBePublicWithout x) -> hash_maybepublicwithout x
304+
| Any (MustBeProtectedBy x) -> hash_mustbeprotectedby x
305+
| Any (EvalFunvar e) -> CilType.Exp.hash e
306+
| Any (EvalInt e) -> CilType.Exp.hash e
307+
| Any (EvalStr e) -> CilType.Exp.hash e
308+
| Any (EvalLength e) -> CilType.Exp.hash e
309+
| Any (BlobSize e) -> CilType.Exp.hash e
310+
| Any (CondVars e) -> CilType.Exp.hash e
311+
| Any (PartAccess p) -> hash_partaccess p
312+
| Any (IterPrevVars i) -> 0
313+
| Any (IterVars i) -> 0
314+
| Any (MustBeEqual (e1, e2)) -> [%hash: CilType.Exp.t * CilType.Exp.t] (e1, e2)
315+
| Any (MayBeEqual (e1, e2)) -> [%hash: CilType.Exp.t * CilType.Exp.t] (e1, e2)
316+
| Any (MayBeLess (e1, e2)) -> [%hash: CilType.Exp.t * CilType.Exp.t] (e1, e2)
317+
| Any (IsHeapVar v) -> CilType.Varinfo.hash v
318+
| Any (IsMultiple v) -> CilType.Varinfo.hash v
319+
| Any (EvalThread e) -> CilType.Exp.hash e
320+
| Any (WarnGlobal vi) -> Hashtbl.hash vi
321+
(* only argumentless queries should remain *)
322+
| _ -> 0
323+
324+
let hash x = 31 * order x + hash_arg x
295325
end

0 commit comments

Comments
 (0)