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.
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.
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.
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/>.
20 * The authors reserve the right to distribute this or future versions of
21 * Coccinelle under other licenses.
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 *)
28 (* have to add in the whole inherited env because inherited variables are
29 not returned by get_fvs. It would be better if that were the case, but
30 since at the moment I think we can only inherit one value per variable,
31 perhaps it doesn't matter - these bindings will always be the same no matter
32 how we reached a particular match *)
34 module Ast
= Ast_cocci
36 let extra_counter = ref 0
38 let ctr = !extra_counter in
39 extra_counter := !extra_counter + 1;
40 "__extra_counter__"^
(string_of_int
ctr)
43 let ctr = !extra_counter in
44 extra_counter := !extra_counter + 1;
45 seed^
(string_of_int
ctr)
49 let s = read_line
() in
50 match Parse_c.tokens_of_string
s with
51 [Parser_c.TIdent _
; Parser_c.EOF _
] -> s
52 | [Parser_c.EOF _
] -> get_extra()
53 | _
-> failwith
("wrong fresh id: " ^
s)
54 with End_of_file
-> get_extra()
56 let get_vars = function
57 Lib_engine.Match
(re
) -> (Ast.get_fvs re
, Ast.get_fresh re
)
60 let string2val str
= Lib_engine.NormalMetaVal
(Ast_c.MetaIdVal
(str
,[]))
62 (* ----------------------------------------------------------------------- *)
63 (* Get values for fresh variables *)
65 let process_tree inherited_env l
=
66 let (all_fresh
,local_freshs
,new_triples
) =
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
,
74 (node
,env@inherited_env
,pred
)::new_triples
))
76 let local_freshs = List.rev
local_freshs in
77 let new_triples = List.rev
new_triples in
81 ((r
,n
) as fresh
,Ast.NoVal
) ->
82 Printf.printf
"%s: name for %s: " r n
; (* not debugging code!!! *)
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
) ->
96 (match List.assoc id
env with
97 Lib_engine.NormalMetaVal
(Ast_c.MetaIdVal
(str
,_
)) ->
99 | _
-> failwith
"bad id value")
101 Not_found
-> failwith
"fresh: no binding for meta")
103 string2val(String.concat
"" strings)))
108 (function freshs_node_env_preds
->
109 function (fresh
,fn
) ->
111 (function (freshs
,((node
,env,pred
) as cur
)) ->
113 let _ = List.assoc fresh freshs
in
114 (freshs
,(node
,(fresh
,fn
env)::env,pred
))
115 with Not_found
-> (freshs
,cur
))
116 freshs_node_env_preds
)
117 (List.combine
local_freshs new_triples)
119 (List.rev res
, fresh_env)
121 (* ----------------------------------------------------------------------- *)
122 (* Create the environment to be used afterwards *)
123 (* anything that a used after fresh variable refers to has to have a unique
124 value, by virtue of the placement of the quantifier. thus we augment
125 inherited env with the first element of l, if any *)
127 let collect_used_after used_after envs l inherited_env
=
129 (function env -> function l
->
134 (* l represents the result from a single tree. fse is a complete
135 environment in that tree. for a fresh seed, the environments
136 for all leaves contain the same information *)
139 (function (v,vl
) -> (v,vl
inherited_env))
140 (List.filter
(function (v,vl
) -> List.mem
v used_after
) env))
143 (* ----------------------------------------------------------------------- *)
144 (* distinguish between distinct witness trees, each gets an index n *)
146 let numberify trees
=
148 Common.fold_left_with_index
149 (function acc
-> function xs
-> function n
->
150 (List.map
(function x
-> (n
,x
)) xs
) @ acc
)
155 let (same
,diff
) = List.partition
(function (ns
,xs
) -> x
= xs
) res
in
157 [(ns
,xs
)] -> (n
::ns
,xs
)::diff
161 (* ----------------------------------------------------------------------- *)
164 let process used_after
inherited_env l
=
165 let (trees, fresh_envs
) =
166 List.split
(List.map
(process_tree inherited_env) l
) in
167 let trees = numberify trees in
168 (trees, collect_used_after used_after fresh_envs l
inherited_env)