2 * Copyright 2005-2010, 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.
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.
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.
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/>.
18 * The authors reserve the right to distribute this or future versions of
19 * Coccinelle under other licenses.
24 * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen
25 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
26 * This file is part of Coccinelle.
28 * Coccinelle is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, according to version 2 of the License.
32 * Coccinelle is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
40 * The authors reserve the right to distribute this or future versions of
41 * Coccinelle under other licenses.
45 (*****************************************************************************)
47 (*****************************************************************************)
49 (* This module was introduced to factorize code between
50 * pattern.ml and transformation.ml. In both cases we need
51 * to "compare" a piece of C with a piece of Cocci, and depending
52 * if we want just to pattern or transform, we perform different
53 * actions on the tokens. So, the common code is in this module
54 * and the module specific actions are in pattern.ml and transformation.ml.
56 * We could have used a visitor approach as in visitor_c but I prefer
57 * this time to use a functor. The specific actions are passed
58 * via a module to the functor.
60 * If the functor is too complex too understand, you can look at
61 * the comments in pattern.ml and transformation.ml to look at
62 * how it was done before, which may help to understand how
65 * You can also look at the papers on parser combinators in haskell
66 * (cf a pearl by meijer in ICFP) to understand our monadic
67 * approach to matching/unifying.
71 (* should be used as less as possible. Most of the time the code in
72 * cocci_vs_c should be the same if we pattern or transform *)
73 type mode
= PatternMode
| TransformMode
75 (* used in both pattern and transform, in envf *)
76 val equal_metavarval
:
77 Ast_c.metavar_binding_kind
-> Ast_c.metavar_binding_kind
-> bool
79 (* for inherited metavariables. no declaration link on expressions *)
80 val equal_inh_metavarval
:
81 Ast_c.metavar_binding_kind
-> Ast_c.metavar_binding_kind
-> bool
83 (*****************************************************************************)
84 (* The parameter of the functor (the specific actions) *)
85 (*****************************************************************************)
93 (* a matcher between 'a' and 'b' take 'a' and 'b' in parameter,
94 * and "something" (tin; a state that is threaded across calls),
95 * and return a new 'a' and 'b' encapsulated in "something" (tout)
97 type ('a
, 'b
) matcher
= 'a
-> 'b
-> tin
-> ('a
* 'b
) tout
101 (* -------------------------------------------------------------------- *)
102 (* The monadic combinators *)
103 (* -------------------------------------------------------------------- *)
105 (* it kinds of take a matcher in parameter, and another matcher,
106 * and returns a matcher, so =~ matcher -> matcher -> matcher
109 (tin
-> ('a
* 'b
) tout
) ->
110 ('a
-> 'b
-> tin
-> ('c
* 'd
) tout
) ->
111 tin
-> ('c
* 'd
) tout
113 val return
: 'a
* 'b
-> tin
-> ('a
* 'b
) tout
114 val fail
: tin
-> ('a
* 'b
) tout
116 val ( >||> ) : (tin
-> 'a tout
) -> (tin
-> 'a tout
) -> tin
-> 'a tout
117 val ( >|+|> ) : (tin
-> 'a tout
) -> (tin
-> 'a tout
) -> tin
-> 'a tout
118 val ( >&&> ) : (tin
-> bool) -> (tin
-> 'a tout
) -> tin
-> 'a tout
120 (* -------------------------------------------------------------------- *)
122 (* -------------------------------------------------------------------- *)
123 val tokenf
: ('a
Ast_cocci.mcode
, Ast_c.info
) matcher
124 val tokenf_mck
: (Ast_cocci.mcodekind
, Ast_c.info
) matcher
126 (* -------------------------------------------------------------------- *)
127 (* Distr_f functions, to tag a range of tokens *)
128 (* -------------------------------------------------------------------- *)
131 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.expression
) matcher
134 (Ast_cocci.meta_name
Ast_cocci.mcode
,
135 (Ast_c.argument
, Ast_c.il
) Common.either list
)
139 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.fullType
) matcher
142 (Ast_cocci.meta_name
Ast_cocci.mcode
,
143 (Ast_c.parameterType
, Ast_c.il
) Common.either list
)
146 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.parameterType
) matcher
149 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.initialiser
) matcher
152 (Ast_cocci.meta_name
Ast_cocci.mcode
, Control_flow_c.node
) matcher
154 val distrf_define_params
:
155 (Ast_cocci.meta_name
Ast_cocci.mcode
,
156 (string Ast_c.wrap
, Ast_c.il
) Common.either list
)
159 val distrf_struct_fields
:
160 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.field list
)
164 (Ast_cocci.meta_name
Ast_cocci.mcode
,
165 (Ast_c.constant
, string) Common.either
Ast_c.wrap
)
168 (* -------------------------------------------------------------------- *)
169 (* Modifying nested expression and nested types, with Exp and Ty *)
170 (* -------------------------------------------------------------------- *)
173 (Ast_cocci.expression
, Ast_c.expression
) matcher
->
174 (Ast_cocci.expression
, Control_flow_c.node
) matcher
177 (Ast_cocci.expression
, Ast_c.expression
) matcher
->
178 (Ast_cocci.expression
, Ast_c.expression
) matcher
181 (Ast_cocci.fullType
, Ast_c.fullType
) matcher
->
182 (Ast_cocci.fullType
, Control_flow_c.node
) matcher
185 (Ast_cocci.initialiser
, Ast_c.initialiser
) matcher
->
186 (Ast_cocci.initialiser
, Control_flow_c.node
) matcher
188 (* -------------------------------------------------------------------- *)
189 (* Environment manipulation. Extract info from tin, the "something" *)
190 (* -------------------------------------------------------------------- *)
192 Ast_cocci.keep_binding
->
193 Ast_cocci.inherited
->
194 Ast_cocci.meta_name
Ast_cocci.mcode
* Ast_c.metavar_binding_kind
*
195 (* pos info, if needed *)
196 (unit -> Common.filename
* string * Ast_c.posl
* Ast_c.posl
) ->
197 (unit -> tin
-> 'x tout
) -> (tin
-> 'x tout
)
199 val check_idconstraint
:
200 ('a
-> 'b
-> bool) -> 'a
-> 'b
->
201 (unit -> tin
-> 'x tout
) -> (tin
-> 'x tout
)
203 val check_constraints_ne
:
204 ('a
, 'b
) matcher
-> 'a list
-> 'b
->
205 (unit -> tin
-> 'x tout
) -> (tin
-> 'x tout
)
207 val all_bound
: Ast_cocci.meta_name list
-> tin
-> bool
210 val optional_storage_flag
: (bool -> tin
-> 'x tout
) -> (tin
-> 'x tout
)
211 val optional_qualifier_flag
: (bool -> tin
-> 'x tout
) -> (tin
-> 'x tout
)
212 val value_format_flag
: (bool -> tin
-> 'x tout
) -> (tin
-> 'x tout
)
218 (*****************************************************************************)
219 (* The functor itself *)
220 (*****************************************************************************)
223 functor (X
: PARAM
) ->
225 type ('a
, 'b
) matcher
= 'a
-> 'b
-> X.tin
-> ('a
* 'b
) X.tout
227 val rule_elem_node
: (Ast_cocci.rule_elem
, Control_flow_c.node
) matcher
229 val expression
: (Ast_cocci.expression
, Ast_c.expression
) matcher
231 (* there are far more functions in this functor but they do not have