Release coccinelle-0.2.3rc1
[bpt/coccinelle.git] / engine / cocci_vs_c.mli
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 (*****************************************************************************)
46 (* Cocci vs C *)
47 (*****************************************************************************)
48
49 (* This module was introduced to factorize code between
50 * pattern.ml and transformation.ml. In both cases we need
51 * to "compare" a piece of C with a piece of Cocci, and depending
52 * if we want just to pattern or transform, we perform different
53 * actions on the tokens. So, the common code is in this module
54 * and the module specific actions are in pattern.ml and transformation.ml.
55 *
56 * We could have used a visitor approach as in visitor_c but I prefer
57 * this time to use a functor. The specific actions are passed
58 * via a module to the functor.
59 *
60 * If the functor is too complex too understand, you can look at
61 * the comments in pattern.ml and transformation.ml to look at
62 * how it was done before, which may help to understand how
63 * it is done now.
64 *
65 * You can also look at the papers on parser combinators in haskell
66 * (cf a pearl by meijer in ICFP) to understand our monadic
67 * approach to matching/unifying.
68 *)
69
70
71 (* should be used as less as possible. Most of the time the code in
72 * cocci_vs_c should be the same if we pattern or transform *)
73 type mode = PatternMode | TransformMode
74
75 (* used in both pattern and transform, in envf *)
76 val equal_metavarval :
77 Ast_c.metavar_binding_kind -> Ast_c.metavar_binding_kind -> bool
78
79 (* for inherited metavariables. no declaration link on expressions *)
80 val equal_inh_metavarval :
81 Ast_c.metavar_binding_kind -> Ast_c.metavar_binding_kind -> bool
82
83 (*****************************************************************************)
84 (* The parameter of the functor (the specific actions) *)
85 (*****************************************************************************)
86
87
88 module type PARAM =
89 sig
90 type tin
91 type 'a tout
92
93 (* a matcher between 'a' and 'b' take 'a' and 'b' in parameter,
94 * and "something" (tin; a state that is threaded across calls),
95 * and return a new 'a' and 'b' encapsulated in "something" (tout)
96 *)
97 type ('a, 'b) matcher = 'a -> 'b -> tin -> ('a * 'b) tout
98
99 val mode : mode
100
101 (* -------------------------------------------------------------------- *)
102 (* The monadic combinators *)
103 (* -------------------------------------------------------------------- *)
104
105 (* it kinds of take a matcher in parameter, and another matcher,
106 * and returns a matcher, so =~ matcher -> matcher -> matcher
107 *)
108 val ( >>= ) :
109 (tin -> ('a * 'b) tout) ->
110 ('a -> 'b -> tin -> ('c * 'd) tout) ->
111 tin -> ('c * 'd) tout
112
113 val return : 'a * 'b -> tin -> ('a * 'b) tout
114 val fail : tin -> ('a * 'b) tout
115
116 val ( >||> ) : (tin -> 'a tout) -> (tin -> 'a tout) -> tin -> 'a tout
117 val ( >|+|> ) : (tin -> 'a tout) -> (tin -> 'a tout) -> tin -> 'a tout
118 val ( >&&> ) : (tin -> bool) -> (tin -> 'a tout) -> tin -> 'a tout
119
120 (* -------------------------------------------------------------------- *)
121 (* Tokens tagging *)
122 (* -------------------------------------------------------------------- *)
123 val tokenf : ('a Ast_cocci.mcode, Ast_c.info) matcher
124 val tokenf_mck : (Ast_cocci.mcodekind, Ast_c.info) matcher
125
126 (* -------------------------------------------------------------------- *)
127 (* Distr_f functions, to tag a range of tokens *)
128 (* -------------------------------------------------------------------- *)
129
130 val distrf_e :
131 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.expression) matcher
132
133 val distrf_args :
134 (Ast_cocci.meta_name Ast_cocci.mcode,
135 (Ast_c.argument, Ast_c.il) Common.either list)
136 matcher
137
138 val distrf_type :
139 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.fullType) matcher
140
141 val distrf_params :
142 (Ast_cocci.meta_name Ast_cocci.mcode,
143 (Ast_c.parameterType, Ast_c.il) Common.either list)
144 matcher
145 val distrf_param :
146 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.parameterType) matcher
147
148 val distrf_ini :
149 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.initialiser) matcher
150
151 val distrf_node :
152 (Ast_cocci.meta_name Ast_cocci.mcode, Control_flow_c.node) matcher
153
154 val distrf_define_params :
155 (Ast_cocci.meta_name Ast_cocci.mcode,
156 (string Ast_c.wrap, Ast_c.il) Common.either list)
157 matcher
158
159 val distrf_struct_fields :
160 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.field list)
161 matcher
162
163 val distrf_cst :
164 (Ast_cocci.meta_name Ast_cocci.mcode,
165 (Ast_c.constant, string) Common.either Ast_c.wrap)
166 matcher
167
168 (* -------------------------------------------------------------------- *)
169 (* Modifying nested expression and nested types, with Exp and Ty *)
170 (* -------------------------------------------------------------------- *)
171
172 val cocciExp :
173 (Ast_cocci.expression, Ast_c.expression) matcher ->
174 (Ast_cocci.expression, Control_flow_c.node) matcher
175
176 val cocciExpExp :
177 (Ast_cocci.expression, Ast_c.expression) matcher ->
178 (Ast_cocci.expression, Ast_c.expression) matcher
179
180 val cocciTy :
181 (Ast_cocci.fullType, Ast_c.fullType) matcher ->
182 (Ast_cocci.fullType, Control_flow_c.node) matcher
183
184 val cocciInit :
185 (Ast_cocci.initialiser, Ast_c.initialiser) matcher ->
186 (Ast_cocci.initialiser, Control_flow_c.node) matcher
187
188 (* -------------------------------------------------------------------- *)
189 (* Environment manipulation. Extract info from tin, the "something" *)
190 (* -------------------------------------------------------------------- *)
191 val envf :
192 Ast_cocci.keep_binding ->
193 Ast_cocci.inherited ->
194 Ast_cocci.meta_name Ast_cocci.mcode * Ast_c.metavar_binding_kind *
195 (* pos info, if needed *)
196 (unit -> Common.filename * string * Ast_c.posl * Ast_c.posl) ->
197 (unit -> tin -> 'x tout) -> (tin -> 'x tout)
198
199 val check_idconstraint :
200 ('a -> 'b -> bool) -> 'a -> 'b ->
201 (unit -> tin -> 'x tout) -> (tin -> 'x tout)
202
203 val check_constraints_ne :
204 ('a, 'b) matcher -> 'a list -> 'b ->
205 (unit -> tin -> 'x tout) -> (tin -> 'x tout)
206
207 val all_bound : Ast_cocci.meta_name list -> tin -> bool
208
209
210 val optional_storage_flag : (bool -> tin -> 'x tout) -> (tin -> 'x tout)
211 val optional_qualifier_flag : (bool -> tin -> 'x tout) -> (tin -> 'x tout)
212 val value_format_flag: (bool -> tin -> 'x tout) -> (tin -> 'x tout)
213
214
215 end
216
217
218 (*****************************************************************************)
219 (* The functor itself *)
220 (*****************************************************************************)
221
222 module COCCI_VS_C :
223 functor (X : PARAM) ->
224 sig
225 type ('a, 'b) matcher = 'a -> 'b -> X.tin -> ('a * 'b) X.tout
226
227 val rule_elem_node : (Ast_cocci.rule_elem, Control_flow_c.node) matcher
228
229 val expression : (Ast_cocci.expression, Ast_c.expression) matcher
230
231 (* there are far more functions in this functor but they do not have
232 * to be exported
233 *)
234
235 end
236
237