Commit | Line | Data |
---|---|---|
5636bb2c C |
1 | (* |
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. | |
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 | ||
9f8e26f4 | 23 | (* |
ae4735db | 24 | * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen |
9f8e26f4 C |
25 | * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix |
26 | * This file is part of Coccinelle. | |
27 | * | |
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. | |
31 | * | |
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. | |
36 | * | |
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/>. | |
39 | * | |
40 | * The authors reserve the right to distribute this or future versions of | |
41 | * Coccinelle under other licenses. | |
42 | *) | |
43 | ||
44 | ||
34e49164 C |
45 | (*****************************************************************************) |
46 | (* Cocci vs C *) | |
47 | (*****************************************************************************) | |
48 | ||
ae4735db | 49 | (* This module was introduced to factorize code between |
34e49164 C |
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. | |
ae4735db | 55 | * |
34e49164 C |
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. | |
ae4735db | 59 | * |
34e49164 C |
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 | |
63 | * it is done now. | |
ae4735db C |
64 | * |
65 | * You can also look at the papers on parser combinators in haskell | |
34e49164 C |
66 | * (cf a pearl by meijer in ICFP) to understand our monadic |
67 | * approach to matching/unifying. | |
68 | *) | |
69 | ||
70 | ||
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 | |
74 | ||
75 | (* used in both pattern and transform, in envf *) | |
ae4735db | 76 | val equal_metavarval : |
34e49164 C |
77 | Ast_c.metavar_binding_kind -> Ast_c.metavar_binding_kind -> bool |
78 | ||
978fd7e5 | 79 | (* for inherited metavariables. no declaration link on expressions *) |
ae4735db | 80 | val equal_inh_metavarval : |
978fd7e5 C |
81 | Ast_c.metavar_binding_kind -> Ast_c.metavar_binding_kind -> bool |
82 | ||
34e49164 C |
83 | (*****************************************************************************) |
84 | (* The parameter of the functor (the specific actions) *) | |
85 | (*****************************************************************************) | |
86 | ||
87 | ||
88 | module type PARAM = | |
89 | sig | |
90 | type tin | |
91 | type 'a tout | |
92 | ||
ae4735db | 93 | (* a matcher between 'a' and 'b' take 'a' and 'b' in parameter, |
34e49164 C |
94 | * and "something" (tin; a state that is threaded across calls), |
95 | * and return a new 'a' and 'b' encapsulated in "something" (tout) | |
96 | *) | |
97 | type ('a, 'b) matcher = 'a -> 'b -> tin -> ('a * 'b) tout | |
98 | ||
99 | val mode : mode | |
100 | ||
101 | (* -------------------------------------------------------------------- *) | |
102 | (* The monadic combinators *) | |
103 | (* -------------------------------------------------------------------- *) | |
104 | ||
ae4735db | 105 | (* it kinds of take a matcher in parameter, and another matcher, |
34e49164 C |
106 | * and returns a matcher, so =~ matcher -> matcher -> matcher |
107 | *) | |
108 | val ( >>= ) : | |
109 | (tin -> ('a * 'b) tout) -> | |
ae4735db | 110 | ('a -> 'b -> tin -> ('c * 'd) tout) -> |
34e49164 C |
111 | tin -> ('c * 'd) tout |
112 | ||
113 | val return : 'a * 'b -> tin -> ('a * 'b) tout | |
114 | val fail : tin -> ('a * 'b) tout | |
115 | ||
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 | |
119 | ||
120 | (* -------------------------------------------------------------------- *) | |
121 | (* Tokens tagging *) | |
122 | (* -------------------------------------------------------------------- *) | |
123 | val tokenf : ('a Ast_cocci.mcode, Ast_c.info) matcher | |
124 | val tokenf_mck : (Ast_cocci.mcodekind, Ast_c.info) matcher | |
125 | ||
126 | (* -------------------------------------------------------------------- *) | |
127 | (* Distr_f functions, to tag a range of tokens *) | |
128 | (* -------------------------------------------------------------------- *) | |
129 | ||
ae4735db | 130 | val distrf_e : |
34e49164 C |
131 | (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.expression) matcher |
132 | ||
133 | val distrf_args : | |
ae4735db | 134 | (Ast_cocci.meta_name Ast_cocci.mcode, |
34e49164 C |
135 | (Ast_c.argument, Ast_c.il) Common.either list) |
136 | matcher | |
137 | ||
ae4735db | 138 | val distrf_type : |
34e49164 C |
139 | (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.fullType) matcher |
140 | ||
141 | val distrf_params : | |
142 | (Ast_cocci.meta_name Ast_cocci.mcode, | |
143 | (Ast_c.parameterType, Ast_c.il) Common.either list) | |
144 | matcher | |
ae4735db | 145 | val distrf_param : |
34e49164 C |
146 | (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.parameterType) matcher |
147 | ||
ae4735db | 148 | val distrf_ini : |
34e49164 C |
149 | (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.initialiser) matcher |
150 | ||
ae4735db | 151 | val distrf_node : |
34e49164 C |
152 | (Ast_cocci.meta_name Ast_cocci.mcode, Control_flow_c.node) matcher |
153 | ||
ae4735db C |
154 | val distrf_define_params : |
155 | (Ast_cocci.meta_name Ast_cocci.mcode, | |
34e49164 C |
156 | (string Ast_c.wrap, Ast_c.il) Common.either list) |
157 | matcher | |
158 | ||
159 | val distrf_struct_fields : | |
ae4735db | 160 | (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.field list) |
34e49164 C |
161 | matcher |
162 | ||
163 | val distrf_cst : | |
ae4735db | 164 | (Ast_cocci.meta_name Ast_cocci.mcode, |
34e49164 C |
165 | (Ast_c.constant, string) Common.either Ast_c.wrap) |
166 | matcher | |
167 | ||
168 | (* -------------------------------------------------------------------- *) | |
169 | (* Modifying nested expression and nested types, with Exp and Ty *) | |
170 | (* -------------------------------------------------------------------- *) | |
171 | ||
172 | val cocciExp : | |
ae4735db | 173 | (Ast_cocci.expression, Ast_c.expression) matcher -> |
34e49164 C |
174 | (Ast_cocci.expression, Control_flow_c.node) matcher |
175 | ||
176 | val cocciExpExp : | |
ae4735db | 177 | (Ast_cocci.expression, Ast_c.expression) matcher -> |
34e49164 C |
178 | (Ast_cocci.expression, Ast_c.expression) matcher |
179 | ||
180 | val cocciTy : | |
ae4735db | 181 | (Ast_cocci.fullType, Ast_c.fullType) matcher -> |
34e49164 C |
182 | (Ast_cocci.fullType, Control_flow_c.node) matcher |
183 | ||
1be43e12 | 184 | val cocciInit : |
ae4735db | 185 | (Ast_cocci.initialiser, Ast_c.initialiser) matcher -> |
1be43e12 C |
186 | (Ast_cocci.initialiser, Control_flow_c.node) matcher |
187 | ||
34e49164 C |
188 | (* -------------------------------------------------------------------- *) |
189 | (* Environment manipulation. Extract info from tin, the "something" *) | |
190 | (* -------------------------------------------------------------------- *) | |
191 | val envf : | |
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 *) | |
485bce71 | 196 | (unit -> Common.filename * string * Ast_c.posl * Ast_c.posl) -> |
34e49164 C |
197 | (unit -> tin -> 'x tout) -> (tin -> 'x tout) |
198 | ||
951c7801 C |
199 | val check_idconstraint : |
200 | ('a -> 'b -> bool) -> 'a -> 'b -> | |
201 | (unit -> tin -> 'x tout) -> (tin -> 'x tout) | |
202 | ||
203 | val check_constraints_ne : | |
34e49164 C |
204 | ('a, 'b) matcher -> 'a list -> 'b -> |
205 | (unit -> tin -> 'x tout) -> (tin -> 'x tout) | |
206 | ||
207 | val all_bound : Ast_cocci.meta_name list -> tin -> bool | |
208 | ||
209 | ||
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) | |
213 | ||
214 | ||
215 | end | |
216 | ||
217 | ||
218 | (*****************************************************************************) | |
219 | (* The functor itself *) | |
220 | (*****************************************************************************) | |
221 | ||
222 | module COCCI_VS_C : | |
223 | functor (X : PARAM) -> | |
224 | sig | |
225 | type ('a, 'b) matcher = 'a -> 'b -> X.tin -> ('a * 'b) X.tout | |
226 | ||
227 | val rule_elem_node : (Ast_cocci.rule_elem, Control_flow_c.node) matcher | |
228 | ||
229 | val expression : (Ast_cocci.expression, Ast_c.expression) matcher | |
230 | ||
91eba41f | 231 | (* there are far more functions in this functor but they do not have |
ae4735db | 232 | * to be exported |
34e49164 C |
233 | *) |
234 | ||
235 | end | |
236 | ||
237 |