Coccinelle release 1.0.0-rc13
[bpt/coccinelle.git] / engine / cocci_vs_c.mli
CommitLineData
f537ebc4 1(*
17ba0788
C
2 * Copyright 2012, INRIA
3 * Julia Lawall, Gilles Muller
4 * Copyright 2010-2011, INRIA, University of Copenhagen
f537ebc4
C
5 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
6 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
7 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
8 * This file is part of Coccinelle.
9 *
10 * Coccinelle is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
d6ce1786
C
12 * the Free Software Foundation, according to version 2 of the License.
13 *
14 * Coccinelle is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
24 *)
25
26
27# 0 "./cocci_vs_c.mli"
28(*
29 * Copyright 2012, INRIA
30 * Julia Lawall, Gilles Muller
31 * Copyright 2010-2011, INRIA, University of Copenhagen
32 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
33 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
34 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
35 * This file is part of Coccinelle.
36 *
37 * Coccinelle is free software: you can redistribute it and/or modify
38 * it under the terms of the GNU General Public License as published by
f537ebc4
C
39 * the Free Software Foundation, according to version 2 of the License.
40 *
41 * Coccinelle is distributed in the hope that it will be useful,
42 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 * GNU General Public License for more details.
45 *
46 * You should have received a copy of the GNU General Public License
47 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
48 *
49 * The authors reserve the right to distribute this or future versions of
50 * Coccinelle under other licenses.
51 *)
52
53
feec80c3 54# 0 "./cocci_vs_c.mli"
34e49164
C
55(*****************************************************************************)
56(* Cocci vs C *)
57(*****************************************************************************)
58
ae4735db 59(* This module was introduced to factorize code between
34e49164
C
60 * pattern.ml and transformation.ml. In both cases we need
61 * to "compare" a piece of C with a piece of Cocci, and depending
62 * if we want just to pattern or transform, we perform different
63 * actions on the tokens. So, the common code is in this module
64 * and the module specific actions are in pattern.ml and transformation.ml.
ae4735db 65 *
34e49164
C
66 * We could have used a visitor approach as in visitor_c but I prefer
67 * this time to use a functor. The specific actions are passed
68 * via a module to the functor.
ae4735db 69 *
34e49164
C
70 * If the functor is too complex too understand, you can look at
71 * the comments in pattern.ml and transformation.ml to look at
72 * how it was done before, which may help to understand how
73 * it is done now.
ae4735db
C
74 *
75 * You can also look at the papers on parser combinators in haskell
34e49164
C
76 * (cf a pearl by meijer in ICFP) to understand our monadic
77 * approach to matching/unifying.
78 *)
79
80
81(* should be used as less as possible. Most of the time the code in
82 * cocci_vs_c should be the same if we pattern or transform *)
83type mode = PatternMode | TransformMode
84
85(* used in both pattern and transform, in envf *)
ae4735db 86val equal_metavarval :
34e49164
C
87 Ast_c.metavar_binding_kind -> Ast_c.metavar_binding_kind -> bool
88
978fd7e5 89(* for inherited metavariables. no declaration link on expressions *)
ae4735db 90val equal_inh_metavarval :
978fd7e5
C
91 Ast_c.metavar_binding_kind -> Ast_c.metavar_binding_kind -> bool
92
34e49164
C
93(*****************************************************************************)
94(* The parameter of the functor (the specific actions) *)
95(*****************************************************************************)
96
97
98module type PARAM =
99 sig
100 type tin
101 type 'a tout
102
ae4735db 103 (* a matcher between 'a' and 'b' take 'a' and 'b' in parameter,
34e49164
C
104 * and "something" (tin; a state that is threaded across calls),
105 * and return a new 'a' and 'b' encapsulated in "something" (tout)
106 *)
107 type ('a, 'b) matcher = 'a -> 'b -> tin -> ('a * 'b) tout
108
109 val mode : mode
110
111 (* -------------------------------------------------------------------- *)
112 (* The monadic combinators *)
113 (* -------------------------------------------------------------------- *)
114
ae4735db 115 (* it kinds of take a matcher in parameter, and another matcher,
34e49164
C
116 * and returns a matcher, so =~ matcher -> matcher -> matcher
117 *)
118 val ( >>= ) :
119 (tin -> ('a * 'b) tout) ->
ae4735db 120 ('a -> 'b -> tin -> ('c * 'd) tout) ->
34e49164
C
121 tin -> ('c * 'd) tout
122
123 val return : 'a * 'b -> tin -> ('a * 'b) tout
124 val fail : tin -> ('a * 'b) tout
125
126 val ( >||> ) : (tin -> 'a tout) -> (tin -> 'a tout) -> tin -> 'a tout
127 val ( >|+|> ) : (tin -> 'a tout) -> (tin -> 'a tout) -> tin -> 'a tout
128 val ( >&&> ) : (tin -> bool) -> (tin -> 'a tout) -> tin -> 'a tout
129
130 (* -------------------------------------------------------------------- *)
131 (* Tokens tagging *)
132 (* -------------------------------------------------------------------- *)
133 val tokenf : ('a Ast_cocci.mcode, Ast_c.info) matcher
134 val tokenf_mck : (Ast_cocci.mcodekind, Ast_c.info) matcher
135
136 (* -------------------------------------------------------------------- *)
137 (* Distr_f functions, to tag a range of tokens *)
138 (* -------------------------------------------------------------------- *)
139
ae4735db 140 val distrf_e :
34e49164
C
141 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.expression) matcher
142
143 val distrf_args :
ae4735db 144 (Ast_cocci.meta_name Ast_cocci.mcode,
34e49164
C
145 (Ast_c.argument, Ast_c.il) Common.either list)
146 matcher
147
ae4735db 148 val distrf_type :
34e49164
C
149 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.fullType) matcher
150
151 val distrf_params :
152 (Ast_cocci.meta_name Ast_cocci.mcode,
153 (Ast_c.parameterType, Ast_c.il) Common.either list)
154 matcher
ae4735db 155 val distrf_param :
34e49164
C
156 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.parameterType) matcher
157
ae4735db 158 val distrf_ini :
34e49164 159 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.initialiser) matcher
c491d8ee
C
160 val distrf_inis :
161 (Ast_cocci.meta_name Ast_cocci.mcode,
162 (Ast_c.initialiser, Ast_c.il) Common.either list) matcher
413ffc02
C
163 val distrf_decl :
164 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.declaration) matcher
165 val distrf_field :
166 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.field) matcher
34e49164 167
ae4735db 168 val distrf_node :
34e49164
C
169 (Ast_cocci.meta_name Ast_cocci.mcode, Control_flow_c.node) matcher
170
ae4735db
C
171 val distrf_define_params :
172 (Ast_cocci.meta_name Ast_cocci.mcode,
34e49164
C
173 (string Ast_c.wrap, Ast_c.il) Common.either list)
174 matcher
175
c491d8ee
C
176 val distrf_enum_fields :
177 (Ast_cocci.meta_name Ast_cocci.mcode,
178 (Ast_c.oneEnumType, Ast_c.il) Common.either list) matcher
179
34e49164 180 val distrf_struct_fields :
ae4735db 181 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.field list)
34e49164
C
182 matcher
183
184 val distrf_cst :
ae4735db 185 (Ast_cocci.meta_name Ast_cocci.mcode,
34e49164
C
186 (Ast_c.constant, string) Common.either Ast_c.wrap)
187 matcher
188
189 (* -------------------------------------------------------------------- *)
190 (* Modifying nested expression and nested types, with Exp and Ty *)
191 (* -------------------------------------------------------------------- *)
192
193 val cocciExp :
ae4735db 194 (Ast_cocci.expression, Ast_c.expression) matcher ->
34e49164
C
195 (Ast_cocci.expression, Control_flow_c.node) matcher
196
197 val cocciExpExp :
ae4735db 198 (Ast_cocci.expression, Ast_c.expression) matcher ->
34e49164
C
199 (Ast_cocci.expression, Ast_c.expression) matcher
200
201 val cocciTy :
ae4735db 202 (Ast_cocci.fullType, Ast_c.fullType) matcher ->
34e49164
C
203 (Ast_cocci.fullType, Control_flow_c.node) matcher
204
1be43e12 205 val cocciInit :
ae4735db 206 (Ast_cocci.initialiser, Ast_c.initialiser) matcher ->
1be43e12
C
207 (Ast_cocci.initialiser, Control_flow_c.node) matcher
208
34e49164
C
209 (* -------------------------------------------------------------------- *)
210 (* Environment manipulation. Extract info from tin, the "something" *)
211 (* -------------------------------------------------------------------- *)
212 val envf :
213 Ast_cocci.keep_binding ->
214 Ast_cocci.inherited ->
215 Ast_cocci.meta_name Ast_cocci.mcode * Ast_c.metavar_binding_kind *
216 (* pos info, if needed *)
485bce71 217 (unit -> Common.filename * string * Ast_c.posl * Ast_c.posl) ->
34e49164
C
218 (unit -> tin -> 'x tout) -> (tin -> 'x tout)
219
951c7801
C
220 val check_idconstraint :
221 ('a -> 'b -> bool) -> 'a -> 'b ->
222 (unit -> tin -> 'x tout) -> (tin -> 'x tout)
223
224 val check_constraints_ne :
34e49164
C
225 ('a, 'b) matcher -> 'a list -> 'b ->
226 (unit -> tin -> 'x tout) -> (tin -> 'x tout)
227
228 val all_bound : Ast_cocci.meta_name list -> tin -> bool
229
230
231 val optional_storage_flag : (bool -> tin -> 'x tout) -> (tin -> 'x tout)
232 val optional_qualifier_flag : (bool -> tin -> 'x tout) -> (tin -> 'x tout)
233 val value_format_flag: (bool -> tin -> 'x tout) -> (tin -> 'x tout)
5427db06
C
234 val optional_declarer_semicolon_flag :
235 (bool -> tin -> 'x tout) -> (tin -> 'x tout)
34e49164
C
236
237 end
238
239
240(*****************************************************************************)
241(* The functor itself *)
242(*****************************************************************************)
243
244module COCCI_VS_C :
245 functor (X : PARAM) ->
246 sig
247 type ('a, 'b) matcher = 'a -> 'b -> X.tin -> ('a * 'b) X.tout
248
249 val rule_elem_node : (Ast_cocci.rule_elem, Control_flow_c.node) matcher
250
251 val expression : (Ast_cocci.expression, Ast_c.expression) matcher
252
91eba41f 253 (* there are far more functions in this functor but they do not have
ae4735db 254 * to be exported
34e49164
C
255 *)
256
257 end
258
259