2 * Copyright 2012, INRIA
3 * Julia Lawall, Gilles Muller
4 * Copyright 2010-2011, INRIA, University of Copenhagen
5 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
6 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
7 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
8 * This file is part of Coccinelle.
10 * Coccinelle is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, according to version 2 of the License.
14 * Coccinelle is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
27 #
0 "./postprocess_transinfo.ml"
28 (* two goals: first drop from the environments things that are not used,
29 and second prompt for the names of fresh variables that are used *)
31 (* have to add in the whole inherited env because inherited variables are
32 not returned by get_fvs. It would be better if that were the case, but
33 since at the moment I think we can only inherit one value per variable,
34 perhaps it doesn't matter - these bindings will always be the same no matter
35 how we reached a particular match *)
37 module Ast
= Ast_cocci
39 let extra_counter = ref 0
41 let ctr = !extra_counter in
42 extra_counter := !extra_counter + 1;
43 "__extra_counter__"^
(string_of_int
ctr)
46 let ctr = !extra_counter in
47 extra_counter := !extra_counter + 1;
48 seed^
(string_of_int
ctr)
52 let s = read_line
() in
53 match Parse_c.tokens_of_string
s with
54 [Parser_c.TIdent _
; Parser_c.EOF _
] -> s
55 | [Parser_c.EOF _
] -> get_extra()
56 | _
-> failwith
("wrong fresh id: " ^
s)
57 with End_of_file
-> get_extra()
59 let get_vars = function
60 Lib_engine.Match
(re
) -> (Ast.get_fvs re
, Ast.get_fresh re
)
63 let string2val str
= Lib_engine.NormalMetaVal
(Ast_c.MetaIdVal
(str
,[]))
65 (* ----------------------------------------------------------------------- *)
66 (* Get values for fresh variables *)
68 let process_tree inherited_env l
=
69 let (all_fresh
,local_freshs
,new_triples
) =
71 (function (all_fresh
,local_freshs
,new_triples
) ->
72 function (node
,env
,pred
) ->
73 let (other
,fresh
) = get_vars pred
in
74 let env = List.filter
(function (x
,_
) -> List.mem x other
) env in
75 (Common.union_set fresh all_fresh
,
77 (node
,env@inherited_env
,pred
)::new_triples
))
79 let local_freshs = List.rev
local_freshs in
80 let new_triples = List.rev
new_triples in
84 ((r
,n
) as fresh
,Ast.NoVal
) ->
85 Printf.printf
"%s: name for %s: " r n
; (* not debugging code!!! *)
87 (fresh
,let v = string2val(read_fresh_id()) in function _
-> v)
88 | ((r
,n
) as fresh
,Ast.StringSeed seed
) ->
89 (fresh
,let v = string2val(get_seeded seed
) in function _
-> v)
90 | ((r
,n
) as fresh
,Ast.ListSeed seed
) ->
99 (match List.assoc id
env with
100 Lib_engine.NormalMetaVal
(Ast_c.MetaIdVal
(str
,_
)) ->
102 | _
-> failwith
"bad id value")
106 ("fresh: no binding for meta "^
(Dumper.dump id
)))
108 string2val(String.concat
"" strings)))
113 (function freshs_node_env_preds
->
114 function (fresh
,fn
) ->
116 (function (freshs
,((node
,env,pred
) as cur
)) ->
118 let _ = List.assoc fresh freshs
in
119 (freshs
,(node
,(fresh
,fn
env)::env,pred
))
120 with Not_found
-> (freshs
,cur
))
121 freshs_node_env_preds
)
122 (List.combine
local_freshs new_triples)
124 (List.rev res
, fresh_env)
126 (* ----------------------------------------------------------------------- *)
127 (* Create the environment to be used afterwards *)
128 (* anything that a used after fresh variable refers to has to have a unique
129 value, by virtue of the placement of the quantifier. thus we augment
130 inherited env with the first element of l, if any *)
132 let collect_used_after used_after envs l inherited_env
=
134 (function env -> function l
->
139 (* l represents the result from a single tree. fse is a complete
140 environment in that tree. for a fresh seed, the environments
141 for all leaves contain the same information *)
144 (function (v,vl
) -> (v,vl
inherited_env))
145 (List.filter
(function (v,vl
) -> List.mem
v used_after
) env))
148 (* ----------------------------------------------------------------------- *)
149 (* distinguish between distinct witness trees, each gets an index n *)
150 (* index should be global, so that it can extend over environments *)
154 let fold_left_with_index f acc
=
155 let rec fold_lwi_aux acc
= function
160 fold_lwi_aux (f acc x
n) xs
163 let numberify trees
=
166 (function acc
-> function xs
-> function n ->
167 (List.map
(function x
-> (n,x
)) xs
) @ acc
)
172 let (same
,diff
) = List.partition
(function (ns
,xs
) -> x
= xs
) res
in
174 [(ns
,xs
)] -> (n::ns
,xs
)::diff
178 (* ----------------------------------------------------------------------- *)
181 let process used_after
inherited_env l
=
182 let (trees, fresh_envs
) =
183 List.split
(List.map
(process_tree inherited_env) l
) in
184 let trees = numberify trees in
185 (trees, collect_used_after used_after fresh_envs l
inherited_env)