Commit | Line | Data |
---|---|---|
34e49164 C |
1 | (*****************************************************************************) |
2 | (* Cocci vs C *) | |
3 | (*****************************************************************************) | |
4 | ||
ae4735db | 5 | (* This module was introduced to factorize code between |
34e49164 C |
6 | * pattern.ml and transformation.ml. In both cases we need |
7 | * to "compare" a piece of C with a piece of Cocci, and depending | |
8 | * if we want just to pattern or transform, we perform different | |
9 | * actions on the tokens. So, the common code is in this module | |
10 | * and the module specific actions are in pattern.ml and transformation.ml. | |
ae4735db | 11 | * |
34e49164 C |
12 | * We could have used a visitor approach as in visitor_c but I prefer |
13 | * this time to use a functor. The specific actions are passed | |
14 | * via a module to the functor. | |
ae4735db | 15 | * |
34e49164 C |
16 | * If the functor is too complex too understand, you can look at |
17 | * the comments in pattern.ml and transformation.ml to look at | |
18 | * how it was done before, which may help to understand how | |
19 | * it is done now. | |
ae4735db C |
20 | * |
21 | * You can also look at the papers on parser combinators in haskell | |
34e49164 C |
22 | * (cf a pearl by meijer in ICFP) to understand our monadic |
23 | * approach to matching/unifying. | |
24 | *) | |
25 | ||
26 | ||
27 | (* should be used as less as possible. Most of the time the code in | |
28 | * cocci_vs_c should be the same if we pattern or transform *) | |
29 | type mode = PatternMode | TransformMode | |
30 | ||
31 | (* used in both pattern and transform, in envf *) | |
ae4735db | 32 | val equal_metavarval : |
34e49164 C |
33 | Ast_c.metavar_binding_kind -> Ast_c.metavar_binding_kind -> bool |
34 | ||
978fd7e5 | 35 | (* for inherited metavariables. no declaration link on expressions *) |
ae4735db | 36 | val equal_inh_metavarval : |
978fd7e5 C |
37 | Ast_c.metavar_binding_kind -> Ast_c.metavar_binding_kind -> bool |
38 | ||
34e49164 C |
39 | (*****************************************************************************) |
40 | (* The parameter of the functor (the specific actions) *) | |
41 | (*****************************************************************************) | |
42 | ||
43 | ||
44 | module type PARAM = | |
45 | sig | |
46 | type tin | |
47 | type 'a tout | |
48 | ||
ae4735db | 49 | (* a matcher between 'a' and 'b' take 'a' and 'b' in parameter, |
34e49164 C |
50 | * and "something" (tin; a state that is threaded across calls), |
51 | * and return a new 'a' and 'b' encapsulated in "something" (tout) | |
52 | *) | |
53 | type ('a, 'b) matcher = 'a -> 'b -> tin -> ('a * 'b) tout | |
54 | ||
55 | val mode : mode | |
56 | ||
57 | (* -------------------------------------------------------------------- *) | |
58 | (* The monadic combinators *) | |
59 | (* -------------------------------------------------------------------- *) | |
60 | ||
ae4735db | 61 | (* it kinds of take a matcher in parameter, and another matcher, |
34e49164 C |
62 | * and returns a matcher, so =~ matcher -> matcher -> matcher |
63 | *) | |
64 | val ( >>= ) : | |
65 | (tin -> ('a * 'b) tout) -> | |
ae4735db | 66 | ('a -> 'b -> tin -> ('c * 'd) tout) -> |
34e49164 C |
67 | tin -> ('c * 'd) tout |
68 | ||
69 | val return : 'a * 'b -> tin -> ('a * 'b) tout | |
70 | val fail : tin -> ('a * 'b) tout | |
71 | ||
72 | val ( >||> ) : (tin -> 'a tout) -> (tin -> 'a tout) -> tin -> 'a tout | |
73 | val ( >|+|> ) : (tin -> 'a tout) -> (tin -> 'a tout) -> tin -> 'a tout | |
74 | val ( >&&> ) : (tin -> bool) -> (tin -> 'a tout) -> tin -> 'a tout | |
75 | ||
76 | (* -------------------------------------------------------------------- *) | |
77 | (* Tokens tagging *) | |
78 | (* -------------------------------------------------------------------- *) | |
79 | val tokenf : ('a Ast_cocci.mcode, Ast_c.info) matcher | |
80 | val tokenf_mck : (Ast_cocci.mcodekind, Ast_c.info) matcher | |
81 | ||
82 | (* -------------------------------------------------------------------- *) | |
83 | (* Distr_f functions, to tag a range of tokens *) | |
84 | (* -------------------------------------------------------------------- *) | |
85 | ||
ae4735db | 86 | val distrf_e : |
34e49164 C |
87 | (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.expression) matcher |
88 | ||
89 | val distrf_args : | |
ae4735db | 90 | (Ast_cocci.meta_name Ast_cocci.mcode, |
34e49164 C |
91 | (Ast_c.argument, Ast_c.il) Common.either list) |
92 | matcher | |
93 | ||
ae4735db | 94 | val distrf_type : |
34e49164 C |
95 | (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.fullType) matcher |
96 | ||
97 | val distrf_params : | |
98 | (Ast_cocci.meta_name Ast_cocci.mcode, | |
99 | (Ast_c.parameterType, Ast_c.il) Common.either list) | |
100 | matcher | |
ae4735db | 101 | val distrf_param : |
34e49164 C |
102 | (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.parameterType) matcher |
103 | ||
ae4735db | 104 | val distrf_ini : |
34e49164 | 105 | (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.initialiser) matcher |
c491d8ee C |
106 | val distrf_inis : |
107 | (Ast_cocci.meta_name Ast_cocci.mcode, | |
108 | (Ast_c.initialiser, Ast_c.il) Common.either list) matcher | |
413ffc02 C |
109 | val distrf_decl : |
110 | (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.declaration) matcher | |
111 | val distrf_field : | |
112 | (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.field) matcher | |
34e49164 | 113 | |
ae4735db | 114 | val distrf_node : |
34e49164 C |
115 | (Ast_cocci.meta_name Ast_cocci.mcode, Control_flow_c.node) matcher |
116 | ||
ae4735db C |
117 | val distrf_define_params : |
118 | (Ast_cocci.meta_name Ast_cocci.mcode, | |
34e49164 C |
119 | (string Ast_c.wrap, Ast_c.il) Common.either list) |
120 | matcher | |
121 | ||
c491d8ee C |
122 | val distrf_enum_fields : |
123 | (Ast_cocci.meta_name Ast_cocci.mcode, | |
124 | (Ast_c.oneEnumType, Ast_c.il) Common.either list) matcher | |
125 | ||
34e49164 | 126 | val distrf_struct_fields : |
ae4735db | 127 | (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.field list) |
34e49164 C |
128 | matcher |
129 | ||
130 | val distrf_cst : | |
ae4735db | 131 | (Ast_cocci.meta_name Ast_cocci.mcode, |
34e49164 C |
132 | (Ast_c.constant, string) Common.either Ast_c.wrap) |
133 | matcher | |
134 | ||
135 | (* -------------------------------------------------------------------- *) | |
136 | (* Modifying nested expression and nested types, with Exp and Ty *) | |
137 | (* -------------------------------------------------------------------- *) | |
138 | ||
139 | val cocciExp : | |
ae4735db | 140 | (Ast_cocci.expression, Ast_c.expression) matcher -> |
34e49164 C |
141 | (Ast_cocci.expression, Control_flow_c.node) matcher |
142 | ||
143 | val cocciExpExp : | |
ae4735db | 144 | (Ast_cocci.expression, Ast_c.expression) matcher -> |
34e49164 C |
145 | (Ast_cocci.expression, Ast_c.expression) matcher |
146 | ||
147 | val cocciTy : | |
ae4735db | 148 | (Ast_cocci.fullType, Ast_c.fullType) matcher -> |
34e49164 C |
149 | (Ast_cocci.fullType, Control_flow_c.node) matcher |
150 | ||
1be43e12 | 151 | val cocciInit : |
ae4735db | 152 | (Ast_cocci.initialiser, Ast_c.initialiser) matcher -> |
1be43e12 C |
153 | (Ast_cocci.initialiser, Control_flow_c.node) matcher |
154 | ||
34e49164 C |
155 | (* -------------------------------------------------------------------- *) |
156 | (* Environment manipulation. Extract info from tin, the "something" *) | |
157 | (* -------------------------------------------------------------------- *) | |
158 | val envf : | |
159 | Ast_cocci.keep_binding -> | |
160 | Ast_cocci.inherited -> | |
161 | Ast_cocci.meta_name Ast_cocci.mcode * Ast_c.metavar_binding_kind * | |
162 | (* pos info, if needed *) | |
485bce71 | 163 | (unit -> Common.filename * string * Ast_c.posl * Ast_c.posl) -> |
34e49164 C |
164 | (unit -> tin -> 'x tout) -> (tin -> 'x tout) |
165 | ||
951c7801 C |
166 | val check_idconstraint : |
167 | ('a -> 'b -> bool) -> 'a -> 'b -> | |
168 | (unit -> tin -> 'x tout) -> (tin -> 'x tout) | |
169 | ||
170 | val check_constraints_ne : | |
34e49164 C |
171 | ('a, 'b) matcher -> 'a list -> 'b -> |
172 | (unit -> tin -> 'x tout) -> (tin -> 'x tout) | |
173 | ||
174 | val all_bound : Ast_cocci.meta_name list -> tin -> bool | |
175 | ||
176 | ||
177 | val optional_storage_flag : (bool -> tin -> 'x tout) -> (tin -> 'x tout) | |
178 | val optional_qualifier_flag : (bool -> tin -> 'x tout) -> (tin -> 'x tout) | |
179 | val value_format_flag: (bool -> tin -> 'x tout) -> (tin -> 'x tout) | |
180 | ||
181 | ||
182 | end | |
183 | ||
184 | ||
185 | (*****************************************************************************) | |
186 | (* The functor itself *) | |
187 | (*****************************************************************************) | |
188 | ||
189 | module COCCI_VS_C : | |
190 | functor (X : PARAM) -> | |
191 | sig | |
192 | type ('a, 'b) matcher = 'a -> 'b -> X.tin -> ('a * 'b) X.tout | |
193 | ||
194 | val rule_elem_node : (Ast_cocci.rule_elem, Control_flow_c.node) matcher | |
195 | ||
196 | val expression : (Ast_cocci.expression, Ast_c.expression) matcher | |
197 | ||
91eba41f | 198 | (* there are far more functions in this functor but they do not have |
ae4735db | 199 | * to be exported |
34e49164 C |
200 | *) |
201 | ||
202 | end | |
203 | ||
204 |