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