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