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
5 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
6 * This file is part of Coccinelle.
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.
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.
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/>.
20 * The authors reserve the right to distribute this or future versions of
21 * Coccinelle under other licenses.
25 (*****************************************************************************)
27 (*****************************************************************************)
29 (* This module was introduced to factorize code between
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.
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.
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
45 * You can also look at the papers on parser combinators in haskell
46 * (cf a pearl by meijer in ICFP) to understand our monadic
47 * approach to matching/unifying.
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 *)
53 type mode
= PatternMode
| TransformMode
55 (* used in both pattern and transform, in envf *)
56 val equal_metavarval
:
57 Ast_c.metavar_binding_kind
-> Ast_c.metavar_binding_kind
-> bool
59 (* for inherited metavariables. no declaration link on expressions *)
60 val equal_inh_metavarval
:
61 Ast_c.metavar_binding_kind
-> Ast_c.metavar_binding_kind
-> bool
63 (*****************************************************************************)
64 (* The parameter of the functor (the specific actions) *)
65 (*****************************************************************************)
73 (* a matcher between 'a' and 'b' take 'a' and 'b' in parameter,
74 * and "something" (tin; a state that is threaded across calls),
75 * and return a new 'a' and 'b' encapsulated in "something" (tout)
77 type ('a
, 'b
) matcher
= 'a
-> 'b
-> tin
-> ('a
* 'b
) tout
81 (* -------------------------------------------------------------------- *)
82 (* The monadic combinators *)
83 (* -------------------------------------------------------------------- *)
85 (* it kinds of take a matcher in parameter, and another matcher,
86 * and returns a matcher, so =~ matcher -> matcher -> matcher
89 (tin
-> ('a
* 'b
) tout
) ->
90 ('a
-> 'b
-> tin
-> ('c
* 'd
) tout
) ->
93 val return
: 'a
* 'b
-> tin
-> ('a
* 'b
) tout
94 val fail
: tin
-> ('a
* 'b
) tout
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
100 (* -------------------------------------------------------------------- *)
102 (* -------------------------------------------------------------------- *)
103 val tokenf
: ('a
Ast_cocci.mcode
, Ast_c.info
) matcher
104 val tokenf_mck
: (Ast_cocci.mcodekind
, Ast_c.info
) matcher
106 (* -------------------------------------------------------------------- *)
107 (* Distr_f functions, to tag a range of tokens *)
108 (* -------------------------------------------------------------------- *)
111 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.expression
) matcher
114 (Ast_cocci.meta_name
Ast_cocci.mcode
,
115 (Ast_c.argument
, Ast_c.il
) Common.either list
)
119 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.fullType
) matcher
122 (Ast_cocci.meta_name
Ast_cocci.mcode
,
123 (Ast_c.parameterType
, Ast_c.il
) Common.either list
)
126 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.parameterType
) matcher
129 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.initialiser
) matcher
131 (Ast_cocci.meta_name
Ast_cocci.mcode
,
132 (Ast_c.initialiser
, Ast_c.il
) Common.either list
) matcher
134 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.declaration
) matcher
136 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.field
) matcher
139 (Ast_cocci.meta_name
Ast_cocci.mcode
, Control_flow_c.node
) matcher
141 val distrf_define_params
:
142 (Ast_cocci.meta_name
Ast_cocci.mcode
,
143 (string Ast_c.wrap
, Ast_c.il
) Common.either list
)
146 val distrf_enum_fields
:
147 (Ast_cocci.meta_name
Ast_cocci.mcode
,
148 (Ast_c.oneEnumType
, Ast_c.il
) Common.either list
) matcher
150 val distrf_struct_fields
:
151 (Ast_cocci.meta_name
Ast_cocci.mcode
, Ast_c.field list
)
155 (Ast_cocci.meta_name
Ast_cocci.mcode
,
156 (Ast_c.constant
, string) Common.either
Ast_c.wrap
)
159 (* -------------------------------------------------------------------- *)
160 (* Modifying nested expression and nested types, with Exp and Ty *)
161 (* -------------------------------------------------------------------- *)
164 (Ast_cocci.expression
, Ast_c.expression
) matcher
->
165 (Ast_cocci.expression
, Control_flow_c.node
) matcher
168 (Ast_cocci.expression
, Ast_c.expression
) matcher
->
169 (Ast_cocci.expression
, Ast_c.expression
) matcher
172 (Ast_cocci.fullType
, Ast_c.fullType
) matcher
->
173 (Ast_cocci.fullType
, Control_flow_c.node
) matcher
176 (Ast_cocci.initialiser
, Ast_c.initialiser
) matcher
->
177 (Ast_cocci.initialiser
, Control_flow_c.node
) matcher
179 (* -------------------------------------------------------------------- *)
180 (* Environment manipulation. Extract info from tin, the "something" *)
181 (* -------------------------------------------------------------------- *)
183 Ast_cocci.keep_binding
->
184 Ast_cocci.inherited
->
185 Ast_cocci.meta_name
Ast_cocci.mcode
* Ast_c.metavar_binding_kind
*
186 (* pos info, if needed *)
187 (unit -> Common.filename
* string * Ast_c.posl
* Ast_c.posl
) ->
188 (unit -> tin
-> 'x tout
) -> (tin
-> 'x tout
)
190 val check_idconstraint
:
191 ('a
-> 'b
-> bool) -> 'a
-> 'b
->
192 (unit -> tin
-> 'x tout
) -> (tin
-> 'x tout
)
194 val check_constraints_ne
:
195 ('a
, 'b
) matcher
-> 'a list
-> 'b
->
196 (unit -> tin
-> 'x tout
) -> (tin
-> 'x tout
)
198 val all_bound
: Ast_cocci.meta_name list
-> tin
-> bool
201 val optional_storage_flag
: (bool -> tin
-> 'x tout
) -> (tin
-> 'x tout
)
202 val optional_qualifier_flag
: (bool -> tin
-> 'x tout
) -> (tin
-> 'x tout
)
203 val value_format_flag
: (bool -> tin
-> 'x tout
) -> (tin
-> 'x tout
)
209 (*****************************************************************************)
210 (* The functor itself *)
211 (*****************************************************************************)
214 functor (X
: PARAM
) ->
216 type ('a
, 'b
) matcher
= 'a
-> 'b
-> X.tin
-> ('a
* 'b
) X.tout
218 val rule_elem_node
: (Ast_cocci.rule_elem
, Control_flow_c.node
) matcher
220 val expression
: (Ast_cocci.expression
, Ast_c.expression
) matcher
222 (* there are far more functions in this functor but they do not have