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