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