Commit | Line | Data |
---|---|---|
ae4735db C |
1 | (* |
2 | * Copyright (C) 2010, University of Copenhagen DIKU and INRIA. | |
3 | * Copyright (C) 2006, 2007 Julia Lawall | |
978fd7e5 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. | |
951c7801 | 8 | * |
978fd7e5 C |
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. | |
951c7801 | 13 | * |
978fd7e5 C |
14 | * This file was part of Coccinelle. |
15 | *) | |
34e49164 C |
16 | open Common |
17 | ||
18 | (*****************************************************************************) | |
951c7801 | 19 | (* mostly a copy paste of parsing_cocci/pretty_print_cocci.ml |
34e49164 C |
20 | * todo?: try to factorize ? |
21 | *) | |
22 | (*****************************************************************************) | |
23 | ||
24 | module Ast = Ast_cocci | |
25 | ||
26 | let term s = Ast.unwrap_mcode s | |
27 | ||
951c7801 C |
28 | (* or perhaps can have in plus, for instance a Disj, but those Disj must be |
29 | * handled by interactive tool (by proposing alternatives) | |
34e49164 C |
30 | *) |
31 | exception CantBeInPlus | |
32 | ||
33 | (*****************************************************************************) | |
34 | ||
35 | type pos = Before | After | InPlace | |
36 | ||
708f4980 C |
37 | let unknown = -1 |
38 | ||
951c7801 | 39 | let rec do_all |
708f4980 C |
40 | (env, pr, pr_celem, pr_cspace, pr_space, pr_arity, pr_barrier, |
41 | indent, unindent) | |
faf9a90c | 42 | generating xxs before = |
34e49164 C |
43 | |
44 | (* Just to be able to copy paste the code from pretty_print_cocci.ml. *) | |
708f4980 C |
45 | let print_string s line lcol = |
46 | let rcol = if lcol = unknown then unknown else lcol + (String.length s) in | |
47 | pr s line lcol rcol in | |
48 | let print_text s = pr s unknown unknown unknown in | |
34e49164 | 49 | let close_box _ = () in |
fc1ad971 | 50 | let force_newline _ = print_text "\n" in |
34e49164 C |
51 | |
52 | let start_block () = force_newline(); indent() in | |
c3e37e97 | 53 | let end_block () = unindent true; force_newline () in |
34e49164 C |
54 | let print_string_box s = print_string s in |
55 | ||
56 | let print_option = Common.do_option in | |
002099fc C |
57 | let print_option_prespace fn = function |
58 | None -> () | |
59 | | Some x -> pr_space(); fn x in | |
60 | let print_option_space fn = function | |
61 | None -> () | |
62 | | Some x -> fn x; pr_space() in | |
34e49164 C |
63 | let print_between = Common.print_between in |
64 | ||
113803cf C |
65 | let outdent _ = () (* should go to leftmost col, does nothing now *) in |
66 | ||
67 | let pretty_print_c = | |
978fd7e5 | 68 | Pretty_print_c.mk_pretty_printers pr_celem pr_cspace |
c3e37e97 | 69 | force_newline indent outdent (function _ -> unindent true) in |
113803cf | 70 | |
34e49164 | 71 | (* --------------------------------------------------------------------- *) |
faf9a90c | 72 | (* Only for make_hrule, print plus code, unbound metavariables *) |
34e49164 | 73 | |
faf9a90c C |
74 | (* avoid polyvariance problems *) |
75 | let anything : (Ast.anything -> unit) ref = ref (function _ -> ()) in | |
34e49164 | 76 | |
faf9a90c C |
77 | let rec print_anything = function |
78 | [] -> () | |
79 | | stream -> | |
80 | start_block(); | |
81 | print_between force_newline print_anything_list stream; | |
82 | end_block() | |
83 | ||
84 | and print_anything_list = function | |
34e49164 | 85 | [] -> () |
faf9a90c C |
86 | | [x] -> !anything x |
87 | | bef::((aft::_) as rest) -> | |
88 | !anything bef; | |
89 | let space = | |
90 | (match bef with | |
91 | Ast.Rule_elemTag(_) | Ast.AssignOpTag(_) | Ast.BinaryOpTag(_) | |
92 | | Ast.ArithOpTag(_) | Ast.LogicalOpTag(_) | |
93 | | Ast.Token("if",_) | Ast.Token("while",_) -> true | _ -> false) or | |
94 | (match aft with | |
95 | Ast.Rule_elemTag(_) | Ast.AssignOpTag(_) | Ast.BinaryOpTag(_) | |
96 | | Ast.ArithOpTag(_) | Ast.LogicalOpTag(_) | Ast.Token("{",_) -> true | |
97 | | _ -> false) in | |
708f4980 | 98 | if space then pr_space (); |
faf9a90c C |
99 | print_anything_list rest in |
100 | ||
101 | let print_around printer term = function | |
102 | Ast.NOTHING -> printer term | |
951c7801 C |
103 | | Ast.BEFORE(bef,_) -> print_anything bef; printer term |
104 | | Ast.AFTER(aft,_) -> printer term; print_anything aft | |
105 | | Ast.BEFOREAFTER(bef,aft,_) -> | |
faf9a90c C |
106 | print_anything bef; printer term; print_anything aft in |
107 | ||
0708f913 | 108 | let print_string_befaft fn fn1 x info = |
c3e37e97 C |
109 | let print ln col = |
110 | function Ast.Noindent s | Ast.Indent s -> print_string s ln col in | |
708f4980 | 111 | List.iter |
c3e37e97 | 112 | (function (s,ln,col) -> fn1(); print ln col s; force_newline()) |
faf9a90c C |
113 | info.Ast.strbef; |
114 | fn x; | |
708f4980 | 115 | List.iter |
c3e37e97 | 116 | (function (s,ln,col) -> force_newline(); fn1(); print ln col s) |
faf9a90c | 117 | info.Ast.straft in |
708f4980 | 118 | let print_meta (r,x) = print_text x in |
faf9a90c C |
119 | |
120 | let print_pos = function | |
121 | Ast.MetaPos(name,_,_,_,_) -> | |
122 | let name = Ast.unwrap_mcode name in | |
708f4980 | 123 | print_text "@"; print_meta name |
faf9a90c C |
124 | | _ -> () in |
125 | ||
126 | (* --------------------------------------------------------------------- *) | |
127 | ||
708f4980 C |
128 | let mcode fn (s,info,mc,pos) = |
129 | let line = info.Ast.line in | |
130 | let lcol = info.Ast.column in | |
131 | match (generating,mc) with | |
132 | (false,_) -> | |
faf9a90c C |
133 | (* printing for transformation *) |
134 | (* Here we don't care about the annotation on s. *) | |
0708f913 C |
135 | let print_comments lb comments = |
136 | List.fold_left | |
137 | (function line_before -> | |
138 | function (str,line,col) -> | |
139 | match line_before with | |
c3e37e97 C |
140 | None -> |
141 | let str = | |
142 | match str with | |
143 | Ast.Noindent s -> unindent false; s | |
144 | | Ast.Indent s -> s in | |
145 | print_string str line col; Some line | |
708f4980 | 146 | | Some lb when line =|= lb -> |
c3e37e97 C |
147 | Printf.printf "some, line same case\n"; |
148 | let str = match str with Ast.Noindent s | Ast.Indent s -> s in | |
708f4980 | 149 | print_string str line col; Some line |
c3e37e97 | 150 | | _ -> |
aa721442 C |
151 | force_newline(); |
152 | (* not super elegant to put side-effecting unindent in a let | |
153 | expression... *) | |
c3e37e97 C |
154 | let str = |
155 | match str with | |
156 | Ast.Noindent s -> unindent false; s | |
157 | | Ast.Indent s -> s in | |
aa721442 | 158 | print_string str line col; Some line) |
0708f913 C |
159 | lb comments in |
160 | let line_before = print_comments None info.Ast.strbef in | |
161 | (match line_before with | |
162 | None -> () | |
b1b2de81 | 163 | | Some lb when lb =|= info.Ast.line -> () |
708f4980 C |
164 | | _ -> force_newline()); |
165 | fn s line lcol; | |
0708f913 | 166 | let _ = print_comments (Some info.Ast.line) info.Ast.straft in |
fc1ad971 C |
167 | (* newline after a pragma |
168 | should really store parsed versions of the strings, but make a cheap | |
169 | effort here | |
170 | print_comments takes care of interior newlines *) | |
0708f913 | 171 | () |
faf9a90c | 172 | (* printing for rule generation *) |
708f4980 C |
173 | | (true, Ast.MINUS(_,_,_,plus_stream)) -> |
174 | force_newline(); | |
175 | print_text "- "; | |
176 | fn s line lcol; print_pos pos; | |
faf9a90c | 177 | print_anything plus_stream |
708f4980 C |
178 | | (true, Ast.CONTEXT(_,plus_streams)) -> |
179 | let fn s = force_newline(); fn s line lcol; print_pos pos in | |
180 | print_around fn s plus_streams | |
951c7801 | 181 | | (true,Ast.PLUS Ast.ONE) -> |
708f4980 C |
182 | let fn s = |
183 | force_newline(); print_text "+ "; fn s line lcol; print_pos pos in | |
184 | print_string_befaft fn (function _ -> print_text "+ ") s info | |
951c7801 C |
185 | | (true,Ast.PLUS Ast.MANY) -> |
186 | let fn s = | |
187 | force_newline(); print_text "++ "; fn s line lcol; print_pos pos in | |
188 | print_string_befaft fn (function _ -> print_text "++ ") s info | |
34e49164 C |
189 | in |
190 | ||
faf9a90c C |
191 | |
192 | (* --------------------------------------------------------------------- *) | |
193 | ||
194 | let handle_metavar name fn = | |
708f4980 C |
195 | let ((_,b) as s,info,mc,pos) = name in |
196 | let line = info.Ast.line in | |
197 | let lcol = info.Ast.column in | |
198 | match Common.optionise (fun () -> List.assoc s env) with | |
199 | None -> | |
faf9a90c C |
200 | let name_string (_,s) = s in |
201 | if generating | |
708f4980 C |
202 | then |
203 | mcode (function _ -> print_string (name_string s)) name | |
faf9a90c C |
204 | else |
205 | failwith | |
206 | (Printf.sprintf "SP line %d: Not found a value in env for: %s" | |
708f4980 | 207 | line (name_string s)) |
faf9a90c | 208 | | Some e -> |
708f4980 C |
209 | pr_barrier line lcol; |
210 | (if generating | |
211 | then | |
212 | (* call mcode to preserve the -+ annotation *) | |
213 | mcode (fun _ _ _ -> fn e) name | |
214 | else fn e); | |
c3e37e97 C |
215 | let rcol = |
216 | if lcol = unknown then unknown else lcol + (String.length b) in | |
708f4980 | 217 | pr_barrier line rcol |
faf9a90c | 218 | in |
34e49164 C |
219 | (* --------------------------------------------------------------------- *) |
220 | let dots between fn d = | |
221 | match Ast.unwrap d with | |
222 | Ast.DOTS(l) -> print_between between fn l | |
223 | | Ast.CIRCLES(l) -> print_between between fn l | |
224 | | Ast.STARS(l) -> print_between between fn l | |
225 | in | |
226 | ||
faf9a90c C |
227 | let nest_dots multi fn f d = |
228 | let mo s = if multi then "<+"^s else "<"^s in | |
229 | let mc s = if multi then s^"+>" else s^">" in | |
230 | match Ast.unwrap d with | |
231 | Ast.DOTS(l) -> | |
708f4980 | 232 | print_text (mo "..."); f(); start_block(); |
faf9a90c | 233 | print_between force_newline fn l; |
708f4980 | 234 | end_block(); print_text (mc "...") |
faf9a90c | 235 | | Ast.CIRCLES(l) -> |
708f4980 | 236 | print_text (mo "ooo"); f(); start_block(); |
faf9a90c | 237 | print_between force_newline fn l; |
708f4980 | 238 | end_block(); print_text (mc "ooo") |
faf9a90c | 239 | | Ast.STARS(l) -> |
708f4980 | 240 | print_text (mo "***"); f(); start_block(); |
faf9a90c | 241 | print_between force_newline fn l; |
708f4980 | 242 | end_block(); print_text (mc "***") |
faf9a90c | 243 | in |
34e49164 C |
244 | |
245 | (* --------------------------------------------------------------------- *) | |
246 | (* Identifier *) | |
247 | ||
248 | let rec ident i = | |
249 | match Ast.unwrap i with | |
951c7801 C |
250 | Ast.Id(name) -> mcode print_string name |
251 | | Ast.MetaId(name,_,_,_) -> | |
252 | handle_metavar name (function | |
253 | | (Ast_c.MetaIdVal id) -> print_text id | |
254 | | _ -> raise Impossible | |
255 | ) | |
256 | | Ast.MetaFunc(name,_,_,_) -> | |
257 | handle_metavar name (function | |
258 | | (Ast_c.MetaFuncVal id) -> print_text id | |
259 | | _ -> raise Impossible | |
260 | ) | |
261 | | Ast.MetaLocalFunc(name,_,_,_) -> | |
262 | handle_metavar name (function | |
263 | | (Ast_c.MetaLocalFuncVal id) -> print_text id | |
264 | | _ -> raise Impossible | |
265 | ) | |
266 | ||
267 | | Ast.OptIdent(_) | Ast.UniqueIdent(_) -> | |
268 | raise CantBeInPlus | |
34e49164 C |
269 | |
270 | in | |
271 | ||
272 | (* --------------------------------------------------------------------- *) | |
273 | (* Expression *) | |
274 | ||
faf9a90c | 275 | let print_disj_list fn l = |
708f4980 C |
276 | print_text "\n(\n"; |
277 | print_between (function _ -> print_text "\n|\n") fn l; | |
278 | print_text "\n)\n" in | |
faf9a90c | 279 | |
34e49164 C |
280 | let rec expression e = |
281 | match Ast.unwrap e with | |
282 | Ast.Ident(id) -> ident id | |
34e49164 C |
283 | | Ast.Constant(const) -> mcode constant const |
284 | | Ast.FunCall(fn,lp,args,rp) -> | |
285 | expression fn; mcode print_string_box lp; | |
fc1ad971 C |
286 | let comma e = |
287 | expression e; | |
288 | match Ast.unwrap e with | |
289 | Ast.EComma(cm) -> pr_space() | |
290 | | _ -> () in | |
291 | dots (function _ -> ()) comma args; | |
34e49164 C |
292 | close_box(); mcode print_string rp |
293 | | Ast.Assignment(left,op,right,_) -> | |
708f4980 C |
294 | expression left; pr_space(); mcode assignOp op; |
295 | pr_space(); expression right | |
34e49164 | 296 | | Ast.CondExpr(exp1,why,exp2,colon,exp3) -> |
708f4980 C |
297 | expression exp1; pr_space(); mcode print_string why; |
298 | print_option (function e -> pr_space(); expression e) exp2; | |
299 | pr_space(); mcode print_string colon; expression exp3 | |
34e49164 C |
300 | | Ast.Postfix(exp,op) -> expression exp; mcode fixOp op |
301 | | Ast.Infix(exp,op) -> mcode fixOp op; expression exp | |
302 | | Ast.Unary(exp,op) -> mcode unaryOp op; expression exp | |
303 | | Ast.Binary(left,op,right) -> | |
708f4980 | 304 | expression left; pr_space(); mcode binaryOp op; pr_space(); |
34e49164 C |
305 | expression right |
306 | | Ast.Nested(left,op,right) -> failwith "nested only in minus code" | |
307 | | Ast.Paren(lp,exp,rp) -> | |
308 | mcode print_string_box lp; expression exp; close_box(); | |
309 | mcode print_string rp | |
310 | | Ast.ArrayAccess(exp1,lb,exp2,rb) -> | |
311 | expression exp1; mcode print_string_box lb; expression exp2; close_box(); | |
312 | mcode print_string rb | |
313 | | Ast.RecordAccess(exp,pt,field) -> | |
314 | expression exp; mcode print_string pt; ident field | |
315 | | Ast.RecordPtAccess(exp,ar,field) -> | |
316 | expression exp; mcode print_string ar; ident field | |
317 | | Ast.Cast(lp,ty,rp,exp) -> | |
318 | mcode print_string_box lp; fullType ty; close_box(); | |
319 | mcode print_string rp; expression exp | |
320 | | Ast.SizeOfExpr(sizeof,exp) -> | |
321 | mcode print_string sizeof; expression exp | |
322 | | Ast.SizeOfType(sizeof,lp,ty,rp) -> | |
323 | mcode print_string sizeof; | |
324 | mcode print_string_box lp; fullType ty; close_box(); | |
325 | mcode print_string rp | |
326 | | Ast.TypeExp(ty) -> fullType ty | |
327 | ||
ae4735db | 328 | | Ast.MetaErr(name,_,_,_) -> |
34e49164 C |
329 | failwith "metaErr not handled" |
330 | ||
331 | | Ast.MetaExpr (name,_,_,_typedontcare,_formdontcare,_) -> | |
332 | handle_metavar name (function | |
ae4735db | 333 | | Ast_c.MetaExprVal exp -> |
113803cf | 334 | pretty_print_c.Pretty_print_c.expression exp |
34e49164 C |
335 | | _ -> raise Impossible |
336 | ) | |
337 | ||
ae4735db | 338 | | Ast.MetaExprList (name,_,_,_) -> |
faf9a90c | 339 | handle_metavar name (function |
ae4735db | 340 | | Ast_c.MetaExprListVal args -> |
113803cf | 341 | pretty_print_c.Pretty_print_c.arg_list args |
faf9a90c C |
342 | | _ -> raise Impossible |
343 | ) | |
344 | ||
002099fc | 345 | | Ast.EComma(cm) -> mcode print_string cm |
34e49164 | 346 | |
faf9a90c C |
347 | | Ast.DisjExpr(exp_list) -> |
348 | if generating | |
349 | then print_disj_list expression exp_list | |
350 | else raise CantBeInPlus | |
351 | | Ast.NestExpr(expr_dots,Some whencode,multi) when generating -> | |
352 | nest_dots multi expression | |
708f4980 | 353 | (function _ -> print_text " when != "; expression whencode) |
faf9a90c C |
354 | expr_dots |
355 | | Ast.NestExpr(expr_dots,None,multi) when generating -> | |
356 | nest_dots multi expression (function _ -> ()) expr_dots | |
357 | | Ast.NestExpr(_) -> raise CantBeInPlus | |
358 | | Ast.Edots(dots,Some whencode) | |
359 | | Ast.Ecircles(dots,Some whencode) | |
360 | | Ast.Estars(dots,Some whencode) -> | |
361 | if generating | |
362 | then | |
363 | (mcode print_string dots; | |
708f4980 | 364 | print_text " when != "; |
faf9a90c C |
365 | expression whencode) |
366 | else raise CantBeInPlus | |
367 | | Ast.Edots(dots,None) | |
368 | | Ast.Ecircles(dots,None) | |
369 | | Ast.Estars(dots,None) -> | |
370 | if generating | |
371 | then mcode print_string dots | |
372 | else raise CantBeInPlus | |
34e49164 | 373 | |
ae4735db | 374 | | Ast.OptExp(exp) | Ast.UniqueExp(exp) -> |
34e49164 C |
375 | raise CantBeInPlus |
376 | ||
377 | and unaryOp = function | |
378 | Ast.GetRef -> print_string "&" | |
379 | | Ast.DeRef -> print_string "*" | |
380 | | Ast.UnPlus -> print_string "+" | |
381 | | Ast.UnMinus -> print_string "-" | |
382 | | Ast.Tilde -> print_string "~" | |
383 | | Ast.Not -> print_string "!" | |
384 | ||
385 | and assignOp = function | |
386 | Ast.SimpleAssign -> print_string "=" | |
708f4980 C |
387 | | Ast.OpAssign(aop) -> |
388 | (function line -> function lcol -> | |
389 | arithOp aop line lcol; print_string "=" line lcol) | |
34e49164 C |
390 | |
391 | and fixOp = function | |
392 | Ast.Dec -> print_string "--" | |
393 | | Ast.Inc -> print_string "++" | |
394 | ||
395 | and binaryOp = function | |
396 | Ast.Arith(aop) -> arithOp aop | |
397 | | Ast.Logical(lop) -> logicalOp lop | |
398 | ||
399 | and arithOp = function | |
400 | Ast.Plus -> print_string "+" | |
401 | | Ast.Minus -> print_string "-" | |
402 | | Ast.Mul -> print_string "*" | |
403 | | Ast.Div -> print_string "/" | |
404 | | Ast.Mod -> print_string "%" | |
405 | | Ast.DecLeft -> print_string "<<" | |
406 | | Ast.DecRight -> print_string ">>" | |
407 | | Ast.And -> print_string "&" | |
408 | | Ast.Or -> print_string "|" | |
409 | | Ast.Xor -> print_string "^" | |
410 | ||
411 | and logicalOp = function | |
412 | Ast.Inf -> print_string "<" | |
413 | | Ast.Sup -> print_string ">" | |
414 | | Ast.InfEq -> print_string "<=" | |
415 | | Ast.SupEq -> print_string ">=" | |
416 | | Ast.Eq -> print_string "==" | |
417 | | Ast.NotEq -> print_string "!=" | |
418 | | Ast.AndLog -> print_string "&&" | |
419 | | Ast.OrLog -> print_string "||" | |
420 | ||
421 | and constant = function | |
708f4980 | 422 | Ast.String(s) -> print_string ("\""^s^"\"") |
978fd7e5 | 423 | | Ast.Char(s) -> print_string ("\'"^s^"\'") |
34e49164 C |
424 | | Ast.Int(s) -> print_string s |
425 | | Ast.Float(s) -> print_string s | |
426 | ||
427 | (* --------------------------------------------------------------------- *) | |
428 | (* Types *) | |
429 | ||
430 | ||
431 | and fullType ft = | |
432 | match Ast.unwrap ft with | |
002099fc | 433 | Ast.Type(cv,ty) -> print_option_space (mcode const_vol) cv; typeC ty |
34e49164 C |
434 | | Ast.DisjType _ -> failwith "can't be in plus" |
435 | | Ast.OptType(_) | Ast.UniqueType(_) -> | |
436 | raise CantBeInPlus | |
437 | ||
438 | and print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2) fn = | |
439 | fullType ty; mcode print_string lp1; mcode print_string star; fn(); | |
440 | mcode print_string rp1; mcode print_string lp1; | |
441 | parameter_list params; mcode print_string rp2 | |
442 | ||
443 | and print_function_type (ty,lp1,params,rp1) fn = | |
444 | print_option fullType ty; fn(); mcode print_string lp1; | |
445 | parameter_list params; mcode print_string rp1 | |
446 | ||
447 | and typeC ty = | |
448 | match Ast.unwrap ty with | |
faf9a90c C |
449 | Ast.BaseType(ty,strings) -> |
450 | print_between pr_space (mcode print_string) strings | |
002099fc | 451 | | Ast.SignedT(sgn,ty) -> mcode sign sgn; print_option_prespace typeC ty |
34e49164 C |
452 | | Ast.Pointer(ty,star) -> fullType ty; ft_space ty; mcode print_string star |
453 | | Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) -> | |
454 | print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2) | |
455 | (function _ -> ()) | |
456 | | Ast.FunctionType (am,ty,lp1,params,rp1) -> | |
457 | print_function_type (ty,lp1,params,rp1) (function _ -> ()) | |
458 | | Ast.Array(ty,lb,size,rb) -> | |
459 | fullType ty; mcode print_string lb; print_option expression size; | |
460 | mcode print_string rb | |
708f4980 | 461 | | Ast.EnumName(kind,name) -> mcode print_string kind; pr_space(); |
faf9a90c | 462 | ident name |
34e49164 | 463 | | Ast.StructUnionName(kind,name) -> |
002099fc | 464 | mcode structUnion kind; print_option_prespace ident name |
34e49164 | 465 | | Ast.StructUnionDef(ty,lb,decls,rb) -> |
708f4980 | 466 | fullType ty; ft_space ty; |
34e49164 C |
467 | mcode print_string lb; |
468 | dots force_newline declaration decls; | |
469 | mcode print_string rb | |
470 | | Ast.TypeName(name)-> mcode print_string name | |
ae4735db | 471 | | Ast.MetaType(name,_,_) -> |
34e49164 | 472 | handle_metavar name (function |
ae4735db | 473 | Ast_c.MetaTypeVal exp -> |
113803cf | 474 | pretty_print_c.Pretty_print_c.ty exp |
34e49164 C |
475 | | _ -> raise Impossible) |
476 | ||
477 | and baseType = function | |
478 | Ast.VoidType -> print_string "void" | |
479 | | Ast.CharType -> print_string "char" | |
480 | | Ast.ShortType -> print_string "short" | |
481 | | Ast.IntType -> print_string "int" | |
482 | | Ast.DoubleType -> print_string "double" | |
483 | | Ast.FloatType -> print_string "float" | |
484 | | Ast.LongType -> print_string "long" | |
faf9a90c | 485 | | Ast.LongLongType -> print_string "long long" |
34e49164 C |
486 | |
487 | and structUnion = function | |
002099fc C |
488 | Ast.Struct -> print_string "struct" |
489 | | Ast.Union -> print_string "union" | |
34e49164 C |
490 | |
491 | and sign = function | |
34e49164 C |
492 | Ast.Signed -> print_string "signed" |
493 | | Ast.Unsigned -> print_string "unsigned" | |
494 | ||
495 | ||
496 | and const_vol = function | |
002099fc C |
497 | Ast.Const -> print_string "const" |
498 | | Ast.Volatile -> print_string "volatile" | |
34e49164 C |
499 | |
500 | (* --------------------------------------------------------------------- *) | |
501 | (* Function declaration *) | |
502 | ||
503 | and storage = function | |
002099fc C |
504 | Ast.Static -> print_string "static" |
505 | | Ast.Auto -> print_string "auto" | |
506 | | Ast.Register -> print_string "register" | |
507 | | Ast.Extern -> print_string "extern" | |
34e49164 C |
508 | |
509 | (* --------------------------------------------------------------------- *) | |
510 | (* Variable declaration *) | |
511 | ||
512 | and print_named_type ty id = | |
513 | match Ast.unwrap ty with | |
514 | Ast.Type(None,ty1) -> | |
515 | (match Ast.unwrap ty1 with | |
516 | Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) -> | |
517 | print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2) | |
708f4980 | 518 | (function _ -> pr_space(); ident id) |
34e49164 C |
519 | | Ast.FunctionType(am,ty,lp1,params,rp1) -> |
520 | print_function_type (ty,lp1,params,rp1) | |
708f4980 | 521 | (function _ -> pr_space(); ident id) |
34e49164 C |
522 | | Ast.Array(_,_,_,_) -> |
523 | let rec loop ty k = | |
524 | match Ast.unwrap ty with | |
525 | Ast.Array(ty,lb,size,rb) -> | |
526 | (match Ast.unwrap ty with | |
527 | Ast.Type(None,ty) -> | |
528 | loop ty | |
529 | (function _ -> | |
530 | k (); | |
531 | mcode print_string lb; | |
532 | print_option expression size; | |
533 | mcode print_string rb) | |
534 | | _ -> failwith "complex array types not supported") | |
535 | | _ -> typeC ty; ty_space ty; ident id; k () in | |
536 | loop ty1 (function _ -> ()) | |
537 | (*| should have a case here for pointer to array or function type | |
538 | that would put ( * ) around the variable. This makes one wonder | |
539 | why we really need a special case for function pointer *) | |
540 | | _ -> fullType ty; ft_space ty; ident id) | |
541 | | _ -> fullType ty; ft_space ty; ident id | |
542 | ||
543 | and ty_space ty = | |
544 | match Ast.unwrap ty with | |
545 | Ast.Pointer(_,_) -> () | |
708f4980 | 546 | | _ -> pr_space() |
34e49164 C |
547 | |
548 | and ft_space ty = | |
549 | match Ast.unwrap ty with | |
550 | Ast.Type(cv,ty) -> | |
551 | (match Ast.unwrap ty with | |
552 | Ast.Pointer(_,_) -> () | |
c3e37e97 C |
553 | | Ast.MetaType(name,_,_) -> |
554 | (match List.assoc (Ast.unwrap_mcode name) env with | |
555 | Ast_c.MetaTypeVal (tq,ty) -> | |
556 | (match Ast_c.unwrap ty with | |
557 | Ast_c.Pointer(_,_) -> () | |
558 | | _ -> pr_space()) | |
559 | | _ -> pr_space()) | |
708f4980 C |
560 | | _ -> pr_space()) |
561 | | _ -> pr_space() | |
34e49164 C |
562 | |
563 | and declaration d = | |
564 | match Ast.unwrap d with | |
565 | Ast.Init(stg,ty,id,eq,ini,sem) -> | |
566 | print_option (mcode storage) stg; | |
002099fc | 567 | print_option (function _ -> pr_space()) stg; |
34e49164 | 568 | print_named_type ty id; |
708f4980 C |
569 | pr_space(); mcode print_string eq; |
570 | pr_space(); initialiser true ini; mcode print_string sem | |
34e49164 C |
571 | | Ast.UnInit(stg,ty,id,sem) -> |
572 | print_option (mcode storage) stg; | |
002099fc | 573 | print_option (function _ -> pr_space()) stg; |
34e49164 C |
574 | print_named_type ty id; |
575 | mcode print_string sem | |
576 | | Ast.MacroDecl(name,lp,args,rp,sem) -> | |
577 | ident name; mcode print_string_box lp; | |
578 | dots (function _ -> ()) expression args; | |
579 | close_box(); mcode print_string rp; mcode print_string sem | |
580 | | Ast.TyDecl(ty,sem) -> fullType ty; mcode print_string sem | |
581 | | Ast.Typedef(stg,ty,id,sem) -> | |
582 | mcode print_string stg; | |
583 | fullType ty; typeC id; | |
584 | mcode print_string sem | |
585 | | Ast.DisjDecl(_) | Ast.MetaDecl(_,_,_) -> raise CantBeInPlus | |
586 | | Ast.Ddots(_,_) -> raise CantBeInPlus | |
ae4735db | 587 | | Ast.OptDecl(decl) | Ast.UniqueDecl(decl) -> |
34e49164 C |
588 | raise CantBeInPlus |
589 | ||
590 | (* --------------------------------------------------------------------- *) | |
591 | (* Initialiser *) | |
592 | ||
593 | and initialiser nlcomma i = | |
594 | match Ast.unwrap i with | |
ae4735db | 595 | Ast.MetaInit(name,_,_) -> |
113803cf C |
596 | handle_metavar name (function |
597 | Ast_c.MetaInitVal ini -> | |
598 | pretty_print_c.Pretty_print_c.init ini | |
599 | | _ -> raise Impossible) | |
600 | | Ast.InitExpr(exp) -> expression exp | |
34e49164 C |
601 | | Ast.InitList(lb,initlist,rb,[]) -> |
602 | mcode print_string lb; start_block(); | |
603 | (* awkward, because the comma is separate from the initialiser *) | |
604 | let rec loop = function | |
605 | [] -> () | |
606 | | [x] -> initialiser false x | |
607 | | x::xs -> initialiser nlcomma x; loop xs in | |
608 | loop initlist; | |
609 | end_block(); mcode print_string rb | |
610 | | Ast.InitList(lb,initlist,rb,_) -> failwith "unexpected whencode in plus" | |
113803cf | 611 | | Ast.InitGccExt(designators,eq,ini) -> |
708f4980 C |
612 | List.iter designator designators; pr_space(); |
613 | mcode print_string eq; pr_space(); initialiser nlcomma ini | |
34e49164 C |
614 | | Ast.InitGccName(name,eq,ini) -> |
615 | ident name; mcode print_string eq; initialiser nlcomma ini | |
34e49164 C |
616 | | Ast.IComma(comma) -> |
617 | mcode print_string comma; | |
618 | if nlcomma then force_newline() | |
619 | | Ast.OptIni(ini) | Ast.UniqueIni(ini) -> | |
620 | raise CantBeInPlus | |
621 | ||
113803cf C |
622 | and designator = function |
623 | Ast.DesignatorField(dot,id) -> mcode print_string dot; ident id | |
624 | | Ast.DesignatorIndex(lb,exp,rb) -> | |
625 | mcode print_string lb; expression exp; mcode print_string rb | |
626 | | Ast.DesignatorRange(lb,min,dots,max,rb) -> | |
627 | mcode print_string lb; expression min; mcode print_string dots; | |
628 | expression max; mcode print_string rb | |
629 | ||
34e49164 C |
630 | (* --------------------------------------------------------------------- *) |
631 | (* Parameter *) | |
632 | ||
633 | and parameterTypeDef p = | |
634 | match Ast.unwrap p with | |
635 | Ast.VoidParam(ty) -> fullType ty | |
636 | | Ast.Param(ty,Some id) -> print_named_type ty id | |
637 | | Ast.Param(ty,None) -> fullType ty | |
638 | ||
ae4735db | 639 | | Ast.MetaParam(name,_,_) -> |
34e49164 | 640 | failwith "not handling MetaParam" |
ae4735db | 641 | | Ast.MetaParamList(name,_,_,_) -> |
34e49164 C |
642 | failwith "not handling MetaParamList" |
643 | ||
002099fc | 644 | | Ast.PComma(cm) -> mcode print_string cm |
faf9a90c C |
645 | | Ast.Pdots(dots) | Ast.Pcircles(dots) when generating -> |
646 | mcode print_string dots | |
647 | | Ast.Pdots(dots) | Ast.Pcircles(dots) -> raise CantBeInPlus | |
34e49164 C |
648 | | Ast.OptParam(param) | Ast.UniqueParam(param) -> raise CantBeInPlus |
649 | ||
fc1ad971 C |
650 | and parameter_list l = |
651 | let comma p = | |
652 | parameterTypeDef p; | |
653 | match Ast.unwrap p with | |
654 | Ast.PComma(cm) -> pr_space() | |
655 | | _ -> () in | |
656 | dots (function _ -> ()) comma l | |
34e49164 C |
657 | in |
658 | ||
659 | ||
660 | (* --------------------------------------------------------------------- *) | |
661 | (* CPP code *) | |
662 | ||
663 | let rec inc_file = function | |
664 | Ast.Local(elems) -> | |
708f4980 | 665 | print_string ("\""^(String.concat "/" (List.map inc_elem elems))^"\"") |
34e49164 | 666 | | Ast.NonLocal(elems) -> |
708f4980 | 667 | print_string ("<"^(String.concat "/" (List.map inc_elem elems))^">") |
34e49164 C |
668 | |
669 | and inc_elem = function | |
708f4980 C |
670 | Ast.IncPath s -> s |
671 | | Ast.IncDots -> "..." | |
34e49164 C |
672 | |
673 | (* --------------------------------------------------------------------- *) | |
674 | (* Top-level code *) | |
675 | ||
676 | and rule_elem arity re = | |
677 | match Ast.unwrap re with | |
678 | Ast.FunHeader(_,_,fninfo,name,lp,params,rp) -> | |
708f4980 | 679 | pr_arity arity; List.iter print_fninfo fninfo; |
34e49164 C |
680 | ident name; mcode print_string_box lp; |
681 | parameter_list params; close_box(); mcode print_string rp; | |
708f4980 C |
682 | pr_space() |
683 | | Ast.Decl(_,_,decl) -> pr_arity arity; declaration decl | |
34e49164 C |
684 | |
685 | | Ast.SeqStart(brace) -> | |
708f4980 | 686 | pr_arity arity; mcode print_string brace; start_block() |
34e49164 | 687 | | Ast.SeqEnd(brace) -> |
708f4980 | 688 | end_block(); pr_arity arity; mcode print_string brace |
34e49164 C |
689 | |
690 | | Ast.ExprStatement(exp,sem) -> | |
708f4980 | 691 | pr_arity arity; expression exp; mcode print_string sem |
34e49164 C |
692 | |
693 | | Ast.IfHeader(iff,lp,exp,rp) -> | |
708f4980 C |
694 | pr_arity arity; |
695 | mcode print_string iff; pr_space(); mcode print_string_box lp; | |
113803cf | 696 | expression exp; close_box(); mcode print_string rp |
34e49164 | 697 | | Ast.Else(els) -> |
708f4980 | 698 | pr_arity arity; mcode print_string els |
34e49164 C |
699 | |
700 | | Ast.WhileHeader(whl,lp,exp,rp) -> | |
708f4980 C |
701 | pr_arity arity; |
702 | mcode print_string whl; pr_space(); mcode print_string_box lp; | |
113803cf | 703 | expression exp; close_box(); mcode print_string rp |
34e49164 | 704 | | Ast.DoHeader(d) -> |
708f4980 | 705 | pr_arity arity; mcode print_string d |
34e49164 | 706 | | Ast.WhileTail(whl,lp,exp,rp,sem) -> |
708f4980 C |
707 | pr_arity arity; |
708 | mcode print_string whl; pr_space(); mcode print_string_box lp; | |
34e49164 C |
709 | expression exp; close_box(); mcode print_string rp; |
710 | mcode print_string sem | |
711 | | Ast.ForHeader(fr,lp,e1,sem1,e2,sem2,e3,rp) -> | |
708f4980 | 712 | pr_arity arity; |
34e49164 C |
713 | mcode print_string fr; mcode print_string_box lp; |
714 | print_option expression e1; mcode print_string sem1; | |
715 | print_option expression e2; mcode print_string sem2; | |
716 | print_option expression e3; close_box(); | |
113803cf | 717 | mcode print_string rp |
34e49164 | 718 | | Ast.IteratorHeader(nm,lp,args,rp) -> |
708f4980 C |
719 | pr_arity arity; |
720 | ident nm; pr_space(); mcode print_string_box lp; | |
34e49164 | 721 | dots (function _ -> ()) expression args; close_box(); |
113803cf | 722 | mcode print_string rp |
34e49164 C |
723 | |
724 | | Ast.SwitchHeader(switch,lp,exp,rp) -> | |
708f4980 C |
725 | pr_arity arity; |
726 | mcode print_string switch; pr_space(); mcode print_string_box lp; | |
113803cf | 727 | expression exp; close_box(); mcode print_string rp |
34e49164 C |
728 | |
729 | | Ast.Break(br,sem) -> | |
708f4980 | 730 | pr_arity arity; mcode print_string br; mcode print_string sem |
34e49164 | 731 | | Ast.Continue(cont,sem) -> |
708f4980 | 732 | pr_arity arity; mcode print_string cont; mcode print_string sem |
34e49164 C |
733 | | Ast.Label(l,dd) -> ident l; mcode print_string dd |
734 | | Ast.Goto(goto,l,sem) -> | |
735 | mcode print_string goto; ident l; mcode print_string sem | |
736 | | Ast.Return(ret,sem) -> | |
ae4735db | 737 | pr_arity arity; mcode print_string ret; |
34e49164 C |
738 | mcode print_string sem |
739 | | Ast.ReturnExpr(ret,exp,sem) -> | |
708f4980 | 740 | pr_arity arity; mcode print_string ret; pr_space(); |
34e49164 C |
741 | expression exp; mcode print_string sem |
742 | ||
708f4980 C |
743 | | Ast.Exp(exp) -> pr_arity arity; expression exp |
744 | | Ast.TopExp(exp) -> pr_arity arity; expression exp | |
745 | | Ast.Ty(ty) -> pr_arity arity; fullType ty | |
1be43e12 | 746 | | Ast.TopInit(init) -> initialiser false init |
34e49164 | 747 | | Ast.Include(inc,s) -> |
708f4980 | 748 | mcode print_string inc; print_text " "; mcode inc_file s |
34e49164 | 749 | | Ast.DefineHeader(def,id,params) -> |
ae4735db | 750 | mcode print_string def; pr_space(); ident id; |
34e49164 C |
751 | print_define_parameters params |
752 | | Ast.Default(def,colon) -> | |
708f4980 | 753 | mcode print_string def; mcode print_string colon; pr_space() |
34e49164 | 754 | | Ast.Case(case,exp,colon) -> |
708f4980 C |
755 | mcode print_string case; pr_space(); expression exp; |
756 | mcode print_string colon; pr_space() | |
faf9a90c C |
757 | | Ast.DisjRuleElem(res) -> |
758 | if generating | |
759 | then | |
708f4980 C |
760 | (pr_arity arity; print_text "\n(\n"; |
761 | print_between (function _ -> print_text "\n|\n") (rule_elem arity) | |
faf9a90c | 762 | res; |
708f4980 | 763 | print_text "\n)") |
faf9a90c | 764 | else raise CantBeInPlus |
34e49164 C |
765 | |
766 | | Ast.MetaRuleElem(name,_,_) -> | |
767 | raise Impossible | |
768 | ||
769 | | Ast.MetaStmt(name,_,_,_) -> | |
770 | handle_metavar name (function | |
113803cf C |
771 | | Ast_c.MetaStmtVal stm -> |
772 | pretty_print_c.Pretty_print_c.statement stm | |
34e49164 C |
773 | | _ -> raise Impossible |
774 | ) | |
775 | | Ast.MetaStmtList(name,_,_) -> | |
776 | failwith | |
777 | "MetaStmtList not supported (not even in ast_c metavars binding)" | |
778 | ||
779 | and print_define_parameters params = | |
780 | match Ast.unwrap params with | |
781 | Ast.NoParams -> () | |
782 | | Ast.DParams(lp,params,rp) -> | |
783 | mcode print_string lp; | |
784 | dots (function _ -> ()) print_define_param params; mcode print_string rp | |
785 | ||
786 | and print_define_param param = | |
787 | match Ast.unwrap param with | |
788 | Ast.DParam(id) -> ident id | |
789 | | Ast.DPComma(comma) -> mcode print_string comma | |
790 | | Ast.DPdots(dots) -> mcode print_string dots | |
791 | | Ast.DPcircles(circles) -> mcode print_string circles | |
708f4980 C |
792 | | Ast.OptDParam(dp) -> print_text "?"; print_define_param dp |
793 | | Ast.UniqueDParam(dp) -> print_text "!"; print_define_param dp | |
34e49164 C |
794 | |
795 | and print_fninfo = function | |
796 | Ast.FStorage(stg) -> mcode storage stg | |
797 | | Ast.FType(ty) -> fullType ty | |
708f4980 C |
798 | | Ast.FInline(inline) -> mcode print_string inline; pr_space() |
799 | | Ast.FAttr(attr) -> mcode print_string attr; pr_space() in | |
34e49164 | 800 | |
faf9a90c C |
801 | let indent_if_needed s f = |
802 | match Ast.unwrap s with | |
708f4980 | 803 | Ast.Seq(lbrace,body,rbrace) -> pr_space(); f() |
faf9a90c C |
804 | | _ -> |
805 | (*no newline at the end - someone else will do that*) | |
c3e37e97 | 806 | start_block(); f(); unindent true in |
faf9a90c | 807 | |
34e49164 C |
808 | let rec statement arity s = |
809 | match Ast.unwrap s with | |
708f4980 | 810 | Ast.Seq(lbrace,body,rbrace) -> |
34e49164 | 811 | rule_elem arity lbrace; |
34e49164 C |
812 | dots force_newline (statement arity) body; |
813 | rule_elem arity rbrace | |
814 | ||
815 | | Ast.IfThen(header,branch,_) -> | |
faf9a90c C |
816 | rule_elem arity header; |
817 | indent_if_needed branch (function _ -> statement arity branch) | |
34e49164 | 818 | | Ast.IfThenElse(header,branch1,els,branch2,_) -> |
faf9a90c C |
819 | rule_elem arity header; |
820 | indent_if_needed branch1 (function _ -> statement arity branch1); | |
708f4980 | 821 | force_newline(); |
faf9a90c C |
822 | rule_elem arity els; |
823 | indent_if_needed branch2 (function _ -> statement arity branch2) | |
34e49164 | 824 | | Ast.While(header,body,_) -> |
faf9a90c C |
825 | rule_elem arity header; |
826 | indent_if_needed body (function _ -> statement arity body) | |
34e49164 | 827 | | Ast.Do(header,body,tail) -> |
faf9a90c C |
828 | rule_elem arity header; |
829 | indent_if_needed body (function _ -> statement arity body); | |
34e49164 C |
830 | rule_elem arity tail |
831 | | Ast.For(header,body,_) -> | |
faf9a90c C |
832 | rule_elem arity header; |
833 | indent_if_needed body (function _ -> statement arity body) | |
34e49164 | 834 | | Ast.Iterator(header,body,(_,_,_,aft)) -> |
faf9a90c C |
835 | rule_elem arity header; |
836 | indent_if_needed body (function _ -> statement arity body); | |
708f4980 | 837 | mcode (fun _ _ _ -> ()) ((),Ast.no_info,aft,Ast.NoMetaPos) |
34e49164 | 838 | |
fc1ad971 | 839 | | Ast.Switch(header,lb,decls,cases,rb) -> |
708f4980 | 840 | rule_elem arity header; pr_space(); rule_elem arity lb; |
fc1ad971 | 841 | dots force_newline (statement arity) decls; |
34e49164 C |
842 | List.iter (function x -> case_line arity x; force_newline()) cases; |
843 | rule_elem arity rb | |
844 | ||
845 | | Ast.Atomic(re) -> rule_elem arity re | |
846 | ||
708f4980 | 847 | | Ast.FunDecl(header,lbrace,body,rbrace) -> |
34e49164 | 848 | rule_elem arity header; rule_elem arity lbrace; |
34e49164 C |
849 | dots force_newline (statement arity) body; rule_elem arity rbrace |
850 | ||
851 | | Ast.Define(header,body) -> | |
708f4980 | 852 | rule_elem arity header; pr_space(); |
34e49164 C |
853 | dots force_newline (statement arity) body |
854 | ||
faf9a90c C |
855 | | Ast.Disj([stmt_dots]) -> |
856 | if generating | |
857 | then | |
708f4980 | 858 | (pr_arity arity; |
faf9a90c C |
859 | dots force_newline (statement arity) stmt_dots) |
860 | else raise CantBeInPlus | |
861 | | Ast.Disj(stmt_dots_list) -> (* ignores newline directive for readability *) | |
862 | if generating | |
863 | then | |
708f4980 C |
864 | (pr_arity arity; print_text "\n(\n"; |
865 | print_between (function _ -> print_text "\n|\n") | |
faf9a90c C |
866 | (dots force_newline (statement arity)) |
867 | stmt_dots_list; | |
708f4980 | 868 | print_text "\n)") |
faf9a90c C |
869 | else raise CantBeInPlus |
870 | | Ast.Nest(stmt_dots,whn,multi,_,_) when generating -> | |
708f4980 | 871 | pr_arity arity; |
faf9a90c C |
872 | nest_dots multi (statement arity) |
873 | (function _ -> | |
874 | print_between force_newline | |
875 | (whencode (dots force_newline (statement "")) (statement "")) whn; | |
876 | force_newline()) | |
877 | stmt_dots | |
878 | | Ast.Nest(_) -> raise CantBeInPlus | |
879 | | Ast.Dots(d,whn,_,_) | Ast.Circles(d,whn,_,_) | Ast.Stars(d,whn,_,_) -> | |
880 | if generating | |
881 | then | |
708f4980 | 882 | (pr_arity arity; mcode print_string d; |
faf9a90c C |
883 | print_between force_newline |
884 | (whencode (dots force_newline (statement "")) (statement "")) whn; | |
885 | force_newline()) | |
886 | else raise CantBeInPlus | |
34e49164 | 887 | |
ae4735db | 888 | | Ast.OptStm(s) | Ast.UniqueStm(s) -> |
34e49164 C |
889 | raise CantBeInPlus |
890 | ||
faf9a90c C |
891 | and whencode notfn alwaysfn = function |
892 | Ast.WhenNot a -> | |
708f4980 | 893 | print_text " WHEN != "; notfn a |
faf9a90c | 894 | | Ast.WhenAlways a -> |
708f4980 C |
895 | print_text " WHEN = "; alwaysfn a |
896 | | Ast.WhenModifier x -> print_text " WHEN "; print_when_modif x | |
faf9a90c | 897 | | Ast.WhenNotTrue a -> |
708f4980 | 898 | print_text " WHEN != TRUE "; rule_elem "" a |
faf9a90c | 899 | | Ast.WhenNotFalse a -> |
708f4980 | 900 | print_text " WHEN != FALSE "; rule_elem "" a |
faf9a90c C |
901 | |
902 | and print_when_modif = function | |
708f4980 C |
903 | | Ast.WhenAny -> print_text "ANY" |
904 | | Ast.WhenStrict -> print_text "STRICT" | |
905 | | Ast.WhenForall -> print_text "FORALL" | |
906 | | Ast.WhenExists -> print_text "EXISTS" | |
faf9a90c | 907 | |
34e49164 C |
908 | and case_line arity c = |
909 | match Ast.unwrap c with | |
910 | Ast.CaseLine(header,code) -> | |
708f4980 | 911 | rule_elem arity header; pr_space(); |
34e49164 C |
912 | dots force_newline (statement arity) code |
913 | | Ast.OptCase(case) -> raise CantBeInPlus in | |
914 | ||
915 | let top_level t = | |
916 | match Ast.unwrap t with | |
917 | Ast.FILEINFO(old_file,new_file) -> raise CantBeInPlus | |
918 | | Ast.DECL(stmt) -> statement "" stmt | |
919 | | Ast.CODE(stmt_dots) -> dots force_newline (statement "") stmt_dots | |
920 | | Ast.ERRORWORDS(exps) -> raise CantBeInPlus | |
921 | in | |
922 | ||
ae4735db | 923 | (* |
34e49164 C |
924 | let rule = |
925 | print_between (function _ -> force_newline(); force_newline()) top_level | |
926 | in | |
927 | *) | |
928 | ||
929 | let if_open_brace = function "{" -> true | _ -> false in | |
930 | ||
0708f913 | 931 | (* boolean result indicates whether an indent is needed *) |
34e49164 C |
932 | let rec pp_any = function |
933 | (* assert: normally there is only CONTEXT NOTHING tokens in any *) | |
934 | Ast.FullTypeTag(x) -> fullType x; false | |
708f4980 C |
935 | | Ast.BaseTypeTag(x) -> baseType x unknown unknown; false |
936 | | Ast.StructUnionTag(x) -> structUnion x unknown unknown; false | |
937 | | Ast.SignTag(x) -> sign x unknown unknown; false | |
34e49164 C |
938 | |
939 | | Ast.IdentTag(x) -> ident x; false | |
940 | ||
941 | | Ast.ExpressionTag(x) -> expression x; false | |
942 | ||
708f4980 C |
943 | | Ast.ConstantTag(x) -> constant x unknown unknown; false |
944 | | Ast.UnaryOpTag(x) -> unaryOp x unknown unknown; false | |
945 | | Ast.AssignOpTag(x) -> assignOp x unknown unknown; false | |
946 | | Ast.FixOpTag(x) -> fixOp x unknown unknown; false | |
947 | | Ast.BinaryOpTag(x) -> binaryOp x unknown unknown; false | |
948 | | Ast.ArithOpTag(x) -> arithOp x unknown unknown; false | |
949 | | Ast.LogicalOpTag(x) -> logicalOp x unknown unknown; false | |
34e49164 C |
950 | |
951 | | Ast.InitTag(x) -> initialiser false x; false | |
952 | | Ast.DeclarationTag(x) -> declaration x; false | |
953 | ||
708f4980 C |
954 | | Ast.StorageTag(x) -> storage x unknown unknown; false |
955 | | Ast.IncFileTag(x) -> inc_file x unknown unknown; false | |
34e49164 C |
956 | |
957 | | Ast.Rule_elemTag(x) -> rule_elem "" x; false | |
958 | | Ast.StatementTag(x) -> statement "" x; false | |
959 | | Ast.CaseLineTag(x) -> case_line "" x; false | |
960 | ||
708f4980 | 961 | | Ast.ConstVolTag(x) -> const_vol x unknown unknown; false |
c3e37e97 C |
962 | | Ast.Pragma(xs) -> |
963 | let print = function Ast.Noindent s | Ast.Indent s -> print_text s in | |
964 | print_between force_newline print xs; false | |
708f4980 | 965 | | Ast.Token(x,None) -> print_text x; if_open_brace x |
ae4735db | 966 | | Ast.Token(x,Some info) -> |
34e49164 | 967 | mcode |
708f4980 | 968 | (fun x line lcol -> |
34e49164 | 969 | (match x with |
708f4980 | 970 | "else" -> force_newline() |
34e49164 | 971 | | _ -> ()); |
002099fc | 972 | print_string x line lcol) |
faf9a90c C |
973 | (let nomcodekind = Ast.CONTEXT(Ast.DontCarePos,Ast.NOTHING) in |
974 | (x,info,nomcodekind,Ast.NoMetaPos)); | |
34e49164 C |
975 | if_open_brace x |
976 | ||
977 | | Ast.Code(x) -> let _ = top_level x in false | |
978 | ||
ae4735db | 979 | (* this is not '...', but a list of expr/statement/params, and |
34e49164 C |
980 | normally there should be no '...' inside them *) |
981 | | Ast.ExprDotsTag(x) -> dots (function _ -> ()) expression x; false | |
982 | | Ast.ParamDotsTag(x) -> parameter_list x; false | |
708f4980 C |
983 | | Ast.StmtDotsTag(x) -> dots force_newline (statement "") x; false |
984 | | Ast.DeclDotsTag(x) -> dots force_newline declaration x; false | |
34e49164 C |
985 | |
986 | | Ast.TypeCTag(x) -> typeC x; false | |
987 | | Ast.ParamTag(x) -> parameterTypeDef x; false | |
988 | | Ast.SgrepStartTag(x) -> failwith "unexpected start tag" | |
989 | | Ast.SgrepEndTag(x) -> failwith "unexpected end tag" | |
990 | in | |
991 | ||
faf9a90c C |
992 | anything := (function x -> let _ = pp_any x in ()); |
993 | ||
34e49164 C |
994 | (* todo? imitate what is in pretty_print_cocci ? *) |
995 | match xxs with | |
996 | [] -> () | |
ae4735db | 997 | | x::xs -> |
34e49164 C |
998 | (* for many tags, we must not do a newline before the first '+' *) |
999 | let isfn s = | |
1000 | match Ast.unwrap s with Ast.FunDecl _ -> true | _ -> false in | |
1001 | let unindent_before = function | |
1002 | (* need to get unindent before newline for } *) | |
1003 | (Ast.Token ("}",_)::_) -> true | |
1004 | | _ -> false in | |
1005 | let prnl x = | |
c3e37e97 | 1006 | (if unindent_before x then unindent true); |
708f4980 | 1007 | force_newline() in |
34e49164 | 1008 | let newline_before _ = |
b1b2de81 | 1009 | if before =*= After |
34e49164 C |
1010 | then |
1011 | let hd = List.hd xxs in | |
1012 | match hd with | |
708f4980 C |
1013 | (Ast.StatementTag s::_) when isfn s -> |
1014 | force_newline(); force_newline() | |
0708f913 | 1015 | | (Ast.Pragma _::_) |
34e49164 C |
1016 | | (Ast.Rule_elemTag _::_) | (Ast.StatementTag _::_) |
1017 | | (Ast.InitTag _::_) | |
1018 | | (Ast.DeclarationTag _::_) | (Ast.Token ("}",_)::_) -> prnl hd | |
1019 | | _ -> () in | |
1020 | let newline_after _ = | |
b1b2de81 | 1021 | if before =*= Before |
34e49164 C |
1022 | then |
1023 | match List.rev(List.hd(List.rev xxs)) with | |
0708f913 | 1024 | (Ast.StatementTag s::_) -> |
708f4980 C |
1025 | (if isfn s then force_newline()); |
1026 | force_newline() | |
0708f913 C |
1027 | | (Ast.Pragma _::_) |
1028 | | (Ast.Rule_elemTag _::_) | (Ast.InitTag _::_) | |
708f4980 C |
1029 | | (Ast.DeclarationTag _::_) | (Ast.Token ("{",_)::_) -> |
1030 | force_newline() | |
34e49164 C |
1031 | | _ -> () in |
1032 | (* print a newline at the beginning, if needed *) | |
1033 | newline_before(); | |
1034 | (* print a newline before each of the rest *) | |
1035 | let rec loop leading_newline indent_needed = function | |
1036 | [] -> () | |
1037 | | x::xs -> | |
1038 | (if leading_newline | |
1039 | then | |
1040 | match (indent_needed,unindent_before x) with | |
708f4980 C |
1041 | (true,true) -> force_newline() |
1042 | | (true,false) -> force_newline(); indent() | |
c3e37e97 | 1043 | | (false,true) -> unindent true; force_newline() |
708f4980 | 1044 | | (false,false) -> force_newline()); |
002099fc C |
1045 | let space_needed_before = function |
1046 | Ast.ParamTag(x) -> | |
1047 | (match Ast.unwrap x with | |
1048 | Ast.PComma _ -> false | |
1049 | | _ -> true) | |
1050 | | Ast.ExpressionTag(x) -> | |
1051 | (match Ast.unwrap x with | |
1052 | Ast.EComma _ -> false | |
1053 | | _ -> true) | |
1054 | | Ast.InitTag(x) -> | |
1055 | (match Ast.unwrap x with | |
1056 | Ast.IComma _ -> false | |
1057 | | _ -> true) | |
1058 | | Ast.Token(t,_) when List.mem t [",";";";"(";")"] -> false | |
1059 | | _ -> true in | |
1060 | let space_needed_after = function | |
1061 | Ast.Token(t,_) when List.mem t ["("] -> (*never needed*) false | |
1062 | | Ast.Token(t,_) when List.mem t ["if";"for";"while";"do"] -> | |
1063 | (* space always needed *) | |
1064 | pr_space(); false | |
1065 | | _ -> true in | |
34e49164 | 1066 | let indent_needed = |
002099fc C |
1067 | let rec loop space_after indent_needed = function |
1068 | [] -> indent_needed | |
1069 | | x::xs -> | |
1070 | (if space_after && space_needed_before x | |
1071 | then pr_space()); | |
1072 | let indent_needed = pp_any x in | |
1073 | let space_after = space_needed_after x in | |
1074 | loop space_after indent_needed xs in | |
1075 | loop false false x in | |
34e49164 C |
1076 | loop true indent_needed xs in |
1077 | loop false false (x::xs); | |
1078 | (* print a newline at the end, if needed *) | |
1079 | newline_after() | |
1080 | ||
951c7801 C |
1081 | let rec pp_list_list_any (envs, pr, pr_celem, pr_cspace, pr_space, pr_arity, |
1082 | pr_barrier, indent, unindent) | |
1083 | generating xxs before = | |
1084 | List.iter | |
1085 | (function env -> | |
1086 | do_all (env, pr, pr_celem, pr_cspace, pr_space, pr_arity, pr_barrier, | |
1087 | indent, unindent) | |
1088 | generating xxs before) | |
1089 | envs |