coccinelle release 1.0.0-rc2
[bpt/coccinelle.git] / engine / postprocess_transinfo.ml
CommitLineData
f537ebc4
C
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
34e49164
C
25(* two goals: first drop from the environments things that are not used,
26 and second prompt for the names of fresh variables that are used *)
27
28(* have to add in the whole inherited env because inherited variables are
29not returned by get_fvs. It would be better if that were the case, but
30since at the moment I think we can only inherit one value per variable,
31perhaps it doesn't matter - these bindings will always be the same no matter
32how we reached a particular match *)
33
34module Ast = Ast_cocci
35
36let extra_counter = ref 0
37let get_extra _ =
38 let ctr = !extra_counter in
39 extra_counter := !extra_counter + 1;
40 "__extra_counter__"^(string_of_int ctr)
41
b1b2de81
C
42let get_seeded seed =
43 let ctr = !extra_counter in
44 extra_counter := !extra_counter + 1;
45 seed^(string_of_int ctr)
46
47let read_fresh_id _ =
ae4735db 48 try
34e49164 49 let s = read_line () in
485bce71 50 match Parse_c.tokens_of_string s with
34e49164 51 [Parser_c.TIdent _; Parser_c.EOF _] -> s
b1b2de81 52 | [Parser_c.EOF _] -> get_extra()
34e49164
C
53 | _ -> failwith ("wrong fresh id: " ^ s)
54 with End_of_file -> get_extra()
55
56let get_vars = function
57 Lib_engine.Match(re) -> (Ast.get_fvs re, Ast.get_fresh re)
58 | _ -> ([],[])
59
5636bb2c 60let string2val str = Lib_engine.NormalMetaVal(Ast_c.MetaIdVal(str,[]))
34e49164
C
61
62(* ----------------------------------------------------------------------- *)
63(* Get values for fresh variables *)
64
65let process_tree inherited_env l =
66 let (all_fresh,local_freshs,new_triples) =
67 List.fold_left
68 (function (all_fresh,local_freshs,new_triples) ->
69 function (node,env,pred) ->
70 let (other,fresh) = get_vars pred in
71 let env = List.filter (function (x,_) -> List.mem x other) env in
72 (Common.union_set fresh all_fresh,
73 fresh::local_freshs,
74 (node,env@inherited_env,pred)::new_triples))
75 ([],[],[]) l in
76 let local_freshs = List.rev local_freshs in
77 let new_triples = List.rev new_triples in
78 let fresh_env =
79 List.map
b1b2de81 80 (function
978fd7e5 81 ((r,n) as fresh,Ast.NoVal) ->
b1b2de81
C
82 Printf.printf "%s: name for %s: " r n; (* not debugging code!!! *)
83 flush stdout;
978fd7e5
C
84 (fresh,let v = string2val(read_fresh_id()) in function _ -> v)
85 | ((r,n) as fresh,Ast.StringSeed seed) ->
86 (fresh,let v = string2val(get_seeded seed) in function _ -> v)
87 | ((r,n) as fresh,Ast.ListSeed seed) ->
88 (fresh,
89 function env ->
90 let strings =
91 List.map
92 (function
93 Ast.SeedString s -> s
94 | Ast.SeedId id ->
95 try
96 (match List.assoc id env with
5636bb2c 97 Lib_engine.NormalMetaVal(Ast_c.MetaIdVal(str,_)) ->
978fd7e5
C
98 str
99 | _ -> failwith "bad id value")
100 with
8babbc8f
C
101 Not_found ->
102 failwith
103 ("fresh: no binding for meta "^(Dumper.dump id)))
978fd7e5
C
104 seed in
105 string2val(String.concat "" strings)))
34e49164
C
106 all_fresh in
107 let (_,res) =
108 List.split
109 (List.fold_left
110 (function freshs_node_env_preds ->
978fd7e5 111 function (fresh,fn) ->
34e49164
C
112 List.map
113 (function (freshs,((node,env,pred) as cur)) ->
b1b2de81
C
114 try
115 let _ = List.assoc fresh freshs in
978fd7e5 116 (freshs,(node,(fresh,fn env)::env,pred))
b1b2de81 117 with Not_found -> (freshs,cur))
34e49164
C
118 freshs_node_env_preds)
119 (List.combine local_freshs new_triples)
120 fresh_env) in
121 (List.rev res, fresh_env)
122
123(* ----------------------------------------------------------------------- *)
124(* Create the environment to be used afterwards *)
978fd7e5
C
125(* anything that a used after fresh variable refers to has to have a unique
126value, by virtue of the placement of the quantifier. thus we augment
127inherited env with the first element of l, if any *)
128
129let collect_used_after used_after envs l inherited_env =
130 List.map2
131 (function env -> function l ->
132 let inherited_env =
133 match l with
134 [] -> inherited_env
135 | (_,fse,_)::_ ->
136 (* l represents the result from a single tree. fse is a complete
137 environment in that tree. for a fresh seed, the environments
138 for all leaves contain the same information *)
139 fse@inherited_env in
140 List.map
141 (function (v,vl) -> (v,vl inherited_env))
142 (List.filter (function (v,vl) -> List.mem v used_after) env))
143 envs l
34e49164 144
708f4980
C
145(* ----------------------------------------------------------------------- *)
146(* distinguish between distinct witness trees, each gets an index n *)
8babbc8f
C
147(* index should be global, so that it can extend over environments *)
148
149let index = ref (-1)
150
151let fold_left_with_index f acc =
152 let rec fold_lwi_aux acc = function
153 | [] -> acc
154 | x::xs ->
155 let n = !index in
156 index := !index + 1;
157 fold_lwi_aux (f acc x n) xs
158 in fold_lwi_aux acc
708f4980
C
159
160let numberify trees =
161 let trees =
8babbc8f 162 fold_left_with_index
708f4980
C
163 (function acc -> function xs -> function n ->
164 (List.map (function x -> (n,x)) xs) @ acc)
165 [] trees in
8babbc8f 166 List.fold_left
708f4980
C
167 (function res ->
168 function (n,x) ->
169 let (same,diff) = List.partition (function (ns,xs) -> x = xs) res in
170 match same with
171 [(ns,xs)] -> (n::ns,xs)::diff
172 | _ -> ([n],x)::res)
173 [] trees
174
34e49164
C
175(* ----------------------------------------------------------------------- *)
176(* entry point *)
177
178let process used_after inherited_env l =
34e49164
C
179 let (trees, fresh_envs) =
180 List.split (List.map (process_tree inherited_env) l) in
708f4980 181 let trees = numberify trees in
978fd7e5 182 (trees, collect_used_after used_after fresh_envs l inherited_env)