coccinelle release 0.2.5
[bpt/coccinelle.git] / parsing_c / pretty_print_c.ml
CommitLineData
0708f913 1(* Yoann Padioleau, Julia Lawall
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 and DIKU
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
17open Ast_c
18
978fd7e5
C
19module F = Control_flow_c
20
708f4980
C
21(*****************************************************************************)
22(* Wrappers *)
23(*****************************************************************************)
24let pr2, pr2_once = Common.mk_pr2_wrappers Flag_parsing_c.verbose_unparsing
25
978fd7e5
C
26(*****************************************************************************)
27(* Types *)
708f4980 28(*****************************************************************************)
113803cf 29
978fd7e5 30type type_with_ident =
113803cf 31 (string * Ast_c.info) option ->
ae4735db 32 (Ast_c.storage * Ast_c.il) option ->
978fd7e5
C
33 Ast_c.fullType ->
34 Ast_c.attribute list -> unit
35
ae4735db 36type 'a printer = 'a -> unit
978fd7e5
C
37
38type pretty_printers = {
39 expression : Ast_c.expression printer;
40 arg_list : (Ast_c.argument Ast_c.wrap2 list) printer;
41 statement : Ast_c.statement printer;
42 decl : Ast_c.declaration printer;
413ffc02 43 field : Ast_c.field printer;
190f1acf 44 field_list : Ast_c.field list printer;
978fd7e5
C
45 init : Ast_c.initialiser printer;
46 param : Ast_c.parameterType printer;
413ffc02 47 paramlist : (Ast_c.parameterType Ast_c.wrap2 list) printer;
978fd7e5
C
48 ty : Ast_c.fullType printer;
49 type_with_ident : type_with_ident;
50 toplevel : Ast_c.toplevel printer;
51 flow : Control_flow_c.node printer
52}
113803cf 53
34e49164 54
485bce71 55
34e49164
C
56(*****************************************************************************)
57
58(* This module is used by unparse_c, but because unparse_c have also
113803cf 59 * the list of tokens, pretty_print_c could be useless in the future
34e49164
C
60 * (except that the ast_c have some fake tokens not present in the list
61 * of tokens so it's still useful). But this module is also useful to
62 * unparse C when you don't have the ordered list of tokens separately,
63 * or tokens without position information, for instance when you want
64 * to pretty print some piece of C that was generated, or some
65 * abstract-lined piece of code, etc. *)
66
978fd7e5 67let mk_pretty_printers
ae4735db
C
68 ~pr_elem ~pr_space
69 ~pr_nl ~pr_indent ~pr_outdent ~pr_unindent
978fd7e5 70 =
113803cf
C
71 let start_block () = pr_nl(); pr_indent() in
72 let end_block () = pr_unindent(); pr_nl() in
ae4735db 73
708f4980
C
74 let indent_if_needed st f =
75 match Ast_c.unwrap_st st with
113803cf
C
76 Compound _ -> pr_space(); f()
77 | _ ->
78 (*no newline at the end - someone else will do that*)
79 start_block(); f(); pr_unindent() in
978fd7e5
C
80
81
82
ae4735db
C
83
84 let rec pp_expression = fun ((exp, typ), ii) ->
34e49164 85 (match exp, ii with
b1b2de81 86 | Ident (ident), [] -> pp_name ident
34e49164 87 (* only a MultiString can have multiple ii *)
0708f913 88 | Constant (MultiString _), is -> is +> List.iter pr_elem
ae4735db
C
89 | Constant (c), [i] -> pr_elem i
90 | FunCall (e, es), [i1;i2] ->
91 pp_expression e; pr_elem i1;
113803cf 92 pp_arg_list es;
34e49164 93 pr_elem i2;
ae4735db
C
94
95 | CondExpr (e1, e2, e3), [i1;i2] ->
34e49164 96 pp_expression e1; pr_space(); pr_elem i1; pr_space();
ae4735db 97 do_option (function x -> pp_expression x; pr_space()) e2; pr_elem i2;
aba5c457 98 pr_space(); pp_expression e3
ae4735db 99 | Sequence (e1, e2), [i] ->
34e49164 100 pp_expression e1; pr_elem i; pr_space(); pp_expression e2
ae4735db 101 | Assignment (e1, op, e2), [i] ->
34e49164 102 pp_expression e1; pr_space(); pr_elem i; pr_space(); pp_expression e2
ae4735db 103
34e49164
C
104 | Postfix (e, op), [i] -> pp_expression e; pr_elem i;
105 | Infix (e, op), [i] -> pr_elem i; pp_expression e;
106 | Unary (e, op), [i] -> pr_elem i; pp_expression e
ae4735db 107 | Binary (e1, op, e2), [i] ->
34e49164 108 pp_expression e1; pr_space(); pr_elem i; pr_space(); pp_expression e2
ae4735db
C
109
110 | ArrayAccess (e1, e2), [i1;i2] ->
34e49164 111 pp_expression e1; pr_elem i1; pp_expression e2; pr_elem i2
ae4735db 112 | RecordAccess (e, name), [i1] ->
b1b2de81 113 pp_expression e; pr_elem i1; pp_name name;
ae4735db 114 | RecordPtAccess (e, name), [i1] ->
b1b2de81 115 pp_expression e; pr_elem i1; pp_name name;
ae4735db 116
951c7801
C
117 | SizeOfExpr (e), [i] ->
118 pr_elem i;
119 (match Ast_c.unwrap e with
120 ParenExpr (e), _ -> ()
121 | _ -> pr_space());
122 pp_expression e
ae4735db 123 | SizeOfType (t), [i1;i2;i3] ->
113803cf 124 pr_elem i1; pr_elem i2; pp_type t; pr_elem i3
ae4735db 125 | Cast (t, e), [i1;i2] ->
113803cf 126 pr_elem i1; pp_type t; pr_elem i2; pp_expression e
ae4735db
C
127
128 | StatementExpr (statxs, [ii1;ii2]), [i1;i2] ->
34e49164
C
129 pr_elem i1;
130 pr_elem ii1;
113803cf 131 statxs +> List.iter pp_statement_seq;
34e49164
C
132 pr_elem ii2;
133 pr_elem i2;
ae4735db 134 | Constructor (t, xs), lp::rp::i1::i2::iicommaopt ->
34e49164 135 pr_elem lp;
113803cf 136 pp_type t;
34e49164
C
137 pr_elem rp;
138 pr_elem i1;
ae4735db 139 xs +> List.iter (fun (x, ii) ->
34e49164
C
140 assert (List.length ii <= 1);
141 ii +> List.iter (function x -> pr_elem x; pr_space());
113803cf 142 pp_init x
34e49164
C
143 );
144 iicommaopt +> List.iter pr_elem;
145 pr_elem i2;
ae4735db 146
34e49164 147 | ParenExpr (e), [i1;i2] -> pr_elem i1; pp_expression e; pr_elem i2;
ae4735db 148
4dfbc1c2
C
149 | New (t), [i1] -> pr_elem i1; pp_argument t
150 | Delete(t), [i1] -> pr_elem i1; pp_expression t
f59c9fb7 151
ae4735db 152 | (Ident (_) | Constant _ | FunCall (_,_) | CondExpr (_,_,_)
113803cf 153 | Sequence (_,_)
ae4735db 154 | Assignment (_,_,_)
113803cf
C
155 | Postfix (_,_) | Infix (_,_) | Unary (_,_) | Binary (_,_,_)
156 | ArrayAccess (_,_) | RecordAccess (_,_) | RecordPtAccess (_,_)
ae4735db 157 | SizeOfExpr (_) | SizeOfType (_) | Cast (_,_)
113803cf 158 | StatementExpr (_) | Constructor _
4dfbc1c2 159 | ParenExpr (_) | New (_) | Delete (_)),_ -> raise Impossible
34e49164 160 );
ae4735db 161
34e49164
C
162 if !Flag_parsing_c.pretty_print_type_info
163 then begin
164 pr_elem (Ast_c.fakeInfo() +> Ast_c.rewrap_str "/*");
165 !typ +>
166 (fun (ty,_test) -> ty +>
113803cf
C
167 Common.do_option
168 (fun (x,l) -> pp_type x;
169 let s = match l with
170 Ast_c.LocalVar _ -> ", local"
171 | _ -> "" in
172 pr_elem (Ast_c.fakeInfo() +> Ast_c.rewrap_str s)));
34e49164
C
173 pr_elem (Ast_c.fakeInfo() +> Ast_c.rewrap_str "*/");
174 end
ae4735db 175
113803cf 176 and pp_arg_list es =
ae4735db 177 es +> List.iter (fun (e, opt) ->
113803cf
C
178 assert (List.length opt <= 1); (* opt must be a comma? *)
179 opt +> List.iter (function x -> pr_elem x; pr_space());
180 pp_argument e)
ae4735db
C
181
182 and pp_argument argument =
113803cf
C
183 let rec pp_action (ActMisc ii) = ii +> List.iter pr_elem in
184 match argument with
185 | Left e -> pp_expression e
ae4735db 186 | Right weird ->
0708f913 187 (match weird with
113803cf
C
188 | ArgType param -> pp_param param
189 | ArgAction action -> pp_action action)
ae4735db 190
b1b2de81
C
191(* ---------------------- *)
192 and pp_name = function
ae4735db 193 | RegularName (s, ii) ->
b1b2de81
C
194 let (i1) = Common.tuple_of_list1 ii in
195 pr_elem i1
ae4735db
C
196 | CppConcatenatedName xs ->
197 xs +> List.iter (fun ((x,ii1), ii2) ->
b1b2de81
C
198 ii2 +> List.iter pr_elem;
199 ii1 +> List.iter pr_elem;
200 )
ae4735db 201 | CppVariadicName (s, ii) ->
b1b2de81 202 ii +> List.iter pr_elem
ae4735db 203 | CppIdentBuilder ((s,iis), xs) ->
b1b2de81
C
204 let (iis, iop, icp) = Common.tuple_of_list3 iis in
205 pr_elem iis;
206 pr_elem iop;
ae4735db 207 xs +> List.iter (fun ((x,iix), iicomma) ->
b1b2de81
C
208 iicomma +> List.iter pr_elem;
209 iix +> List.iter pr_elem;
210 );
211 pr_elem icp
212
34e49164 213(* ---------------------- *)
ae4735db 214 and pp_statement = fun st ->
708f4980 215 match Ast_c.get_st_and_ii st with
b1b2de81
C
216 | Labeled (Label (name, st)), ii ->
217 let (i2) = Common.tuple_of_list1 ii in
218 pr_outdent(); pp_name name; pr_elem i2; pr_nl(); pp_statement st
ae4735db 219 | Labeled (Case (e, st)), [i1;i2] ->
113803cf
C
220 pr_unindent();
221 pr_elem i1; pp_expression e; pr_elem i2; pr_nl(); pr_indent();
222 pp_statement st
ae4735db 223 | Labeled (CaseRange (e, e2, st)), [i1;i2;i3] ->
113803cf 224 pr_unindent();
34e49164 225 pr_elem i1; pp_expression e; pr_elem i2; pp_expression e2; pr_elem i3;
113803cf 226 pr_nl(); pr_indent();
34e49164 227 pp_statement st
113803cf
C
228 | Labeled (Default st), [i1;i2] ->
229 pr_unindent(); pr_elem i1; pr_elem i2; pr_nl(); pr_indent();
230 pp_statement st
ae4735db 231 | Compound statxs, [i1;i2] ->
113803cf
C
232 pr_elem i1; start_block();
233 statxs +> Common.print_between pr_nl pp_statement_seq;
234 end_block(); pr_elem i2;
ae4735db 235
34e49164
C
236 | ExprStatement (None), [i] -> pr_elem i;
237 | ExprStatement (None), [] -> ()
238 | ExprStatement (Some e), [i] -> pp_expression e; pr_elem i
239 (* the last ExprStatement of a for does not have a trailing
240 ';' hence the [] for ii *)
ae4735db
C
241 | ExprStatement (Some e), [] -> pp_expression e;
242 | Selection (If (e, st1, st2)), i1::i2::i3::is ->
113803cf
C
243 pr_elem i1; pr_space(); pr_elem i2; pp_expression e; pr_elem i3;
244 indent_if_needed st1 (function _ -> pp_statement st1);
708f4980 245 (match (Ast_c.get_st_and_ii st2, is) with
34e49164
C
246 | ((ExprStatement None, []), []) -> ()
247 | ((ExprStatement None, []), [iifakend]) -> pr_elem iifakend
708f4980 248 | _st2, [i4;iifakend] -> pr_elem i4;
113803cf
C
249 indent_if_needed st2 (function _ -> pp_statement st2);
250 pr_elem iifakend
34e49164
C
251 | x -> raise Impossible
252 )
ae4735db 253 | Selection (Switch (e, st)), [i1;i2;i3;iifakend] ->
113803cf
C
254 pr_elem i1; pr_space(); pr_elem i2; pp_expression e; pr_elem i3;
255 indent_if_needed st (function _-> pp_statement st); pr_elem iifakend
ae4735db 256 | Iteration (While (e, st)), [i1;i2;i3;iifakend] ->
113803cf
C
257 pr_elem i1; pr_space(); pr_elem i2; pp_expression e; pr_elem i3;
258 indent_if_needed st (function _-> pp_statement st); pr_elem iifakend
ae4735db 259 | Iteration (DoWhile (st, e)), [i1;i2;i3;i4;i5;iifakend] ->
113803cf
C
260 pr_elem i1;
261 indent_if_needed st (function _ -> pp_statement st);
ae4735db 262 pr_elem i2; pr_elem i3; pp_expression e;
34e49164
C
263 pr_elem i4; pr_elem i5;
264 pr_elem iifakend
ae4735db
C
265
266
34e49164
C
267 | Iteration (For ((e1opt,il1),(e2opt,il2),(e3opt, il3),st)),
268 [i1;i2;i3;iifakend] ->
ae4735db 269
113803cf
C
270 pr_elem i1; pr_space();
271 pr_elem i2;
708f4980
C
272 pp_statement (Ast_c.mk_st (ExprStatement e1opt) il1);
273 pp_statement (Ast_c.mk_st (ExprStatement e2opt) il2);
113803cf 274 assert (null il3);
708f4980 275 pp_statement (Ast_c.mk_st (ExprStatement e3opt) il3);
113803cf
C
276 pr_elem i3;
277 indent_if_needed st (function _ -> pp_statement st);
278 pr_elem iifakend
ae4735db 279
34e49164 280 | Iteration (MacroIteration (s,es,st)), [i1;i2;i3;iifakend] ->
113803cf 281 pr_elem i1; pr_space();
34e49164 282 pr_elem i2;
ae4735db
C
283
284 es +> List.iter (fun (e, opt) ->
34e49164
C
285 assert (List.length opt <= 1);
286 opt +> List.iter pr_elem;
113803cf 287 pp_argument e;
34e49164 288 );
ae4735db 289
34e49164 290 pr_elem i3;
113803cf 291 indent_if_needed st (function _ -> pp_statement st);
34e49164 292 pr_elem iifakend
ae4735db
C
293
294 | Jump (Goto name), ii ->
b1b2de81
C
295 let (i1, i3) = Common.tuple_of_list2 ii in
296 pr_elem i1; pr_space(); pp_name name; pr_elem i3;
34e49164
C
297 | Jump ((Continue|Break|Return)), [i1;i2] -> pr_elem i1; pr_elem i2;
298 | Jump (ReturnExpr e), [i1;i2] ->
299 pr_elem i1; pr_space(); pp_expression e; pr_elem i2
ae4735db 300 | Jump (GotoComputed e), [i1;i2;i3] ->
34e49164 301 pr_elem i1; pr_elem i2; pp_expression e; pr_elem i3
ae4735db 302
113803cf 303 | Decl decl, [] -> pp_decl decl
ae4735db 304 | Asm asmbody, ii ->
34e49164 305 (match ii with
ae4735db 306 | [iasm;iopar;icpar;iptvirg] ->
34e49164 307 pr_elem iasm; pr_elem iopar;
113803cf 308 pp_asmbody asmbody;
34e49164 309 pr_elem icpar; pr_elem iptvirg
ae4735db
C
310 | [iasm;ivolatile;iopar;icpar;iptvirg] ->
311 pr_elem iasm; pr_elem ivolatile; pr_elem iopar;
113803cf 312 pp_asmbody asmbody;
34e49164
C
313 pr_elem icpar; pr_elem iptvirg
314 | _ -> raise Impossible
315 )
ae4735db
C
316
317 | NestedFunc def, ii ->
34e49164 318 assert (null ii);
113803cf 319 pp_def def
ae4735db 320 | MacroStmt, ii ->
34e49164 321 ii +> List.iter pr_elem ;
ae4735db
C
322
323 | (Labeled (Case (_,_))
113803cf 324 | Labeled (CaseRange (_,_,_)) | Labeled (Default _)
ae4735db 325 | Compound _ | ExprStatement _
113803cf 326 | Selection (If (_, _, _)) | Selection (Switch (_, _))
ae4735db 327 | Iteration (While (_, _)) | Iteration (DoWhile (_, _))
113803cf
C
328 | Iteration (For ((_,_), (_,_), (_, _), _))
329 | Iteration (MacroIteration (_,_,_))
b1b2de81 330 | Jump ((Continue|Break|Return)) | Jump (ReturnExpr _)
113803cf 331 | Jump (GotoComputed _)
ae4735db 332 | Decl _
113803cf 333 ), _ -> raise Impossible
ae4735db 334
113803cf
C
335 and pp_statement_seq = function
336 | StmtElem st -> pp_statement st
337 | IfdefStmt ifdef -> pp_ifdef ifdef
338 | CppDirectiveStmt cpp -> pp_directive cpp
339 | IfdefStmt2 (ifdef, xxs) -> pp_ifdef_tree_sequence ifdef xxs
ae4735db 340
485bce71 341(* ifdef XXX elsif YYY elsif ZZZ endif *)
ae4735db 342 and pp_ifdef_tree_sequence ifdef xxs =
113803cf 343 match ifdef with
ae4735db 344 | if1::ifxs ->
113803cf
C
345 pp_ifdef if1;
346 pp_ifdef_tree_sequence_aux ifxs xxs
347 | _ -> raise Impossible
ae4735db 348
485bce71 349(* XXX elsif YYY elsif ZZZ endif *)
ae4735db
C
350 and pp_ifdef_tree_sequence_aux ifdefs xxs =
351 Common.zip ifdefs xxs +> List.iter (fun (ifdef, xs) ->
113803cf
C
352 xs +> List.iter pp_statement_seq;
353 pp_ifdef ifdef
354 )
ae4735db
C
355
356
357
358
359
485bce71 360(* ---------------------- *)
ae4735db 361 and pp_asmbody (string_list, colon_list) =
113803cf 362 string_list +> List.iter pr_elem ;
ae4735db 363 colon_list +> List.iter (fun (Colon xs, ii) ->
113803cf 364 ii +> List.iter pr_elem;
ae4735db 365 xs +> List.iter (fun (x,iicomma) ->
113803cf
C
366 assert ((List.length iicomma) <= 1);
367 iicomma +> List.iter (function x -> pr_elem x; pr_space());
ae4735db 368 (match x with
113803cf 369 | ColonMisc, ii -> ii +> List.iter pr_elem;
ae4735db 370 | ColonExpr e, [istring;iopar;icpar] ->
113803cf
C
371 pr_elem istring;
372 pr_elem iopar;
373 pp_expression e;
374 pr_elem icpar
c491d8ee
C
375 (* the following case used to be just raise Impossible, but
376 the code __asm__ __volatile__ ("dcbz 0, %[input]"
377 ::[input]"r"(&coherence_data[i]));
378 in linux-2.6.34/drivers/video/fsl-diu-fb.c matches this case *)
379 | (ColonExpr e), ii ->
380 (match List.rev ii with
381 icpar::iopar::istring::rest ->
382 List.iter pr_elem (List.rev rest);
383 pr_elem istring;
384 pr_elem iopar;
385 pp_expression e;
386 pr_elem icpar
387 | _ -> raise Impossible))
113803cf 388 ))
ae4735db
C
389
390
34e49164 391(* ---------------------- *)
ae4735db 392
485bce71 393(*
113803cf
C
394 pp_type_with_ident
395 pp_base_type
396 pp_type_with_ident_rest
397 pp_type_left
398 pp_type_right
399 pp_type
ae4735db 400
113803cf 401 pp_decl
485bce71 402*)
ae4735db
C
403 and (pp_type_with_ident:
404 (string * info) option -> (storage * il) option ->
113803cf 405 fullType -> attribute list ->
ae4735db 406 unit) =
708f4980
C
407 fun ident sto ft attrs ->
408 pp_base_type ft sto;
409 (match (ident, Ast_c.unwrap_typeC ft) with
113803cf
C
410 (Some _,_) | (_,Pointer _) -> pr_space()
411 | _ -> ());
708f4980 412 pp_type_with_ident_rest ident ft attrs
ae4735db
C
413
414
415 and (pp_base_type: fullType -> (storage * il) option -> unit) =
416 fun (qu, (ty, iity)) sto ->
417 let get_sto sto =
418 match sto with
113803cf
C
419 | None -> [] | Some (s, iis) -> (*assert (List.length iis = 1);*) iis
420 in
ae4735db 421 let print_sto_qu (sto, (qu, iiqu)) =
113803cf 422 let all_ii = get_sto sto ++ iiqu in
ae4735db 423 all_ii
34e49164 424 +> List.sort Ast_c.compare_pos
113803cf 425 +> Common.print_between pr_space pr_elem
ae4735db 426
113803cf 427 in
ae4735db 428 let print_sto_qu_ty (sto, (qu, iiqu), iity) =
113803cf
C
429 let all_ii = get_sto sto ++ iiqu ++ iity in
430 let all_ii2 = all_ii +> List.sort Ast_c.compare_pos in
ae4735db 431
113803cf 432 if all_ii <> all_ii2
ae4735db
C
433 then begin
434 (* TODO in fact for pointer, the qualifier is after the type
34e49164
C
435 * cf -test strangeorder
436 *)
113803cf
C
437 pr2 "STRANGEORDER";
438 all_ii2 +> Common.print_between pr_space pr_elem
439 end
440 else all_ii2 +> Common.print_between pr_space pr_elem
441 in
ae4735db 442
113803cf 443 match ty, iity with
f59c9fb7 444 | (NoType,_) -> ()
113803cf
C
445 | (Pointer t, [i]) -> pp_base_type t sto
446 | (ParenType t, _) -> pp_base_type t sto
447 | (Array (eopt, t), [i1;i2]) -> pp_base_type t sto
ae4735db 448 | (FunctionType (returnt, paramst), [i1;i2]) ->
5636bb2c 449 pp_base_type returnt sto;
ae4735db
C
450
451
452 | (StructUnion (su, sopt, fields),iis) ->
113803cf 453 print_sto_qu (sto, qu);
ae4735db 454
113803cf 455 (match sopt,iis with
ae4735db
C
456 | Some s , [i1;i2;i3;i4] ->
457 pr_elem i1; pr_elem i2; pr_elem i3;
458 | None, [i1;i2;i3] ->
459 pr_elem i1; pr_elem i2;
113803cf
C
460 | x -> raise Impossible
461 );
ae4735db 462
413ffc02 463 fields +> List.iter pp_field;
ae4735db 464
113803cf
C
465 (match sopt,iis with
466 | Some s , [i1;i2;i3;i4] -> pr_elem i4
ae4735db 467 | None, [i1;i2;i3] -> pr_elem i3;
113803cf
C
468 | x -> raise Impossible
469 );
ae4735db
C
470
471
472
473 | (Enum (sopt, enumt), iis) ->
113803cf 474 print_sto_qu (sto, qu);
ae4735db 475
113803cf 476 (match sopt, iis with
ae4735db 477 | (Some s, ([i1;i2;i3;i4]|[i1;i2;i3;i4;_])) ->
113803cf 478 pr_elem i1; pr_elem i2; pr_elem i3;
ae4735db 479 | (None, ([i1;i2;i3]|[i1;i2;i3;_])) ->
113803cf
C
480 pr_elem i1; pr_elem i2
481 | x -> raise Impossible
482 );
ae4735db
C
483
484 enumt +> List.iter (fun ((name, eopt), iicomma) ->
113803cf
C
485 assert (List.length iicomma <= 1);
486 iicomma +> List.iter (function x -> pr_elem x; pr_space());
b1b2de81 487 pp_name name;
ae4735db 488 eopt +> Common.do_option (fun (ieq, e) ->
b1b2de81
C
489 pr_elem ieq;
490 pp_expression e;
113803cf 491 ));
ae4735db 492
113803cf
C
493 (match sopt, iis with
494 | (Some s, [i1;i2;i3;i4]) -> pr_elem i4
ae4735db 495 | (Some s, [i1;i2;i3;i4;i5]) ->
113803cf
C
496 pr_elem i5; pr_elem i4 (* trailing comma *)
497 | (None, [i1;i2;i3]) -> pr_elem i3
ae4735db 498 | (None, [i1;i2;i3;i4]) ->
113803cf 499 pr_elem i4; pr_elem i3 (* trailing comma *)
ae4735db
C
500
501
113803cf
C
502 | x -> raise Impossible
503 );
ae4735db
C
504
505
506 | (BaseType _, iis) ->
113803cf 507 print_sto_qu_ty (sto, qu, iis);
ae4735db
C
508
509 | (StructUnionName (s, structunion), iis) ->
b1b2de81 510 assert (List.length iis =|= 2);
113803cf 511 print_sto_qu_ty (sto, qu, iis);
ae4735db
C
512
513 | (EnumName s, iis) ->
b1b2de81 514 assert (List.length iis =|= 2);
113803cf 515 print_sto_qu_ty (sto, qu, iis);
ae4735db
C
516
517 | (TypeName (name,typ), noii) ->
b1b2de81 518 assert (null noii);
708f4980 519 let (_s, iis) = get_s_and_info_of_name name in
b1b2de81 520 print_sto_qu_ty (sto, qu, [iis]);
708f4980
C
521
522 if !Flag_parsing_c.pretty_print_typedef_value
523 then begin
524 pr_elem (Ast_c.fakeInfo() +> Ast_c.rewrap_str "{*");
ae4735db 525 typ +> Common.do_option (fun typ ->
708f4980
C
526 pp_type typ;
527 );
528 pr_elem (Ast_c.fakeInfo() +> Ast_c.rewrap_str "*}");
529 end;
ae4735db
C
530
531 | (TypeOfExpr (e), iis) ->
113803cf
C
532 print_sto_qu (sto, qu);
533 (match iis with
ae4735db 534 | [itypeof;iopar;icpar] ->
113803cf
C
535 pr_elem itypeof; pr_elem iopar;
536 pp_expression e;
537 pr_elem icpar;
538 | _ -> raise Impossible
539 )
ae4735db
C
540
541 | (TypeOfType (t), iis) ->
113803cf
C
542 print_sto_qu (sto, qu);
543 (match iis with
ae4735db 544 | [itypeof;iopar;icpar] ->
113803cf 545 pr_elem itypeof; pr_elem iopar;
ae4735db 546 pp_type t;
113803cf
C
547 pr_elem icpar;
548 | _ -> raise Impossible
549 )
ae4735db
C
550
551 | (Pointer _ | (*ParenType _ |*) Array _ | FunctionType _
485bce71
C
552 (* | StructUnion _ | Enum _ | BaseType _ *)
553 (* | StructUnionName _ | EnumName _ | TypeName _ *)
554 (* | TypeOfExpr _ | TypeOfType _ *)
113803cf 555 ), _ -> raise Impossible
ae4735db 556
190f1acf 557 and pp_field_list fields = fields +> Common.print_between pr_nl pp_field
413ffc02
C
558 and pp_field = function
559 DeclarationField(FieldDeclList(onefield_multivars,iiptvirg))->
560 (match onefield_multivars with
561 x::xs ->
562 (* handling the first var. Special case, with the
563 first var, we print the whole type *)
564
565 (match x with
566 (Simple (nameopt, typ)), iivirg ->
567 (* first var cant have a preceding ',' *)
568 assert (List.length iivirg =|= 0);
569 let identinfo =
570 match nameopt with
571 | None -> None
572 | Some name -> Some (get_s_and_info_of_name name)
573 in
574 pp_type_with_ident identinfo None typ Ast_c.noattr;
575
576 | (BitField (nameopt, typ, iidot, expr)), iivirg ->
577 (* first var cant have a preceding ',' *)
578 assert (List.length iivirg =|= 0);
579 (match nameopt with
580 | None ->
581 pp_type typ;
582 | Some name ->
583 let (s, is) = get_s_and_info_of_name name in
584 pp_type_with_ident
585 (Some (s, is)) None typ Ast_c.noattr;
586 );
587 pr_elem iidot;
588 pp_expression expr
589
590 ); (* match x, first onefield_multivars *)
591
592 (* for other vars *)
593 xs +> List.iter (function
594 | (Simple (nameopt, typ)), iivirg ->
595 iivirg +> List.iter pr_elem;
596 let identinfo =
597 match nameopt with
598 | None -> None
599 | Some name -> Some (get_s_and_info_of_name name)
600 in
601 pp_type_with_ident_rest identinfo typ Ast_c.noattr
602
603 | (BitField (nameopt, typ, iidot, expr)), iivirg ->
604 iivirg +> List.iter pr_elem;
605 (match nameopt with
606 | Some name ->
607 let (s,is) = get_s_and_info_of_name name in
608 pp_type_with_ident_rest
609 (Some (s, is)) typ Ast_c.noattr;
610 pr_elem iidot;
611 pp_expression expr
c491d8ee
C
612 | None ->
613 (* was raise Impossible, but have no idea why because
614 nameless bit fields are accepted by the parser and
615 nothing seems to be done to give them names *)
616 pr_elem iidot;
617 pp_expression expr
413ffc02
C
618 )); (* iter other vars *)
619
620 | [] -> raise Impossible
621 ); (* onefield_multivars *)
622 assert (List.length iiptvirg =|= 1);
623 iiptvirg +> List.iter pr_elem;
624
625
626 | MacroDeclField ((s, es), ii) ->
627 let (iis, lp, rp, iiend, ifakestart) =
628 Common.tuple_of_list5 ii in
629 (* iis::lp::rp::iiend::ifakestart::iisto
630 iisto +> List.iter pr_elem; (* static and const *)
631 *)
632 pr_elem ifakestart;
633 pr_elem iis;
634 pr_elem lp;
635 es +> List.iter (fun (e, opt) ->
636 assert (List.length opt <= 1);
637 opt +> List.iter pr_elem;
638 pp_argument e;
639 );
640
641 pr_elem rp;
642 pr_elem iiend;
ae4735db
C
643
644
413ffc02
C
645
646 | EmptyField iipttvirg_when_emptyfield ->
647 pr_elem iipttvirg_when_emptyfield
648
649 | CppDirectiveStruct cpp -> pp_directive cpp
650 | IfdefStruct ifdef -> pp_ifdef ifdef
651
ae4735db
C
652(* used because of DeclList, in int i,*j[23]; we dont print anymore the
653 int before *j *)
654 and (pp_type_with_ident_rest: (string * info) option ->
655 fullType -> attribute list -> unit) =
656
657 fun ident (((qu, iiqu), (ty, iity)) as fullt) attrs ->
658
659 let print_ident ident = Common.do_option (fun (s, iis) ->
485bce71
C
660 (* XXX attrs +> pp_attributes pr_elem pr_space; *)
661 pr_elem iis
113803cf 662 ) ident
34e49164 663 in
ae4735db 664
34e49164
C
665 match ty, iity with
666 (* the work is to do in base_type !! *)
f59c9fb7 667 | (NoType _, iis) -> failwith "printing notype"
34e49164
C
668 | (BaseType _, iis) -> print_ident ident
669 | (Enum (sopt, enumt), iis) -> print_ident ident
670 | (StructUnion (_, sopt, fields),iis) -> print_ident ident
671 | (StructUnionName (s, structunion), iis) -> print_ident ident
672 | (EnumName s, iis) -> print_ident ident
b1b2de81 673 | (TypeName (_name,_typ), iis) -> print_ident ident
34e49164
C
674 | (TypeOfExpr (e), iis) -> print_ident ident
675 | (TypeOfType (e), iis) -> print_ident ident
ae4735db
C
676
677
678
679 | (Pointer t, [i]) ->
680 (* subtil: void ( *done)(int i) is a Pointer
34e49164
C
681 (FunctionType (return=void, params=int i) *)
682 (*WRONG I THINK, use left & right function *)
683 (* bug: pp_type_with_ident_rest None t; print_ident ident *)
ae4735db 684 pr_elem i;
34e49164 685 iiqu +> List.iter pr_elem; (* le const est forcement apres le '*' *)
113803cf 686 pp_type_with_ident_rest ident t attrs;
ae4735db
C
687
688 (* ugly special case ... todo? maybe sufficient in practice *)
689 | (ParenType ttop, [i1;i2]) ->
708f4980 690 (match Ast_c.get_ty_and_ii ttop with
ae4735db 691 | (_q1, (Pointer t2, [ipointer])) ->
708f4980 692 (match Ast_c.get_ty_and_ii t2 with
ae4735db 693 | (q2, (FunctionType t, ii3)) ->
708f4980
C
694
695 pp_type_left (q2, mk_tybis (FunctionType t) ii3);
696 pr_elem i1;
697 pr_elem ipointer;
698 print_ident ident;
699 pr_elem i2;
700 pp_type_right (q2, mk_tybis (FunctionType t) ii3);
ae4735db 701 | _ ->
708f4980
C
702 pr2 "PB PARENTYPE ZARB, I forget about the ()";
703 pp_type_with_ident_rest ident ttop attrs;
704 )
705 (* another ugly special case *)
ae4735db
C
706 | _q1, (Array (eopt,t2 ), [iarray1;iarray2]) ->
707 (match Ast_c.get_ty_and_ii t2 with
708 | (_q2, (Pointer t3, [ipointer])) ->
708f4980 709 (match Ast_c.get_ty_and_ii t3 with
ae4735db
C
710 | (q3, (FunctionType t, iifunc)) ->
711
708f4980
C
712 pp_type_left (q3, mk_tybis (FunctionType t) iifunc);
713 pr_elem i1;
714 pr_elem ipointer;
715 print_ident ident;
716 pr_elem iarray1;
717 do_option pp_expression eopt;
718 pr_elem iarray2;
719 pr_elem i2;
720 pp_type_right (q3, mk_tybis (FunctionType t) iifunc)
ae4735db 721 | _ ->
708f4980
C
722 pr2 "PB PARENTYPE ZARB, I forget about the ()";
723 pp_type_with_ident_rest ident ttop attrs;
724 )
ae4735db 725 | _ ->
708f4980
C
726 pr2 "PB PARENTYPE ZARB, I forget about the ()";
727 pp_type_with_ident_rest ident ttop attrs;
728 )
ae4735db 729 | _t ->
708f4980
C
730
731 pr2 "PB PARENTYPE ZARB, I forget about the ()";
732 pp_type_with_ident_rest ident ttop attrs;
733 )
ae4735db
C
734
735
736 | (Array (eopt, t), [i1;i2]) ->
113803cf 737 pp_type_left fullt;
ae4735db 738
34e49164
C
739 iiqu +> List.iter pr_elem;
740 print_ident ident;
ae4735db 741
113803cf 742 pp_type_right fullt;
ae4735db
C
743
744
745 | (FunctionType (returnt, paramst), [i1;i2]) ->
113803cf 746 pp_type_left fullt;
ae4735db 747
34e49164
C
748 iiqu +> List.iter pr_elem;
749 print_ident ident;
ae4735db 750
113803cf 751 pp_type_right fullt;
ae4735db
C
752
753
113803cf
C
754 | (FunctionType _ | Array _ | ParenType _ | Pointer _), _ ->
755 raise Impossible
ae4735db
C
756
757
758 and (pp_type_left: fullType -> unit) =
759 fun ((qu, iiqu), (ty, iity)) ->
34e49164 760 match ty, iity with
f59c9fb7 761 (NoType,_) -> failwith "pp_type_left: unexpected NoType"
ae4735db
C
762 | (Pointer t, [i]) ->
763 pr_elem i;
34e49164
C
764 iiqu +> List.iter pr_elem; (* le const est forcement apres le '*' *)
765 pp_type_left t
ae4735db 766
34e49164
C
767 | (Array (eopt, t), [i1;i2]) -> pp_type_left t
768 | (FunctionType (returnt, paramst), [i1;i2]) -> pp_type_left returnt
ae4735db 769
34e49164 770 | (ParenType t, _) -> failwith "parenType"
ae4735db
C
771
772
773 | (BaseType _, iis) -> ()
774 | (Enum (sopt, enumt), iis) -> ()
775 | (StructUnion (_, sopt, fields),iis) -> ()
776 | (StructUnionName (s, structunion), iis) -> ()
777 | (EnumName s, iis) -> ()
b1b2de81 778 | (TypeName (_name,_typ), iis) -> ()
ae4735db 779
485bce71
C
780 | TypeOfType _, _ -> ()
781 | TypeOfExpr _, _ -> ()
ae4735db 782
113803cf 783 | (FunctionType _ | Array _ | Pointer _), _ -> raise Impossible
485bce71 784
ae4735db
C
785
786 and pp_param param =
b1b2de81
C
787 let {p_namei = nameopt;
788 p_register = (b,iib);
789 p_type=t;} = param in
ae4735db 790
b1b2de81
C
791 iib +> List.iter pr_elem;
792
793 match nameopt with
ae4735db 794 | None ->
b1b2de81 795 pp_type t
ae4735db 796 | Some name ->
708f4980 797 let (s,i1) = get_s_and_info_of_name name in
113803cf 798 pp_type_with_ident
b1b2de81 799 (Some (s, i1)) None t Ast_c.noattr
ae4735db
C
800
801
802
803
804 and pp_type_right (((qu, iiqu), (ty, iity)) : fullType) =
113803cf 805 match ty, iity with
f59c9fb7 806 (NoType,_) -> failwith "pp_type_right: unexpected NoType"
113803cf 807 | (Pointer t, [i]) -> pp_type_right t
ae4735db
C
808
809 | (Array (eopt, t), [i1;i2]) ->
113803cf
C
810 pr_elem i1;
811 eopt +> do_option pp_expression;
812 pr_elem i2;
813 pp_type_right t
ae4735db 814
113803cf 815 | (ParenType t, _) -> failwith "parenType"
ae4735db 816 | (FunctionType (returnt, paramst), [i1;i2]) ->
113803cf
C
817 pr_elem i1;
818 (match paramst with
ae4735db
C
819 | (ts, (b, iib)) ->
820 ts +> List.iter (fun (param,iicomma) ->
113803cf
C
821 assert ((List.length iicomma) <= 1);
822 iicomma +> List.iter (function x -> pr_elem x; pr_space());
ae4735db 823
113803cf
C
824 pp_param param;
825 );
826 iib +> List.iter pr_elem;
827 );
828 pr_elem i2
ae4735db
C
829
830 | (BaseType _, iis) -> ()
831 | (Enum (sopt, enumt), iis) -> ()
832 | (StructUnion (_, sopt, fields),iis)-> ()
833 | (StructUnionName (s, structunion), iis) -> ()
834 | (EnumName s, iis) -> ()
b1b2de81 835 | (TypeName (name,_typ), iis) -> ()
ae4735db 836
113803cf
C
837 | TypeOfType _, _ -> ()
838 | TypeOfExpr _, _ -> ()
ae4735db 839
113803cf 840 | (FunctionType _ | Array _ | Pointer _), _ -> raise Impossible
ae4735db 841
113803cf
C
842 and pp_type t =
843 pp_type_with_ident None None t Ast_c.noattr
ae4735db 844
34e49164 845(* ---------------------- *)
113803cf 846 and pp_decl = function
ae4735db 847 | DeclList ((({v_namei = var;
b1b2de81 848 v_type = returnType;
ae4735db 849 v_storage = storage;
b1b2de81 850 v_attr = attrs;
ae4735db
C
851 },[])::xs),
852 iivirg::ifakestart::iisto) ->
853
b1b2de81 854 pr_elem ifakestart;
ae4735db 855
b1b2de81 856 (* old: iisto +> List.iter pr_elem; *)
ae4735db
C
857
858
b1b2de81
C
859 (* handling the first var. Special case, we print the whole type *)
860 (match var with
ae4735db 861 | Some (name, iniopt) ->
708f4980 862 let (s,iis) = get_s_and_info_of_name name in
b1b2de81
C
863 pp_type_with_ident
864 (Some (s, iis)) (Some (storage, iisto))
865 returnType attrs;
4dfbc1c2
C
866 (match iniopt with
867 Ast_c.NoInit -> ()
868 | Ast_c.ValInit(iini,init) -> pr_elem iini; pp_init init
869 | Ast_c.ConstrInit((init,[lp;rp])) ->
870 pr_elem lp; pp_arg_list init; pr_elem rp
871 | Ast_c.ConstrInit _ -> raise Impossible)
b1b2de81
C
872 | None -> pp_type returnType
873 );
ae4735db 874
34e49164 875 (* for other vars, we just call pp_type_with_ident_rest. *)
b1b2de81
C
876 xs +> List.iter (function
877 | ({v_namei = Some (name, iniopt);
878 v_type = returnType;
879 v_storage = storage2;
880 v_attr = attrs;
881 }, iivirg) ->
ae4735db 882
708f4980 883 let (s,iis) = get_s_and_info_of_name name in
b1b2de81
C
884 assert (storage2 =*= storage);
885 iivirg +> List.iter pr_elem;
886 pp_type_with_ident_rest
887 (Some (s, iis)) returnType attrs;
4dfbc1c2
C
888 (match iniopt with
889 Ast_c.NoInit -> ()
890 | Ast_c.ValInit(iini,init) -> pr_elem iini; pp_init init
891 | Ast_c.ConstrInit((init,[lp;rp])) ->
892 pr_elem lp; pp_arg_list init; pr_elem rp
893 | Ast_c.ConstrInit _ -> raise Impossible);
ae4735db
C
894
895
b1b2de81
C
896 | x -> raise Impossible
897 );
ae4735db 898
b1b2de81 899 pr_elem iivirg;
ae4735db
C
900
901 | MacroDecl ((s, es), iis::lp::rp::iiend::ifakestart::iisto) ->
113803cf
C
902 pr_elem ifakestart;
903 iisto +> List.iter pr_elem; (* static and const *)
904 pr_elem iis;
905 pr_elem lp;
ae4735db 906 es +> List.iter (fun (e, opt) ->
113803cf
C
907 assert (List.length opt <= 1);
908 opt +> List.iter pr_elem;
909 pp_argument e;
910 );
ae4735db 911
113803cf
C
912 pr_elem rp;
913 pr_elem iiend;
ae4735db 914
113803cf 915 | (DeclList (_, _) | (MacroDecl _)) -> raise Impossible
ae4735db
C
916
917
34e49164 918(* ---------------------- *)
113803cf
C
919and pp_init (init, iinit) =
920 match init, iinit with
921 | InitExpr e, [] -> pp_expression e;
ae4735db 922 | InitList xs, i1::i2::iicommaopt ->
113803cf 923 pr_elem i1; start_block();
ae4735db 924 xs +> List.iter (fun (x, ii) ->
113803cf
C
925 assert (List.length ii <= 1);
926 ii +> List.iter (function e -> pr_elem e; pr_nl());
927 pp_init x
928 );
929 iicommaopt +> List.iter pr_elem;
930 end_block();
931 pr_elem i2;
ae4735db 932
113803cf
C
933 | InitDesignators (xs, initialiser), [i1] -> (* : *)
934 xs +> List.iter pp_designator;
935 pr_elem i1;
936 pp_init initialiser
ae4735db 937
34e49164 938 (* no use of '=' in the "Old" style *)
113803cf
C
939 | InitFieldOld (string, initialiser), [i1;i2] -> (* label: in oldgcc *)
940 pr_elem i1; pr_elem i2; pp_init initialiser
941 | InitIndexOld (expression, initialiser), [i1;i2] -> (* [1] in oldgcc *)
ae4735db 942 pr_elem i1; pp_expression expression; pr_elem i2;
113803cf 943 pp_init initialiser
ae4735db
C
944
945 | (InitIndexOld _ | InitFieldOld _ | InitDesignators _
113803cf
C
946 | InitList _ | InitExpr _
947 ), _ -> raise Impossible
ae4735db
C
948
949
950
113803cf 951 and pp_designator = function
ae4735db
C
952 | DesignatorField (s), [i1; i2] ->
953 pr_elem i1; pr_elem i2;
954 | DesignatorIndex (expression), [i1;i2] ->
955 pr_elem i1; pp_expression expression; pr_elem i2;
956
957 | DesignatorRange (e1, e2), [iocro;iellipsis;iccro] ->
113803cf 958 pr_elem iocro; pp_expression e1; pr_elem iellipsis;
ae4735db
C
959 pp_expression e2; pr_elem iccro;
960
113803cf
C
961 | (DesignatorField _ | DesignatorIndex _ | DesignatorRange _
962 ), _ -> raise Impossible
ae4735db
C
963
964
485bce71 965(* ---------------------- *)
113803cf 966 and pp_attributes pr_elem pr_space attrs =
ae4735db 967 attrs +> List.iter (fun (attr, ii) ->
113803cf
C
968 ii +> List.iter pr_elem;
969 );
ae4735db 970
34e49164 971(* ---------------------- *)
ae4735db 972 and pp_def def =
113803cf 973 let defbis, ii = def in
ae4735db
C
974 match ii with
975 | iifunc1::iifunc2::i1::i2::ifakestart::isto ->
b1b2de81
C
976 let {f_name = name;
977 f_type = (returnt, (paramst, (b, iib)));
978 f_storage = sto;
979 f_body = statxs;
980 f_attr = attrs;
981 } = defbis
113803cf 982 in
113803cf 983 pr_elem ifakestart;
ae4735db
C
984
985 pp_type_with_ident None (Some (sto, isto))
113803cf 986 returnt Ast_c.noattr;
ae4735db 987
113803cf 988 pp_attributes pr_elem pr_space attrs;
c491d8ee 989 pr_space();
b1b2de81 990 pp_name name;
ae4735db 991
113803cf 992 pr_elem iifunc1;
ae4735db
C
993
994 (* not anymore, cf tests/optional_name_parameter and
34e49164 995 macro_parameter_shortcut.c
113803cf 996 (match paramst with
ae4735db
C
997 | [(((bool, None, t), ii_b_s), iicomma)] ->
998 assert
999 (match t with
113803cf 1000 | qu, (BaseType Void, ii) -> true
ae4735db 1001 | _ -> true
113803cf
C
1002 );
1003 assert (null iicomma);
1004 assert (null ii_b_s);
1005 pp_type_with_ident None None t
ae4735db
C
1006
1007 | paramst ->
113803cf
C
1008 paramst +> List.iter (fun (((bool, s, t), ii_b_s), iicomma) ->
1009 iicomma +> List.iter pr_elem;
ae4735db 1010
113803cf 1011 (match b, s, ii_b_s with
ae4735db 1012 | false, Some s, [i1] ->
113803cf 1013 pp_type_with_ident (Some (s, i1)) None t;
ae4735db 1014 | true, Some s, [i1;i2] ->
113803cf
C
1015 pr_elem i1;
1016 pp_type_with_ident (Some (s, i2)) None t;
ae4735db 1017
34e49164 1018 (* in definition we have name for params, except when f(void) *)
ae4735db
C
1019 | _, None, _ -> raise Impossible
1020 | false, None, [] ->
1021
113803cf
C
1022 | _ -> raise Impossible
1023 )));
ae4735db 1024
34e49164
C
1025 (* normally ii represent the ",..." but it is also abused
1026 with the f(void) case *)
1027 (* assert (List.length iib <= 2);*)
113803cf 1028 iib +> List.iter pr_elem;
ae4735db 1029
34e49164 1030 *)
413ffc02 1031 pp_param_list paramst;
113803cf 1032 iib +> List.iter pr_elem;
ae4735db
C
1033
1034
c491d8ee 1035 pr_elem iifunc2; pr_space();
ae4735db 1036 pr_elem i1;
113803cf
C
1037 statxs +> List.iter pp_statement_seq;
1038 pr_elem i2;
1039 | _ -> raise Impossible
ae4735db 1040
413ffc02
C
1041 and pp_param_list paramst =
1042 paramst +> List.iter (fun (param,iicomma) ->
1043 assert ((List.length iicomma) <= 1);
1044 iicomma +> List.iter (function x -> pr_elem x; pr_space());
1045 pp_param param)
ae4735db 1046
485bce71 1047(* ---------------------- *)
ae4735db
C
1048
1049 and pp_ifdef ifdef =
113803cf 1050 match ifdef with
ae4735db 1051 | IfdefDirective (ifdef, ii) ->
113803cf 1052 List.iter pr_elem ii
ae4735db
C
1053
1054
113803cf 1055 and pp_directive = function
ae4735db 1056 | Include {i_include = (s, ii);} ->
113803cf 1057 let (i1,i2) = Common.tuple_of_list2 ii in
c491d8ee 1058 pr_elem i1; pr_space(); pr_elem i2
ae4735db 1059 | Define ((s,ii), (defkind, defval)) ->
113803cf
C
1060 let (idefine,iident,ieol) = Common.tuple_of_list3 ii in
1061 pr_elem idefine;
1062 pr_elem iident;
ae4735db 1063
113803cf
C
1064 let define_val = function
1065 | DefineExpr e -> pp_expression e
1066 | DefineStmt st -> pp_statement st
ae4735db 1067 | DefineDoWhileZero ((st,e), ii) ->
113803cf 1068 (match ii with
ae4735db 1069 | [ido;iwhile;iopar;icpar] ->
113803cf
C
1070 pr_elem ido;
1071 pp_statement st;
ae4735db 1072 pr_elem iwhile; pr_elem iopar;
113803cf
C
1073 pp_expression e;
1074 pr_elem icpar
1075 | _ -> raise Impossible
1076 )
1077 | DefineFunction def -> pp_def def
ae4735db 1078
113803cf
C
1079 | DefineType ty -> pp_type ty
1080 | DefineText (s, ii) -> List.iter pr_elem ii
1081 | DefineEmpty -> ()
1082 | DefineInit ini -> pp_init ini
ae4735db 1083
113803cf
C
1084 | DefineTodo -> pr2 "DefineTodo"
1085 in
1086 (match defkind with
3a314143 1087 | DefineVar | Undef -> ()
ae4735db 1088 | DefineFunc (params, ii) ->
113803cf 1089 let (i1,i2) = tuple_of_list2 ii in
ae4735db
C
1090 pr_elem i1;
1091 params +> List.iter (fun ((s,iis), iicomma) ->
113803cf
C
1092 assert (List.length iicomma <= 1);
1093 iicomma +> List.iter pr_elem;
1094 iis +> List.iter pr_elem;
1095 );
1096 pr_elem i2;
1097 );
1098 define_val defval;
1099 pr_elem ieol
ae4735db 1100
ae4735db 1101 | PragmaAndCo (ii) ->
113803cf 1102 List.iter pr_elem ii in
ae4735db
C
1103
1104
1105
1106
113803cf
C
1107 let pp_toplevel = function
1108 | Declaration decl -> pp_decl decl
1109 | Definition def -> pp_def def
1110
ae4735db
C
1111 | CppTop directive -> pp_directive directive
1112
1113
1114 | MacroTop (s, es, [i1;i2;i3;i4]) ->
113803cf
C
1115 pr_elem i1;
1116 pr_elem i2;
ae4735db 1117 es +> List.iter (fun (e, opt) ->
113803cf
C
1118 assert (List.length opt <= 1);
1119 opt +> List.iter pr_elem;
1120 pp_argument e;
1121 );
1122 pr_elem i3;
1123 pr_elem i4;
ae4735db
C
1124
1125
113803cf 1126 | EmptyDef ii -> ii +> List.iter pr_elem
ae4735db 1127 | NotParsedCorrectly ii ->
113803cf 1128 assert (List.length ii >= 1);
ae4735db 1129 ii +> List.iter pr_elem
113803cf 1130 | FinalDef info -> pr_elem (Ast_c.rewrap_str "" info)
ae4735db 1131
113803cf 1132 | IfdefTop ifdefdir -> pp_ifdef ifdefdir
ae4735db 1133
113803cf
C
1134 | (MacroTop _) -> raise Impossible in
1135
1136
1137
1138
1139 let pp_flow n =
1140 match F.unwrap n with
1141 | F.FunHeader ({f_name =idb;
1142 f_type = (rett, (paramst,(isvaargs,iidotsb)));
1143 f_storage = stob;
1144 f_body = body;
1145 f_attr = attrs},ii) ->
ae4735db 1146
113803cf 1147 assert(null body);
485bce71 1148 (*
113803cf
C
1149 iif ii;
1150 iif iidotsb;
1151 attrs +> List.iter (vk_attribute bigf);
1152 vk_type bigf rett;
1153 paramst +> List.iter (fun (param, iicomma) ->
1154 vk_param bigf param;
1155 iif iicomma;
1156 );
485bce71 1157 *)
113803cf 1158 pr2 "Def";
485bce71
C
1159
1160
ae4735db 1161 | F.Decl decl ->
485bce71 1162 (* vk_decl bigf decl *)
ae4735db
C
1163 pr2 "Decl"
1164
1165 | F.ExprStatement (st, (eopt, ii)) ->
1166 pp_statement (Ast_c.mk_st (ExprStatement eopt) ii)
1167
1168 | F.IfHeader (_, (e,ii))
113803cf
C
1169 | F.SwitchHeader (_, (e,ii))
1170 | F.WhileHeader (_, (e,ii))
ae4735db 1171 | F.DoWhileTail (e,ii) ->
485bce71 1172 (*
113803cf
C
1173 iif ii;
1174 vk_expr bigf e
485bce71 1175 *)
113803cf 1176 pr2 "XXX";
ae4735db
C
1177
1178
1179 | F.ForHeader (_st, (((e1opt,i1), (e2opt,i2), (e3opt,i3)), ii)) ->
485bce71 1180 (*
113803cf
C
1181 iif i1; iif i2; iif i3;
1182 iif ii;
1183 e1opt +> do_option (vk_expr bigf);
1184 e2opt +> do_option (vk_expr bigf);
1185 e3opt +> do_option (vk_expr bigf);
485bce71 1186 *)
113803cf 1187 pr2 "XXX"
ae4735db
C
1188
1189 | F.MacroIterHeader (_s, ((s,es), ii)) ->
485bce71 1190 (*
113803cf
C
1191 iif ii;
1192 vk_argument_list bigf es;
485bce71 1193 *)
113803cf 1194 pr2 "XXX"
ae4735db
C
1195
1196
1197 | F.ReturnExpr (_st, (e,ii)) ->
485bce71 1198 (* iif ii; vk_expr bigf e*)
113803cf 1199 pr2 "XXX"
ae4735db
C
1200
1201
1202 | F.Case (_st, (e,ii)) ->
91eba41f 1203 (* iif ii; vk_expr bigf e *)
113803cf 1204 pr2 "XXX"
ae4735db
C
1205
1206 | F.CaseRange (_st, ((e1, e2),ii)) ->
485bce71 1207 (* iif ii; vk_expr bigf e1; vk_expr bigf e2 *)
113803cf 1208 pr2 "XXX"
ae4735db
C
1209
1210
1211
113803cf 1212 | F.CaseNode i -> ()
ae4735db
C
1213
1214 | F.DefineExpr e ->
485bce71 1215 (* vk_expr bigf e *)
113803cf 1216 pr2 "XXX"
ae4735db
C
1217
1218 | F.DefineType ft ->
485bce71 1219 (* vk_type bigf ft *)
113803cf 1220 pr2 "XXX"
ae4735db
C
1221
1222 | F.DefineHeader ((s,ii), (defkind)) ->
485bce71 1223 (*
113803cf
C
1224 iif ii;
1225 vk_define_kind bigf defkind;
485bce71 1226 *)
113803cf 1227 pr2 "XXX"
ae4735db
C
1228
1229
1230 | F.DefineDoWhileZeroHeader (((),ii)) ->
485bce71 1231 (* iif ii *)
113803cf 1232 pr2 "XXX"
ae4735db
C
1233
1234
1235 | F.Include {i_include = (s, ii);} ->
485bce71 1236 (* iif ii; *)
113803cf 1237 pr2 "XXX"
ae4735db
C
1238
1239
1240 | F.MacroTop (s, args, ii) ->
485bce71 1241 (* iif ii;
113803cf
C
1242 vk_argument_list bigf args *)
1243 pr2 "XXX"
ae4735db
C
1244
1245
1246 | F.Break (st,((),ii)) ->
485bce71 1247 (* iif ii *)
113803cf 1248 pr2 "XXX"
ae4735db 1249 | F.Continue (st,((),ii)) ->
485bce71 1250 (* iif ii *)
113803cf 1251 pr2 "XXX"
ae4735db 1252 | F.Default (st,((),ii)) ->
485bce71 1253 (* iif ii *)
113803cf 1254 pr2 "XXX"
ae4735db 1255 | F.Return (st,((),ii)) ->
485bce71 1256 (* iif ii *)
113803cf 1257 pr2 "XXX"
ae4735db 1258 | F.Goto (st, name, ((),ii)) ->
485bce71 1259 (* iif ii *)
113803cf 1260 pr2 "XXX"
ae4735db 1261 | F.Label (st, name, ((),ii)) ->
485bce71 1262 (* iif ii *)
113803cf 1263 pr2 "XXX"
ae4735db 1264 | F.EndStatement iopt ->
485bce71 1265 (* do_option infof iopt *)
113803cf 1266 pr2 "XXX"
ae4735db 1267 | F.DoHeader (st, info) ->
485bce71 1268 (* infof info *)
113803cf 1269 pr2 "XXX"
ae4735db 1270 | F.Else info ->
485bce71 1271 (* infof info *)
113803cf 1272 pr2 "XXX"
ae4735db 1273 | F.SeqEnd (i, info) ->
485bce71 1274 (* infof info *)
113803cf 1275 pr2 "XXX"
ae4735db 1276 | F.SeqStart (st, i, info) ->
485bce71 1277 (* infof info *)
113803cf 1278 pr2 "XXX"
ae4735db
C
1279
1280 | F.MacroStmt (st, ((),ii)) ->
485bce71 1281 (* iif ii *)
113803cf 1282 pr2 "XXX"
ae4735db 1283 | F.Asm (st, (asmbody,ii)) ->
485bce71 1284 (*
113803cf
C
1285 iif ii;
1286 vk_asmbody bigf asmbody
485bce71 1287 *)
113803cf 1288 pr2 "XXX"
ae4735db
C
1289
1290
1291 | F.IfdefHeader (info) ->
113803cf 1292 pp_ifdef info
ae4735db 1293 | F.IfdefElse (info) ->
113803cf 1294 pp_ifdef info
ae4735db 1295 | F.IfdefEndif (info) ->
113803cf 1296 pp_ifdef info
ae4735db
C
1297
1298 | F.DefineTodo ->
113803cf 1299 pr2 "XXX"
ae4735db
C
1300
1301
113803cf 1302 | (F.TopNode|F.EndNode|
951c7801
C
1303 F.ErrorExit|F.Exit|F.Enter|F.LoopFallThroughNode|F.FallThroughNode|
1304 F.AfterNode|F.FalseNode|F.TrueNode|F.InLoopNode|
113803cf
C
1305 F.Fake) ->
1306 pr2 "YYY" in
1307
1308
978fd7e5 1309 { expression = pp_expression;
413ffc02
C
1310 arg_list = pp_arg_list;
1311 statement = pp_statement;
1312 decl = pp_decl;
1313 field = pp_field;
190f1acf 1314 field_list = pp_field_list;
413ffc02
C
1315 init = pp_init;
1316 param = pp_param;
190f1acf 1317 paramlist = pp_param_list;
413ffc02 1318 ty = pp_type;
113803cf 1319 type_with_ident = pp_type_with_ident;
413ffc02
C
1320 toplevel = pp_toplevel;
1321 flow = pp_flow;
978fd7e5 1322 }
ae4735db 1323
113803cf 1324(*****************************************************************************)
ae4735db 1325
113803cf
C
1326(* Here we do not use (mcode, env). It is a simple C pretty printer. *)
1327let pr_elem info =
1328 let s = Ast_c.str_of_info info in
0708f913
C
1329 if !Flag_parsing_c.pretty_print_comment_info then begin
1330 let before = !(info.comments_tag).mbefore in
1331 if not (null before) then begin
1332 pp "-->";
ae4735db 1333 before +> List.iter (fun (comment_like, pinfo) ->
0708f913
C
1334 let s = pinfo.Common.str in
1335 pp s
1336 );
1337 pp "<--";
1338 end;
1339 end;
113803cf 1340 pp s
ae4735db 1341
113803cf 1342let pr_space _ = Format.print_space()
485bce71 1343
113803cf
C
1344let pr_nl _ = ()
1345let pr_indent _ = ()
1346let pr_outdent _ = ()
1347let pr_unindent _ = ()
485bce71 1348
978fd7e5 1349
113803cf 1350let ppc =
ae4735db 1351 mk_pretty_printers
978fd7e5
C
1352 ~pr_elem ~pr_space ~pr_nl ~pr_outdent ~pr_indent ~pr_unindent
1353
113803cf 1354let pp_expression_simple = ppc.expression
413ffc02
C
1355let pp_decl_simple = ppc.decl
1356let pp_field_simple = ppc.field
113803cf
C
1357let pp_statement_simple = ppc.statement
1358let pp_type_simple = ppc.ty
1359let pp_init_simple = ppc.init
1360let pp_toplevel_simple = ppc.toplevel
1361let pp_flow_simple = ppc.flow
485bce71
C
1362
1363
978fd7e5
C
1364let pp_elem_sp ~pr_elem ~pr_space =
1365 mk_pretty_printers
ae4735db 1366 ~pr_elem ~pr_space
978fd7e5
C
1367 ~pr_nl ~pr_outdent ~pr_indent ~pr_unindent
1368
1369let pp_expression_gen ~pr_elem ~pr_space =
113803cf 1370 (pp_elem_sp pr_elem pr_space).expression
485bce71 1371
413ffc02 1372let pp_arg_list_gen ~pr_elem ~pr_space =
113803cf 1373 (pp_elem_sp pr_elem pr_space).arg_list
485bce71 1374
978fd7e5 1375let pp_statement_gen ~pr_elem ~pr_space =
113803cf 1376 (pp_elem_sp pr_elem pr_space).statement
34e49164 1377
413ffc02 1378let pp_decl_gen ~pr_elem ~pr_space =
113803cf 1379 (pp_elem_sp pr_elem pr_space).decl
34e49164 1380
413ffc02
C
1381let pp_field_gen ~pr_elem ~pr_space =
1382 (pp_elem_sp pr_elem pr_space).field
1383
190f1acf
C
1384let pp_field_list_gen ~pr_elem ~pr_space =
1385 (pp_elem_sp pr_elem pr_space).field_list
1386
978fd7e5 1387let pp_init_gen ~pr_elem ~pr_space =
113803cf 1388 (pp_elem_sp pr_elem pr_space).init
34e49164 1389
978fd7e5 1390let pp_param_gen ~pr_elem ~pr_space =
113803cf 1391 (pp_elem_sp pr_elem pr_space).param
34e49164 1392
413ffc02
C
1393let pp_param_list_gen ~pr_elem ~pr_space =
1394 (pp_elem_sp pr_elem pr_space).paramlist
1395
978fd7e5 1396let pp_type_gen ~pr_elem ~pr_space =
113803cf 1397 (pp_elem_sp pr_elem pr_space).ty
34e49164 1398
113803cf
C
1399let pp_type_with_ident_gen pr_elem pr_space =
1400 (pp_elem_sp pr_elem pr_space).type_with_ident
34e49164 1401
978fd7e5 1402let pp_program_gen ~pr_elem ~pr_space =
113803cf 1403 (pp_elem_sp pr_elem pr_space).toplevel
485bce71 1404
91eba41f 1405
ae4735db 1406let string_of_expression e =
91eba41f
C
1407 Common.format_to_string (fun () ->
1408 pp_expression_simple e
978fd7e5 1409 )
708f4980 1410
ae4735db 1411let string_of_toplevel top =
708f4980
C
1412 Common.format_to_string (fun () ->
1413 pp_toplevel_simple top
978fd7e5 1414 )
ae4735db 1415
113803cf 1416let (debug_info_of_node:
ae4735db
C
1417 Ograph_extended.nodei -> Control_flow_c.cflow -> string) =
1418 fun nodei flow ->
113803cf
C
1419 let node = flow#nodes#assoc nodei in
1420 let s = Common.format_to_string (fun () ->
1421 pp_flow_simple node
1422 ) in
1423 let pos = Lib_parsing_c.min_pinfo_of_node node in
1424 (spf "%s(n%d)--> %s" (Common.string_of_parse_info_bis pos) nodei s)
ae4735db 1425