Release coccinelle-0.1.2
[bpt/coccinelle.git] / engine / cocci_vs_c.mli
1 (*****************************************************************************)
2 (* Cocci vs C *)
3 (*****************************************************************************)
4
5 (* This module was introduced to factorize code between
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.
11 *
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.
15 *
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.
20 *
21 * You can also look at the papers on parser combinators in haskell
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 *)
32 val equal_metavarval :
33 Ast_c.metavar_binding_kind -> Ast_c.metavar_binding_kind -> bool
34
35 (*****************************************************************************)
36 (* The parameter of the functor (the specific actions) *)
37 (*****************************************************************************)
38
39
40 module type PARAM =
41 sig
42 type tin
43 type 'a tout
44
45 (* a matcher between 'a' and 'b' take 'a' and 'b' in parameter,
46 * and "something" (tin; a state that is threaded across calls),
47 * and return a new 'a' and 'b' encapsulated in "something" (tout)
48 *)
49 type ('a, 'b) matcher = 'a -> 'b -> tin -> ('a * 'b) tout
50
51 val mode : mode
52
53 (* -------------------------------------------------------------------- *)
54 (* The monadic combinators *)
55 (* -------------------------------------------------------------------- *)
56
57 (* it kinds of take a matcher in parameter, and another matcher,
58 * and returns a matcher, so =~ matcher -> matcher -> matcher
59 *)
60 val ( >>= ) :
61 (tin -> ('a * 'b) tout) ->
62 ('a -> 'b -> tin -> ('c * 'd) tout) ->
63 tin -> ('c * 'd) tout
64
65 val return : 'a * 'b -> tin -> ('a * 'b) tout
66 val fail : tin -> ('a * 'b) tout
67
68 val ( >||> ) : (tin -> 'a tout) -> (tin -> 'a tout) -> tin -> 'a tout
69 val ( >|+|> ) : (tin -> 'a tout) -> (tin -> 'a tout) -> tin -> 'a tout
70 val ( >&&> ) : (tin -> bool) -> (tin -> 'a tout) -> tin -> 'a tout
71
72 (* -------------------------------------------------------------------- *)
73 (* Tokens tagging *)
74 (* -------------------------------------------------------------------- *)
75 val tokenf : ('a Ast_cocci.mcode, Ast_c.info) matcher
76 val tokenf_mck : (Ast_cocci.mcodekind, Ast_c.info) matcher
77
78 (* -------------------------------------------------------------------- *)
79 (* Distr_f functions, to tag a range of tokens *)
80 (* -------------------------------------------------------------------- *)
81
82 val distrf_e :
83 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.expression) matcher
84
85 val distrf_args :
86 (Ast_cocci.meta_name Ast_cocci.mcode,
87 (Ast_c.argument, Ast_c.il) Common.either list)
88 matcher
89
90 val distrf_type :
91 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.fullType) matcher
92
93 val distrf_params :
94 (Ast_cocci.meta_name Ast_cocci.mcode,
95 (Ast_c.parameterType, Ast_c.il) Common.either list)
96 matcher
97 val distrf_param :
98 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.parameterType) matcher
99
100 val distrf_ini :
101 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.initialiser) matcher
102
103 val distrf_node :
104 (Ast_cocci.meta_name Ast_cocci.mcode, Control_flow_c.node) matcher
105
106 val distrf_define_params :
107 (Ast_cocci.meta_name Ast_cocci.mcode,
108 (string Ast_c.wrap, Ast_c.il) Common.either list)
109 matcher
110
111 val distrf_struct_fields :
112 (Ast_cocci.meta_name Ast_cocci.mcode, Ast_c.field list)
113 matcher
114
115 val distrf_cst :
116 (Ast_cocci.meta_name Ast_cocci.mcode,
117 (Ast_c.constant, string) Common.either Ast_c.wrap)
118 matcher
119
120 (* -------------------------------------------------------------------- *)
121 (* Modifying nested expression and nested types, with Exp and Ty *)
122 (* -------------------------------------------------------------------- *)
123
124 val cocciExp :
125 (Ast_cocci.expression, Ast_c.expression) matcher ->
126 (Ast_cocci.expression, Control_flow_c.node) matcher
127
128 val cocciExpExp :
129 (Ast_cocci.expression, Ast_c.expression) matcher ->
130 (Ast_cocci.expression, Ast_c.expression) matcher
131
132 val cocciTy :
133 (Ast_cocci.fullType, Ast_c.fullType) matcher ->
134 (Ast_cocci.fullType, Control_flow_c.node) matcher
135
136 val cocciInit :
137 (Ast_cocci.initialiser, Ast_c.initialiser) matcher ->
138 (Ast_cocci.initialiser, Control_flow_c.node) matcher
139
140 (* -------------------------------------------------------------------- *)
141 (* Environment manipulation. Extract info from tin, the "something" *)
142 (* -------------------------------------------------------------------- *)
143 val envf :
144 Ast_cocci.keep_binding ->
145 Ast_cocci.inherited ->
146 Ast_cocci.meta_name Ast_cocci.mcode * Ast_c.metavar_binding_kind *
147 (* pos info, if needed *)
148 (unit -> Common.filename * string * Ast_c.posl * Ast_c.posl) ->
149 (unit -> tin -> 'x tout) -> (tin -> 'x tout)
150
151 val check_constraints :
152 ('a, 'b) matcher -> 'a list -> 'b ->
153 (unit -> tin -> 'x tout) -> (tin -> 'x tout)
154
155 val all_bound : Ast_cocci.meta_name list -> tin -> bool
156
157
158 val optional_storage_flag : (bool -> tin -> 'x tout) -> (tin -> 'x tout)
159 val optional_qualifier_flag : (bool -> tin -> 'x tout) -> (tin -> 'x tout)
160 val value_format_flag: (bool -> tin -> 'x tout) -> (tin -> 'x tout)
161
162
163 end
164
165
166 (*****************************************************************************)
167 (* The functor itself *)
168 (*****************************************************************************)
169
170 module COCCI_VS_C :
171 functor (X : PARAM) ->
172 sig
173 type ('a, 'b) matcher = 'a -> 'b -> X.tin -> ('a * 'b) X.tout
174
175 val rule_elem_node : (Ast_cocci.rule_elem, Control_flow_c.node) matcher
176
177 val expression : (Ast_cocci.expression, Ast_c.expression) matcher
178
179 (* there is far more functions in this functor but they do not have
180 * to be exported
181 *)
182
183 end
184
185