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