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