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.
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.
23 (*****************************************************************************)
25 (*****************************************************************************)
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.
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.
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
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.
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 *)
51 type mode
= PatternMode
| TransformMode
53 (* used in both pattern and transform, in envf *)
54 val equal_metavarval
:
55 Ast_c.metavar_binding_kind
-> Ast_c.metavar_binding_kind
-> bool
57 (* for inherited metavariables. no declaration link on expressions *)
58 val equal_inh_metavarval
:
59 Ast_c.metavar_binding_kind
-> Ast_c.metavar_binding_kind
-> bool
61 (*****************************************************************************)
62 (* The parameter of the functor (the specific actions) *)
63 (*****************************************************************************)
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)
75 type ('a
, 'b
) matcher
= 'a
-> 'b
-> tin
-> ('a
* 'b
) tout
79 (* -------------------------------------------------------------------- *)
80 (* The monadic combinators *)
81 (* -------------------------------------------------------------------- *)
83 (* it kinds of take a matcher in parameter, and another matcher,
84 * and returns a matcher, so =~ matcher -> matcher -> matcher
87 (tin
-> ('a
* 'b
) tout
) ->
88 ('a
-> 'b
-> tin
-> ('c
* 'd
) tout
) ->
91 val return
: 'a
* 'b
-> tin
-> ('a
* 'b
) tout
92 val fail
: tin
-> ('a
* 'b
) tout
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
98 (* -------------------------------------------------------------------- *)
100 (* -------------------------------------------------------------------- *)
101 val tokenf
: ('a
Ast_cocci.mcode
, Ast_c.info
) matcher
102 val tokenf_mck
: (Ast_cocci.mcodekind
, Ast_c.info
) matcher
104 (* -------------------------------------------------------------------- *)
105 (* Distr_f functions, to tag a range of tokens *)
106 (* -------------------------------------------------------------------- *)
109 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.expression
) matcher
112 (Ast_cocci.meta_name
Ast_cocci.mcode
,
113 (Ast_c.argument
, Ast_c.il
) Common.either list
)
117 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.fullType
) matcher
120 (Ast_cocci.meta_name
Ast_cocci.mcode
,
121 (Ast_c.parameterType
, Ast_c.il
) Common.either list
)
124 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.parameterType
) matcher
127 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.initialiser
) matcher
130 (Ast_cocci.meta_name
Ast_cocci.mcode
, Control_flow_c.node
) matcher
132 val distrf_define_params
:
133 (Ast_cocci.meta_name
Ast_cocci.mcode
,
134 (string Ast_c.wrap
, Ast_c.il
) Common.either list
)
137 val distrf_struct_fields
:
138 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.field list
)
142 (Ast_cocci.meta_name
Ast_cocci.mcode
,
143 (Ast_c.constant
, string) Common.either
Ast_c.wrap
)
146 (* -------------------------------------------------------------------- *)
147 (* Modifying nested expression and nested types, with Exp and Ty *)
148 (* -------------------------------------------------------------------- *)
151 (Ast_cocci.expression
, Ast_c.expression
) matcher
->
152 (Ast_cocci.expression
, Control_flow_c.node
) matcher
155 (Ast_cocci.expression
, Ast_c.expression
) matcher
->
156 (Ast_cocci.expression
, Ast_c.expression
) matcher
159 (Ast_cocci.fullType
, Ast_c.fullType
) matcher
->
160 (Ast_cocci.fullType
, Control_flow_c.node
) matcher
163 (Ast_cocci.initialiser
, Ast_c.initialiser
) matcher
->
164 (Ast_cocci.initialiser
, Control_flow_c.node
) matcher
166 (* -------------------------------------------------------------------- *)
167 (* Environment manipulation. Extract info from tin, the "something" *)
168 (* -------------------------------------------------------------------- *)
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 *)
174 (unit -> Common.filename
* string * Ast_c.posl
* Ast_c.posl
) ->
175 (unit -> tin
-> 'x tout
) -> (tin
-> 'x tout
)
177 val check_idconstraint
:
178 ('a
-> 'b
-> bool) -> 'a
-> 'b
->
179 (unit -> tin
-> 'x tout
) -> (tin
-> 'x tout
)
181 val check_constraints_ne
:
182 ('a
, 'b
) matcher
-> 'a list
-> 'b
->
183 (unit -> tin
-> 'x tout
) -> (tin
-> 'x tout
)
185 val all_bound
: Ast_cocci.meta_name list
-> tin
-> bool
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
)
196 (*****************************************************************************)
197 (* The functor itself *)
198 (*****************************************************************************)
201 functor (X
: PARAM
) ->
203 type ('a
, 'b
) matcher
= 'a
-> 'b
-> X.tin
-> ('a
* 'b
) X.tout
205 val rule_elem_node
: (Ast_cocci.rule_elem
, Control_flow_c.node
) matcher
207 val expression
: (Ast_cocci.expression
, Ast_c.expression
) matcher
209 (* there are far more functions in this functor but they do not have