3a0980e04ed7c8c8267f9e0e60a04dcf64ddc2ba
[bpt/coccinelle.git] / popl / insert_quantifiers.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 module Ast = Ast_cocci
26 module Past = Ast_popl
27
28 (* --------------------------------------------------------------------- *)
29
30 let rec fvs_sequence = function
31 Past.Seq(elem,seq) ->
32 Common.union_set (fvs_element elem) (fvs_sequence seq)
33 | Past.Empty -> []
34 | Past.SExists(var,seq) -> failwith "not possible"
35
36 and fvs_element = function
37 Past.Term(term) -> Ast.get_fvs term
38 | Past.Or(seq1,seq2) ->
39 Common.union_set (fvs_sequence seq1) (fvs_sequence seq2)
40 | Past.DInfo(dots,seq_bef,seq_aft) ->
41 List.fold_left
42 (function prev ->
43 function cur ->
44 Common.union_set (fvs_element cur) prev)
45 (fvs_dots dots) seq_bef
46 | Past.EExists(var,seq) -> failwith "not possible"
47
48 and fvs_dots = function
49 Past.Dots -> []
50 | Past.Nest(seq) -> fvs_sequence seq
51 | Past.When(dots,seq) -> Common.union_set (fvs_dots dots) (fvs_sequence seq)
52 | Past.DExists(var,dots) -> failwith "not possible"
53
54 (* --------------------------------------------------------------------- *)
55
56 let rec quant_sequence bound = function
57 Past.Seq(elem,seq) ->
58 let fe = fvs_element elem in
59 let fs = fvs_sequence seq in
60 let inter = Common.inter_set fe fs in
61 let free = Common.minus_set inter bound in
62 let new_bound = free @ bound in
63 List.fold_right (function cur -> function rest -> Past.SExists(cur,rest))
64 free (Past.Seq(quant_element new_bound elem,
65 quant_sequence new_bound seq))
66 | Past.Empty -> Past.Empty
67 | Past.SExists(var,seq) -> failwith "not possible"
68
69 and quant_element bound = function
70 Past.Term(term) as x ->
71 let free = Common.minus_set (fvs_element x) bound in
72 List.fold_right (function cur -> function rest -> Past.EExists(cur,rest))
73 free x
74 | Past.Or(seq1,seq2) ->
75 Past.Or(quant_sequence bound seq1,quant_sequence bound seq2)
76 | Past.DInfo(dots,seq_bef,seq_aft) ->
77 Past.DInfo(quant_dots bound dots,seq_bef,
78 List.map (quant_element bound) seq_aft)
79 | Past.EExists(var,seq) -> failwith "not possible"
80
81 and quant_dots bound = function
82 Past.Dots -> Past.Dots
83 | Past.Nest(seq) -> Past.Nest(quant_sequence bound seq)
84 | Past.When(dots,seq) ->
85 let fd = fvs_dots dots in
86 let fs = fvs_sequence seq in
87 let inter = Common.inter_set fd fs in
88 let free = Common.minus_set inter bound in
89 let new_bound = free @ bound in
90 List.fold_right (function cur -> function rest -> Past.DExists(cur,rest))
91 free (Past.When(quant_dots new_bound dots,
92 quant_sequence new_bound seq))
93 | Past.DExists(var,dots) -> failwith "not possible"
94
95 (* --------------------------------------------------------------------- *)
96
97 let insert_quantifiers x = quant_sequence [] x