Release coccinelle-0.2.3rc1
[bpt/coccinelle.git] / popl09 / insert_quantifiers.ml
1 (*
2 * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen
3 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
4 * This file is part of Coccinelle.
5 *
6 * Coccinelle is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, according to version 2 of the License.
9 *
10 * Coccinelle is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * The authors reserve the right to distribute this or future versions of
19 * Coccinelle under other licenses.
20 *)
21
22
23 (*
24 * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen
25 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
26 * This file is part of Coccinelle.
27 *
28 * Coccinelle is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, according to version 2 of the License.
31 *
32 * Coccinelle is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
36 *
37 * You should have received a copy of the GNU General Public License
38 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
39 *
40 * The authors reserve the right to distribute this or future versions of
41 * Coccinelle under other licenses.
42 *)
43
44
45 module Ast = Ast_cocci
46 module Past = Ast_popl
47
48 (* --------------------------------------------------------------------- *)
49
50 let rec fvs_sequence = function
51 Past.Seq(elem,seq) ->
52 Common.union_set (fvs_element elem) (fvs_sequence seq)
53 | Past.Empty -> []
54 | Past.SExists(var,seq) -> failwith "not possible"
55
56 and fvs_term = function
57 Past.Atomic(term) -> Ast.get_fvs term
58 | Past.IfThen(test,thn,(afvs,_,_,_)) ->
59 Common.union_set afvs
60 (Common.union_set (fvs_term test) (fvs_term thn))
61 | Past.TExists(var,term) -> failwith "not possible"
62
63 and fvs_element = function
64 Past.Term(term,_) -> fvs_term term
65 | Past.Or(seq1,seq2) ->
66 Common.union_set (fvs_sequence seq1) (fvs_sequence seq2)
67 | Past.DInfo(dots) -> fvs_dots dots
68 | Past.EExists(var,seq) -> failwith "not possible"
69
70 and fvs_dots = function
71 Past.Dots -> []
72 | Past.Nest(seq) -> fvs_sequence seq
73 | Past.When(dots,seq) -> Common.union_set (fvs_dots dots) (fvs_sequence seq)
74
75 (* --------------------------------------------------------------------- *)
76
77 let inter_set l1 l2 = List.filter (function l1e -> List.mem l1e l2) l1
78
79 let minus_set l1 l2 = List.filter (function l1e -> not (List.mem l1e l2)) l1
80
81 let rec quant_sequence bound = function
82 Past.Seq(elem,seq) ->
83 let fe = fvs_element elem in
84 let fs = fvs_sequence seq in
85 let inter = inter_set fe fs in
86 let free = minus_set inter bound in
87 let new_bound = free @ bound in
88 List.fold_right (function cur -> function rest -> Past.SExists(cur,rest))
89 free (Past.Seq(quant_element new_bound elem,
90 quant_sequence new_bound seq))
91 | Past.Empty -> Past.Empty
92 | Past.SExists(var,seq) -> failwith "not possible"
93
94 and quant_term bound = function
95 (Past.Atomic(term)) as x ->
96 let free = minus_set (Ast.get_fvs term) bound in
97 List.fold_right (function cur -> function rest -> Past.TExists(cur,rest))
98 free x
99 | Past.IfThen(test,thn,((afvs,_,_,_) as aft)) ->
100 let fts = fvs_term test in
101 let fth = fvs_term thn in
102 let inter = inter_set fts fth in
103 let free = minus_set inter bound in
104 let new_bound = free @ bound in
105 List.fold_right (function cur -> function rest -> Past.TExists(cur,rest))
106 free (Past.IfThen(quant_term new_bound test,
107 quant_term new_bound thn,
108 aft))
109 | Past.TExists(var,term) -> failwith "not possible"
110
111 and quant_element bound = function
112 Past.Term(term,ba) ->
113 Past.Term(quant_term bound term,dots_bef_aft bound ba)
114 | Past.Or(seq1,seq2) ->
115 Past.Or(quant_sequence bound seq1,quant_sequence bound seq2)
116 | Past.DInfo(dots) ->
117 Past.DInfo(quant_dots bound dots)
118 | Past.EExists(var,seq) -> failwith "not possible"
119
120 and dots_bef_aft bound = function
121 Past.AddingBetweenDots (brace_term,n) ->
122 Past.AddingBetweenDots (quant_term bound brace_term,n)
123 | Past.DroppingBetweenDots (brace_term,n) ->
124 Past.DroppingBetweenDots (quant_term bound brace_term,n)
125 | Past.NoDots -> Past.NoDots
126
127 and quant_dots bound = function
128 Past.Dots -> Past.Dots
129 | Past.Nest(seq) -> Past.Nest(quant_sequence bound seq)
130 | Past.When(dots,seq) ->
131 Past.When(quant_dots bound dots, quant_sequence bound seq)
132
133 (* --------------------------------------------------------------------- *)
134
135 let insert_quantifiers x = quant_sequence [] x