Release coccinelle-0.2.3rc4
[bpt/coccinelle.git] / engine / cocci_vs_c.mli
CommitLineData
5636bb2c 1(*
90aeb998
C
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
9f8e26f4
C
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
34e49164
C
25(*****************************************************************************)
26(* Cocci vs C *)
27(*****************************************************************************)
28
ae4735db 29(* This module was introduced to factorize code between
34e49164
C
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.
ae4735db 35 *
34e49164
C
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.
ae4735db 39 *
34e49164
C
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.
ae4735db
C
44 *
45 * You can also look at the papers on parser combinators in haskell
34e49164
C
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 *)
53type mode = PatternMode | TransformMode
54
55(* used in both pattern and transform, in envf *)
ae4735db 56val equal_metavarval :
34e49164
C
57 Ast_c.metavar_binding_kind -> Ast_c.metavar_binding_kind -> bool
58
978fd7e5 59(* for inherited metavariables. no declaration link on expressions *)
ae4735db 60val equal_inh_metavarval :
978fd7e5
C
61 Ast_c.metavar_binding_kind -> Ast_c.metavar_binding_kind -> bool
62
34e49164
C
63(*****************************************************************************)
64(* The parameter of the functor (the specific actions) *)
65(*****************************************************************************)
66
67
68module type PARAM =
69 sig
70 type tin
71 type 'a tout
72
ae4735db 73 (* a matcher between 'a' and 'b' take 'a' and 'b' in parameter,
34e49164
C
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
ae4735db 85 (* it kinds of take a matcher in parameter, and another matcher,
34e49164
C
86 * and returns a matcher, so =~ matcher -> matcher -> matcher
87 *)
88 val ( >>= ) :
89 (tin -> ('a * 'b) tout) ->
ae4735db 90 ('a -> 'b -> tin -> ('c * 'd) tout) ->
34e49164
C
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
ae4735db 110 val distrf_e :
34e49164
C
111 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.expression) matcher
112
113 val distrf_args :
ae4735db 114 (Ast_cocci.meta_name Ast_cocci.mcode,
34e49164
C
115 (Ast_c.argument, Ast_c.il) Common.either list)
116 matcher
117
ae4735db 118 val distrf_type :
34e49164
C
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
ae4735db 125 val distrf_param :
34e49164
C
126 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.parameterType) matcher
127
ae4735db 128 val distrf_ini :
34e49164
C
129 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.initialiser) matcher
130
ae4735db 131 val distrf_node :
34e49164
C
132 (Ast_cocci.meta_name Ast_cocci.mcode, Control_flow_c.node) matcher
133
ae4735db
C
134 val distrf_define_params :
135 (Ast_cocci.meta_name Ast_cocci.mcode,
34e49164
C
136 (string Ast_c.wrap, Ast_c.il) Common.either list)
137 matcher
138
139 val distrf_struct_fields :
ae4735db 140 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.field list)
34e49164
C
141 matcher
142
143 val distrf_cst :
ae4735db 144 (Ast_cocci.meta_name Ast_cocci.mcode,
34e49164
C
145 (Ast_c.constant, string) Common.either Ast_c.wrap)
146 matcher
147
148 (* -------------------------------------------------------------------- *)
149 (* Modifying nested expression and nested types, with Exp and Ty *)
150 (* -------------------------------------------------------------------- *)
151
152 val cocciExp :
ae4735db 153 (Ast_cocci.expression, Ast_c.expression) matcher ->
34e49164
C
154 (Ast_cocci.expression, Control_flow_c.node) matcher
155
156 val cocciExpExp :
ae4735db 157 (Ast_cocci.expression, Ast_c.expression) matcher ->
34e49164
C
158 (Ast_cocci.expression, Ast_c.expression) matcher
159
160 val cocciTy :
ae4735db 161 (Ast_cocci.fullType, Ast_c.fullType) matcher ->
34e49164
C
162 (Ast_cocci.fullType, Control_flow_c.node) matcher
163
1be43e12 164 val cocciInit :
ae4735db 165 (Ast_cocci.initialiser, Ast_c.initialiser) matcher ->
1be43e12
C
166 (Ast_cocci.initialiser, Control_flow_c.node) matcher
167
34e49164
C
168 (* -------------------------------------------------------------------- *)
169 (* Environment manipulation. Extract info from tin, the "something" *)
170 (* -------------------------------------------------------------------- *)
171 val envf :
172 Ast_cocci.keep_binding ->
173 Ast_cocci.inherited ->
174 Ast_cocci.meta_name Ast_cocci.mcode * Ast_c.metavar_binding_kind *
175 (* pos info, if needed *)
485bce71 176 (unit -> Common.filename * string * Ast_c.posl * Ast_c.posl) ->
34e49164
C
177 (unit -> tin -> 'x tout) -> (tin -> 'x tout)
178
951c7801
C
179 val check_idconstraint :
180 ('a -> 'b -> bool) -> 'a -> 'b ->
181 (unit -> tin -> 'x tout) -> (tin -> 'x tout)
182
183 val check_constraints_ne :
34e49164
C
184 ('a, 'b) matcher -> 'a list -> 'b ->
185 (unit -> tin -> 'x tout) -> (tin -> 'x tout)
186
187 val all_bound : Ast_cocci.meta_name list -> tin -> bool
188
189
190 val optional_storage_flag : (bool -> tin -> 'x tout) -> (tin -> 'x tout)
191 val optional_qualifier_flag : (bool -> tin -> 'x tout) -> (tin -> 'x tout)
192 val value_format_flag: (bool -> tin -> 'x tout) -> (tin -> 'x tout)
193
194
195 end
196
197
198(*****************************************************************************)
199(* The functor itself *)
200(*****************************************************************************)
201
202module COCCI_VS_C :
203 functor (X : PARAM) ->
204 sig
205 type ('a, 'b) matcher = 'a -> 'b -> X.tin -> ('a * 'b) X.tout
206
207 val rule_elem_node : (Ast_cocci.rule_elem, Control_flow_c.node) matcher
208
209 val expression : (Ast_cocci.expression, Ast_c.expression) matcher
210
91eba41f 211 (* there are far more functions in this functor but they do not have
ae4735db 212 * to be exported
34e49164
C
213 *)
214
215 end
216
217