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