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