1 (* two goals: first drop from the environments things that are not used,
2 and second prompt for the names of fresh variables that are used *)
4 (* have to add in the whole inherited env because inherited variables are
5 not returned by get_fvs. It would be better if that were the case, but
6 since at the moment I think we can only inherit one value per variable,
7 perhaps it doesn't matter - these bindings will always be the same no matter
8 how we reached a particular match *)
10 module Ast
= Ast_cocci
12 let extra_counter = ref 0
14 let ctr = !extra_counter in
15 extra_counter := !extra_counter + 1;
16 "__extra_counter__"^
(string_of_int
ctr)
19 let ctr = !extra_counter in
20 extra_counter := !extra_counter + 1;
21 seed^
(string_of_int
ctr)
25 let s = read_line
() in
26 match Parse_c.tokens_of_string
s with
27 [Parser_c.TIdent _
; Parser_c.EOF _
] -> s
28 | [Parser_c.EOF _
] -> get_extra()
29 | _
-> failwith
("wrong fresh id: " ^
s)
30 with End_of_file
-> get_extra()
32 let get_vars = function
33 Lib_engine.Match
(re
) -> (Ast.get_fvs re
, Ast.get_fresh re
)
36 let string2val str
= Lib_engine.NormalMetaVal
(Ast_c.MetaIdVal
(str
))
38 (* ----------------------------------------------------------------------- *)
39 (* Get values for fresh variables *)
41 let process_tree inherited_env l
=
42 let (all_fresh
,local_freshs
,new_triples
) =
44 (function (all_fresh
,local_freshs
,new_triples
) ->
45 function (node
,env
,pred
) ->
46 let (other
,fresh
) = get_vars pred
in
47 let env = List.filter
(function (x
,_
) -> List.mem x other
) env in
48 (Common.union_set fresh all_fresh
,
50 (node
,env@inherited_env
,pred
)::new_triples
))
52 let local_freshs = List.rev
local_freshs in
53 let new_triples = List.rev
new_triples in
57 ((r
,n
) as fresh
,Ast.NoVal
) ->
58 Printf.printf
"%s: name for %s: " r n
; (* not debugging code!!! *)
60 (fresh
,let v = string2val(read_fresh_id()) in function _
-> v)
61 | ((r
,n
) as fresh
,Ast.StringSeed seed
) ->
62 (fresh
,let v = string2val(get_seeded seed
) in function _
-> v)
63 | ((r
,n
) as fresh
,Ast.ListSeed seed
) ->
72 (match List.assoc id
env with
73 Lib_engine.NormalMetaVal
(Ast_c.MetaIdVal
(str
)) ->
75 | _
-> failwith
"bad id value")
77 Not_found
-> failwith
"fresh: no binding for meta")
79 string2val(String.concat
"" strings)))
84 (function freshs_node_env_preds
->
85 function (fresh
,fn
) ->
87 (function (freshs
,((node
,env,pred
) as cur
)) ->
89 let _ = List.assoc fresh freshs
in
90 (freshs
,(node
,(fresh
,fn
env)::env,pred
))
91 with Not_found
-> (freshs
,cur
))
92 freshs_node_env_preds
)
93 (List.combine
local_freshs new_triples)
95 (List.rev res
, fresh_env)
97 (* ----------------------------------------------------------------------- *)
98 (* Create the environment to be used afterwards *)
99 (* anything that a used after fresh variable refers to has to have a unique
100 value, by virtue of the placement of the quantifier. thus we augment
101 inherited env with the first element of l, if any *)
103 let collect_used_after used_after envs l inherited_env
=
105 (function env -> function l
->
110 (* l represents the result from a single tree. fse is a complete
111 environment in that tree. for a fresh seed, the environments
112 for all leaves contain the same information *)
115 (function (v,vl
) -> (v,vl
inherited_env))
116 (List.filter
(function (v,vl
) -> List.mem
v used_after
) env))
119 (* ----------------------------------------------------------------------- *)
120 (* distinguish between distinct witness trees, each gets an index n *)
122 let numberify trees
=
124 Common.fold_left_with_index
125 (function acc
-> function xs
-> function n
->
126 (List.map
(function x
-> (n
,x
)) xs
) @ acc
)
131 let (same
,diff
) = List.partition
(function (ns
,xs
) -> x
= xs
) res
in
133 [(ns
,xs
)] -> (n
::ns
,xs
)::diff
137 (* ----------------------------------------------------------------------- *)
140 let process used_after
inherited_env l
=
141 let (trees, fresh_envs
) =
142 List.split
(List.map
(process_tree inherited_env) l
) in
143 let trees = numberify trees in
144 (trees, collect_used_after used_after fresh_envs l
inherited_env)