2 * Copyright 2012, INRIA
3 * Julia Lawall, Gilles Muller
4 * Copyright 2010-2011, INRIA, University of Copenhagen
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.
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.
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.
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/>.
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
27 (*****************************************************************************)
29 (*****************************************************************************)
31 (* This module was introduced to factorize code between
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.
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.
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
47 * You can also look at the papers on parser combinators in haskell
48 * (cf a pearl by meijer in ICFP) to understand our monadic
49 * approach to matching/unifying.
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 *)
55 type mode
= PatternMode
| TransformMode
57 (* used in both pattern and transform, in envf *)
58 val equal_metavarval
:
59 Ast_c.metavar_binding_kind
-> Ast_c.metavar_binding_kind
-> bool
61 (* for inherited metavariables. no declaration link on expressions *)
62 val equal_inh_metavarval
:
63 Ast_c.metavar_binding_kind
-> Ast_c.metavar_binding_kind
-> bool
65 (*****************************************************************************)
66 (* The parameter of the functor (the specific actions) *)
67 (*****************************************************************************)
75 (* a matcher between 'a' and 'b' take 'a' and 'b' in parameter,
76 * and "something" (tin; a state that is threaded across calls),
77 * and return a new 'a' and 'b' encapsulated in "something" (tout)
79 type ('a
, 'b
) matcher
= 'a
-> 'b
-> tin
-> ('a
* 'b
) tout
83 (* -------------------------------------------------------------------- *)
84 (* The monadic combinators *)
85 (* -------------------------------------------------------------------- *)
87 (* it kinds of take a matcher in parameter, and another matcher,
88 * and returns a matcher, so =~ matcher -> matcher -> matcher
91 (tin
-> ('a
* 'b
) tout
) ->
92 ('a
-> 'b
-> tin
-> ('c
* 'd
) tout
) ->
95 val return
: 'a
* 'b
-> tin
-> ('a
* 'b
) tout
96 val fail
: tin
-> ('a
* 'b
) tout
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
102 (* -------------------------------------------------------------------- *)
104 (* -------------------------------------------------------------------- *)
105 val tokenf
: ('a
Ast_cocci.mcode
, Ast_c.info
) matcher
106 val tokenf_mck
: (Ast_cocci.mcodekind
, Ast_c.info
) matcher
108 (* -------------------------------------------------------------------- *)
109 (* Distr_f functions, to tag a range of tokens *)
110 (* -------------------------------------------------------------------- *)
113 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.expression
) matcher
116 (Ast_cocci.meta_name
Ast_cocci.mcode
,
117 (Ast_c.argument
, Ast_c.il
) Common.either list
)
121 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.fullType
) matcher
124 (Ast_cocci.meta_name
Ast_cocci.mcode
,
125 (Ast_c.parameterType
, Ast_c.il
) Common.either list
)
128 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.parameterType
) matcher
131 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.initialiser
) matcher
133 (Ast_cocci.meta_name
Ast_cocci.mcode
,
134 (Ast_c.initialiser
, Ast_c.il
) Common.either list
) matcher
136 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.declaration
) matcher
138 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.field
) matcher
141 (Ast_cocci.meta_name
Ast_cocci.mcode
, Control_flow_c.node
) matcher
143 val distrf_define_params
:
144 (Ast_cocci.meta_name
Ast_cocci.mcode
,
145 (string Ast_c.wrap
, Ast_c.il
) Common.either list
)
148 val distrf_enum_fields
:
149 (Ast_cocci.meta_name
Ast_cocci.mcode
,
150 (Ast_c.oneEnumType
, Ast_c.il
) Common.either list
) matcher
152 val distrf_struct_fields
:
153 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.field list
)
157 (Ast_cocci.meta_name
Ast_cocci.mcode
,
158 (Ast_c.constant
, string) Common.either
Ast_c.wrap
)
161 (* -------------------------------------------------------------------- *)
162 (* Modifying nested expression and nested types, with Exp and Ty *)
163 (* -------------------------------------------------------------------- *)
166 (Ast_cocci.expression
, Ast_c.expression
) matcher
->
167 (Ast_cocci.expression
, Control_flow_c.node
) matcher
170 (Ast_cocci.expression
, Ast_c.expression
) matcher
->
171 (Ast_cocci.expression
, Ast_c.expression
) matcher
174 (Ast_cocci.fullType
, Ast_c.fullType
) matcher
->
175 (Ast_cocci.fullType
, Control_flow_c.node
) matcher
178 (Ast_cocci.initialiser
, Ast_c.initialiser
) matcher
->
179 (Ast_cocci.initialiser
, Control_flow_c.node
) matcher
181 (* -------------------------------------------------------------------- *)
182 (* Environment manipulation. Extract info from tin, the "something" *)
183 (* -------------------------------------------------------------------- *)
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 *)
189 (unit -> Common.filename
* string * Ast_c.posl
* Ast_c.posl
) ->
190 (unit -> tin
-> 'x tout
) -> (tin
-> 'x tout
)
192 val check_idconstraint
:
193 ('a
-> 'b
-> bool) -> 'a
-> 'b
->
194 (unit -> tin
-> 'x tout
) -> (tin
-> 'x tout
)
196 val check_constraints_ne
:
197 ('a
, 'b
) matcher
-> 'a list
-> 'b
->
198 (unit -> tin
-> 'x tout
) -> (tin
-> 'x tout
)
200 val all_bound
: Ast_cocci.meta_name list
-> tin
-> bool
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
)
206 val optional_declarer_semicolon_flag
:
207 (bool -> tin
-> 'x tout
) -> (tin
-> 'x tout
)
212 (*****************************************************************************)
213 (* The functor itself *)
214 (*****************************************************************************)
217 functor (X
: PARAM
) ->
219 type ('a
, 'b
) matcher
= 'a
-> 'b
-> X.tin
-> ('a
* 'b
) X.tout
221 val rule_elem_node
: (Ast_cocci.rule_elem
, Control_flow_c.node
) matcher
223 val expression
: (Ast_cocci.expression
, Ast_c.expression
) matcher
225 (* there are far more functions in this functor but they do not have