1 (* Yoann Padioleau, Julia Lawall
3 * Copyright (C) 2006, 2007, 2008, 2009 Ecole des Mines de Nantes and DIKU
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License (GPL)
7 * version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * file license.txt for more details.
18 module F
= Control_flow_c
20 (*****************************************************************************)
22 (*****************************************************************************)
23 let pr2, pr2_once
= Common.mk_pr2_wrappers
Flag_parsing_c.verbose_unparsing
25 (*****************************************************************************)
27 (*****************************************************************************)
29 type type_with_ident
=
30 (string * Ast_c.info
) option ->
31 (Ast_c.storage
* Ast_c.il
) option ->
33 Ast_c.attribute list
-> unit
35 type 'a printer
= 'a
-> unit
37 type pretty_printers
= {
38 expression
: Ast_c.expression printer
;
39 arg_list
: (Ast_c.argument
Ast_c.wrap2 list
) printer
;
40 statement
: Ast_c.statement printer
;
41 decl
: Ast_c.declaration printer
;
42 init
: Ast_c.initialiser printer
;
43 param
: Ast_c.parameterType printer
;
44 ty
: Ast_c.fullType printer
;
45 type_with_ident
: type_with_ident
;
46 toplevel
: Ast_c.toplevel printer
;
47 flow
: Control_flow_c.node printer
52 (*****************************************************************************)
54 (* This module is used by unparse_c, but because unparse_c have also
55 * the list of tokens, pretty_print_c could be useless in the future
56 * (except that the ast_c have some fake tokens not present in the list
57 * of tokens so it's still useful). But this module is also useful to
58 * unparse C when you don't have the ordered list of tokens separately,
59 * or tokens without position information, for instance when you want
60 * to pretty print some piece of C that was generated, or some
61 * abstract-lined piece of code, etc. *)
63 let mk_pretty_printers
65 ~pr_nl ~pr_indent ~pr_outdent ~pr_unindent
67 let start_block () = pr_nl
(); pr_indent
() in
68 let end_block () = pr_unindent
(); pr_nl
() in
70 let indent_if_needed st f
=
71 match Ast_c.unwrap_st st
with
72 Compound _
-> pr_space
(); f
()
74 (*no newline at the end - someone else will do that*)
75 start_block(); f
(); pr_unindent
() in
80 let rec pp_expression = fun ((exp
, typ
), ii
) ->
82 | Ident
(ident
), [] -> pp_name ident
83 (* only a MultiString can have multiple ii *)
84 | Constant
(MultiString _
), is
-> is
+> List.iter pr_elem
85 | Constant
(c
), [i
] -> pr_elem i
86 | FunCall
(e
, es
), [i1
;i2
] ->
87 pp_expression e
; pr_elem i1
;
91 | CondExpr
(e1
, e2
, e3
), [i1
;i2
] ->
92 pp_expression e1
; pr_space
(); pr_elem i1
; pr_space
();
93 do_option
(function x
-> pp_expression x
; pr_space
()) e2
; pr_elem i2
;
95 | Sequence
(e1
, e2
), [i
] ->
96 pp_expression e1
; pr_elem i
; pr_space
(); pp_expression e2
97 | Assignment
(e1
, op
, e2
), [i
] ->
98 pp_expression e1
; pr_space
(); pr_elem i
; pr_space
(); pp_expression e2
100 | Postfix
(e
, op
), [i
] -> pp_expression e
; pr_elem i
;
101 | Infix
(e
, op
), [i
] -> pr_elem i
; pp_expression e
;
102 | Unary
(e
, op
), [i
] -> pr_elem i
; pp_expression e
103 | Binary
(e1
, op
, e2
), [i
] ->
104 pp_expression e1
; pr_space
(); pr_elem i
; pr_space
(); pp_expression e2
106 | ArrayAccess
(e1
, e2
), [i1
;i2
] ->
107 pp_expression e1
; pr_elem i1
; pp_expression e2
; pr_elem i2
108 | RecordAccess
(e
, name
), [i1
] ->
109 pp_expression e
; pr_elem i1
; pp_name name
;
110 | RecordPtAccess
(e
, name
), [i1
] ->
111 pp_expression e
; pr_elem i1
; pp_name name
;
113 | SizeOfExpr
(e
), [i
] ->
115 (match Ast_c.unwrap e
with
116 ParenExpr
(e
), _
-> ()
119 | SizeOfType
(t
), [i1
;i2
;i3
] ->
120 pr_elem i1
; pr_elem i2
; pp_type t
; pr_elem i3
121 | Cast
(t
, e
), [i1
;i2
] ->
122 pr_elem i1
; pp_type t
; pr_elem i2
; pp_expression e
124 | StatementExpr
(statxs
, [ii1
;ii2
]), [i1
;i2
] ->
127 statxs
+> List.iter pp_statement_seq
;
130 | Constructor
(t
, xs
), lp
::rp
::i1
::i2
::iicommaopt
->
135 xs
+> List.iter
(fun (x
, ii
) ->
136 assert (List.length ii
<= 1);
137 ii
+> List.iter
(function x
-> pr_elem x
; pr_space
());
140 iicommaopt
+> List.iter pr_elem
;
143 | ParenExpr
(e
), [i1
;i2
] -> pr_elem i1
; pp_expression e
; pr_elem i2
;
145 | (Ident
(_
) | Constant _
| FunCall
(_
,_
) | CondExpr
(_
,_
,_
)
148 | Postfix
(_
,_
) | Infix
(_
,_
) | Unary
(_
,_
) | Binary
(_
,_
,_
)
149 | ArrayAccess
(_
,_
) | RecordAccess
(_
,_
) | RecordPtAccess
(_
,_
)
150 | SizeOfExpr
(_
) | SizeOfType
(_
) | Cast
(_
,_
)
151 | StatementExpr
(_
) | Constructor _
152 | ParenExpr
(_
)),_
-> raise Impossible
155 if !Flag_parsing_c.pretty_print_type_info
157 pr_elem
(Ast_c.fakeInfo
() +> Ast_c.rewrap_str
"/*");
159 (fun (ty
,_test
) -> ty
+>
161 (fun (x
,l
) -> pp_type x
;
163 Ast_c.LocalVar _
-> ", local"
165 pr_elem
(Ast_c.fakeInfo
() +> Ast_c.rewrap_str
s)));
166 pr_elem
(Ast_c.fakeInfo
() +> Ast_c.rewrap_str
"*/");
170 es
+> List.iter
(fun (e
, opt
) ->
171 assert (List.length opt
<= 1); (* opt must be a comma? *)
172 opt
+> List.iter
(function x
-> pr_elem x
; pr_space
());
175 and pp_argument argument
=
176 let rec pp_action (ActMisc ii
) = ii
+> List.iter pr_elem
in
178 | Left e
-> pp_expression e
181 | ArgType param
-> pp_param param
182 | ArgAction action
-> pp_action action
)
184 (* ---------------------- *)
185 and pp_name
= function
186 | RegularName
(s, ii
) ->
187 let (i1
) = Common.tuple_of_list1 ii
in
189 | CppConcatenatedName xs
->
190 xs
+> List.iter
(fun ((x
,ii1
), ii2
) ->
191 ii2
+> List.iter pr_elem
;
192 ii1
+> List.iter pr_elem
;
194 | CppVariadicName
(s, ii
) ->
195 ii
+> List.iter pr_elem
196 | CppIdentBuilder
((s,iis
), xs
) ->
197 let (iis
, iop
, icp
) = Common.tuple_of_list3 iis
in
200 xs
+> List.iter
(fun ((x
,iix
), iicomma
) ->
201 iicomma
+> List.iter pr_elem
;
202 iix
+> List.iter pr_elem
;
206 (* ---------------------- *)
207 and pp_statement
= fun st
->
208 match Ast_c.get_st_and_ii st
with
209 | Labeled
(Label
(name
, st
)), ii
->
210 let (i2
) = Common.tuple_of_list1 ii
in
211 pr_outdent
(); pp_name name
; pr_elem i2
; pr_nl
(); pp_statement st
212 | Labeled
(Case
(e
, st
)), [i1
;i2
] ->
214 pr_elem i1
; pp_expression e
; pr_elem i2
; pr_nl
(); pr_indent
();
216 | Labeled
(CaseRange
(e
, e2
, st
)), [i1
;i2
;i3
] ->
218 pr_elem i1
; pp_expression e
; pr_elem i2
; pp_expression e2
; pr_elem i3
;
219 pr_nl
(); pr_indent
();
221 | Labeled
(Default st
), [i1
;i2
] ->
222 pr_unindent
(); pr_elem i1
; pr_elem i2
; pr_nl
(); pr_indent
();
224 | Compound statxs
, [i1
;i2
] ->
225 pr_elem i1
; start_block();
226 statxs
+> Common.print_between pr_nl pp_statement_seq
;
227 end_block(); pr_elem i2
;
229 | ExprStatement
(None
), [i
] -> pr_elem i
;
230 | ExprStatement
(None
), [] -> ()
231 | ExprStatement
(Some e
), [i
] -> pp_expression e
; pr_elem i
232 (* the last ExprStatement of a for does not have a trailing
233 ';' hence the [] for ii *)
234 | ExprStatement
(Some e
), [] -> pp_expression e
;
235 | Selection
(If
(e
, st1
, st2
)), i1
::i2
::i3
::is
->
236 pr_elem i1
; pr_space
(); pr_elem i2
; pp_expression e
; pr_elem i3
;
237 indent_if_needed st1
(function _
-> pp_statement st1
);
238 (match (Ast_c.get_st_and_ii st2
, is
) with
239 | ((ExprStatement None
, []), []) -> ()
240 | ((ExprStatement None
, []), [iifakend
]) -> pr_elem iifakend
241 | _st2
, [i4
;iifakend
] -> pr_elem i4
;
242 indent_if_needed st2
(function _
-> pp_statement st2
);
244 | x
-> raise Impossible
246 | Selection
(Switch
(e
, st
)), [i1
;i2
;i3
;iifakend
] ->
247 pr_elem i1
; pr_space
(); pr_elem i2
; pp_expression e
; pr_elem i3
;
248 indent_if_needed st
(function _
-> pp_statement st
); pr_elem iifakend
249 | Iteration
(While
(e
, st
)), [i1
;i2
;i3
;iifakend
] ->
250 pr_elem i1
; pr_space
(); pr_elem i2
; pp_expression e
; pr_elem i3
;
251 indent_if_needed st
(function _
-> pp_statement st
); pr_elem iifakend
252 | Iteration
(DoWhile
(st
, e
)), [i1
;i2
;i3
;i4
;i5
;iifakend
] ->
254 indent_if_needed st
(function _
-> pp_statement st
);
255 pr_elem i2
; pr_elem i3
; pp_expression e
;
256 pr_elem i4
; pr_elem i5
;
260 | Iteration
(For
((e1opt
,il1
),(e2opt
,il2
),(e3opt
, il3
),st
)),
261 [i1
;i2
;i3
;iifakend
] ->
263 pr_elem i1
; pr_space
();
265 pp_statement
(Ast_c.mk_st
(ExprStatement e1opt
) il1
);
266 pp_statement
(Ast_c.mk_st
(ExprStatement e2opt
) il2
);
268 pp_statement
(Ast_c.mk_st
(ExprStatement e3opt
) il3
);
270 indent_if_needed st
(function _
-> pp_statement st
);
273 | Iteration
(MacroIteration
(s,es
,st
)), [i1
;i2
;i3
;iifakend
] ->
274 pr_elem i1
; pr_space
();
277 es
+> List.iter
(fun (e
, opt
) ->
278 assert (List.length opt
<= 1);
279 opt
+> List.iter pr_elem
;
284 indent_if_needed st
(function _
-> pp_statement st
);
287 | Jump
(Goto name
), ii
->
288 let (i1
, i3
) = Common.tuple_of_list2 ii
in
289 pr_elem i1
; pr_space
(); pp_name name
; pr_elem i3
;
290 | Jump
((Continue
|Break
|Return
)), [i1
;i2
] -> pr_elem i1
; pr_elem i2
;
291 | Jump
(ReturnExpr e
), [i1
;i2
] ->
292 pr_elem i1
; pr_space
(); pp_expression e
; pr_elem i2
293 | Jump
(GotoComputed e
), [i1
;i2
;i3
] ->
294 pr_elem i1
; pr_elem i2
; pp_expression e
; pr_elem i3
296 | Decl decl
, [] -> pp_decl decl
299 | [iasm
;iopar
;icpar
;iptvirg
] ->
300 pr_elem iasm
; pr_elem iopar
;
302 pr_elem icpar
; pr_elem iptvirg
303 | [iasm
;ivolatile
;iopar
;icpar
;iptvirg
] ->
304 pr_elem iasm
; pr_elem ivolatile
; pr_elem iopar
;
306 pr_elem icpar
; pr_elem iptvirg
307 | _
-> raise Impossible
310 | NestedFunc def
, ii
->
314 ii
+> List.iter pr_elem
;
316 | (Labeled
(Case
(_
,_
))
317 | Labeled
(CaseRange
(_
,_
,_
)) | Labeled
(Default _
)
318 | Compound _
| ExprStatement _
319 | Selection
(If
(_
, _
, _
)) | Selection
(Switch
(_
, _
))
320 | Iteration
(While
(_
, _
)) | Iteration
(DoWhile
(_
, _
))
321 | Iteration
(For
((_
,_
), (_
,_
), (_
, _
), _
))
322 | Iteration
(MacroIteration
(_
,_
,_
))
323 | Jump
((Continue
|Break
|Return
)) | Jump
(ReturnExpr _
)
324 | Jump
(GotoComputed _
)
326 ), _
-> raise Impossible
328 and pp_statement_seq
= function
329 | StmtElem st
-> pp_statement st
330 | IfdefStmt ifdef
-> pp_ifdef ifdef
331 | CppDirectiveStmt cpp
-> pp_directive cpp
332 | IfdefStmt2
(ifdef
, xxs
) -> pp_ifdef_tree_sequence ifdef xxs
334 (* ifdef XXX elsif YYY elsif ZZZ endif *)
335 and pp_ifdef_tree_sequence ifdef xxs
=
339 pp_ifdef_tree_sequence_aux ifxs xxs
340 | _
-> raise Impossible
342 (* XXX elsif YYY elsif ZZZ endif *)
343 and pp_ifdef_tree_sequence_aux ifdefs xxs
=
344 Common.zip ifdefs xxs
+> List.iter
(fun (ifdef
, xs
) ->
345 xs
+> List.iter pp_statement_seq
;
353 (* ---------------------- *)
354 and pp_asmbody
(string_list
, colon_list
) =
355 string_list
+> List.iter pr_elem
;
356 colon_list
+> List.iter
(fun (Colon xs
, ii
) ->
357 ii
+> List.iter pr_elem
;
358 xs
+> List.iter
(fun (x
,iicomma
) ->
359 assert ((List.length iicomma
) <= 1);
360 iicomma
+> List.iter
(function x
-> pr_elem x
; pr_space
());
362 | ColonMisc
, ii
-> ii
+> List.iter pr_elem
;
363 | ColonExpr e
, [istring
;iopar
;icpar
] ->
368 | (ColonExpr _
), _
-> raise Impossible
)
372 (* ---------------------- *)
377 pp_type_with_ident_rest
384 and (pp_type_with_ident
:
385 (string * info
) option -> (storage
* il
) option ->
386 fullType
-> attribute list
->
388 fun ident sto ft attrs
->
390 (match (ident
, Ast_c.unwrap_typeC ft
) with
391 (Some _
,_
) | (_
,Pointer _
) -> pr_space
()
393 pp_type_with_ident_rest ident ft attrs
396 and (pp_base_type
: fullType
-> (storage
* il
) option -> unit) =
397 fun (qu
, (ty
, iity
)) sto
->
400 | None
-> [] | Some
(s, iis
) -> (*assert (List.length iis = 1);*) iis
402 let print_sto_qu (sto
, (qu
, iiqu
)) =
403 let all_ii = get_sto sto
++ iiqu
in
405 +> List.sort
Ast_c.compare_pos
406 +> Common.print_between pr_space pr_elem
409 let print_sto_qu_ty (sto
, (qu
, iiqu
), iity
) =
410 let all_ii = get_sto sto
++ iiqu
++ iity
in
411 let all_ii2 = all_ii +> List.sort
Ast_c.compare_pos
in
415 (* TODO in fact for pointer, the qualifier is after the type
416 * cf -test strangeorder
419 all_ii2 +> Common.print_between pr_space pr_elem
421 else all_ii2 +> Common.print_between pr_space pr_elem
425 | (Pointer t
, [i
]) -> pp_base_type t sto
426 | (ParenType t
, _
) -> pp_base_type t sto
427 | (Array
(eopt
, t
), [i1
;i2
]) -> pp_base_type t sto
428 | (FunctionType
(returnt
, paramst
), [i1
;i2
]) ->
429 pp_base_type returnt sto
432 | (StructUnion
(su
, sopt
, fields
),iis
) ->
433 print_sto_qu (sto
, qu
);
436 | Some
s , [i1
;i2
;i3
;i4
] ->
437 pr_elem i1
; pr_elem i2
; pr_elem i3
;
438 | None
, [i1
;i2
;i3
] ->
439 pr_elem i1
; pr_elem i2
;
440 | x
-> raise Impossible
447 | DeclarationField
(FieldDeclList
(onefield_multivars
,iiptvirg
))->
448 (match onefield_multivars
with
450 (* handling the first var. Special case, with the
451 first var, we print the whole type *)
454 | (Simple
(nameopt
, typ
)), iivirg
->
455 (* first var cant have a preceding ',' *)
456 assert (List.length iivirg
=|= 0);
460 | Some name
-> Some
(get_s_and_info_of_name name
)
462 pp_type_with_ident
identinfo None typ
Ast_c.noattr
;
464 | (BitField
(nameopt
, typ
, iidot
, expr
)), iivirg
->
465 (* first var cant have a preceding ',' *)
466 assert (List.length iivirg
=|= 0);
471 let (s, is
) = get_s_and_info_of_name name
in
473 (Some
(s, is
)) None typ
Ast_c.noattr
;
478 ); (* match x, first onefield_multivars *)
481 xs
+> List.iter
(function
482 | (Simple
(nameopt
, typ
)), iivirg
->
483 iivirg
+> List.iter pr_elem
;
487 | Some name
-> Some
(get_s_and_info_of_name name
)
489 pp_type_with_ident_rest
identinfo typ
Ast_c.noattr
491 | (BitField
(nameopt
, typ
, iidot
, expr
)), iivirg
->
492 iivirg
+> List.iter pr_elem
;
495 let (s,is
) = get_s_and_info_of_name name
in
496 pp_type_with_ident_rest
497 (Some
(s, is
)) typ
Ast_c.noattr
;
500 | x
-> raise Impossible
501 )); (* iter other vars *)
503 | [] -> raise Impossible
504 ); (* onefield_multivars *)
505 assert (List.length iiptvirg
=|= 1);
506 iiptvirg
+> List.iter pr_elem
;
509 | MacroDeclField
((s, es
), ii
) ->
510 let (iis
, lp
, rp
, iiend
, ifakestart
) =
511 Common.tuple_of_list5 ii
in
512 (* iis::lp::rp::iiend::ifakestart::iisto
513 iisto +> List.iter pr_elem; (* static and const *)
518 es
+> List.iter
(fun (e
, opt
) ->
519 assert (List.length opt
<= 1);
520 opt
+> List.iter pr_elem
;
529 | EmptyField iipttvirg_when_emptyfield
->
530 pr_elem iipttvirg_when_emptyfield
532 | CppDirectiveStruct cpp
-> pp_directive cpp
533 | IfdefStruct ifdef
-> pp_ifdef ifdef
537 | Some
s , [i1
;i2
;i3
;i4
] -> pr_elem i4
538 | None
, [i1
;i2
;i3
] -> pr_elem i3
;
539 | x
-> raise Impossible
544 | (Enum
(sopt
, enumt
), iis
) ->
545 print_sto_qu (sto
, qu
);
547 (match sopt
, iis
with
548 | (Some
s, ([i1
;i2
;i3
;i4
]|[i1
;i2
;i3
;i4
;_
])) ->
549 pr_elem i1
; pr_elem i2
; pr_elem i3
;
550 | (None
, ([i1
;i2
;i3
]|[i1
;i2
;i3
;_
])) ->
551 pr_elem i1
; pr_elem i2
552 | x
-> raise Impossible
555 enumt
+> List.iter
(fun ((name
, eopt
), iicomma
) ->
556 assert (List.length iicomma
<= 1);
557 iicomma
+> List.iter
(function x
-> pr_elem x
; pr_space
());
559 eopt
+> Common.do_option
(fun (ieq
, e
) ->
564 (match sopt
, iis
with
565 | (Some
s, [i1
;i2
;i3
;i4
]) -> pr_elem i4
566 | (Some
s, [i1
;i2
;i3
;i4
;i5
]) ->
567 pr_elem i5
; pr_elem i4
(* trailing comma *)
568 | (None
, [i1
;i2
;i3
]) -> pr_elem i3
569 | (None
, [i1
;i2
;i3
;i4
]) ->
570 pr_elem i4
; pr_elem i3
(* trailing comma *)
573 | x
-> raise Impossible
577 | (BaseType _
, iis
) ->
578 print_sto_qu_ty (sto
, qu
, iis
);
580 | (StructUnionName
(s, structunion
), iis
) ->
581 assert (List.length iis
=|= 2);
582 print_sto_qu_ty (sto
, qu
, iis
);
584 | (EnumName
s, iis
) ->
585 assert (List.length iis
=|= 2);
586 print_sto_qu_ty (sto
, qu
, iis
);
588 | (TypeName
(name
,typ
), noii
) ->
590 let (_s
, iis
) = get_s_and_info_of_name name
in
591 print_sto_qu_ty (sto
, qu
, [iis
]);
593 if !Flag_parsing_c.pretty_print_typedef_value
595 pr_elem
(Ast_c.fakeInfo
() +> Ast_c.rewrap_str
"{*");
596 typ
+> Common.do_option
(fun typ
->
599 pr_elem
(Ast_c.fakeInfo
() +> Ast_c.rewrap_str
"*}");
602 | (TypeOfExpr
(e
), iis
) ->
603 print_sto_qu (sto
, qu
);
605 | [itypeof
;iopar
;icpar
] ->
606 pr_elem itypeof
; pr_elem iopar
;
609 | _
-> raise Impossible
612 | (TypeOfType
(t
), iis
) ->
613 print_sto_qu (sto
, qu
);
615 | [itypeof
;iopar
;icpar
] ->
616 pr_elem itypeof
; pr_elem iopar
;
619 | _
-> raise Impossible
622 | (Pointer _
| (*ParenType _ |*) Array _
| FunctionType _
623 (* | StructUnion _ | Enum _ | BaseType _ *)
624 (* | StructUnionName _ | EnumName _ | TypeName _ *)
625 (* | TypeOfExpr _ | TypeOfType _ *)
626 ), _
-> raise Impossible
630 (* used because of DeclList, in int i,*j[23]; we dont print anymore the
632 and (pp_type_with_ident_rest
: (string * info
) option ->
633 fullType
-> attribute list
-> unit) =
635 fun ident
(((qu
, iiqu
), (ty
, iity
)) as fullt
) attrs
->
637 let print_ident ident
= Common.do_option
(fun (s, iis
) ->
638 (* XXX attrs +> pp_attributes pr_elem pr_space; *)
644 (* the work is to do in base_type !! *)
645 | (BaseType _
, iis
) -> print_ident ident
646 | (Enum
(sopt
, enumt
), iis
) -> print_ident ident
647 | (StructUnion
(_
, sopt
, fields
),iis
) -> print_ident ident
648 | (StructUnionName
(s, structunion
), iis
) -> print_ident ident
649 | (EnumName
s, iis
) -> print_ident ident
650 | (TypeName
(_name
,_typ
), iis
) -> print_ident ident
651 | (TypeOfExpr
(e
), iis
) -> print_ident ident
652 | (TypeOfType
(e
), iis
) -> print_ident ident
656 | (Pointer t
, [i
]) ->
657 (* subtil: void ( *done)(int i) is a Pointer
658 (FunctionType (return=void, params=int i) *)
659 (*WRONG I THINK, use left & right function *)
660 (* bug: pp_type_with_ident_rest None t; print_ident ident *)
662 iiqu
+> List.iter pr_elem
; (* le const est forcement apres le '*' *)
663 pp_type_with_ident_rest ident t attrs
;
665 (* ugly special case ... todo? maybe sufficient in practice *)
666 | (ParenType ttop
, [i1
;i2
]) ->
667 (match Ast_c.get_ty_and_ii ttop
with
668 | (_q1
, (Pointer t2
, [ipointer
])) ->
669 (match Ast_c.get_ty_and_ii t2
with
670 | (q2
, (FunctionType t
, ii3
)) ->
672 pp_type_left
(q2
, mk_tybis
(FunctionType t
) ii3
);
677 pp_type_right
(q2
, mk_tybis
(FunctionType t
) ii3
);
679 pr2 "PB PARENTYPE ZARB, I forget about the ()";
680 pp_type_with_ident_rest ident ttop attrs
;
682 (* another ugly special case *)
683 | _q1
, (Array
(eopt
,t2
), [iarray1
;iarray2
]) ->
684 (match Ast_c.get_ty_and_ii t2
with
685 | (_q2
, (Pointer t3
, [ipointer
])) ->
686 (match Ast_c.get_ty_and_ii t3
with
687 | (q3
, (FunctionType t
, iifunc
)) ->
689 pp_type_left
(q3
, mk_tybis
(FunctionType t
) iifunc
);
694 do_option
pp_expression eopt
;
697 pp_type_right
(q3
, mk_tybis
(FunctionType t
) iifunc
)
699 pr2 "PB PARENTYPE ZARB, I forget about the ()";
700 pp_type_with_ident_rest ident ttop attrs
;
703 pr2 "PB PARENTYPE ZARB, I forget about the ()";
704 pp_type_with_ident_rest ident ttop attrs
;
708 pr2 "PB PARENTYPE ZARB, I forget about the ()";
709 pp_type_with_ident_rest ident ttop attrs
;
713 | (Array
(eopt
, t
), [i1
;i2
]) ->
716 iiqu
+> List.iter pr_elem
;
722 | (FunctionType
(returnt
, paramst
), [i1
;i2
]) ->
725 iiqu
+> List.iter pr_elem
;
731 | (FunctionType _
| Array _
| ParenType _
| Pointer _
), _
->
735 and (pp_type_left
: fullType
-> unit) =
736 fun ((qu
, iiqu
), (ty
, iity
)) ->
738 | (Pointer t
, [i
]) ->
740 iiqu
+> List.iter pr_elem
; (* le const est forcement apres le '*' *)
743 | (Array
(eopt
, t
), [i1
;i2
]) -> pp_type_left t
744 | (FunctionType
(returnt
, paramst
), [i1
;i2
]) -> pp_type_left returnt
746 | (ParenType t
, _
) -> failwith
"parenType"
749 | (BaseType _
, iis
) -> ()
750 | (Enum
(sopt
, enumt
), iis
) -> ()
751 | (StructUnion
(_
, sopt
, fields
),iis
) -> ()
752 | (StructUnionName
(s, structunion
), iis
) -> ()
753 | (EnumName
s, iis
) -> ()
754 | (TypeName
(_name
,_typ
), iis
) -> ()
756 | TypeOfType _
, _
-> ()
757 | TypeOfExpr _
, _
-> ()
759 | (FunctionType _
| Array _
| Pointer _
), _
-> raise Impossible
763 let {p_namei
= nameopt
;
764 p_register
= (b
,iib
);
765 p_type
=t
;} = param
in
767 iib
+> List.iter pr_elem
;
773 let (s,i1
) = get_s_and_info_of_name name
in
775 (Some
(s, i1
)) None t
Ast_c.noattr
780 and pp_type_right
(((qu
, iiqu
), (ty
, iity
)) : fullType
) =
782 | (Pointer t
, [i
]) -> pp_type_right t
784 | (Array
(eopt
, t
), [i1
;i2
]) ->
786 eopt
+> do_option
pp_expression;
790 | (ParenType t
, _
) -> failwith
"parenType"
791 | (FunctionType
(returnt
, paramst
), [i1
;i2
]) ->
795 ts
+> List.iter
(fun (param
,iicomma
) ->
796 assert ((List.length iicomma
) <= 1);
797 iicomma
+> List.iter
(function x
-> pr_elem x
; pr_space
());
801 iib
+> List.iter pr_elem
;
805 | (BaseType _
, iis
) -> ()
806 | (Enum
(sopt
, enumt
), iis
) -> ()
807 | (StructUnion
(_
, sopt
, fields
),iis
)-> ()
808 | (StructUnionName
(s, structunion
), iis
) -> ()
809 | (EnumName
s, iis
) -> ()
810 | (TypeName
(name
,_typ
), iis
) -> ()
812 | TypeOfType _
, _
-> ()
813 | TypeOfExpr _
, _
-> ()
815 | (FunctionType _
| Array _
| Pointer _
), _
-> raise Impossible
818 pp_type_with_ident None None t
Ast_c.noattr
820 (* ---------------------- *)
821 and pp_decl
= function
822 | DeclList
((({v_namei
= var
;
827 iivirg
::ifakestart
::iisto
) ->
831 (* old: iisto +> List.iter pr_elem; *)
834 (* handling the first var. Special case, we print the whole type *)
836 | Some
(name
, iniopt
) ->
837 let (s,iis
) = get_s_and_info_of_name name
in
839 (Some
(s, iis
)) (Some
(storage
, iisto
))
841 iniopt
+> do_option
(fun (iini
, init
) ->
844 | None
-> pp_type returnType
847 (* for other vars, we just call pp_type_with_ident_rest. *)
848 xs
+> List.iter
(function
849 | ({v_namei
= Some
(name
, iniopt
);
851 v_storage
= storage2
;
855 let (s,iis
) = get_s_and_info_of_name name
in
856 assert (storage2
=*= storage
);
857 iivirg
+> List.iter pr_elem
;
858 pp_type_with_ident_rest
859 (Some
(s, iis
)) returnType attrs
;
860 iniopt
+> do_option
(fun (iini
, init
) ->
861 pr_elem iini
; pp_init init
865 | x
-> raise Impossible
870 | MacroDecl
((s, es
), iis
::lp
::rp
::iiend
::ifakestart
::iisto
) ->
872 iisto
+> List.iter pr_elem
; (* static and const *)
875 es
+> List.iter
(fun (e
, opt
) ->
876 assert (List.length opt
<= 1);
877 opt
+> List.iter pr_elem
;
884 | (DeclList
(_
, _
) | (MacroDecl _
)) -> raise Impossible
887 (* ---------------------- *)
888 and pp_init
(init
, iinit
) =
889 match init
, iinit
with
890 | InitExpr e
, [] -> pp_expression e
;
891 | InitList xs
, i1
::i2
::iicommaopt
->
892 pr_elem i1
; start_block();
893 xs
+> List.iter
(fun (x
, ii
) ->
894 assert (List.length ii
<= 1);
895 ii
+> List.iter
(function e
-> pr_elem e
; pr_nl
());
898 iicommaopt
+> List.iter pr_elem
;
902 | InitDesignators
(xs
, initialiser
), [i1
] -> (* : *)
903 xs
+> List.iter pp_designator
;
907 (* no use of '=' in the "Old" style *)
908 | InitFieldOld
(string, initialiser
), [i1
;i2
] -> (* label: in oldgcc *)
909 pr_elem i1
; pr_elem i2
; pp_init initialiser
910 | InitIndexOld
(expression
, initialiser
), [i1
;i2
] -> (* [1] in oldgcc *)
911 pr_elem i1
; pp_expression expression
; pr_elem i2
;
914 | (InitIndexOld _
| InitFieldOld _
| InitDesignators _
915 | InitList _
| InitExpr _
916 ), _
-> raise Impossible
920 and pp_designator
= function
921 | DesignatorField
(s), [i1
; i2
] ->
922 pr_elem i1
; pr_elem i2
;
923 | DesignatorIndex
(expression
), [i1
;i2
] ->
924 pr_elem i1
; pp_expression expression
; pr_elem i2
;
926 | DesignatorRange
(e1
, e2
), [iocro
;iellipsis
;iccro
] ->
927 pr_elem iocro
; pp_expression e1
; pr_elem iellipsis
;
928 pp_expression e2
; pr_elem iccro
;
930 | (DesignatorField _
| DesignatorIndex _
| DesignatorRange _
931 ), _
-> raise Impossible
934 (* ---------------------- *)
935 and pp_attributes pr_elem pr_space attrs
=
936 attrs
+> List.iter
(fun (attr
, ii
) ->
937 ii
+> List.iter pr_elem
;
940 (* ---------------------- *)
942 let defbis, ii
= def
in
944 | iifunc1
::iifunc2
::i1
::i2
::ifakestart
::isto
->
946 f_type
= (returnt
, (paramst
, (b
, iib
)));
954 pp_type_with_ident None
(Some
(sto
, isto
))
955 returnt
Ast_c.noattr
;
957 pp_attributes pr_elem pr_space attrs
;
962 (* not anymore, cf tests/optional_name_parameter and
963 macro_parameter_shortcut.c
965 | [(((bool, None, t), ii_b_s), iicomma)] ->
968 | qu, (BaseType Void, ii) -> true
971 assert (null iicomma);
972 assert (null ii_b_s);
973 pp_type_with_ident None None t
976 paramst +> List.iter (fun (((bool, s, t), ii_b_s), iicomma) ->
977 iicomma +> List.iter pr_elem;
979 (match b, s, ii_b_s with
980 | false, Some s, [i1] ->
981 pp_type_with_ident (Some (s, i1)) None t;
982 | true, Some s, [i1;i2] ->
984 pp_type_with_ident (Some (s, i2)) None t;
986 (* in definition we have name for params, except when f(void) *)
987 | _
, None
, _
-> raise Impossible
990 | _
-> raise Impossible
993 (* normally ii represent the ",..." but it is also abused
994 with the f(void) case *)
995 (* assert (List.length iib <= 2);*)
996 iib
+> List.iter pr_elem
;
999 paramst
+> List.iter
(fun (param
,iicomma
) ->
1000 assert ((List.length iicomma
) <= 1);
1001 iicomma
+> List.iter
(function x
-> pr_elem x
; pr_space
());
1005 iib
+> List.iter pr_elem
;
1010 statxs
+> List.iter pp_statement_seq
;
1012 | _
-> raise Impossible
1016 (* ---------------------- *)
1018 and pp_ifdef ifdef
=
1020 | IfdefDirective
(ifdef
, ii
) ->
1021 List.iter pr_elem ii
1024 and pp_directive
= function
1025 | Include
{i_include
= (s, ii
);} ->
1026 let (i1
,i2
) = Common.tuple_of_list2 ii
in
1027 pr_elem i1
; pr_elem i2
1028 | Define
((s,ii
), (defkind
, defval
)) ->
1029 let (idefine
,iident
,ieol
) = Common.tuple_of_list3 ii
in
1033 let define_val = function
1034 | DefineExpr e
-> pp_expression e
1035 | DefineStmt st
-> pp_statement st
1036 | DefineDoWhileZero
((st
,e
), ii
) ->
1038 | [ido
;iwhile
;iopar
;icpar
] ->
1041 pr_elem iwhile
; pr_elem iopar
;
1044 | _
-> raise Impossible
1046 | DefineFunction def
-> pp_def def
1048 | DefineType ty
-> pp_type ty
1049 | DefineText
(s, ii
) -> List.iter pr_elem ii
1051 | DefineInit ini
-> pp_init ini
1053 | DefineTodo
-> pr2 "DefineTodo"
1057 | DefineFunc
(params
, ii
) ->
1058 let (i1
,i2
) = tuple_of_list2 ii
in
1060 params
+> List.iter
(fun ((s,iis
), iicomma
) ->
1061 assert (List.length iicomma
<= 1);
1062 iicomma
+> List.iter pr_elem
;
1063 iis
+> List.iter pr_elem
;
1071 List.iter pr_elem ii
1072 | PragmaAndCo
(ii
) ->
1073 List.iter pr_elem ii
in
1078 let pp_toplevel = function
1079 | Declaration decl
-> pp_decl decl
1080 | Definition def
-> pp_def def
1082 | CppTop directive
-> pp_directive directive
1085 | MacroTop
(s, es
, [i1
;i2
;i3
;i4
]) ->
1088 es
+> List.iter
(fun (e
, opt
) ->
1089 assert (List.length opt
<= 1);
1090 opt
+> List.iter pr_elem
;
1097 | EmptyDef ii
-> ii
+> List.iter pr_elem
1098 | NotParsedCorrectly ii
->
1099 assert (List.length ii
>= 1);
1100 ii
+> List.iter pr_elem
1101 | FinalDef info
-> pr_elem
(Ast_c.rewrap_str
"" info
)
1103 | IfdefTop ifdefdir
-> pp_ifdef ifdefdir
1105 | (MacroTop _
) -> raise Impossible
in
1111 match F.unwrap n
with
1112 | F.FunHeader
({f_name
=idb
;
1113 f_type
= (rett
, (paramst
,(isvaargs
,iidotsb
)));
1116 f_attr
= attrs
},ii
) ->
1122 attrs +> List.iter (vk_attribute bigf);
1124 paramst +> List.iter (fun (param, iicomma) ->
1125 vk_param bigf param;
1133 (* vk_decl bigf decl *)
1136 | F.ExprStatement
(st
, (eopt
, ii
)) ->
1137 pp_statement
(Ast_c.mk_st
(ExprStatement eopt
) ii
)
1139 | F.IfHeader
(_
, (e
,ii
))
1140 | F.SwitchHeader
(_
, (e
,ii
))
1141 | F.WhileHeader
(_
, (e
,ii
))
1142 | F.DoWhileTail
(e
,ii
) ->
1150 | F.ForHeader
(_st
, (((e1opt
,i1
), (e2opt
,i2
), (e3opt
,i3
)), ii
)) ->
1152 iif i1; iif i2; iif i3;
1154 e1opt +> do_option (vk_expr bigf);
1155 e2opt +> do_option (vk_expr bigf);
1156 e3opt +> do_option (vk_expr bigf);
1160 | F.MacroIterHeader
(_s
, ((s,es
), ii
)) ->
1163 vk_argument_list bigf es;
1168 | F.ReturnExpr
(_st
, (e
,ii
)) ->
1169 (* iif ii; vk_expr bigf e*)
1173 | F.Case
(_st
, (e
,ii
)) ->
1174 (* iif ii; vk_expr bigf e *)
1177 | F.CaseRange
(_st
, ((e1
, e2
),ii
)) ->
1178 (* iif ii; vk_expr bigf e1; vk_expr bigf e2 *)
1183 | F.CaseNode i
-> ()
1186 (* vk_expr bigf e *)
1189 | F.DefineType ft
->
1190 (* vk_type bigf ft *)
1193 | F.DefineHeader
((s,ii
), (defkind
)) ->
1196 vk_define_kind bigf defkind;
1201 | F.DefineDoWhileZeroHeader
(((),ii
)) ->
1206 | F.Include
{i_include
= (s, ii
);} ->
1211 | F.MacroTop
(s, args
, ii
) ->
1213 vk_argument_list bigf args *)
1217 | F.Break
(st
,((),ii
)) ->
1220 | F.Continue
(st
,((),ii
)) ->
1223 | F.Default
(st
,((),ii
)) ->
1226 | F.Return
(st
,((),ii
)) ->
1229 | F.Goto
(st
, name
, ((),ii
)) ->
1232 | F.Label
(st
, name
, ((),ii
)) ->
1235 | F.EndStatement iopt
->
1236 (* do_option infof iopt *)
1238 | F.DoHeader
(st
, info
) ->
1244 | F.SeqEnd
(i
, info
) ->
1247 | F.SeqStart
(st
, i
, info
) ->
1251 | F.MacroStmt
(st
, ((),ii
)) ->
1254 | F.Asm
(st
, (asmbody
,ii
)) ->
1257 vk_asmbody bigf asmbody
1262 | F.IfdefHeader
(info
) ->
1264 | F.IfdefElse
(info
) ->
1266 | F.IfdefEndif
(info
) ->
1273 | (F.TopNode
|F.EndNode
|
1274 F.ErrorExit
|F.Exit
|F.Enter
|F.LoopFallThroughNode
|F.FallThroughNode
|
1275 F.AfterNode
|F.FalseNode
|F.TrueNode
|F.InLoopNode
|
1280 { expression
= pp_expression;
1281 arg_list
= pp_arg_list
;
1282 statement
= pp_statement
;
1287 type_with_ident
= pp_type_with_ident
;
1288 toplevel
= pp_toplevel;
1292 (*****************************************************************************)
1294 (* Here we do not use (mcode, env). It is a simple C pretty printer. *)
1296 let s = Ast_c.str_of_info info
in
1297 if !Flag_parsing_c.pretty_print_comment_info
then begin
1298 let before = !(info
.comments_tag
).mbefore
in
1299 if not
(null
before) then begin
1301 before +> List.iter
(fun (comment_like
, pinfo
) ->
1302 let s = pinfo
.Common.str
in
1310 let pr_space _
= Format.print_space
()
1313 let pr_indent _
= ()
1314 let pr_outdent _
= ()
1315 let pr_unindent _
= ()
1320 ~
pr_elem ~
pr_space ~
pr_nl ~
pr_outdent ~
pr_indent ~
pr_unindent
1322 let pp_expression_simple = ppc.expression
1323 let pp_statement_simple = ppc.statement
1324 let pp_type_simple = ppc.ty
1325 let pp_init_simple = ppc.init
1326 let pp_toplevel_simple = ppc.toplevel
1327 let pp_flow_simple = ppc.flow
1330 let pp_elem_sp ~
pr_elem ~
pr_space =
1333 ~
pr_nl ~
pr_outdent ~
pr_indent ~
pr_unindent
1335 let pp_expression_gen ~
pr_elem ~
pr_space =
1336 (pp_elem_sp pr_elem pr_space).expression
1338 let pp_arg_list_gen pr_elem pr_space =
1339 (pp_elem_sp pr_elem pr_space).arg_list
1341 let pp_statement_gen ~
pr_elem ~
pr_space =
1342 (pp_elem_sp pr_elem pr_space).statement
1344 let pp_decl_gen pr_elem pr_space =
1345 (pp_elem_sp pr_elem pr_space).decl
1347 let pp_init_gen ~
pr_elem ~
pr_space =
1348 (pp_elem_sp pr_elem pr_space).init
1350 let pp_param_gen ~
pr_elem ~
pr_space =
1351 (pp_elem_sp pr_elem pr_space).param
1353 let pp_type_gen ~
pr_elem ~
pr_space =
1354 (pp_elem_sp pr_elem pr_space).ty
1356 let pp_type_with_ident_gen pr_elem pr_space =
1357 (pp_elem_sp pr_elem pr_space).type_with_ident
1359 let pp_program_gen ~
pr_elem ~
pr_space =
1360 (pp_elem_sp pr_elem pr_space).toplevel
1363 let string_of_expression e
=
1364 Common.format_to_string
(fun () ->
1365 pp_expression_simple e
1368 let string_of_toplevel top
=
1369 Common.format_to_string
(fun () ->
1370 pp_toplevel_simple top
1373 let (debug_info_of_node
:
1374 Ograph_extended.nodei
-> Control_flow_c.cflow
-> string) =
1376 let node = flow#nodes#assoc nodei
in
1377 let s = Common.format_to_string
(fun () ->
1380 let pos = Lib_parsing_c.min_pinfo_of_node
node in
1381 (spf
"%s(n%d)--> %s" (Common.string_of_parse_info_bis
pos) nodei
s)