Coccinelle release 0.2.5-rc7.
[bpt/coccinelle.git] / parsing_cocci / pretty_print_cocci.ml
CommitLineData
c491d8ee
C
1(*
2 * Copyright 2010, INRIA, University of Copenhagen
3 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
4 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
5 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
6 * This file is part of Coccinelle.
7 *
8 * Coccinelle is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, according to version 2 of the License.
11 *
12 * Coccinelle is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * The authors reserve the right to distribute this or future versions of
21 * Coccinelle under other licenses.
22 *)
23
24
34e49164
C
25open Format
26module Ast = Ast_cocci
27
28let print_plus_flag = ref true
29let print_minus_flag = ref true
30let print_newlines_disj = ref true
31
32let start_block str =
33 force_newline(); print_string " "; open_box 0
34
35let end_block str =
36 close_box(); force_newline ()
37
38let print_string_box s = print_string s; open_box 0
39
40
41let print_option = Common.do_option
42let print_between = Common.print_between
43
44(* --------------------------------------------------------------------- *)
45(* Modified code *)
46
47(* avoid polyvariance problems *)
48let anything : (Ast.anything -> unit) ref = ref (function _ -> ())
49
50let rec print_anything str = function
51 [] -> ()
52 | stream ->
53 start_block();
54 print_between force_newline
55 (function x ->
56 print_string str; open_box 0; print_anything_list x; close_box())
57 stream;
58 end_block()
59
60and print_anything_list = function
61 [] -> ()
62 | [x] -> !anything x
63 | bef::((aft::_) as rest) ->
64 !anything bef;
65 let space =
66 (match bef with
67 Ast.Rule_elemTag(_) | Ast.AssignOpTag(_) | Ast.BinaryOpTag(_)
68 | Ast.ArithOpTag(_) | Ast.LogicalOpTag(_)
69 | Ast.Token("if",_) | Ast.Token("while",_) -> true | _ -> false) or
70 (match aft with
71 Ast.Rule_elemTag(_) | Ast.AssignOpTag(_) | Ast.BinaryOpTag(_)
72 | Ast.ArithOpTag(_) | Ast.LogicalOpTag(_) | Ast.Token("{",_) -> true
73 | _ -> false) in
74 if space then print_string " ";
75 print_anything_list rest
76
77let print_around printer term = function
78 Ast.NOTHING -> printer term
951c7801
C
79 | Ast.BEFORE(bef,_) -> print_anything "<<< " bef; printer term
80 | Ast.AFTER(aft,_) -> printer term; print_anything ">>> " aft
81 | Ast.BEFOREAFTER(bef,aft,_) ->
34e49164
C
82 print_anything "<<< " bef; printer term; print_anything ">>> " aft
83
84let print_string_befaft fn x info =
c3e37e97
C
85 let print = function Ast.Noindent s | Ast.Indent s -> print_string s in
86 List.iter (function (s,_,_) -> print s; force_newline()) info.Ast.strbef;
34e49164 87 fn x;
c3e37e97 88 List.iter (function (s,_,_) -> force_newline(); print s) info.Ast.straft
34e49164
C
89
90let print_meta (r,x) = print_string r; print_string ":"; print_string x
91
92let print_pos = function
93 Ast.MetaPos(name,_,_,_,_) ->
94 let name = Ast.unwrap_mcode name in
95 print_string "@"; print_meta name
96 | _ -> ()
97
98let mcode fn = function
708f4980 99 (x, _, Ast.MINUS(_,_,adj,plus_stream), pos) ->
34e49164
C
100 if !print_minus_flag
101 then print_string (if !Flag.sgrep_mode2 then "*" else "-");
102 fn x; print_pos pos;
faf9a90c 103 if !print_plus_flag
34e49164 104 then print_anything ">>> " plus_stream
faf9a90c 105 | (x, _, Ast.CONTEXT(_,plus_streams), pos) ->
34e49164
C
106 if !print_plus_flag
107 then
108 let fn x = fn x; print_pos pos in
109 print_around fn x plus_streams
110 else (fn x; print_pos pos)
951c7801 111 | (x, info, Ast.PLUS _, pos) ->
34e49164
C
112 let fn x = fn x; print_pos pos in
113 print_string_befaft fn x info
114
faf9a90c 115let print_mcodekind = function
708f4980 116 Ast.MINUS(_,_,_,plus_stream) ->
34e49164
C
117 print_string "MINUS";
118 print_anything ">>> " plus_stream
119 | Ast.CONTEXT(_,plus_streams) ->
120 print_around (function _ -> print_string "CONTEXT") () plus_streams
951c7801 121 | Ast.PLUS _ -> print_string "PLUS"
34e49164
C
122
123(* --------------------------------------------------------------------- *)
124(* --------------------------------------------------------------------- *)
125(* Dots *)
126
127let dots between fn d =
128 match Ast.unwrap d with
129 Ast.DOTS(l) -> print_between between fn l
130 | Ast.CIRCLES(l) -> print_between between fn l
131 | Ast.STARS(l) -> print_between between fn l
132
5636bb2c
C
133let nest_dots starter ender fn f d =
134 mcode print_string starter;
135 f(); start_block();
136 (match Ast.unwrap d with
137 Ast.DOTS(l) -> print_between force_newline fn l
138 | Ast.CIRCLES(l) -> print_between force_newline fn l
139 | Ast.STARS(l) -> print_between force_newline fn l);
140 end_block();
141 mcode print_string ender
34e49164 142
d3f655c6
C
143(* --------------------------------------------------------------------- *)
144(* Disjunctions *)
145
146let print_disj_list fn l =
147 if !print_newlines_disj
148 then (force_newline(); print_string "("; force_newline())
149 else print_string "(";
150 print_between
151 (function _ ->
152 if !print_newlines_disj
153 then (force_newline(); print_string "|"; force_newline())
154 else print_string " | ")
155 fn l;
156 if !print_newlines_disj
157 then (force_newline(); print_string ")"; force_newline())
158 else print_string ")"
159
34e49164
C
160(* --------------------------------------------------------------------- *)
161
162let print_type keep info = function
163 None -> ()
164 (* print_string "/* ";
165 print_string "keep:"; print_unitary keep;
166 print_string " inherited:"; print_bool inherited;
167 print_string " */"*)
168 | Some ty -> ()
169 (*;
170 print_string "/* ";
171 print_between (function _ -> print_string ", ") Type_cocci.typeC ty;(*
172 print_string "keep:"; print_unitary keep;
173 print_string " inherited:"; print_bool inherited;*)
174 print_string " */"*)
175
951c7801
C
176(* --------------------------------------------------------------------- *)
177(* Contraint on Identifier and Function *)
178(* FIXME: Not called at the moment *)
179
5636bb2c
C
180let rec idconstraint = function
181 Ast.IdNoConstraint -> print_string "/* No constraint */"
182 | Ast.IdNegIdSet (str,meta) ->
183 List.iter (function s -> print_string (" "^s)) str;
184 List.iter (function (r,n) -> print_string " "; print_meta(r,n)) meta
185 | Ast.IdRegExpConstraint re -> regconstraint re
186
187and regconstraint = function
188 Ast.IdRegExp (re,_) ->
189 print_string "~= \""; print_string re; print_string "\""
190 | Ast.IdNotRegExp (re,_) ->
191 print_string "~!= \""; print_string re; print_string "\""
951c7801 192
34e49164
C
193(* --------------------------------------------------------------------- *)
194(* Identifier *)
195
196let rec ident i =
197 match Ast.unwrap i with
d3f655c6
C
198 Ast.Id(name) -> mcode print_string name
199 | Ast.MetaId(name,_,keep,inherited) -> mcode print_meta name
200 | Ast.MetaFunc(name,_,_,_) -> mcode print_meta name
201 | Ast.MetaLocalFunc(name,_,_,_) -> mcode print_meta name
202 | Ast.DisjId(id_list) -> print_disj_list ident id_list
203 | Ast.OptIdent(id) -> print_string "?"; ident id
204 | Ast.UniqueIdent(id) -> print_string "!"; ident id
34e49164
C
205
206and print_unitary = function
207 Type_cocci.Unitary -> print_string "unitary"
208 | Type_cocci.Nonunitary -> print_string "nonunitary"
209 | Type_cocci.Saved -> print_string "saved"
210
211(* --------------------------------------------------------------------- *)
212(* Expression *)
213
34e49164
C
214let rec expression e =
215 match Ast.unwrap e with
216 Ast.Ident(id) -> ident id
217 | Ast.Constant(const) -> mcode constant const
218 | Ast.FunCall(fn,lp,args,rp) ->
219 expression fn; mcode print_string_box lp;
220 dots (function _ -> ()) expression args;
221 close_box(); mcode print_string rp
222 | Ast.Assignment(left,op,right,simple) ->
223 expression left; print_string " "; mcode assignOp op;
224 print_string " "; expression right
225 | Ast.CondExpr(exp1,why,exp2,colon,exp3) ->
226 expression exp1; print_string " "; mcode print_string why;
227 print_option (function e -> print_string " "; expression e) exp2;
228 print_string " "; mcode print_string colon; expression exp3
229 | Ast.Postfix(exp,op) -> expression exp; mcode fixOp op
230 | Ast.Infix(exp,op) -> mcode fixOp op; expression exp
231 | Ast.Unary(exp,op) -> mcode unaryOp op; expression exp
232 | Ast.Binary(left,op,right) ->
233 expression left; print_string " "; mcode binaryOp op; print_string " ";
234 expression right
235 | Ast.Nested(left,op,right) ->
236 expression left; print_string " "; mcode binaryOp op; print_string " ";
237 expression right
238 | Ast.Paren(lp,exp,rp) ->
239 mcode print_string_box lp; expression exp; close_box();
240 mcode print_string rp
241 | Ast.ArrayAccess(exp1,lb,exp2,rb) ->
242 expression exp1; mcode print_string_box lb; expression exp2; close_box();
243 mcode print_string rb
244 | Ast.RecordAccess(exp,pt,field) ->
245 expression exp; mcode print_string pt; ident field
246 | Ast.RecordPtAccess(exp,ar,field) ->
247 expression exp; mcode print_string ar; ident field
248 | Ast.Cast(lp,ty,rp,exp) ->
249 mcode print_string_box lp; fullType ty; close_box();
250 mcode print_string rp; expression exp
251 | Ast.SizeOfExpr(sizeof,exp) ->
252 mcode print_string sizeof; expression exp
253 | Ast.SizeOfType(sizeof,lp,ty,rp) ->
254 mcode print_string sizeof;
255 mcode print_string_box lp; fullType ty; close_box();
256 mcode print_string rp
257 | Ast.TypeExp(ty) -> fullType ty
258
259 | Ast.MetaErr(name,_,_,_) -> mcode print_meta name
260 | Ast.MetaExpr(name,_,keep,ty,form,inherited) ->
261 mcode print_meta name; print_type keep inherited ty
262 | Ast.MetaExprList(name,_,_,_) -> mcode print_meta name
263 | Ast.EComma(cm) -> mcode print_string cm; print_space()
264 | Ast.DisjExpr(exp_list) -> print_disj_list expression exp_list
5636bb2c
C
265 | Ast.NestExpr(starter,expr_dots,ender,Some whencode,multi) ->
266 nest_dots starter ender expression
34e49164
C
267 (function _ -> print_string " when != "; expression whencode)
268 expr_dots
5636bb2c
C
269 | Ast.NestExpr(starter,expr_dots,ender,None,multi) ->
270 nest_dots starter ender expression (function _ -> ()) expr_dots
34e49164
C
271 | Ast.Edots(dots,Some whencode)
272 | Ast.Ecircles(dots,Some whencode)
273 | Ast.Estars(dots,Some whencode) ->
274 mcode print_string dots; print_string " when != "; expression whencode
275 | Ast.Edots(dots,None)
276 | Ast.Ecircles(dots,None)
277 | Ast.Estars(dots,None) -> mcode print_string dots
278 | Ast.OptExp(exp) -> print_string "?"; expression exp
279 | Ast.UniqueExp(exp) -> print_string "!"; expression exp
280
281and unaryOp = function
282 Ast.GetRef -> print_string "&"
283 | Ast.DeRef -> print_string "*"
284 | Ast.UnPlus -> print_string "+"
285 | Ast.UnMinus -> print_string "-"
286 | Ast.Tilde -> print_string "~"
287 | Ast.Not -> print_string "!"
288
289and assignOp = function
290 Ast.SimpleAssign -> print_string "="
291 | Ast.OpAssign(aop) -> arithOp aop; print_string "="
292
293and fixOp = function
294 Ast.Dec -> print_string "--"
295 | Ast.Inc -> print_string "++"
296
297and binaryOp = function
298 Ast.Arith(aop) -> arithOp aop
299 | Ast.Logical(lop) -> logicalOp lop
300
301and arithOp = function
302 Ast.Plus -> print_string "+"
303 | Ast.Minus -> print_string "-"
304 | Ast.Mul -> print_string "*"
305 | Ast.Div -> print_string "/"
306 | Ast.Mod -> print_string "%"
307 | Ast.DecLeft -> print_string "<<"
308 | Ast.DecRight -> print_string ">>"
309 | Ast.And -> print_string "&"
310 | Ast.Or -> print_string "|"
311 | Ast.Xor -> print_string "^"
312
313and logicalOp = function
314 Ast.Inf -> print_string "<"
315 | Ast.Sup -> print_string ">"
316 | Ast.InfEq -> print_string "<="
317 | Ast.SupEq -> print_string ">="
318 | Ast.Eq -> print_string "=="
319 | Ast.NotEq -> print_string "!="
320 | Ast.AndLog -> print_string "&&"
321 | Ast.OrLog -> print_string "||"
322
323and constant = function
324 Ast.String(s) -> print_string "\""; print_string s; print_string "\""
325 | Ast.Char(s) -> print_string "'"; print_string s; print_string "'"
326 | Ast.Int(s) -> print_string s
327 | Ast.Float(s) -> print_string s
328
329(* --------------------------------------------------------------------- *)
330(* Declarations *)
331
332and storage = function
333 Ast.Static -> print_string "static "
334 | Ast.Auto -> print_string "auto "
335 | Ast.Register -> print_string "register "
336 | Ast.Extern -> print_string "extern "
337
338(* --------------------------------------------------------------------- *)
339(* Types *)
340
341and fullType ft =
342 match Ast.unwrap ft with
343 Ast.Type(cv,ty) ->
344 print_option (function x -> mcode const_vol x; print_string " ") cv;
345 typeC ty
346 | Ast.DisjType(decls) -> print_disj_list fullType decls
347 | Ast.OptType(ty) -> print_string "?"; fullType ty
348 | Ast.UniqueType(ty) -> print_string "!"; fullType ty
349
350and print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2) fn =
351 fullType ty; mcode print_string lp1; mcode print_string star; fn();
352 mcode print_string rp1; mcode print_string lp1;
353 parameter_list params; mcode print_string rp2
354
355and print_function_type (ty,lp1,params,rp1) fn =
356 print_option fullType ty; fn(); mcode print_string lp1;
357 parameter_list params; mcode print_string rp1
358
359and print_fninfo = function
360 Ast.FStorage(stg) -> mcode storage stg
361 | Ast.FType(ty) -> fullType ty
362 | Ast.FInline(inline) -> mcode print_string inline; print_string " "
363 | Ast.FAttr(attr) -> mcode print_string attr; print_string " "
364
365and typeC ty =
366 match Ast.unwrap ty with
faf9a90c
C
367 Ast.BaseType(ty,strings) ->
368 List.iter (function s -> mcode print_string s; print_string " ") strings
369 | Ast.SignedT(sgn,ty) -> mcode sign sgn; print_option typeC ty
34e49164
C
370 | Ast.Pointer(ty,star) -> fullType ty; mcode print_string star
371 | Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
372 print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2)
373 (function _ -> ())
374 | Ast.FunctionType (_,ty,lp1,params,rp1) ->
375 print_function_type (ty,lp1,params,rp1) (function _ -> ())
376 | Ast.Array(ty,lb,size,rb) ->
377 fullType ty; mcode print_string lb; print_option expression size;
378 mcode print_string rb
c491d8ee
C
379 | Ast.EnumName(kind,name) ->
380 mcode print_string kind;
381 print_option (function x -> ident x; print_string " ") name
382 | Ast.EnumDef(ty,lb,ids,rb) ->
383 fullType ty; mcode print_string lb;
384 dots force_newline expression ids;
385 mcode print_string rb
34e49164
C
386 | Ast.StructUnionName(kind,name) ->
387 mcode structUnion kind;
388 print_option (function x -> ident x; print_string " ") name
389 | Ast.StructUnionDef(ty,lb,decls,rb) ->
390 fullType ty; mcode print_string lb;
391 dots force_newline declaration decls;
392 mcode print_string rb
393 | Ast.TypeName(name) -> mcode print_string name; print_string " "
394 | Ast.MetaType(name,_,_) ->
395 mcode print_meta name; print_string " "
396
397and baseType = function
398 Ast.VoidType -> print_string "void "
399 | Ast.CharType -> print_string "char "
400 | Ast.ShortType -> print_string "short "
401 | Ast.IntType -> print_string "int "
402 | Ast.DoubleType -> print_string "double "
403 | Ast.FloatType -> print_string "float "
404 | Ast.LongType -> print_string "long "
faf9a90c 405 | Ast.LongLongType -> print_string "long long "
1eddfd50
C
406 | Ast.SizeType -> print_string "size_t "
407 | Ast.SSizeType -> print_string "ssize_t "
408 | Ast.PtrDiffType -> print_string "ptrdiff_t "
34e49164
C
409
410and structUnion = function
411 Ast.Struct -> print_string "struct "
412 | Ast.Union -> print_string "union "
413
414and sign = function
415 Ast.Signed -> print_string "signed "
416 | Ast.Unsigned -> print_string "unsigned "
417
418and const_vol = function
419 Ast.Const -> print_string "const"
420 | Ast.Volatile -> print_string "volatile"
421
422(* --------------------------------------------------------------------- *)
423(* Variable declaration *)
424(* Even if the Cocci program specifies a list of declarations, they are
425 split out into multiple declarations of a single variable each. *)
426
427and print_named_type ty id =
428 match Ast.unwrap ty with
429 Ast.Type(None,ty1) ->
430 (match Ast.unwrap ty1 with
431 Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
432 print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2)
433 (function _ -> print_string " "; ident id)
434 | Ast.FunctionType(_,ty,lp1,params,rp1) ->
435 print_function_type (ty,lp1,params,rp1)
436 (function _ -> print_string " "; ident id)
437 | Ast.Array(ty,lb,size,rb) ->
438 let rec loop ty k =
439 match Ast.unwrap ty with
440 Ast.Array(ty,lb,size,rb) ->
441 (match Ast.unwrap ty with
442 Ast.Type(None,ty) ->
443 loop ty
444 (function _ ->
445 k ();
446 mcode print_string lb;
447 print_option expression size;
448 mcode print_string rb)
449 | _ -> failwith "complex array types not supported")
450 | _ -> typeC ty; ident id; k () in
451 loop ty1 (function _ -> ())
452 | _ -> fullType ty; ident id)
453 | _ -> fullType ty; ident id
454
455and declaration d =
456 match Ast.unwrap d with
413ffc02
C
457 Ast.MetaDecl(name,_,_) | Ast.MetaField(name,_,_) -> mcode print_meta name
458 | Ast.Init(stg,ty,id,eq,ini,sem) ->
34e49164
C
459 print_option (mcode storage) stg; print_named_type ty id;
460 print_string " "; mcode print_string eq;
461 print_string " "; initialiser ini; mcode print_string sem
462 | Ast.UnInit(stg,ty,id,sem) ->
463 print_option (mcode storage) stg; print_named_type ty id;
464 mcode print_string sem
465 | Ast.MacroDecl(name,lp,args,rp,sem) ->
466 ident name; mcode print_string_box lp;
467 dots (function _ -> ()) expression args;
468 close_box(); mcode print_string rp; mcode print_string sem
469 | Ast.TyDecl(ty,sem) -> fullType ty; mcode print_string sem
470 | Ast.Typedef(stg,ty,id,sem) ->
471 mcode print_string stg; print_string " "; fullType ty; typeC id;
472 mcode print_string sem
473 | Ast.DisjDecl(decls) -> print_disj_list declaration decls
faf9a90c 474 | Ast.Ddots(dots,Some whencode) ->
34e49164
C
475 mcode print_string dots; print_string " when != "; declaration whencode
476 | Ast.Ddots(dots,None) -> mcode print_string dots
34e49164
C
477 | Ast.OptDecl(decl) -> print_string "?"; declaration decl
478 | Ast.UniqueDecl(decl) -> print_string "!"; declaration decl
479
480(* --------------------------------------------------------------------- *)
481(* Initialiser *)
482
483and initialiser i =
484 match Ast.unwrap i with
113803cf
C
485 Ast.MetaInit(name,_,_) ->
486 mcode print_meta name; print_string " "
487 | Ast.InitExpr(exp) -> expression exp
c491d8ee
C
488 | Ast.ArInitList(lb,initlist,rb) ->
489 mcode print_string lb; open_box 0;
490 dots force_newline initialiser initlist; close_box();
491 mcode print_string rb
492 | Ast.StrInitList(allminus,lb,initlist,rb,whencode) ->
34e49164
C
493 mcode print_string lb; open_box 0;
494 if not (whencode = [])
495 then
496 (print_string " WHEN != ";
497 print_between (function _ -> print_string " v ")
498 initialiser whencode;
499 force_newline());
500 List.iter initialiser initlist; close_box();
501 mcode print_string rb
113803cf
C
502 | Ast.InitGccExt(designators,eq,ini) ->
503 List.iter designator designators; print_string " ";
34e49164
C
504 mcode print_string eq; print_string " "; initialiser ini
505 | Ast.InitGccName(name,eq,ini) ->
506 ident name; mcode print_string eq; initialiser ini
34e49164 507 | Ast.IComma(comma) -> mcode print_string comma; force_newline()
c491d8ee
C
508 | Ast.Idots(dots,Some whencode) ->
509 mcode print_string dots; print_string " when != "; initialiser whencode
510 | Ast.Idots(dots,None) -> mcode print_string dots
34e49164
C
511 | Ast.OptIni(ini) -> print_string "?"; initialiser ini
512 | Ast.UniqueIni(ini) -> print_string "!"; initialiser ini
513
113803cf
C
514and designator = function
515 Ast.DesignatorField(dot,id) -> mcode print_string dot; ident id
516 | Ast.DesignatorIndex(lb,exp,rb) ->
517 mcode print_string lb; expression exp; mcode print_string rb
518 | Ast.DesignatorRange(lb,min,dots,max,rb) ->
519 mcode print_string lb; expression min; mcode print_string dots;
520 expression max; mcode print_string rb
521
34e49164
C
522(* --------------------------------------------------------------------- *)
523(* Parameter *)
524
525and parameterTypeDef p =
526 match Ast.unwrap p with
527 Ast.VoidParam(ty) -> fullType ty
528 | Ast.Param(ty,Some id) -> print_named_type ty id
529 | Ast.Param(ty,None) -> fullType ty
530 | Ast.MetaParam(name,_,_) -> mcode print_meta name
531 | Ast.MetaParamList(name,_,_,_) -> mcode print_meta name
532 | Ast.PComma(cm) -> mcode print_string cm; print_space()
533 | Ast.Pdots(dots) -> mcode print_string dots
534 | Ast.Pcircles(dots) -> mcode print_string dots
535 | Ast.OptParam(param) -> print_string "?"; parameterTypeDef param
536 | Ast.UniqueParam(param) -> print_string "!"; parameterTypeDef param
537
538and parameter_list l = dots (function _ -> ()) parameterTypeDef l
539
540(* --------------------------------------------------------------------- *)
541(* Top-level code *)
542
543let rec rule_elem arity re =
544 match Ast.unwrap re with
545 Ast.FunHeader(bef,allminus,fninfo,name,lp,params,rp) ->
546 mcode (function _ -> ()) ((),Ast.no_info,bef,Ast.NoMetaPos);
547 print_string arity; List.iter print_fninfo fninfo;
548 ident name; mcode print_string_box lp;
549 parameter_list params; close_box(); mcode print_string rp;
550 print_string " "
551 | Ast.Decl(bef,allminus,decl) ->
552 mcode (function _ -> ()) ((),Ast.no_info,bef,Ast.NoMetaPos);
553 print_string arity;
554 declaration decl
555 | Ast.SeqStart(brace) ->
556 print_string arity; mcode print_string brace;
557 if !print_newlines_disj then start_block()
558 | Ast.SeqEnd(brace) ->
559 if !print_newlines_disj then end_block();
560 print_string arity; mcode print_string brace
561 | Ast.ExprStatement(exp,sem) ->
562 print_string arity; expression exp; mcode print_string sem
563 | Ast.IfHeader(iff,lp,exp,rp) ->
564 print_string arity;
565 mcode print_string iff; print_string " "; mcode print_string_box lp;
566 expression exp; close_box(); mcode print_string rp; print_string " "
567 | Ast.Else(els) ->
568 print_string arity; mcode print_string els; print_string " "
569 | Ast.WhileHeader(whl,lp,exp,rp) ->
570 print_string arity;
571 mcode print_string whl; print_string " "; mcode print_string_box lp;
572 expression exp; close_box(); mcode print_string rp; print_string " "
573 | Ast.DoHeader(d) ->
574 print_string arity; mcode print_string d; print_string " "
575 | Ast.WhileTail(whl,lp,exp,rp,sem) ->
576 print_string arity;
577 mcode print_string whl; print_string " "; mcode print_string_box lp;
578 expression exp; close_box(); mcode print_string rp;
579 mcode print_string sem
580 | Ast.ForHeader(fr,lp,e1,sem1,e2,sem2,e3,rp) ->
581 print_string arity;
582 mcode print_string fr; mcode print_string_box lp;
583 print_option expression e1; mcode print_string sem1;
584 print_option expression e2; mcode print_string sem2;
585 print_option expression e3; close_box();
586 mcode print_string rp; print_string " "
587 | Ast.IteratorHeader(nm,lp,args,rp) ->
588 print_string arity;
589 ident nm; print_string " "; mcode print_string_box lp;
590 dots (function _ -> ()) expression args; close_box();
591 mcode print_string rp; print_string " "
592 | Ast.SwitchHeader(switch,lp,exp,rp) ->
593 print_string arity;
594 mcode print_string switch; print_string " "; mcode print_string_box lp;
595 expression exp; close_box(); mcode print_string rp; print_string " "
596 | Ast.Break(br,sem) ->
597 print_string arity; mcode print_string br; mcode print_string sem
598 | Ast.Continue(cont,sem) ->
599 print_string arity; mcode print_string cont; mcode print_string sem
600 | Ast.Label(l,dd) -> ident l; mcode print_string dd
601 | Ast.Goto(goto,l,sem) ->
602 mcode print_string goto; ident l; mcode print_string sem
603 | Ast.Return(ret,sem) ->
604 print_string arity; mcode print_string ret; mcode print_string sem
605 | Ast.ReturnExpr(ret,exp,sem) ->
606 print_string arity; mcode print_string ret; print_string " ";
607 expression exp; mcode print_string sem
608 | Ast.MetaRuleElem(name,_,_) ->
609 print_string arity; mcode print_meta name
610 | Ast.MetaStmt(name,_,_,_) ->
611 print_string arity; mcode print_meta name
612 | Ast.MetaStmtList(name,_,_) ->
613 print_string arity; mcode print_meta name
614 | Ast.Exp(exp) -> print_string arity; expression exp
615 | Ast.TopExp(exp) -> print_string arity; expression exp
616 | Ast.Ty(ty) -> print_string arity; fullType ty
1be43e12 617 | Ast.TopInit(init) -> initialiser init
34e49164
C
618 | Ast.Include(inc,s) ->
619 mcode print_string inc; print_string " "; mcode inc_file s
3a314143
C
620 | Ast.Undef(def,id) ->
621 mcode print_string def; print_string " "; ident id
34e49164
C
622 | Ast.DefineHeader(def,id,params) ->
623 mcode print_string def; print_string " "; ident id;
624 print_define_parameters params
625 | Ast.Default(def,colon) ->
626 mcode print_string def; mcode print_string colon; print_string " "
627 | Ast.Case(case,exp,colon) ->
628 mcode print_string case; print_string " "; expression exp;
629 mcode print_string colon; print_string " "
630 | Ast.DisjRuleElem(res) ->
631 print_string arity;
632 force_newline(); print_string "("; force_newline();
633 print_between
634 (function _ -> force_newline();print_string "|"; force_newline())
635 (rule_elem arity)
636 res;
637 force_newline(); print_string ")"
638
639
640and print_define_parameters params =
641 match Ast.unwrap params with
642 Ast.NoParams -> ()
643 | Ast.DParams(lp,params,rp) ->
644 mcode print_string lp;
645 dots (function _ -> ()) print_define_param params; mcode print_string rp
646
647and print_define_param param =
648 match Ast.unwrap param with
649 Ast.DParam(id) -> ident id
650 | Ast.DPComma(comma) -> mcode print_string comma
651 | Ast.DPdots(dots) -> mcode print_string dots
652 | Ast.DPcircles(circles) -> mcode print_string circles
653 | Ast.OptDParam(dp) -> print_string "?"; print_define_param dp
654 | Ast.UniqueDParam(dp) -> print_string "!"; print_define_param dp
655
656and statement arity s =
657 match Ast.unwrap s with
708f4980 658 Ast.Seq(lbrace,body,rbrace) ->
34e49164 659 rule_elem arity lbrace;
34e49164
C
660 dots force_newline (statement arity) body;
661 rule_elem arity rbrace
662 | Ast.IfThen(header,branch,(_,_,_,aft)) ->
663 rule_elem arity header; statement arity branch;
664 mcode (function _ -> ()) ((),Ast.no_info,aft,Ast.NoMetaPos)
665 | Ast.IfThenElse(header,branch1,els,branch2,(_,_,_,aft)) ->
666 rule_elem arity header; statement arity branch1; print_string " ";
667 rule_elem arity els; statement arity branch2;
668 mcode (function _ -> ()) ((),Ast.no_info,aft,Ast.NoMetaPos)
669 | Ast.While(header,body,(_,_,_,aft)) ->
670 rule_elem arity header; statement arity body;
671 mcode (function _ -> ()) ((),Ast.no_info,aft,Ast.NoMetaPos)
672 | Ast.Do(header,body,tail) ->
673 rule_elem arity header; statement arity body;
674 rule_elem arity tail
675 | Ast.For(header,body,(_,_,_,aft)) ->
676 rule_elem arity header; statement arity body;
677 mcode (function _ -> ()) ((),Ast.no_info,aft,Ast.NoMetaPos)
678 | Ast.Iterator(header,body,(_,_,_,aft)) ->
679 rule_elem arity header; statement arity body;
680 mcode (function _ -> ()) ((),Ast.no_info,aft,Ast.NoMetaPos)
fc1ad971 681 | Ast.Switch(header,lb,decls,cases,rb) ->
34e49164 682 rule_elem arity header; rule_elem arity lb;
fc1ad971 683 dots force_newline (statement arity) decls;
34e49164
C
684 List.iter (function x -> case_line arity x; force_newline()) cases;
685 rule_elem arity rb
686 | Ast.Atomic(re) -> rule_elem arity re
708f4980 687 | Ast.FunDecl(header,lbrace,body,rbrace) ->
34e49164 688 rule_elem arity header; rule_elem arity lbrace;
34e49164
C
689 dots force_newline (statement arity) body;
690 rule_elem arity rbrace
691 | Ast.Disj([stmt_dots]) ->
692 print_string arity;
693 dots (function _ -> if !print_newlines_disj then force_newline())
694 (statement arity) stmt_dots
695 | Ast.Disj(stmt_dots_list) -> (* ignores newline directive for readability *)
696 print_string arity;
697 force_newline(); print_string "("; force_newline();
698 print_between
699 (function _ -> force_newline();print_string "|"; force_newline())
700 (dots force_newline (statement arity))
701 stmt_dots_list;
702 force_newline(); print_string ")"
703 | Ast.Define(header,body) ->
704 rule_elem arity header; print_string " ";
705 dots force_newline (statement arity) body
5636bb2c 706 | Ast.Nest(starter,stmt_dots,ender,whn,multi,_,_) ->
34e49164 707 print_string arity;
5636bb2c 708 nest_dots starter ender (statement arity)
34e49164
C
709 (function _ ->
710 open_box 0;
711 print_between force_newline
712 (whencode (dots force_newline (statement "")) (statement "")) whn;
713 close_box(); force_newline())
714 stmt_dots
715 | Ast.Dots(d,whn,_,_) | Ast.Circles(d,whn,_,_) | Ast.Stars(d,whn,_,_) ->
716 print_string arity; mcode print_string d;
717 open_box 0;
718 print_between force_newline
719 (whencode (dots force_newline (statement "")) (statement "")) whn;
720 close_box(); force_newline()
721 | Ast.OptStm(s) -> statement "?" s
722 | Ast.UniqueStm(s) -> statement "!" s
723
724and print_statement_when whencode =
725 print_string " WHEN != ";
726 open_box 0;
727 print_between (function _ -> print_string " &"; force_newline())
728 (dots force_newline (statement "")) whencode;
729 close_box()
730
731
732and whencode notfn alwaysfn = function
733 Ast.WhenNot a ->
734 print_string " WHEN != "; open_box 0; notfn a; close_box()
735 | Ast.WhenAlways a ->
736 print_string " WHEN = "; open_box 0; alwaysfn a; close_box()
737 | Ast.WhenModifier x -> print_string " WHEN "; print_when_modif x
1be43e12
C
738 | Ast.WhenNotTrue a ->
739 print_string " WHEN != TRUE "; open_box 0; rule_elem "" a; close_box()
740 | Ast.WhenNotFalse a ->
741 print_string " WHEN != FALSE "; open_box 0; rule_elem "" a; close_box()
34e49164
C
742
743and print_when_modif = function
744 | Ast.WhenAny -> print_string "ANY"
745 | Ast.WhenStrict -> print_string "STRICT"
746 | Ast.WhenForall -> print_string "FORALL"
747 | Ast.WhenExists -> print_string "EXISTS"
748
749and case_line arity c =
750 match Ast.unwrap c with
751 Ast.CaseLine(header,code) ->
752 rule_elem arity header; print_string " ";
753 dots force_newline (statement arity) code
754 | Ast.OptCase(case) -> case_line "?" case
755
756(* --------------------------------------------------------------------- *)
757(* CPP code *)
758
759and inc_file = function
760 Ast.Local(elems) ->
761 print_string "\"";
762 print_between (function _ -> print_string "/") inc_elem elems;
763 print_string "\""
764 | Ast.NonLocal(elems) ->
765 print_string "<";
766 print_between (function _ -> print_string "/") inc_elem elems;
767 print_string ">"
768
769and inc_elem = function
770 Ast.IncPath s -> print_string s
771 | Ast.IncDots -> print_string "..."
772
773(* for export only *)
774let statement_dots l = dots force_newline (statement "") l
775
776let top_level t =
777 match Ast.unwrap t with
778 Ast.FILEINFO(old_file,new_file) ->
779 print_string "--- "; mcode print_string old_file; force_newline();
780 print_string "+++ "; mcode print_string new_file
781 | Ast.DECL(stmt) -> statement "" stmt
782 | Ast.CODE(stmt_dots) ->
783 dots force_newline (statement "") stmt_dots
784 | Ast.ERRORWORDS(exps) ->
785 print_string "error words = [";
786 print_between (function _ -> print_string ", ") expression exps;
787 print_string "]"
788
789let rule =
790 print_between (function _ -> force_newline(); force_newline()) top_level
791
792let pp_print_anything x = !anything x
793
794let _ =
795 anything := function
796 Ast.FullTypeTag(x) -> fullType x
797 | Ast.BaseTypeTag(x) -> baseType x
798 | Ast.StructUnionTag(x) -> structUnion x
799 | Ast.SignTag(x) -> sign x
800 | Ast.IdentTag(x) -> ident x
801 | Ast.ExpressionTag(x) -> expression x
802 | Ast.ConstantTag(x) -> constant x
803 | Ast.UnaryOpTag(x) -> unaryOp x
804 | Ast.AssignOpTag(x) -> assignOp x
805 | Ast.FixOpTag(x) -> fixOp x
806 | Ast.BinaryOpTag(x) -> binaryOp x
807 | Ast.ArithOpTag(x) -> arithOp x
808 | Ast.LogicalOpTag(x) -> logicalOp x
809 | Ast.InitTag(x) -> initialiser x
810 | Ast.DeclarationTag(x) -> declaration x
811 | Ast.StorageTag(x) -> storage x
812 | Ast.IncFileTag(x) -> inc_file x
813 | Ast.Rule_elemTag(x) -> rule_elem "" x
814 | Ast.StatementTag(x) -> statement "" x
815 | Ast.CaseLineTag(x) -> case_line "" x
816 | Ast.ConstVolTag(x) -> const_vol x
817 | Ast.Token(x,Some info) -> print_string_befaft print_string x info
818 | Ast.Token(x,None) -> print_string x
c3e37e97
C
819 | Ast.Pragma(xs) ->
820 let print = function Ast.Noindent s | Ast.Indent s -> print_string s in
821 print_between force_newline print xs
34e49164
C
822 | Ast.Code(x) -> let _ = top_level x in ()
823 | Ast.ExprDotsTag(x) -> dots (function _ -> ()) expression x
824 | Ast.ParamDotsTag(x) -> parameter_list x
825 | Ast.StmtDotsTag(x) -> dots (function _ -> ()) (statement "") x
826 | Ast.DeclDotsTag(x) -> dots (function _ -> ()) declaration x
827 | Ast.TypeCTag(x) -> typeC x
828 | Ast.ParamTag(x) -> parameterTypeDef x
829 | Ast.SgrepStartTag(x) -> print_string x
830 | Ast.SgrepEndTag(x) -> print_string x
831
832let rec dep in_and = function
833 Ast.Dep(s) -> print_string s
834 | Ast.AntiDep(s) -> print_string "!"; print_string s
835 | Ast.EverDep(s) -> print_string "ever "; print_string s
836 | Ast.NeverDep(s) -> print_string "never "; print_string s
837 | Ast.AndDep(s1,s2) ->
838 let print_and _ = dep true s1; print_string " && "; dep true s2 in
839 if in_and
840 then print_and ()
841 else (print_string "("; print_and(); print_string ")")
842 | Ast.OrDep(s1,s2) ->
843 let print_or _ = dep false s1; print_string " || "; dep false s2 in
844 if not in_and
845 then print_or ()
846 else (print_string "("; print_or(); print_string ")")
7f004419
C
847 | Ast.NoDep -> print_string "no_dep"
848 | Ast.FailDep -> print_string "fail_dep"
34e49164 849
c3e37e97
C
850let script_header str lang deps code =
851 print_string "@@";
852 force_newline();
853 print_string (str ^ ":" ^ lang);
854 (match deps with
855 Ast.NoDep -> ()
856 | _ -> print_string " depends on "; dep true deps);
857 force_newline();
858 print_string "@@";
859 force_newline();
860 print_string code;
861 force_newline()
862
34e49164
C
863let unparse z =
864 match z with
174d1640 865 Ast.InitialScriptRule (name,lang,deps,code) ->
c3e37e97 866 script_header "initialize" lang deps code
174d1640 867 | Ast.FinalScriptRule (name,lang,deps,code) ->
c3e37e97 868 script_header "finalize" lang deps code
413ffc02 869 | Ast.ScriptRule (name,lang,deps,bindings,script_vars,code) ->
c3e37e97 870 script_header "script" lang deps code
faf9a90c 871 | Ast.CocciRule (nm, (deps, drops, exists), x, _, _) ->
b1b2de81
C
872 print_string "@@";
873 force_newline();
874 print_string nm;
875 (match deps with
876 Ast.NoDep -> ()
877 | _ -> print_string " depends on "; dep true deps);
34e49164
C
878 (*
879 print_string "line ";
880 print_int (Ast.get_line (List.hd x));
881 *)
b1b2de81
C
882 force_newline();
883 print_string "@@";
884 print_newlines_disj := true;
885 force_newline();
886 force_newline();
887 rule x;
888 force_newline()
34e49164
C
889
890let rule_elem_to_string x =
891 print_newlines_disj := true;
892 Common.format_to_string (function _ -> rule_elem "" x)
893
894let ident_to_string x =
895 print_newlines_disj := true;
896 Common.format_to_string (function _ -> ident x)
897
898let unparse_to_string x =
899 print_newlines_disj := true;
900 Common.format_to_string (function _ -> unparse x)
901
902let print_rule_elem re =
903 let nl = !print_newlines_disj in
904 print_newlines_disj := false;
905 rule_elem "" re;
906 print_newlines_disj := nl
907