Coccinelle release-1.0.0-rc11
[bpt/coccinelle.git] / parsing_c / visitor_c.ml
CommitLineData
0708f913 1(* Yoann Padioleau
ae4735db
C
2 *
3 * Copyright (C) 2010, University of Copenhagen DIKU and INRIA.
0708f913 4 * Copyright (C) 2006, 2007, 2008, 2009 Ecole des Mines de Nantes
34e49164
C
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License (GPL)
8 * version 2 as published by the Free Software Foundation.
ae4735db 9 *
34e49164
C
10 * This program 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 * file license.txt for more details.
14 *)
15open Common
16
17
18open Ast_c
19module F = Control_flow_c
20
91eba41f
C
21(*****************************************************************************)
22(* Prelude *)
23(*****************************************************************************)
24
25(* todo? dont go in Include. Have a visitor flag ? disable_go_include ?
26 * disable_go_type_annotation ?
27 *)
28
708f4980
C
29(*****************************************************************************)
30(* Wrappers *)
31(*****************************************************************************)
32let pr2, pr2_once = Common.mk_pr2_wrappers Flag_parsing_c.verbose_visit
33
34e49164
C
34(*****************************************************************************)
35(* Functions to visit the Ast, and now also the CFG nodes *)
36(*****************************************************************************)
37
ae4735db
C
38(* Why this module ?
39 *
40 * The problem is that we manipulate the AST of C programs
41 * and some of our analysis need only to specify an action for
113803cf 42 * specific cases, such as the function call case, and recurse
ae4735db
C
43 * for the other cases.
44 * Here is a simplification of our AST:
45 *
46 * type ctype =
113803cf
C
47 * | Basetype of ...
48 * | Pointer of ctype
49 * | Array of expression option * ctype
50 * | ...
ae4735db 51 * and expression =
113803cf
C
52 * | Ident of string
53 * | FunCall of expression * expression list
54 * | Postfix of ...
55 * | RecordAccess of ..
56 * | ...
ae4735db 57 * and statement =
113803cf
C
58 * ...
59 * and declaration =
60 * ...
ae4735db 61 * and program =
113803cf
C
62 * ...
63 *
ae4735db
C
64 * What we want is really write code like
65 *
66 * let my_analysis program =
113803cf
C
67 * analyze_all_expressions program (fun expr ->
68 * match expr with
69 * | FunCall (e, es) -> do_something()
ae4735db 70 * | _ -> <find_a_way_to_recurse_for_all_the_other_cases>
113803cf 71 * )
ae4735db 72 *
113803cf
C
73 * The problem is how to write analyze_all_expressions
74 * and find_a_way_to_recurse_for_all_the_other_cases.
ae4735db
C
75 *
76 * Our solution is to mix the ideas of visitor, pattern matching,
113803cf 77 * and continuation. Here is how it looks like
ae4735db
C
78 * using our hybrid-visitor API:
79 *
80 * let my_analysis program =
113803cf 81 * Visitor.visit_iter program {
ae4735db 82 * Visitor.kexpr = (fun k e ->
113803cf
C
83 * match e with
84 * | FunCall (e, es) -> do_something()
85 * | _ -> k e
86 * );
87 * }
ae4735db
C
88 *
89 * You can of course also give action "hooks" for
113803cf
C
90 * kstatement, ktype, or kdeclaration. But we don't overuse
91 * visitors and so it would be stupid to provide
92 * kfunction_call, kident, kpostfix hooks as one can just
93 * use pattern matching with kexpr to achieve the same effect.
ae4735db 94 *
0708f913
C
95 * Note: when want to apply recursively, always apply the continuator
96 * on the toplevel expression, otherwise may miss some intermediate steps.
97 * Do
98 * match expr with
99 * | FunCall (e, es) -> ...
100 * k expr
101 * Or
102 * match expr with
103 * | FunCall (e, es) -> ...
104 * Visitor_c.vk_expr bigf e
105 * Not
106 * match expr with
107 * | FunCall (e, es) -> ...
108 * k e
109 *
ae4735db
C
110 *
111 *
112 *
113 *
113803cf
C
114 * Alternatives: from the caml mailing list:
115 * "You should have a look at the Camlp4 metaprogramming facilities :
116 * http://brion.inria.fr/gallium/index.php/Camlp4MapGenerator
117 * You would write something like" :
118 * let my_analysis program =
119 * let analysis = object (self)
120 * inherit fold as super
121 * method expr = function
122 * | FunCall (e, es) -> do_something (); self
123 * | other -> super#expr other
124 * end in analysis#expr
ae4735db
C
125 *
126 * The problem is that you don't have control about what is generated
113803cf 127 * and in our case we sometimes dont want to visit too much. For instance
b1b2de81 128 * our visitor don't recurse on the type annotation of expressions
ae4735db 129 * Ok, this could be worked around, but the pb remains, you
113803cf
C
130 * don't have control and at some point you may want. In the same
131 * way we want to enforce a certain order in the visit (ok this is not good,
132 * but it's convenient) of ast elements. For instance first
133 * processing the left part 'e' of a Funcall(e,es), then the arguments 'es'.
ae4735db 134 *
113803cf 135 *)
34e49164 136
ae4735db
C
137(* Visitor based on continuation. Cleaner than the one based on mutable
138 * pointer functions that I had before.
485bce71 139 * src: based on a (vague) idea from Remy Douence.
ae4735db
C
140 *
141 *
142 *
34e49164 143 * Diff with Julia's visitor ? She does:
ae4735db 144 *
34e49164
C
145 * let ident r k i =
146 * ...
147 * let expression r k e =
ae4735db 148 * ...
34e49164
C
149 * ... (List.map r.V0.combiner_expression expr_list) ...
150 * ...
ae4735db 151 * let res = V0.combiner bind option_default
34e49164
C
152 * mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
153 * donothing donothing donothing donothing
154 * ident expression typeC donothing parameter declaration statement
155 * donothing in
156 * ...
157 * collect_unitary_nonunitary
158 * (List.concat (List.map res.V0.combiner_top_level t))
ae4735db
C
159 *
160 *
161 *
34e49164 162 * So she has to remember at which position you must put the 'expression'
ae4735db
C
163 * function. I use record which is easier.
164 *
34e49164 165 * When she calls recursively, her res.V0.combiner_xxx does not take bigf
ae4735db
C
166 * in param whereas I do
167 * | F.Decl decl -> Visitor_c.vk_decl bigf decl
34e49164
C
168 * And with the record she gets, she does not have to do my
169 * multiple defs of function such as 'let al_type = V0.vk_type_s bigf'
ae4735db 170 *
34e49164
C
171 * The code of visitor.ml is cleaner with julia because mutual recursive calls
172 * are clean such as ... 'expression e' ... and not 'f (k, bigf) e'
173 * or 'vk_expr bigf e'.
ae4735db 174 *
34e49164
C
175 * So it is very dual:
176 * - I give a record but then I must handle bigf.
177 * - She gets a record, and gives a list of function
ae4735db
C
178 *
179 *)
180
34e49164 181
ae4735db 182(* old: first version (only visiting expr)
34e49164
C
183
184let (iter_expr:((expression -> unit) -> expression -> unit) -> expression -> unit)
185 = fun f expr ->
ae4735db 186 let rec k e =
34e49164
C
187 match e with
188 | Constant c -> ()
189 | FunCall (e, es) -> f k e; List.iter (f k) es
190 | CondExpr (e1, e2, e3) -> f k e1; f k e2; f k e3
191 | Sequence (e1, e2) -> f k e1; f k e2;
192 | Assignment (e1, op, e2) -> f k e1; f k e2;
ae4735db 193
34e49164
C
194 | Postfix (e, op) -> f k e
195 | Infix (e, op) -> f k e
196 | Unary (e, op) -> f k e
197 | Binary (e1, op, e2) -> f k e1; f k e2;
ae4735db 198
34e49164
C
199 | ArrayAccess (e1, e2) -> f k e1; f k e2;
200 | RecordAccess (e, s) -> f k e
201 | RecordPtAccess (e, s) -> f k e
202
203 | SizeOfExpr e -> f k e
204 | SizeOfType t -> ()
205 | _ -> failwith "to complete"
206
207 in f k expr
208
ae4735db 209let ex1 = Sequence (Sequence (Constant (Ident "1"), Constant (Ident "2")),
34e49164 210 Constant (Ident "4"))
ae4735db 211let test =
34e49164
C
212 iter_expr (fun k e -> match e with
213 | Constant (Ident x) -> Common.pr2 x
214 | rest -> k rest
ae4735db
C
215 ) ex1
216==>
34e49164
C
2171
2182
2194
220
221*)
222
223(*****************************************************************************)
224(* Side effect style visitor *)
225(*****************************************************************************)
226
227(* Visitors for all langage concept, not just for expression.
ae4735db 228 *
34e49164
C
229 * Note that I don't visit necesserally in the order of the token
230 * found in the original file. So don't assume such hypothesis!
ae4735db 231 *
951c7801 232 * todo? parameter ?
34e49164 233 *)
ae4735db
C
234type visitor_c =
235 {
34e49164
C
236 kexpr: (expression -> unit) * visitor_c -> expression -> unit;
237 kstatement: (statement -> unit) * visitor_c -> statement -> unit;
238 ktype: (fullType -> unit) * visitor_c -> fullType -> unit;
239
240 kdecl: (declaration -> unit) * visitor_c -> declaration -> unit;
951c7801
C
241 konedecl: (onedecl -> unit) * visitor_c -> onedecl -> unit;
242 kparam: (parameterType -> unit) * visitor_c -> parameterType -> unit;
ae4735db 243 kdef: (definition -> unit) * visitor_c -> definition -> unit;
b1b2de81
C
244 kname : (name -> unit) * visitor_c -> name -> unit;
245
ae4735db 246 kini: (initialiser -> unit) * visitor_c -> initialiser -> unit;
b1b2de81 247 kfield: (field -> unit) * visitor_c -> field -> unit;
34e49164 248
485bce71
C
249 kcppdirective: (cpp_directive -> unit) * visitor_c -> cpp_directive -> unit;
250 kdefineval : (define_val -> unit) * visitor_c -> define_val -> unit;
251 kstatementseq: (statement_sequencable -> unit) * visitor_c -> statement_sequencable -> unit;
34e49164 252
0708f913 253
34e49164
C
254 (* CFG *)
255 knode: (F.node -> unit) * visitor_c -> F.node -> unit;
256 (* AST *)
257 ktoplevel: (toplevel -> unit) * visitor_c -> toplevel -> unit;
485bce71
C
258
259 kinfo: (info -> unit) * visitor_c -> info -> unit;
ae4735db 260 }
34e49164 261
ae4735db 262let default_visitor_c =
b1b2de81
C
263 { kexpr = (fun (k,_) e -> k e);
264 kstatement = (fun (k,_) st -> k st);
265 ktype = (fun (k,_) t -> k t);
266 kdecl = (fun (k,_) d -> k d);
951c7801
C
267 konedecl = (fun (k,_) d -> k d);
268 kparam = (fun (k,_) d -> k d);
b1b2de81
C
269 kdef = (fun (k,_) d -> k d);
270 kini = (fun (k,_) ie -> k ie);
271 kname = (fun (k,_) x -> k x);
272 kinfo = (fun (k,_) ii -> k ii);
273 knode = (fun (k,_) n -> k n);
274 ktoplevel = (fun (k,_) p -> k p);
485bce71 275 kcppdirective = (fun (k,_) p -> k p);
b1b2de81
C
276 kdefineval = (fun (k,_) p -> k p);
277 kstatementseq = (fun (k,_) p -> k p);
278 kfield = (fun (k,_) p -> k p);
ae4735db 279 }
34e49164 280
485bce71
C
281
282(* ------------------------------------------------------------------------ *)
283
284
34e49164
C
285let rec vk_expr = fun bigf expr ->
286 let iif ii = vk_ii bigf ii in
287
288 let rec exprf e = bigf.kexpr (k,bigf) e
91eba41f 289 (* !!! dont go in _typ !!! *)
ae4735db 290 and k ((e,_typ), ii) =
34e49164
C
291 iif ii;
292 match e with
b1b2de81 293 | Ident (name) -> vk_name bigf name
34e49164 294 | Constant (c) -> ()
ae4735db
C
295 | FunCall (e, es) ->
296 exprf e;
485bce71 297 vk_argument_list bigf es;
ae4735db 298 | CondExpr (e1, e2, e3) ->
34e49164
C
299 exprf e1; do_option (exprf) e2; exprf e3
300 | Sequence (e1, e2) -> exprf e1; exprf e2;
301 | Assignment (e1, op, e2) -> exprf e1; exprf e2;
ae4735db 302
34e49164
C
303 | Postfix (e, op) -> exprf e
304 | Infix (e, op) -> exprf e
305 | Unary (e, op) -> exprf e
306 | Binary (e1, op, e2) -> exprf e1; exprf e2;
ae4735db 307
34e49164 308 | ArrayAccess (e1, e2) -> exprf e1; exprf e2;
b1b2de81
C
309 | RecordAccess (e, name) -> exprf e; vk_name bigf name
310 | RecordPtAccess (e, name) -> exprf e; vk_name bigf name
34e49164
C
311
312 | SizeOfExpr (e) -> exprf e
313 | SizeOfType (t) -> vk_type bigf t
314 | Cast (t, e) -> vk_type bigf t; exprf e
315
ae4735db
C
316 (* old: | StatementExpr (((declxs, statxs), is)), is2 ->
317 * List.iter (vk_decl bigf) declxs;
318 * List.iter (vk_statement bigf) statxs
34e49164 319 *)
ae4735db 320 | StatementExpr ((statxs, is)) ->
34e49164 321 iif is;
485bce71 322 statxs +> List.iter (vk_statement_sequencable bigf);
34e49164 323
7fe62b65
C
324 | Constructor (t, init) ->
325 vk_type bigf t; vk_ini bigf init
ae4735db 326
34e49164
C
327 | ParenExpr (e) -> exprf e
328
4dfbc1c2
C
329 | New t -> vk_argument bigf t
330 | Delete e -> vk_expr bigf e
f59c9fb7 331
34e49164
C
332
333 in exprf expr
334
34e49164 335
b1b2de81 336(* ------------------------------------------------------------------------ *)
ae4735db 337and vk_name = fun bigf ident ->
b1b2de81 338 let iif ii = vk_ii bigf ii in
34e49164 339
ae4735db
C
340 let rec namef x = bigf.kname (k,bigf) x
341 and k id =
b1b2de81
C
342 match id with
343 | RegularName (s, ii) -> iif ii
ae4735db
C
344 | CppConcatenatedName xs ->
345 xs +> List.iter (fun ((x,ii1), ii2) ->
b1b2de81
C
346 iif ii2;
347 iif ii1;
348 );
349 | CppVariadicName (s, ii) -> iif ii
ae4735db 350 | CppIdentBuilder ((s,iis), xs) ->
b1b2de81 351 iif iis;
ae4735db 352 xs +> List.iter (fun ((x,iix), iicomma) ->
b1b2de81
C
353 iif iicomma;
354 iif iix;
355 )
356 in
357 namef ident
358
359(* ------------------------------------------------------------------------ *)
34e49164
C
360
361
ae4735db 362and vk_statement = fun bigf (st: Ast_c.statement) ->
34e49164
C
363 let iif ii = vk_ii bigf ii in
364
ae4735db
C
365 let rec statf x = bigf.kstatement (k,bigf) x
366 and k st =
34e49164
C
367 let (unwrap_st, ii) = st in
368 iif ii;
369 match unwrap_st with
ae4735db 370 | Labeled (Label (name, st)) ->
708f4980
C
371 vk_name bigf name;
372 statf st;
34e49164 373 | Labeled (Case (e, st)) -> vk_expr bigf e; statf st;
ae4735db 374 | Labeled (CaseRange (e, e2, st)) ->
34e49164
C
375 vk_expr bigf e; vk_expr bigf e2; statf st;
376 | Labeled (Default st) -> statf st;
377
ae4735db 378 | Compound statxs ->
485bce71 379 statxs +> List.iter (vk_statement_sequencable bigf)
34e49164
C
380 | ExprStatement (eopt) -> do_option (vk_expr bigf) eopt;
381
ae4735db 382 | Selection (If (e, st1, st2)) ->
34e49164 383 vk_expr bigf e; statf st1; statf st2;
ae4735db 384 | Selection (Switch (e, st)) ->
34e49164 385 vk_expr bigf e; statf st;
ae4735db 386 | Iteration (While (e, st)) ->
34e49164 387 vk_expr bigf e; statf st;
ae4735db
C
388 | Iteration (DoWhile (st, e)) -> statf st; vk_expr bigf e;
389 | Iteration (For ((e1opt,i1), (e2opt,i2), (e3opt,i3), st)) ->
390 statf (mk_st (ExprStatement (e1opt)) i1);
391 statf (mk_st (ExprStatement (e2opt)) i2);
392 statf (mk_st (ExprStatement (e3opt)) i3);
34e49164
C
393 statf st;
394
ae4735db 395 | Iteration (MacroIteration (s, es, st)) ->
485bce71 396 vk_argument_list bigf es;
34e49164 397 statf st;
ae4735db 398
b1b2de81 399 | Jump (Goto name) -> vk_name bigf name
34e49164
C
400 | Jump ((Continue|Break|Return)) -> ()
401 | Jump (ReturnExpr e) -> vk_expr bigf e;
402 | Jump (GotoComputed e) -> vk_expr bigf e;
403
ae4735db 404 | Decl decl -> vk_decl bigf decl
34e49164
C
405 | Asm asmbody -> vk_asmbody bigf asmbody
406 | NestedFunc def -> vk_def bigf def
407 | MacroStmt -> ()
408
409 in statf st
410
ae4735db
C
411and vk_statement_sequencable = fun bigf stseq ->
412 let f = bigf.kstatementseq in
485bce71 413
ae4735db 414 let rec k stseq =
485bce71
C
415 match stseq with
416 | StmtElem st -> vk_statement bigf st
ae4735db 417 | CppDirectiveStmt directive ->
485bce71 418 vk_cpp_directive bigf directive
ae4735db 419 | IfdefStmt ifdef ->
485bce71 420 vk_ifdef_directive bigf ifdef
ae4735db 421 | IfdefStmt2 (ifdef, xxs) ->
485bce71 422 ifdef +> List.iter (vk_ifdef_directive bigf);
ae4735db 423 xxs +> List.iter (fun xs ->
485bce71
C
424 xs +> List.iter (vk_statement_sequencable bigf)
425 )
ae4735db 426
485bce71
C
427 in f (k, bigf) stseq
428
34e49164 429
34e49164 430
ae4735db 431and vk_type = fun bigf t ->
34e49164
C
432 let iif ii = vk_ii bigf ii in
433
ae4735db
C
434 let rec typef x = bigf.ktype (k, bigf) x
435 and k t =
34e49164
C
436 let (q, t) = t in
437 let (unwrap_q, iiq) = q in
438 let (unwrap_t, iit) = t in
439 iif iiq;
440 iif iit;
441 match unwrap_t with
190f1acf 442 | NoType -> ()
34e49164
C
443 | BaseType _ -> ()
444 | Pointer t -> typef t
ae4735db 445 | Array (eopt, t) ->
34e49164 446 do_option (vk_expr bigf) eopt;
ae4735db
C
447 typef t
448 | FunctionType (returnt, paramst) ->
34e49164
C
449 typef returnt;
450 (match paramst with
ae4735db 451 | (ts, (b,iihas3dots)) ->
34e49164 452 iif iihas3dots;
485bce71 453 vk_param_list bigf ts
34e49164
C
454 )
455
ae4735db 456 | Enum (sopt, enumt) ->
c491d8ee 457 vk_enum_fields bigf enumt
ae4735db
C
458
459 | StructUnion (sopt, _su, fields) ->
34e49164
C
460 vk_struct_fields bigf fields
461
462 | StructUnionName (s, structunion) -> ()
463 | EnumName s -> ()
464
465 (* dont go in _typ *)
ae4735db 466 | TypeName (name,_typ) ->
b1b2de81 467 vk_name bigf name
34e49164
C
468
469 | ParenType t -> typef t
470 | TypeOfExpr e -> vk_expr bigf e
471 | TypeOfType t -> typef t
472
473 in typef t
474
485bce71 475
ae4735db 476and vk_attribute = fun bigf attr ->
485bce71
C
477 let iif ii = vk_ii bigf ii in
478 match attr with
ae4735db 479 | Attribute s, ii ->
485bce71
C
480 iif ii
481
482
483(* ------------------------------------------------------------------------ *)
484
ae4735db 485and vk_decl = fun bigf d ->
34e49164
C
486 let iif ii = vk_ii bigf ii in
487
ae4735db
C
488 let f = bigf.kdecl in
489 let rec k decl =
490 match decl with
785a3008
C
491 | DeclList (xs,ii) ->
492 iif ii;
493 xs +> List.iter (fun (x,ii) ->
91eba41f 494 iif ii;
17ba0788 495 vk_onedecl bigf x
91eba41f 496 );
5427db06 497 | MacroDecl ((s, args, ptvg),ii) ->
17ba0788
C
498 iif ii;
499 vk_argument_list bigf args
500 | MacroDeclInit ((s, args, ini),ii) ->
34e49164 501 iif ii;
485bce71 502 vk_argument_list bigf args;
17ba0788 503 vk_ini bigf ini
ae4735db 504 in f (k, bigf) d
91eba41f 505
190f1acf
C
506and vk_decl_list = fun bigf ts ->
507 ts +> List.iter (vk_decl bigf)
91eba41f 508
ae4735db 509and vk_onedecl = fun bigf onedecl ->
91eba41f 510 let iif ii = vk_ii bigf ii in
ae4735db
C
511 let f = bigf.konedecl in
512 let rec k onedecl =
91eba41f 513 match onedecl with
ae4735db
C
514 | ({v_namei = var;
515 v_type = t;
978fd7e5 516 v_type_bis = tbis;
ae4735db
C
517 v_storage = _sto;
518 v_attr = attrs}) ->
34e49164 519
34e49164 520 vk_type bigf t;
978fd7e5 521 (* dont go in tbis *)
485bce71 522 attrs +> List.iter (vk_attribute bigf);
ae4735db 523 var +> Common.do_option (fun (name, iniopt) ->
b1b2de81 524 vk_name bigf name;
4dfbc1c2
C
525 (match iniopt with
526 Ast_c.NoInit -> ()
527 | Ast_c.ValInit(iini,init) -> iif [iini]; vk_ini bigf init
528 | Ast_c.ConstrInit((init,ii)) -> iif ii; vk_argument_list bigf init)
b1b2de81 529 )
951c7801 530 in f (k, bigf) onedecl
34e49164 531
ae4735db 532and vk_ini = fun bigf ini ->
34e49164
C
533 let iif ii = vk_ii bigf ii in
534
ae4735db
C
535 let rec inif x = bigf.kini (k, bigf) x
536 and k (ini, iini) =
34e49164
C
537 iif iini;
538 match ini with
539 | InitExpr e -> vk_expr bigf e
ae4735db
C
540 | InitList initxs ->
541 initxs +> List.iter (fun (ini, ii) ->
34e49164
C
542 inif ini;
543 iif ii;
ae4735db
C
544 )
545 | InitDesignators (xs, e) ->
34e49164
C
546 xs +> List.iter (vk_designator bigf);
547 inif e
548
549 | InitFieldOld (s, e) -> inif e
550 | InitIndexOld (e1, e) ->
551 vk_expr bigf e1; inif e
552
485bce71 553
34e49164
C
554 in inif ini
555
8f657093
C
556and vk_ini_list = fun bigf ts ->
557 let iif ii = vk_ii bigf ii in
558 ts +> List.iter (fun (ini,iicomma) ->
559 vk_ini bigf ini;
560 iif iicomma;
561 )
34e49164 562
ae4735db 563and vk_designator = fun bigf design ->
34e49164
C
564 let iif ii = vk_ii bigf ii in
565 let (designator, ii) = design in
566 iif ii;
567 match designator with
568 | DesignatorField s -> ()
569 | DesignatorIndex e -> vk_expr bigf e
570 | DesignatorRange (e1, e2) -> vk_expr bigf e1; vk_expr bigf e2
571
485bce71
C
572
573(* ------------------------------------------------------------------------ *)
574
ae4735db 575and vk_struct_fields = fun bigf fields ->
0708f913
C
576 fields +> List.iter (vk_struct_field bigf);
577
ae4735db 578and vk_struct_field = fun bigf field ->
34e49164
C
579 let iif ii = vk_ii bigf ii in
580
0708f913 581 let f = bigf.kfield in
ae4735db 582 let rec k field =
0708f913 583
ae4735db
C
584 match field with
585 | DeclarationField
586 (FieldDeclList (onefield_multivars, iiptvirg)) ->
485bce71
C
587 vk_struct_fieldkinds bigf onefield_multivars;
588 iif iiptvirg;
708f4980 589 | EmptyField info -> iif [info]
ae4735db 590 | MacroDeclField ((s, args),ii) ->
708f4980
C
591 iif ii;
592 vk_argument_list bigf args;
485bce71 593
ae4735db 594 | CppDirectiveStruct directive ->
485bce71 595 vk_cpp_directive bigf directive
ae4735db 596 | IfdefStruct ifdef ->
485bce71 597 vk_ifdef_directive bigf ifdef
0708f913
C
598 in
599 f (k, bigf) field
485bce71 600
34e49164 601
ae4735db
C
602
603
604and vk_struct_fieldkinds = fun bigf onefield_multivars ->
34e49164
C
605 let iif ii = vk_ii bigf ii in
606 onefield_multivars +> List.iter (fun (field, iicomma) ->
607 iif iicomma;
608 match field with
ae4735db 609 | Simple (nameopt, t) ->
b1b2de81
C
610 Common.do_option (vk_name bigf) nameopt;
611 vk_type bigf t;
ae4735db 612 | BitField (nameopt, t, info, expr) ->
b1b2de81
C
613 Common.do_option (vk_name bigf) nameopt;
614 vk_info bigf info;
34e49164 615 vk_expr bigf expr;
ae4735db 616 vk_type bigf t
34e49164
C
617 )
618
c491d8ee
C
619
620and vk_enum_fields = fun bigf enumt ->
621 let iif ii = vk_ii bigf ii in
622 enumt +> List.iter (fun ((name, eopt), iicomma) ->
623 vk_oneEnum bigf (name, eopt);
624 iif iicomma)
625
626and vk_oneEnum = fun bigf (name, eopt) ->
627 let iif ii = vk_ii bigf ii in
628 vk_name bigf name;
629 eopt +> Common.do_option (fun (info, e) ->
630 iif [info];
631 vk_expr bigf e
632 )
633
485bce71 634(* ------------------------------------------------------------------------ *)
34e49164
C
635
636
ae4735db 637and vk_def = fun bigf d ->
34e49164
C
638 let iif ii = vk_ii bigf ii in
639
640 let f = bigf.kdef in
ae4735db 641 let rec k d =
34e49164 642 match d with
708f4980 643 | {f_name = name;
485bce71
C
644 f_type = (returnt, (paramst, (b, iib)));
645 f_storage = sto;
646 f_body = statxs;
647 f_attr = attrs;
91eba41f 648 f_old_c_style = oldstyle;
ae4735db
C
649 }, ii
650 ->
34e49164
C
651 iif ii;
652 iif iib;
485bce71 653 attrs +> List.iter (vk_attribute bigf);
34e49164 654 vk_type bigf returnt;
708f4980 655 vk_name bigf name;
ae4735db 656 paramst +> List.iter (fun (param,iicomma) ->
34e49164
C
657 vk_param bigf param;
658 iif iicomma;
659 );
ae4735db 660 oldstyle +> Common.do_option (fun decls ->
91eba41f
C
661 decls +> List.iter (vk_decl bigf);
662 );
663
485bce71 664 statxs +> List.iter (vk_statement_sequencable bigf)
ae4735db 665 in f (k, bigf) d
34e49164
C
666
667
668
669
ae4735db 670and vk_toplevel = fun bigf p ->
34e49164
C
671 let f = bigf.ktoplevel in
672 let iif ii = vk_ii bigf ii in
ae4735db 673 let rec k p =
34e49164
C
674 match p with
675 | Declaration decl -> (vk_decl bigf decl)
676 | Definition def -> (vk_def bigf def)
677 | EmptyDef ii -> iif ii
ae4735db 678 | MacroTop (s, xs, ii) ->
485bce71
C
679 vk_argument_list bigf xs;
680 iif ii
681
682 | CppTop top -> vk_cpp_directive bigf top
683 | IfdefTop ifdefdir -> vk_ifdef_directive bigf ifdefdir
ae4735db 684
485bce71
C
685 | NotParsedCorrectly ii -> iif ii
686 | FinalDef info -> vk_info bigf info
687 in f (k, bigf) p
688
ae4735db 689and vk_program = fun bigf xs ->
485bce71
C
690 xs +> List.iter (vk_toplevel bigf)
691
ae4735db 692and vk_ifdef_directive bigf directive =
485bce71
C
693 let iif ii = vk_ii bigf ii in
694 match directive with
695 | IfdefDirective (ifkind, ii) -> iif ii
696
697
698and vk_cpp_directive bigf directive =
699 let iif ii = vk_ii bigf ii in
700 let f = bigf.kcppdirective in
ae4735db 701 let rec k directive =
485bce71
C
702 match directive with
703 | Include {i_include = (s, ii);
704 i_content = copt;
705 }
ae4735db 706 ->
91eba41f
C
707 (* go inside ? yes, can be useful, for instance for type_annotater.
708 * The only pb may be that when we want to unparse the code we
ae4735db 709 * don't want to unparse the included file but the unparser
91eba41f
C
710 * and pretty_print do not use visitor_c so no problem.
711 *)
485bce71 712 iif ii;
ae4735db 713 copt +> Common.do_option (fun (file, asts) ->
485bce71
C
714 vk_program bigf asts
715 );
ae4735db 716 | Define ((s,ii), (defkind, defval)) ->
34e49164
C
717 iif ii;
718 vk_define_kind bigf defkind;
719 vk_define_val bigf defval
ae4735db 720 | PragmaAndCo (ii) ->
485bce71
C
721 iif ii
722 in f (k, bigf) directive
34e49164 723
34e49164 724
ae4735db 725and vk_define_kind bigf defkind =
34e49164
C
726 match defkind with
727 | DefineVar -> ()
ae4735db 728 | DefineFunc (params, ii) ->
34e49164 729 vk_ii bigf ii;
ae4735db 730 params +> List.iter (fun ((s,iis), iicomma) ->
34e49164
C
731 vk_ii bigf iis;
732 vk_ii bigf iicomma;
733 )
3a314143 734 | Undef -> ()
34e49164 735
ae4735db
C
736and vk_define_val bigf defval =
737 let f = bigf.kdefineval in
485bce71 738
ae4735db 739 let rec k defval =
34e49164 740 match defval with
ae4735db 741 | DefineExpr e ->
34e49164
C
742 vk_expr bigf e
743 | DefineStmt stmt -> vk_statement bigf stmt
ae4735db 744 | DefineDoWhileZero ((stmt, e), ii) ->
34e49164 745 vk_statement bigf stmt;
485bce71 746 vk_expr bigf e;
34e49164
C
747 vk_ii bigf ii
748 | DefineFunction def -> vk_def bigf def
749 | DefineType ty -> vk_type bigf ty
750 | DefineText (s, ii) -> vk_ii bigf ii
751 | DefineEmpty -> ()
485bce71
C
752 | DefineInit ini -> vk_ini bigf ini
753
ae4735db 754 | DefineTodo ->
91eba41f 755 pr2_once "DefineTodo";
485bce71
C
756 ()
757 in f (k, bigf) defval
34e49164 758
ae4735db
C
759
760
34e49164
C
761
762(* ------------------------------------------------------------------------ *)
ae4735db 763(* Now keep fullstatement inside the control flow node,
34e49164 764 * so that can then get in a MetaStmtVar the fullstatement to later
ae4735db 765 * pp back when the S is in a +. But that means that
34e49164
C
766 * Exp will match an Ifnode even if there is no such exp
767 * inside the condition of the Ifnode (because the exp may
768 * be deeper, in the then branch). So have to not visit
769 * all inside a node anymore.
ae4735db 770 *
485bce71 771 * update: j'ai choisi d'accrocher au noeud du CFG a la
ae4735db 772 * fois le fullstatement et le partialstatement et appeler le
34e49164
C
773 * visiteur que sur le partialstatement.
774 *)
775
ae4735db 776and vk_node = fun bigf node ->
34e49164
C
777 let iif ii = vk_ii bigf ii in
778 let infof info = vk_info bigf info in
779
780 let f = bigf.knode in
ae4735db 781 let rec k n =
34e49164
C
782 match F.unwrap n with
783
91eba41f
C
784 | F.FunHeader (def) ->
785 assert(null (fst def).f_body);
786 vk_def bigf def;
34e49164 787
ae4735db
C
788 | F.Decl decl -> vk_decl bigf decl
789 | F.ExprStatement (st, (eopt, ii)) ->
34e49164
C
790 iif ii;
791 eopt +> do_option (vk_expr bigf)
792
ae4735db 793 | F.IfHeader (_, (e,ii))
34e49164
C
794 | F.SwitchHeader (_, (e,ii))
795 | F.WhileHeader (_, (e,ii))
ae4735db 796 | F.DoWhileTail (e,ii) ->
34e49164
C
797 iif ii;
798 vk_expr bigf e
799
ae4735db 800 | F.ForHeader (_st, (((e1opt,i1), (e2opt,i2), (e3opt,i3)), ii)) ->
34e49164
C
801 iif i1; iif i2; iif i3;
802 iif ii;
803 e1opt +> do_option (vk_expr bigf);
804 e2opt +> do_option (vk_expr bigf);
805 e3opt +> do_option (vk_expr bigf);
ae4735db 806 | F.MacroIterHeader (_s, ((s,es), ii)) ->
34e49164 807 iif ii;
485bce71 808 vk_argument_list bigf es;
ae4735db 809
34e49164 810 | F.ReturnExpr (_st, (e,ii)) -> iif ii; vk_expr bigf e
ae4735db 811
34e49164 812 | F.Case (_st, (e,ii)) -> iif ii; vk_expr bigf e
ae4735db 813 | F.CaseRange (_st, ((e1, e2),ii)) ->
34e49164
C
814 iif ii; vk_expr bigf e1; vk_expr bigf e2
815
816
817 | F.CaseNode i -> ()
818
819 | F.DefineExpr e -> vk_expr bigf e
820 | F.DefineType ft -> vk_type bigf ft
ae4735db 821 | F.DefineHeader ((s,ii), (defkind)) ->
34e49164
C
822 iif ii;
823 vk_define_kind bigf defkind;
824
825 | F.DefineDoWhileZeroHeader (((),ii)) -> iif ii
ae4735db 826 | F.DefineTodo ->
91eba41f 827 pr2_once "DefineTodo";
485bce71
C
828 ()
829
485bce71 830 | F.Include {i_include = (s, ii);} -> iif ii;
34e49164 831
ae4735db 832 | F.MacroTop (s, args, ii) ->
34e49164 833 iif ii;
485bce71 834 vk_argument_list bigf args
34e49164 835
ae4735db
C
836 | F.IfdefHeader (info) -> vk_ifdef_directive bigf info
837 | F.IfdefElse (info) -> vk_ifdef_directive bigf info
838 | F.IfdefEndif (info) -> vk_ifdef_directive bigf info
34e49164
C
839
840 | F.Break (st,((),ii)) -> iif ii
841 | F.Continue (st,((),ii)) -> iif ii
842 | F.Default (st,((),ii)) -> iif ii
843 | F.Return (st,((),ii)) -> iif ii
b1b2de81
C
844 | F.Goto (st, name, ((),ii)) -> vk_name bigf name; iif ii
845 | F.Label (st, name, ((),ii)) -> vk_name bigf name; iif ii
485bce71 846
34e49164 847 | F.DoHeader (st, info) -> infof info
485bce71 848
34e49164 849 | F.Else info -> infof info
485bce71
C
850 | F.EndStatement iopt -> do_option infof iopt
851
34e49164
C
852 | F.SeqEnd (i, info) -> infof info
853 | F.SeqStart (st, i, info) -> infof info
854
855 | F.MacroStmt (st, ((),ii)) -> iif ii
ae4735db 856 | F.Asm (st, (asmbody,ii)) ->
34e49164
C
857 iif ii;
858 vk_asmbody bigf asmbody
859
860 | (
861 F.TopNode|F.EndNode|
951c7801
C
862 F.ErrorExit|F.Exit|F.Enter|F.LoopFallThroughNode|F.FallThroughNode|
863 F.AfterNode|F.FalseNode|F.TrueNode|F.InLoopNode|
34e49164
C
864 F.Fake
865 ) -> ()
866
867
868
869 in
870 f (k, bigf) node
871
872(* ------------------------------------------------------------------------ *)
ae4735db 873and vk_info = fun bigf info ->
34e49164
C
874 let rec infof ii = bigf.kinfo (k, bigf) ii
875 and k i = ()
876 in
877 infof info
878
ae4735db 879and vk_ii = fun bigf ii ->
34e49164
C
880 List.iter (vk_info bigf) ii
881
882
485bce71 883(* ------------------------------------------------------------------------ *)
ae4735db
C
884and vk_argument = fun bigf arg ->
885 let rec do_action = function
485bce71
C
886 | (ActMisc ii) -> vk_ii bigf ii
887 in
888 match arg with
889 | Left e -> (vk_expr bigf) e
890 | Right (ArgType param) -> vk_param bigf param
891 | Right (ArgAction action) -> do_action action
892
ae4735db 893and vk_argument_list = fun bigf es ->
485bce71 894 let iif ii = vk_ii bigf ii in
ae4735db 895 es +> List.iter (fun (e, ii) ->
485bce71
C
896 iif ii;
897 vk_argument bigf e
898 )
899
900
901
b1b2de81 902and vk_param = fun bigf param ->
34e49164 903 let iif ii = vk_ii bigf ii in
ae4735db 904 let f = bigf.kparam in
951c7801
C
905 let rec k param =
906 let {p_namei = swrapopt; p_register = (b, iib); p_type=ft} = param in
907 swrapopt +> Common.do_option (vk_name bigf);
908 iif iib;
909 vk_type bigf ft
910 in f (k, bigf) param
34e49164 911
ae4735db 912and vk_param_list = fun bigf ts ->
485bce71 913 let iif ii = vk_ii bigf ii in
ae4735db 914 ts +> List.iter (fun (param,iicomma) ->
485bce71
C
915 vk_param bigf param;
916 iif iicomma;
917 )
918
919
920
921(* ------------------------------------------------------------------------ *)
ae4735db 922and vk_asmbody = fun bigf (string_list, colon_list) ->
485bce71
C
923 let iif ii = vk_ii bigf ii in
924
925 iif string_list;
ae4735db 926 colon_list +> List.iter (fun (Colon xs, ii) ->
485bce71 927 iif ii;
ae4735db 928 xs +> List.iter (fun (x,iicomma) ->
485bce71
C
929 iif iicomma;
930 (match x with
ae4735db
C
931 | ColonMisc, ii -> iif ii
932 | ColonExpr e, ii ->
485bce71
C
933 vk_expr bigf e;
934 iif ii
935 )
936 ))
937
34e49164 938
485bce71 939(* ------------------------------------------------------------------------ *)
c491d8ee 940let vk_splitted element = fun bigf args_splitted ->
34e49164 941 let iif ii = vk_ii bigf ii in
ae4735db 942 args_splitted +> List.iter (function
c491d8ee 943 | Left arg -> element bigf arg
34e49164
C
944 | Right ii -> iif ii
945 )
946
c491d8ee
C
947let vk_args_splitted = vk_splitted vk_argument
948let vk_define_params_splitted = vk_splitted (fun bigf (_,ii) -> vk_ii bigf ii)
949let vk_params_splitted = vk_splitted vk_param
950let vk_enum_fields_splitted = vk_splitted vk_oneEnum
951let vk_inis_splitted = vk_splitted vk_ini
34e49164 952
485bce71 953(* ------------------------------------------------------------------------ *)
ae4735db 954let vk_cst = fun bigf (cst, ii) ->
34e49164
C
955 let iif ii = vk_ii bigf ii in
956 iif ii;
957 (match cst with
958 | Left cst -> ()
959 | Right s -> ()
960 )
961
962
ae4735db 963
34e49164
C
964
965(*****************************************************************************)
966(* "syntetisized attributes" style *)
967(*****************************************************************************)
485bce71
C
968
969(* TODO port the xxs_s to new cpp construct too *)
970
ae4735db 971type 'a inout = 'a -> 'a
34e49164 972
ae4735db 973(* _s for synthetizized attributes
34e49164
C
974 *
975 * Note that I don't visit necesserally in the order of the token
976 * found in the original file. So don't assume such hypothesis!
977 *)
ae4735db 978type visitor_c_s = {
34e49164
C
979 kexpr_s: (expression inout * visitor_c_s) -> expression inout;
980 kstatement_s: (statement inout * visitor_c_s) -> statement inout;
981 ktype_s: (fullType inout * visitor_c_s) -> fullType inout;
34e49164
C
982
983 kdecl_s: (declaration inout * visitor_c_s) -> declaration inout;
ae4735db 984 kdef_s: (definition inout * visitor_c_s) -> definition inout;
b1b2de81 985 kname_s: (name inout * visitor_c_s) -> name inout;
34e49164 986
ae4735db 987 kini_s: (initialiser inout * visitor_c_s) -> initialiser inout;
34e49164 988
485bce71 989 kcppdirective_s: (cpp_directive inout * visitor_c_s) -> cpp_directive inout;
34e49164 990 kdefineval_s: (define_val inout * visitor_c_s) -> define_val inout;
485bce71
C
991 kstatementseq_s: (statement_sequencable inout * visitor_c_s) -> statement_sequencable inout;
992 kstatementseq_list_s: (statement_sequencable list inout * visitor_c_s) -> statement_sequencable list inout;
993
994 knode_s: (F.node inout * visitor_c_s) -> F.node inout;
34e49164 995
485bce71
C
996
997 ktoplevel_s: (toplevel inout * visitor_c_s) -> toplevel inout;
34e49164 998 kinfo_s: (info inout * visitor_c_s) -> info inout;
ae4735db 999 }
34e49164 1000
ae4735db 1001let default_visitor_c_s =
34e49164
C
1002 { kexpr_s = (fun (k,_) e -> k e);
1003 kstatement_s = (fun (k,_) st -> k st);
1004 ktype_s = (fun (k,_) t -> k t);
1005 kdecl_s = (fun (k,_) d -> k d);
1006 kdef_s = (fun (k,_) d -> k d);
b1b2de81 1007 kname_s = (fun (k,_) x -> k x);
34e49164
C
1008 kini_s = (fun (k,_) d -> k d);
1009 ktoplevel_s = (fun (k,_) p -> k p);
1010 knode_s = (fun (k,_) n -> k n);
1011 kinfo_s = (fun (k,_) i -> k i);
1012 kdefineval_s = (fun (k,_) x -> k x);
485bce71
C
1013 kstatementseq_s = (fun (k,_) x -> k x);
1014 kstatementseq_list_s = (fun (k,_) x -> k x);
1015 kcppdirective_s = (fun (k,_) x -> k x);
ae4735db 1016 }
34e49164
C
1017
1018let rec vk_expr_s = fun bigf expr ->
1019 let iif ii = vk_ii_s bigf ii in
1020 let rec exprf e = bigf.kexpr_s (k, bigf) e
ae4735db 1021 and k e =
34e49164 1022 let ((unwrap_e, typ), ii) = e in
91eba41f 1023 (* !!! don't analyse optional type !!!
ae4735db 1024 * old: typ +> map_option (vk_type_s bigf) in
34e49164 1025 *)
ae4735db
C
1026 let typ' = typ in
1027 let e' =
34e49164 1028 match unwrap_e with
b1b2de81 1029 | Ident (name) -> Ident (vk_name_s bigf name)
34e49164 1030 | Constant (c) -> Constant (c)
ae4735db 1031 | FunCall (e, es) ->
34e49164 1032 FunCall (exprf e,
ae4735db 1033 es +> List.map (fun (e,ii) ->
34e49164
C
1034 vk_argument_s bigf e, iif ii
1035 ))
ae4735db 1036
faf9a90c 1037 | CondExpr (e1, e2, e3) -> CondExpr (exprf e1, fmap exprf e2, exprf e3)
34e49164
C
1038 | Sequence (e1, e2) -> Sequence (exprf e1, exprf e2)
1039 | Assignment (e1, op, e2) -> Assignment (exprf e1, op, exprf e2)
ae4735db 1040
34e49164
C
1041 | Postfix (e, op) -> Postfix (exprf e, op)
1042 | Infix (e, op) -> Infix (exprf e, op)
1043 | Unary (e, op) -> Unary (exprf e, op)
1044 | Binary (e1, op, e2) -> Binary (exprf e1, op, exprf e2)
ae4735db 1045
34e49164 1046 | ArrayAccess (e1, e2) -> ArrayAccess (exprf e1, exprf e2)
ae4735db
C
1047 | RecordAccess (e, name) ->
1048 RecordAccess (exprf e, vk_name_s bigf name)
1049 | RecordPtAccess (e, name) ->
1050 RecordPtAccess (exprf e, vk_name_s bigf name)
34e49164
C
1051
1052 | SizeOfExpr (e) -> SizeOfExpr (exprf e)
1053 | SizeOfType (t) -> SizeOfType (vk_type_s bigf t)
1054 | Cast (t, e) -> Cast (vk_type_s bigf t, exprf e)
1055
ae4735db 1056 | StatementExpr (statxs, is) ->
34e49164 1057 StatementExpr (
485bce71 1058 vk_statement_sequencable_list_s bigf statxs,
34e49164 1059 iif is)
7fe62b65
C
1060 | Constructor (t, init) ->
1061 Constructor (vk_type_s bigf t, vk_ini_s bigf init)
ae4735db 1062
34e49164
C
1063 | ParenExpr (e) -> ParenExpr (exprf e)
1064
f59c9fb7 1065 | New t -> New (vk_argument_s bigf t)
4dfbc1c2 1066 | Delete e -> Delete (vk_expr_s bigf e)
f59c9fb7 1067
34e49164
C
1068 in
1069 (e', typ'), (iif ii)
1070 in exprf expr
1071
b1b2de81 1072
ae4735db 1073and vk_argument_s bigf argument =
34e49164 1074 let iif ii = vk_ii_s bigf ii in
ae4735db 1075 let rec do_action = function
34e49164
C
1076 | (ActMisc ii) -> ActMisc (iif ii)
1077 in
1078 (match argument with
1079 | Left e -> Left (vk_expr_s bigf e)
1080 | Right (ArgType param) -> Right (ArgType (vk_param_s bigf param))
1081 | Right (ArgAction action) -> Right (ArgAction (do_action action))
1082 )
1083
b1b2de81
C
1084(* ------------------------------------------------------------------------ *)
1085
34e49164 1086
ae4735db 1087and vk_name_s = fun bigf ident ->
b1b2de81 1088 let iif ii = vk_ii_s bigf ii in
ae4735db
C
1089 let rec namef x = bigf.kname_s (k,bigf) x
1090 and k id =
b1b2de81
C
1091 (match id with
1092 | RegularName (s,ii) -> RegularName (s, iif ii)
ae4735db
C
1093 | CppConcatenatedName xs ->
1094 CppConcatenatedName (xs +> List.map (fun ((x,ii1), ii2) ->
b1b2de81
C
1095 (x, iif ii1), iif ii2
1096 ))
1097 | CppVariadicName (s, ii) -> CppVariadicName (s, iif ii)
ae4735db 1098 | CppIdentBuilder ((s,iis), xs) ->
b1b2de81 1099 CppIdentBuilder ((s, iif iis),
ae4735db 1100 xs +> List.map (fun ((x,iix), iicomma) ->
b1b2de81
C
1101 ((x, iif iix), iif iicomma)))
1102 )
1103 in
1104 namef ident
34e49164 1105
b1b2de81 1106(* ------------------------------------------------------------------------ *)
34e49164
C
1107
1108
1109
ae4735db
C
1110and vk_statement_s = fun bigf st ->
1111 let rec statf st = bigf.kstatement_s (k, bigf) st
1112 and k st =
34e49164 1113 let (unwrap_st, ii) = st in
ae4735db 1114 let st' =
34e49164 1115 match unwrap_st with
ae4735db 1116 | Labeled (Label (name, st)) ->
708f4980 1117 Labeled (Label (vk_name_s bigf name, statf st))
ae4735db 1118 | Labeled (Case (e, st)) ->
34e49164 1119 Labeled (Case ((vk_expr_s bigf) e , statf st))
ae4735db
C
1120 | Labeled (CaseRange (e, e2, st)) ->
1121 Labeled (CaseRange ((vk_expr_s bigf) e,
1122 (vk_expr_s bigf) e2,
34e49164
C
1123 statf st))
1124 | Labeled (Default st) -> Labeled (Default (statf st))
ae4735db 1125 | Compound statxs ->
485bce71 1126 Compound (vk_statement_sequencable_list_s bigf statxs)
34e49164
C
1127 | ExprStatement (None) -> ExprStatement (None)
1128 | ExprStatement (Some e) -> ExprStatement (Some ((vk_expr_s bigf) e))
ae4735db 1129 | Selection (If (e, st1, st2)) ->
34e49164 1130 Selection (If ((vk_expr_s bigf) e, statf st1, statf st2))
ae4735db 1131 | Selection (Switch (e, st)) ->
34e49164 1132 Selection (Switch ((vk_expr_s bigf) e, statf st))
ae4735db 1133 | Iteration (While (e, st)) ->
34e49164 1134 Iteration (While ((vk_expr_s bigf) e, statf st))
ae4735db 1135 | Iteration (DoWhile (st, e)) ->
34e49164 1136 Iteration (DoWhile (statf st, (vk_expr_s bigf) e))
ae4735db 1137 | Iteration (For ((e1opt,i1), (e2opt,i2), (e3opt,i3), st)) ->
708f4980
C
1138 let e1opt' = statf (mk_st (ExprStatement (e1opt)) i1) in
1139 let e2opt' = statf (mk_st (ExprStatement (e2opt)) i2) in
1140 let e3opt' = statf (mk_st (ExprStatement (e3opt)) i3) in
1141
1142 let e1' = Ast_c.unwrap_st e1opt' in
1143 let e2' = Ast_c.unwrap_st e2opt' in
1144 let e3' = Ast_c.unwrap_st e3opt' in
1145 let i1' = Ast_c.get_ii_st_take_care e1opt' in
1146 let i2' = Ast_c.get_ii_st_take_care e2opt' in
1147 let i3' = Ast_c.get_ii_st_take_care e3opt' in
1148
1149 (match (e1', e2', e3') with
ae4735db 1150 | ((ExprStatement x1), (ExprStatement x2), ((ExprStatement x3))) ->
708f4980
C
1151 Iteration (For ((x1,i1'), (x2,i2'), (x3,i3'), statf st))
1152
34e49164
C
1153 | x -> failwith "cant be here if iterator keep ExprStatement as is"
1154 )
1155
ae4735db
C
1156 | Iteration (MacroIteration (s, es, st)) ->
1157 Iteration
34e49164
C
1158 (MacroIteration
1159 (s,
ae4735db 1160 es +> List.map (fun (e, ii) ->
34e49164 1161 vk_argument_s bigf e, vk_ii_s bigf ii
ae4735db 1162 ),
34e49164
C
1163 statf st
1164 ))
1165
ae4735db 1166
b1b2de81 1167 | Jump (Goto name) -> Jump (Goto (vk_name_s bigf name))
34e49164
C
1168 | Jump (((Continue|Break|Return) as x)) -> Jump (x)
1169 | Jump (ReturnExpr e) -> Jump (ReturnExpr ((vk_expr_s bigf) e))
1170 | Jump (GotoComputed e) -> Jump (GotoComputed (vk_expr_s bigf e));
1171
1172 | Decl decl -> Decl (vk_decl_s bigf decl)
1173 | Asm asmbody -> Asm (vk_asmbody_s bigf asmbody)
1174 | NestedFunc def -> NestedFunc (vk_def_s bigf def)
1175 | MacroStmt -> MacroStmt
1176 in
1177 st', vk_ii_s bigf ii
1178 in statf st
1179
485bce71 1180
ae4735db 1181and vk_statement_sequencable_s = fun bigf stseq ->
485bce71 1182 let f = bigf.kstatementseq_s in
ae4735db 1183 let k stseq =
485bce71
C
1184
1185 match stseq with
ae4735db 1186 | StmtElem st ->
485bce71 1187 StmtElem (vk_statement_s bigf st)
ae4735db 1188 | CppDirectiveStmt directive ->
485bce71 1189 CppDirectiveStmt (vk_cpp_directive_s bigf directive)
ae4735db 1190 | IfdefStmt ifdef ->
485bce71 1191 IfdefStmt (vk_ifdef_directive_s bigf ifdef)
ae4735db 1192 | IfdefStmt2 (ifdef, xxs) ->
485bce71 1193 let ifdef' = List.map (vk_ifdef_directive_s bigf) ifdef in
ae4735db 1194 let xxs' = xxs +> List.map (fun xs ->
b1b2de81 1195 xs +> vk_statement_sequencable_list_s bigf
485bce71
C
1196 )
1197 in
1198 IfdefStmt2(ifdef', xxs')
1199 in f (k, bigf) stseq
1200
ae4735db 1201and vk_statement_sequencable_list_s = fun bigf statxs ->
485bce71 1202 let f = bigf.kstatementseq_list_s in
ae4735db 1203 let k xs =
485bce71
C
1204 xs +> List.map (vk_statement_sequencable_s bigf)
1205 in
1206 f (k, bigf) statxs
485bce71
C
1207
1208
ae4735db
C
1209
1210and vk_asmbody_s = fun bigf (string_list, colon_list) ->
34e49164
C
1211 let iif ii = vk_ii_s bigf ii in
1212
1213 iif string_list,
ae4735db
C
1214 colon_list +> List.map (fun (Colon xs, ii) ->
1215 Colon
1216 (xs +> List.map (fun (x, iicomma) ->
34e49164 1217 (match x with
ae4735db 1218 | ColonMisc, ii -> ColonMisc, iif ii
34e49164
C
1219 | ColonExpr e, ii -> ColonExpr (vk_expr_s bigf e), iif ii
1220 ), iif iicomma
ae4735db
C
1221 )),
1222 iif ii
34e49164 1223 )
ae4735db
C
1224
1225
34e49164
C
1226
1227
0708f913 1228(* todo? a visitor for qualifier *)
ae4735db 1229and vk_type_s = fun bigf t ->
34e49164
C
1230 let rec typef t = bigf.ktype_s (k,bigf) t
1231 and iif ii = vk_ii_s bigf ii
ae4735db 1232 and k t =
34e49164
C
1233 let (q, t) = t in
1234 let (unwrap_q, iiq) = q in
faf9a90c
C
1235 (* strip_info_visitor needs iiq to be processed before iit *)
1236 let iif_iiq = iif iiq in
0708f913 1237 let q' = unwrap_q in
34e49164 1238 let (unwrap_t, iit) = t in
ae4735db 1239 let t' =
34e49164 1240 match unwrap_t with
f59c9fb7 1241 | NoType -> NoType
34e49164
C
1242 | BaseType x -> BaseType x
1243 | Pointer t -> Pointer (typef t)
ae4735db
C
1244 | Array (eopt, t) -> Array (fmap (vk_expr_s bigf) eopt, typef t)
1245 | FunctionType (returnt, paramst) ->
1246 FunctionType
1247 (typef returnt,
34e49164 1248 (match paramst with
ae4735db
C
1249 | (ts, (b, iihas3dots)) ->
1250 (ts +> List.map (fun (param,iicomma) ->
34e49164
C
1251 (vk_param_s bigf param, iif iicomma)),
1252 (b, iif iihas3dots))
1253 ))
1254
ae4735db 1255 | Enum (sopt, enumt) ->
c491d8ee 1256 Enum (sopt, vk_enum_fields_s bigf enumt)
ae4735db 1257 | StructUnion (sopt, su, fields) ->
34e49164
C
1258 StructUnion (sopt, su, vk_struct_fields_s bigf fields)
1259
1260
1261 | StructUnionName (s, structunion) -> StructUnionName (s, structunion)
1262 | EnumName s -> EnumName s
b1b2de81 1263 | TypeName (name, typ) -> TypeName (vk_name_s bigf name, typ)
34e49164
C
1264
1265 | ParenType t -> ParenType (typef t)
1266 | TypeOfExpr e -> TypeOfExpr (vk_expr_s bigf e)
1267 | TypeOfType t -> TypeOfType (typef t)
1268 in
ae4735db 1269 (q', iif_iiq),
faf9a90c 1270 (t', iif iit)
34e49164
C
1271
1272
1273 in typef t
1274
ae4735db 1275and vk_attribute_s = fun bigf attr ->
485bce71
C
1276 let iif ii = vk_ii_s bigf ii in
1277 match attr with
ae4735db 1278 | Attribute s, ii ->
485bce71
C
1279 Attribute s, iif ii
1280
1281
1282
ae4735db
C
1283and vk_decl_s = fun bigf d ->
1284 let f = bigf.kdecl_s in
34e49164 1285 let iif ii = vk_ii_s bigf ii in
ae4735db 1286 let rec k decl =
34e49164 1287 match decl with
ae4735db 1288 | DeclList (xs, ii) ->
34e49164 1289 DeclList (List.map aux xs, iif ii)
5427db06 1290 | MacroDecl ((s, args, ptvg),ii) ->
ae4735db
C
1291 MacroDecl
1292 ((s,
5427db06
C
1293 args +> List.map (fun (e,ii) -> vk_argument_s bigf e, iif ii),
1294 ptvg),
34e49164 1295 iif ii)
17ba0788
C
1296 | MacroDeclInit ((s, args, ini),ii) ->
1297 MacroDeclInit
1298 ((s,
1299 args +> List.map (fun (e,ii) -> vk_argument_s bigf e, iif ii),
1300 vk_ini_s bigf ini),
1301 iif ii)
34e49164
C
1302
1303
ae4735db
C
1304 and aux ({v_namei = var;
1305 v_type = t;
1306 v_type_bis = tbis;
1307 v_storage = sto;
1308 v_local= local;
1309 v_attr = attrs}, iicomma) =
1310 {v_namei =
1311 (var +> map_option (fun (name, iniopt) ->
1312 vk_name_s bigf name,
4dfbc1c2
C
1313 (match iniopt with
1314 Ast_c.NoInit -> iniopt
1315 | Ast_c.ValInit(iini,init) ->
1316 Ast_c.ValInit(vk_info_s bigf iini,vk_ini_s bigf init)
1317 | Ast_c.ConstrInit((init,ii)) ->
1318 let init =
1319 init +> List.map (fun (e,ii) -> vk_argument_s bigf e, iif ii) in
1320 Ast_c.ConstrInit((init, List.map (vk_info_s bigf) ii)))
1321 ));
485bce71 1322 v_type = vk_type_s bigf t;
978fd7e5
C
1323 (* !!! dont go in semantic related stuff !!! *)
1324 v_type_bis = tbis;
485bce71
C
1325 v_storage = sto;
1326 v_local = local;
1327 v_attr = attrs +> List.map (vk_attribute_s bigf);
1328 },
1329 iif iicomma
34e49164 1330
ae4735db 1331 in f (k, bigf) d
34e49164 1332
190f1acf
C
1333and vk_decl_list_s = fun bigf decls ->
1334 decls +> List.map (vk_decl_s bigf)
1335
ae4735db 1336and vk_ini_s = fun bigf ini ->
34e49164 1337 let rec inif ini = bigf.kini_s (k,bigf) ini
ae4735db 1338 and k ini =
34e49164 1339 let (unwrap_ini, ii) = ini in
ae4735db 1340 let ini' =
34e49164
C
1341 match unwrap_ini with
1342 | InitExpr e -> InitExpr (vk_expr_s bigf e)
ae4735db
C
1343 | InitList initxs ->
1344 InitList (initxs +> List.map (fun (ini, ii) ->
1345 inif ini, vk_ii_s bigf ii)
34e49164
C
1346 )
1347
1348
ae4735db
C
1349 | InitDesignators (xs, e) ->
1350 InitDesignators
34e49164 1351 (xs +> List.map (vk_designator_s bigf),
ae4735db 1352 inif e
34e49164
C
1353 )
1354
1355 | InitFieldOld (s, e) -> InitFieldOld (s, inif e)
1356 | InitIndexOld (e1, e) -> InitIndexOld (vk_expr_s bigf e1, inif e)
1357
485bce71 1358
34e49164
C
1359 in ini', vk_ii_s bigf ii
1360 in inif ini
1361
1362
ae4735db 1363and vk_designator_s = fun bigf design ->
34e49164
C
1364 let iif ii = vk_ii_s bigf ii in
1365 let (designator, ii) = design in
1366 (match designator with
1367 | DesignatorField s -> DesignatorField s
1368 | DesignatorIndex e -> DesignatorIndex (vk_expr_s bigf e)
ae4735db 1369 | DesignatorRange (e1, e2) ->
34e49164
C
1370 DesignatorRange (vk_expr_s bigf e1, vk_expr_s bigf e2)
1371 ), iif ii
1372
1373
1374
1375
ae4735db 1376and vk_struct_fieldkinds_s = fun bigf onefield_multivars ->
485bce71 1377 let iif ii = vk_ii_s bigf ii in
ae4735db 1378
485bce71
C
1379 onefield_multivars +> List.map (fun (field, iicomma) ->
1380 (match field with
ae4735db
C
1381 | Simple (nameopt, t) ->
1382 Simple (Common.map_option (vk_name_s bigf) nameopt,
b1b2de81 1383 vk_type_s bigf t)
ae4735db
C
1384 | BitField (nameopt, t, info, expr) ->
1385 BitField (Common.map_option (vk_name_s bigf) nameopt,
1386 vk_type_s bigf t,
b1b2de81
C
1387 vk_info_s bigf info,
1388 vk_expr_s bigf expr)
485bce71
C
1389 ), iif iicomma
1390 )
1391
413ffc02 1392and vk_struct_field_s = fun bigf field ->
34e49164
C
1393 let iif ii = vk_ii_s bigf ii in
1394
413ffc02
C
1395 match field with
1396 (DeclarationField (FieldDeclList (onefield_multivars, iiptvirg))) ->
1397 DeclarationField
1398 (FieldDeclList
1399 (vk_struct_fieldkinds_s bigf onefield_multivars, iif iiptvirg))
1400 | EmptyField info -> EmptyField (vk_info_s bigf info)
1401 | MacroDeclField ((s, args),ii) ->
1402 MacroDeclField
1403 ((s,
1404 args +> List.map (fun (e,ii) -> vk_argument_s bigf e, iif ii)
1405 ),
1406 iif ii)
1407
1408 | CppDirectiveStruct directive ->
1409 CppDirectiveStruct (vk_cpp_directive_s bigf directive)
1410 | IfdefStruct ifdef ->
1411 IfdefStruct (vk_ifdef_directive_s bigf ifdef)
485bce71 1412
413ffc02 1413and vk_struct_fields_s = fun bigf fields ->
413ffc02 1414 fields +> List.map (vk_struct_field_s bigf)
34e49164 1415
c491d8ee
C
1416and vk_enum_fields_s = fun bigf enumt ->
1417 let iif ii = vk_ii_s bigf ii in
1418 enumt +> List.map (fun ((name, eopt), iicomma) ->
1419 vk_oneEnum_s bigf (name, eopt), iif iicomma)
1420
1421and vk_oneEnum_s = fun bigf oneEnum ->
1422 let (name,eopt) = oneEnum in
1423 (vk_name_s bigf name,
1424 eopt +> Common.fmap (fun (info, e) ->
1425 vk_info_s bigf info,
1426 vk_expr_s bigf e
1427 ))
34e49164 1428
ae4735db 1429and vk_def_s = fun bigf d ->
34e49164
C
1430 let f = bigf.kdef_s in
1431 let iif ii = vk_ii_s bigf ii in
ae4735db 1432 let rec k d =
34e49164 1433 match d with
708f4980 1434 | {f_name = name;
485bce71
C
1435 f_type = (returnt, (paramst, (b, iib)));
1436 f_storage = sto;
1437 f_body = statxs;
1438 f_attr = attrs;
91eba41f 1439 f_old_c_style = oldstyle;
ae4735db
C
1440 }, ii
1441 ->
708f4980 1442 {f_name = vk_name_s bigf name;
ae4735db
C
1443 f_type =
1444 (vk_type_s bigf returnt,
485bce71
C
1445 (paramst +> List.map (fun (param, iicomma) ->
1446 (vk_param_s bigf param, iif iicomma)
1447 ), (b, iif iib)));
1448 f_storage = sto;
ae4735db 1449 f_body =
485bce71 1450 vk_statement_sequencable_list_s bigf statxs;
ae4735db 1451 f_attr =
91eba41f 1452 attrs +> List.map (vk_attribute_s bigf);
ae4735db
C
1453 f_old_c_style =
1454 oldstyle +> Common.map_option (fun decls ->
91eba41f
C
1455 decls +> List.map (vk_decl_s bigf)
1456 );
485bce71 1457 },
34e49164
C
1458 iif ii
1459
ae4735db 1460 in f (k, bigf) d
34e49164 1461
ae4735db 1462and vk_toplevel_s = fun bigf p ->
34e49164
C
1463 let f = bigf.ktoplevel_s in
1464 let iif ii = vk_ii_s bigf ii in
ae4735db 1465 let rec k p =
34e49164
C
1466 match p with
1467 | Declaration decl -> Declaration (vk_decl_s bigf decl)
1468 | Definition def -> Definition (vk_def_s bigf def)
1469 | EmptyDef ii -> EmptyDef (iif ii)
ae4735db 1470 | MacroTop (s, xs, ii) ->
34e49164 1471 MacroTop
ae4735db
C
1472 (s,
1473 xs +> List.map (fun (elem, iicomma) ->
34e49164
C
1474 vk_argument_s bigf elem, iif iicomma
1475 ),
1476 iif ii
1477 )
485bce71
C
1478 | CppTop top -> CppTop (vk_cpp_directive_s bigf top)
1479 | IfdefTop ifdefdir -> IfdefTop (vk_ifdef_directive_s bigf ifdefdir)
34e49164
C
1480
1481 | NotParsedCorrectly ii -> NotParsedCorrectly (iif ii)
1482 | FinalDef info -> FinalDef (vk_info_s bigf info)
1483 in f (k, bigf) p
1484
ae4735db 1485and vk_program_s = fun bigf xs ->
485bce71
C
1486 xs +> List.map (vk_toplevel_s bigf)
1487
1488
1489and vk_cpp_directive_s = fun bigf top ->
1490 let iif ii = vk_ii_s bigf ii in
1491 let f = bigf.kcppdirective_s in
ae4735db
C
1492 let rec k top =
1493 match top with
485bce71
C
1494 (* go inside ? *)
1495 | Include {i_include = (s, ii);
1496 i_rel_pos = h_rel_pos;
1497 i_is_in_ifdef = b;
1498 i_content = copt;
ae4735db 1499 }
485bce71
C
1500 -> Include {i_include = (s, iif ii);
1501 i_rel_pos = h_rel_pos;
1502 i_is_in_ifdef = b;
ae4735db 1503 i_content = copt +> Common.map_option (fun (file, asts) ->
485bce71
C
1504 file, vk_program_s bigf asts
1505 );
1506 }
ae4735db
C
1507 | Define ((s,ii), (defkind, defval)) ->
1508 Define ((s, iif ii),
485bce71 1509 (vk_define_kind_s bigf defkind, vk_define_val_s bigf defval))
485bce71
C
1510 | PragmaAndCo (ii) -> PragmaAndCo (iif ii)
1511
1512 in f (k, bigf) top
1513
ae4735db 1514and vk_ifdef_directive_s = fun bigf ifdef ->
485bce71
C
1515 let iif ii = vk_ii_s bigf ii in
1516 match ifdef with
1517 | IfdefDirective (ifkind, ii) -> IfdefDirective (ifkind, iif ii)
1518
1519
1520
ae4735db 1521and vk_define_kind_s = fun bigf defkind ->
34e49164 1522 match defkind with
ae4735db
C
1523 | DefineVar -> DefineVar
1524 | DefineFunc (params, ii) ->
1525 DefineFunc
1526 (params +> List.map (fun ((s,iis),iicomma) ->
34e49164
C
1527 ((s, vk_ii_s bigf iis), vk_ii_s bigf iicomma)
1528 ),
1529 vk_ii_s bigf ii
1530 )
3a314143 1531 | Undef -> Undef
34e49164
C
1532
1533
ae4735db 1534and vk_define_val_s = fun bigf x ->
34e49164
C
1535 let f = bigf.kdefineval_s in
1536 let iif ii = vk_ii_s bigf ii in
ae4735db 1537 let rec k x =
34e49164
C
1538 match x with
1539 | DefineExpr e -> DefineExpr (vk_expr_s bigf e)
1540 | DefineStmt st -> DefineStmt (vk_statement_s bigf st)
ae4735db 1541 | DefineDoWhileZero ((st,e),ii) ->
485bce71
C
1542 let st' = vk_statement_s bigf st in
1543 let e' = vk_expr_s bigf e in
1544 DefineDoWhileZero ((st',e'), iif ii)
34e49164
C
1545 | DefineFunction def -> DefineFunction (vk_def_s bigf def)
1546 | DefineType ty -> DefineType (vk_type_s bigf ty)
1547 | DefineText (s, ii) -> DefineText (s, iif ii)
1548 | DefineEmpty -> DefineEmpty
485bce71
C
1549 | DefineInit ini -> DefineInit (vk_ini_s bigf ini)
1550
ae4735db 1551 | DefineTodo ->
91eba41f 1552 pr2_once "DefineTodo";
485bce71 1553 DefineTodo
34e49164
C
1554 in
1555 f (k, bigf) x
34e49164 1556
ae4735db
C
1557
1558and vk_info_s = fun bigf info ->
34e49164
C
1559 let rec infof ii = bigf.kinfo_s (k, bigf) ii
1560 and k i = i
1561 in
1562 infof info
1563
ae4735db 1564and vk_ii_s = fun bigf ii ->
34e49164
C
1565 List.map (vk_info_s bigf) ii
1566
1567(* ------------------------------------------------------------------------ *)
ae4735db 1568and vk_node_s = fun bigf node ->
34e49164
C
1569 let iif ii = vk_ii_s bigf ii in
1570 let infof info = vk_info_s bigf info in
1571
1572 let rec nodef n = bigf.knode_s (k, bigf) n
ae4735db 1573 and k node =
34e49164
C
1574 F.rewrap node (
1575 match F.unwrap node with
ae4735db 1576 | F.FunHeader (def) ->
91eba41f
C
1577 assert (null (fst def).f_body);
1578 F.FunHeader (vk_def_s bigf def)
ae4735db 1579
34e49164 1580 | F.Decl declb -> F.Decl (vk_decl_s bigf declb)
ae4735db 1581 | F.ExprStatement (st, (eopt, ii)) ->
34e49164 1582 F.ExprStatement (st, (eopt +> map_option (vk_expr_s bigf), iif ii))
ae4735db
C
1583
1584 | F.IfHeader (st, (e,ii)) ->
34e49164 1585 F.IfHeader (st, (vk_expr_s bigf e, iif ii))
ae4735db 1586 | F.SwitchHeader (st, (e,ii)) ->
34e49164 1587 F.SwitchHeader(st, (vk_expr_s bigf e, iif ii))
ae4735db 1588 | F.WhileHeader (st, (e,ii)) ->
34e49164 1589 F.WhileHeader (st, (vk_expr_s bigf e, iif ii))
ae4735db 1590 | F.DoWhileTail (e,ii) ->
34e49164
C
1591 F.DoWhileTail (vk_expr_s bigf e, iif ii)
1592
ae4735db 1593 | F.ForHeader (st, (((e1opt,i1), (e2opt,i2), (e3opt,i3)), ii)) ->
34e49164
C
1594 F.ForHeader (st,
1595 (((e1opt +> Common.map_option (vk_expr_s bigf), iif i1),
1596 (e2opt +> Common.map_option (vk_expr_s bigf), iif i2),
1597 (e3opt +> Common.map_option (vk_expr_s bigf), iif i3)),
1598 iif ii))
1599
ae4735db 1600 | F.MacroIterHeader (st, ((s,es), ii)) ->
34e49164
C
1601 F.MacroIterHeader
1602 (st,
1603 ((s, es +> List.map (fun (e, ii) -> vk_argument_s bigf e, iif ii)),
1604 iif ii))
1605
ae4735db
C
1606
1607 | F.ReturnExpr (st, (e,ii)) ->
34e49164 1608 F.ReturnExpr (st, (vk_expr_s bigf e, iif ii))
ae4735db 1609
34e49164 1610 | F.Case (st, (e,ii)) -> F.Case (st, (vk_expr_s bigf e, iif ii))
ae4735db 1611 | F.CaseRange (st, ((e1, e2),ii)) ->
34e49164
C
1612 F.CaseRange (st, ((vk_expr_s bigf e1, vk_expr_s bigf e2), iif ii))
1613
1614 | F.CaseNode i -> F.CaseNode i
1615
ae4735db 1616 | F.DefineHeader((s,ii), (defkind)) ->
34e49164
C
1617 F.DefineHeader ((s, iif ii), (vk_define_kind_s bigf defkind))
1618
1619 | F.DefineExpr e -> F.DefineExpr (vk_expr_s bigf e)
1620 | F.DefineType ft -> F.DefineType (vk_type_s bigf ft)
ae4735db 1621 | F.DefineDoWhileZeroHeader ((),ii) ->
34e49164 1622 F.DefineDoWhileZeroHeader ((),iif ii)
485bce71
C
1623 | F.DefineTodo -> F.DefineTodo
1624
1625 | F.Include {i_include = (s, ii);
1626 i_rel_pos = h_rel_pos;
1627 i_is_in_ifdef = b;
1628 i_content = copt;
ae4735db
C
1629 }
1630 ->
b1b2de81 1631 assert (copt =*= None);
485bce71
C
1632 F.Include {i_include = (s, iif ii);
1633 i_rel_pos = h_rel_pos;
1634 i_is_in_ifdef = b;
1635 i_content = copt;
1636 }
34e49164 1637
ae4735db
C
1638 | F.MacroTop (s, args, ii) ->
1639 F.MacroTop
34e49164
C
1640 (s,
1641 args +> List.map (fun (e, ii) -> vk_argument_s bigf e, iif ii),
1642 iif ii)
1643
1644
1645 | F.MacroStmt (st, ((),ii)) -> F.MacroStmt (st, ((),iif ii))
1646 | F.Asm (st, (body,ii)) -> F.Asm (st, (vk_asmbody_s bigf body,iif ii))
1647
1648 | F.Break (st,((),ii)) -> F.Break (st,((),iif ii))
1649 | F.Continue (st,((),ii)) -> F.Continue (st,((),iif ii))
1650 | F.Default (st,((),ii)) -> F.Default (st,((),iif ii))
1651 | F.Return (st,((),ii)) -> F.Return (st,((),iif ii))
ae4735db 1652 | F.Goto (st, name, ((),ii)) ->
b1b2de81 1653 F.Goto (st, vk_name_s bigf name, ((),iif ii))
ae4735db 1654 | F.Label (st, name, ((),ii)) ->
b1b2de81 1655 F.Label (st, vk_name_s bigf name, ((),iif ii))
34e49164
C
1656 | F.EndStatement iopt -> F.EndStatement (map_option infof iopt)
1657 | F.DoHeader (st, info) -> F.DoHeader (st, infof info)
1658 | F.Else info -> F.Else (infof info)
1659 | F.SeqEnd (i, info) -> F.SeqEnd (i, infof info)
1660 | F.SeqStart (st, i, info) -> F.SeqStart (st, i, infof info)
1661
485bce71
C
1662 | F.IfdefHeader (info) -> F.IfdefHeader (vk_ifdef_directive_s bigf info)
1663 | F.IfdefElse (info) -> F.IfdefElse (vk_ifdef_directive_s bigf info)
1664 | F.IfdefEndif (info) -> F.IfdefEndif (vk_ifdef_directive_s bigf info)
1665
34e49164
C
1666 | (
1667 (
1668 F.TopNode|F.EndNode|
951c7801
C
1669 F.ErrorExit|F.Exit|F.Enter|F.LoopFallThroughNode|F.FallThroughNode|
1670 F.AfterNode|F.FalseNode|F.TrueNode|F.InLoopNode|
34e49164
C
1671 F.Fake
1672 ) as x) -> x
1673
1674
1675 )
1676 in
1677 nodef node
ae4735db 1678
34e49164 1679(* ------------------------------------------------------------------------ *)
ae4735db 1680and vk_param_s = fun bigf param ->
34e49164 1681 let iif ii = vk_ii_s bigf ii in
b1b2de81
C
1682 let {p_namei = swrapopt; p_register = (b, iib); p_type=ft} = param in
1683 { p_namei = swrapopt +> Common.map_option (vk_name_s bigf);
1684 p_register = (b, iif iib);
1685 p_type = vk_type_s bigf ft;
1686 }
faf9a90c 1687
ae4735db 1688let vk_arguments_s = fun bigf args ->
34e49164
C
1689 let iif ii = vk_ii_s bigf ii in
1690 args +> List.map (fun (e, ii) -> vk_argument_s bigf e, iif ii)
1691
8f657093
C
1692let vk_inis_s = fun bigf inis ->
1693 let iif ii = vk_ii_s bigf ii in
1694 inis +> List.map (fun (e, ii) -> vk_ini_s bigf e, iif ii)
1695
ae4735db 1696let vk_params_s = fun bigf args ->
34e49164
C
1697 let iif ii = vk_ii_s bigf ii in
1698 args +> List.map (fun (p,ii) -> vk_param_s bigf p, iif ii)
1699
ae4735db 1700let vk_cst_s = fun bigf (cst, ii) ->
34e49164
C
1701 let iif ii = vk_ii_s bigf ii in
1702 (match cst with
ae4735db 1703 | Left cst -> Left cst
34e49164
C
1704 | Right s -> Right s
1705 ), iif ii
c491d8ee
C
1706
1707(* ------------------------------------------------------------------------ *)
1708
1709let vk_splitted_s element = fun bigf args_splitted ->
1710 let iif ii = vk_ii_s bigf ii in
1711 args_splitted +> List.map (function
1712 | Left arg -> Left (element bigf arg)
1713 | Right ii -> Right (iif ii)
1714 )
1715
1716let vk_args_splitted_s = vk_splitted_s vk_argument_s
1717let vk_params_splitted_s = vk_splitted_s vk_param_s
1718let vk_define_params_splitted_s =
1719 vk_splitted_s (fun bigf (s,ii) -> (s,vk_ii_s bigf ii))
1720let vk_enum_fields_splitted_s = vk_splitted_s vk_oneEnum_s
1721let vk_inis_splitted_s = vk_splitted_s vk_ini_s