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