Release coccinelle-0.2.0
[bpt/coccinelle.git] / engine / cocci_vs_c.mli
CommitLineData
9f8e26f4
C
1(*
2 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
3 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
4 * This file is part of Coccinelle.
5 *
6 * Coccinelle is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, according to version 2 of the License.
9 *
10 * Coccinelle is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * The authors reserve the right to distribute this or future versions of
19 * Coccinelle under other licenses.
20 *)
21
22
34e49164
C
23(*****************************************************************************)
24(* Cocci vs C *)
25(*****************************************************************************)
26
27(* This module was introduced to factorize code between
28 * pattern.ml and transformation.ml. In both cases we need
29 * to "compare" a piece of C with a piece of Cocci, and depending
30 * if we want just to pattern or transform, we perform different
31 * actions on the tokens. So, the common code is in this module
32 * and the module specific actions are in pattern.ml and transformation.ml.
33 *
34 * We could have used a visitor approach as in visitor_c but I prefer
35 * this time to use a functor. The specific actions are passed
36 * via a module to the functor.
37 *
38 * If the functor is too complex too understand, you can look at
39 * the comments in pattern.ml and transformation.ml to look at
40 * how it was done before, which may help to understand how
41 * it is done now.
42 *
43 * You can also look at the papers on parser combinators in haskell
44 * (cf a pearl by meijer in ICFP) to understand our monadic
45 * approach to matching/unifying.
46 *)
47
48
49(* should be used as less as possible. Most of the time the code in
50 * cocci_vs_c should be the same if we pattern or transform *)
51type mode = PatternMode | TransformMode
52
53(* used in both pattern and transform, in envf *)
54val equal_metavarval :
55 Ast_c.metavar_binding_kind -> Ast_c.metavar_binding_kind -> bool
56
978fd7e5
C
57(* for inherited metavariables. no declaration link on expressions *)
58val equal_inh_metavarval :
59 Ast_c.metavar_binding_kind -> Ast_c.metavar_binding_kind -> bool
60
34e49164
C
61(*****************************************************************************)
62(* The parameter of the functor (the specific actions) *)
63(*****************************************************************************)
64
65
66module type PARAM =
67 sig
68 type tin
69 type 'a tout
70
71 (* a matcher between 'a' and 'b' take 'a' and 'b' in parameter,
72 * and "something" (tin; a state that is threaded across calls),
73 * and return a new 'a' and 'b' encapsulated in "something" (tout)
74 *)
75 type ('a, 'b) matcher = 'a -> 'b -> tin -> ('a * 'b) tout
76
77 val mode : mode
78
79 (* -------------------------------------------------------------------- *)
80 (* The monadic combinators *)
81 (* -------------------------------------------------------------------- *)
82
83 (* it kinds of take a matcher in parameter, and another matcher,
84 * and returns a matcher, so =~ matcher -> matcher -> matcher
85 *)
86 val ( >>= ) :
87 (tin -> ('a * 'b) tout) ->
88 ('a -> 'b -> tin -> ('c * 'd) tout) ->
89 tin -> ('c * 'd) tout
90
91 val return : 'a * 'b -> tin -> ('a * 'b) tout
92 val fail : tin -> ('a * 'b) tout
93
94 val ( >||> ) : (tin -> 'a tout) -> (tin -> 'a tout) -> tin -> 'a tout
95 val ( >|+|> ) : (tin -> 'a tout) -> (tin -> 'a tout) -> tin -> 'a tout
96 val ( >&&> ) : (tin -> bool) -> (tin -> 'a tout) -> tin -> 'a tout
97
98 (* -------------------------------------------------------------------- *)
99 (* Tokens tagging *)
100 (* -------------------------------------------------------------------- *)
101 val tokenf : ('a Ast_cocci.mcode, Ast_c.info) matcher
102 val tokenf_mck : (Ast_cocci.mcodekind, Ast_c.info) matcher
103
104 (* -------------------------------------------------------------------- *)
105 (* Distr_f functions, to tag a range of tokens *)
106 (* -------------------------------------------------------------------- *)
107
108 val distrf_e :
109 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.expression) matcher
110
111 val distrf_args :
112 (Ast_cocci.meta_name Ast_cocci.mcode,
113 (Ast_c.argument, Ast_c.il) Common.either list)
114 matcher
115
116 val distrf_type :
117 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.fullType) matcher
118
119 val distrf_params :
120 (Ast_cocci.meta_name Ast_cocci.mcode,
121 (Ast_c.parameterType, Ast_c.il) Common.either list)
122 matcher
123 val distrf_param :
124 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.parameterType) matcher
125
126 val distrf_ini :
127 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.initialiser) matcher
128
129 val distrf_node :
130 (Ast_cocci.meta_name Ast_cocci.mcode, Control_flow_c.node) matcher
131
132 val distrf_define_params :
133 (Ast_cocci.meta_name Ast_cocci.mcode,
134 (string Ast_c.wrap, Ast_c.il) Common.either list)
135 matcher
136
137 val distrf_struct_fields :
485bce71 138 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.field list)
34e49164
C
139 matcher
140
141 val distrf_cst :
142 (Ast_cocci.meta_name Ast_cocci.mcode,
143 (Ast_c.constant, string) Common.either Ast_c.wrap)
144 matcher
145
146 (* -------------------------------------------------------------------- *)
147 (* Modifying nested expression and nested types, with Exp and Ty *)
148 (* -------------------------------------------------------------------- *)
149
150 val cocciExp :
151 (Ast_cocci.expression, Ast_c.expression) matcher ->
152 (Ast_cocci.expression, Control_flow_c.node) matcher
153
154 val cocciExpExp :
155 (Ast_cocci.expression, Ast_c.expression) matcher ->
156 (Ast_cocci.expression, Ast_c.expression) matcher
157
158 val cocciTy :
159 (Ast_cocci.fullType, Ast_c.fullType) matcher ->
160 (Ast_cocci.fullType, Control_flow_c.node) matcher
161
1be43e12
C
162 val cocciInit :
163 (Ast_cocci.initialiser, Ast_c.initialiser) matcher ->
164 (Ast_cocci.initialiser, Control_flow_c.node) matcher
165
34e49164
C
166 (* -------------------------------------------------------------------- *)
167 (* Environment manipulation. Extract info from tin, the "something" *)
168 (* -------------------------------------------------------------------- *)
169 val envf :
170 Ast_cocci.keep_binding ->
171 Ast_cocci.inherited ->
172 Ast_cocci.meta_name Ast_cocci.mcode * Ast_c.metavar_binding_kind *
173 (* pos info, if needed *)
485bce71 174 (unit -> Common.filename * string * Ast_c.posl * Ast_c.posl) ->
34e49164
C
175 (unit -> tin -> 'x tout) -> (tin -> 'x tout)
176
951c7801
C
177 val check_idconstraint :
178 ('a -> 'b -> bool) -> 'a -> 'b ->
179 (unit -> tin -> 'x tout) -> (tin -> 'x tout)
180
181 val check_constraints_ne :
34e49164
C
182 ('a, 'b) matcher -> 'a list -> 'b ->
183 (unit -> tin -> 'x tout) -> (tin -> 'x tout)
184
185 val all_bound : Ast_cocci.meta_name list -> tin -> bool
186
187
188 val optional_storage_flag : (bool -> tin -> 'x tout) -> (tin -> 'x tout)
189 val optional_qualifier_flag : (bool -> tin -> 'x tout) -> (tin -> 'x tout)
190 val value_format_flag: (bool -> tin -> 'x tout) -> (tin -> 'x tout)
191
192
193 end
194
195
196(*****************************************************************************)
197(* The functor itself *)
198(*****************************************************************************)
199
200module COCCI_VS_C :
201 functor (X : PARAM) ->
202 sig
203 type ('a, 'b) matcher = 'a -> 'b -> X.tin -> ('a * 'b) X.tout
204
205 val rule_elem_node : (Ast_cocci.rule_elem, Control_flow_c.node) matcher
206
207 val expression : (Ast_cocci.expression, Ast_c.expression) matcher
208
91eba41f 209 (* there are far more functions in this functor but they do not have
34e49164
C
210 * to be exported
211 *)
212
213 end
214
215