Coccinelle release-1.0.0-rc11
[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
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
34e49164
C
27(*****************************************************************************)
28(* Cocci vs C *)
29(*****************************************************************************)
30
ae4735db 31(* This module was introduced to factorize code between
34e49164
C
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.
ae4735db 37 *
34e49164
C
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.
ae4735db 41 *
34e49164
C
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.
ae4735db
C
46 *
47 * You can also look at the papers on parser combinators in haskell
34e49164
C
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 *)
55type mode = PatternMode | TransformMode
56
57(* used in both pattern and transform, in envf *)
ae4735db 58val equal_metavarval :
34e49164
C
59 Ast_c.metavar_binding_kind -> Ast_c.metavar_binding_kind -> bool
60
978fd7e5 61(* for inherited metavariables. no declaration link on expressions *)
ae4735db 62val equal_inh_metavarval :
978fd7e5
C
63 Ast_c.metavar_binding_kind -> Ast_c.metavar_binding_kind -> bool
64
34e49164
C
65(*****************************************************************************)
66(* The parameter of the functor (the specific actions) *)
67(*****************************************************************************)
68
69
70module type PARAM =
71 sig
72 type tin
73 type 'a tout
74
ae4735db 75 (* a matcher between 'a' and 'b' take 'a' and 'b' in parameter,
34e49164
C
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
ae4735db 87 (* it kinds of take a matcher in parameter, and another matcher,
34e49164
C
88 * and returns a matcher, so =~ matcher -> matcher -> matcher
89 *)
90 val ( >>= ) :
91 (tin -> ('a * 'b) tout) ->
ae4735db 92 ('a -> 'b -> tin -> ('c * 'd) tout) ->
34e49164
C
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
ae4735db 112 val distrf_e :
34e49164
C
113 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.expression) matcher
114
115 val distrf_args :
ae4735db 116 (Ast_cocci.meta_name Ast_cocci.mcode,
34e49164
C
117 (Ast_c.argument, Ast_c.il) Common.either list)
118 matcher
119
ae4735db 120 val distrf_type :
34e49164
C
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
ae4735db 127 val distrf_param :
34e49164
C
128 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.parameterType) matcher
129
ae4735db 130 val distrf_ini :
34e49164 131 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.initialiser) matcher
c491d8ee
C
132 val distrf_inis :
133 (Ast_cocci.meta_name Ast_cocci.mcode,
134 (Ast_c.initialiser, Ast_c.il) Common.either list) matcher
413ffc02
C
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
34e49164 139
ae4735db 140 val distrf_node :
34e49164
C
141 (Ast_cocci.meta_name Ast_cocci.mcode, Control_flow_c.node) matcher
142
ae4735db
C
143 val distrf_define_params :
144 (Ast_cocci.meta_name Ast_cocci.mcode,
34e49164
C
145 (string Ast_c.wrap, Ast_c.il) Common.either list)
146 matcher
147
c491d8ee
C
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
34e49164 152 val distrf_struct_fields :
ae4735db 153 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.field list)
34e49164
C
154 matcher
155
156 val distrf_cst :
ae4735db 157 (Ast_cocci.meta_name Ast_cocci.mcode,
34e49164
C
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 :
ae4735db 166 (Ast_cocci.expression, Ast_c.expression) matcher ->
34e49164
C
167 (Ast_cocci.expression, Control_flow_c.node) matcher
168
169 val cocciExpExp :
ae4735db 170 (Ast_cocci.expression, Ast_c.expression) matcher ->
34e49164
C
171 (Ast_cocci.expression, Ast_c.expression) matcher
172
173 val cocciTy :
ae4735db 174 (Ast_cocci.fullType, Ast_c.fullType) matcher ->
34e49164
C
175 (Ast_cocci.fullType, Control_flow_c.node) matcher
176
1be43e12 177 val cocciInit :
ae4735db 178 (Ast_cocci.initialiser, Ast_c.initialiser) matcher ->
1be43e12
C
179 (Ast_cocci.initialiser, Control_flow_c.node) matcher
180
34e49164
C
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 *)
485bce71 189 (unit -> Common.filename * string * Ast_c.posl * Ast_c.posl) ->
34e49164
C
190 (unit -> tin -> 'x tout) -> (tin -> 'x tout)
191
951c7801
C
192 val check_idconstraint :
193 ('a -> 'b -> bool) -> 'a -> 'b ->
194 (unit -> tin -> 'x tout) -> (tin -> 'x tout)
195
196 val check_constraints_ne :
34e49164
C
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)
5427db06
C
206 val optional_declarer_semicolon_flag :
207 (bool -> tin -> 'x tout) -> (tin -> 'x tout)
34e49164
C
208
209 end
210
211
212(*****************************************************************************)
213(* The functor itself *)
214(*****************************************************************************)
215
216module 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
91eba41f 225 (* there are far more functions in this functor but they do not have
ae4735db 226 * to be exported
34e49164
C
227 *)
228
229 end
230
231