Release coccinelle-0.2.4rc6
[bpt/coccinelle.git] / parsing_cocci / id_utils.ml
1 (*
2 * Copyright 2010, INRIA, University of Copenhagen
3 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
4 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
5 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
6 * This file is part of Coccinelle.
7 *
8 * Coccinelle is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, according to version 2 of the License.
11 *
12 * Coccinelle is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * The authors reserve the right to distribute this or future versions of
21 * Coccinelle under other licenses.
22 *)
23
24
25 (* evaluation of nested and and or using id-utils *)
26
27 module GC = Get_constants2
28
29 let evaluated = Hashtbl.create(100)
30
31 exception Out
32
33 let memoize exp vl = Hashtbl.add evaluated exp vl; vl
34
35 let rec interpret dir exp =
36 let res = try Some (Hashtbl.find evaluated exp) with Not_found -> None in
37 match res with
38 Some x -> x
39 | None ->
40 memoize exp
41 (match exp with
42 GC.Elem oo ->
43 let cmd =
44 Printf.sprintf
45 "lid -f %s/.id-utils.index -l %s -S newline" dir oo in
46 Common.cmd_to_list cmd
47 | GC.And l ->
48 let rec loop = function
49 [] -> failwith "bad and"
50 | [x] -> interpret dir x
51 | x :: xs ->
52 (match interpret dir x with
53 [] -> raise Out
54 | resx ->
55 let resxs = loop xs in
56 Common.inter_set resx resxs) in
57 (try loop l with Out -> [])
58 | GC.Or l ->
59 List.fold_left
60 (function prev -> function cur ->
61 Common.union_set (interpret dir cur) prev)
62 [] l
63 | _ -> failwith "not possible")