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.
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.
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.
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/>.
20 * The authors reserve the right to distribute this or future versions of
21 * Coccinelle under other licenses.
26 module Ast
= Ast_cocci
28 let print_plus_flag = ref true
29 let print_minus_flag = ref true
30 let print_newlines_disj = ref true
33 force_newline
(); print_string
" "; open_box
0
36 close_box
(); force_newline
()
38 let print_string_box s
= print_string s
; open_box
0
41 let print_option = Common.do_option
42 let print_between = Common.print_between
44 (* --------------------------------------------------------------------- *)
47 (* avoid polyvariance problems *)
48 let anything : (Ast.anything -> unit) ref = ref (function _
-> ())
50 let rec print_anything str
= function
54 print_between force_newline
56 print_string str
; open_box
0; print_anything_list x
; close_box
())
60 and print_anything_list
= function
63 | bef
::((aft
::_
) as rest
) ->
67 Ast.Rule_elemTag
(_
) | Ast.AssignOpTag
(_
) | Ast.BinaryOpTag
(_
)
68 | Ast.ArithOpTag
(_
) | Ast.LogicalOpTag
(_
)
69 | Ast.Token
("if",_
) | Ast.Token
("while",_
) -> true | _
-> false) or
71 Ast.Rule_elemTag
(_
) | Ast.AssignOpTag
(_
) | Ast.BinaryOpTag
(_
)
72 | Ast.ArithOpTag
(_
) | Ast.LogicalOpTag
(_
) | Ast.Token
("{",_
) -> true
74 if space then print_string
" ";
75 print_anything_list rest
77 let print_around printer term
= function
78 Ast.NOTHING
-> printer term
79 | Ast.BEFORE
(bef
,_
) -> print_anything "<<< " bef
; printer term
80 | Ast.AFTER
(aft
,_
) -> printer term
; print_anything ">>> " aft
81 | Ast.BEFOREAFTER
(bef
,aft
,_
) ->
82 print_anything "<<< " bef
; printer term
; print_anything ">>> " aft
84 let print_string_befaft fn x info
=
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
;
88 List.iter
(function (s
,_
,_
) -> force_newline
(); print s
) info
.Ast.straft
90 let print_meta (r
,x
) = print_string r
; print_string
":"; print_string x
92 let print_pos = function
93 Ast.MetaPos
(name
,_
,_
,_
,_
) ->
94 let name = Ast.unwrap_mcode
name in
95 print_string
"@"; print_meta name
98 let mcode fn
= function
99 (x
, _
, Ast.MINUS
(_
,_
,adj
,plus_stream
), pos
) ->
101 then print_string
(if !Flag.sgrep_mode2
then "*" else "-");
104 then print_anything ">>> " plus_stream
105 | (x
, _
, Ast.CONTEXT
(_
,plus_streams
), pos
) ->
108 let fn x
= fn x
; print_pos pos
in
109 print_around fn x plus_streams
110 else (fn x
; print_pos pos
)
111 | (x
, info
, Ast.PLUS _
, pos
) ->
112 let fn x
= fn x
; print_pos pos
in
113 print_string_befaft fn x info
115 let print_mcodekind = function
116 Ast.MINUS
(_
,_
,_
,plus_stream
) ->
117 print_string
"MINUS";
118 print_anything ">>> " plus_stream
119 | Ast.CONTEXT
(_
,plus_streams
) ->
120 print_around (function _
-> print_string
"CONTEXT") () plus_streams
121 | Ast.PLUS _
-> print_string
"PLUS"
123 (* --------------------------------------------------------------------- *)
124 (* --------------------------------------------------------------------- *)
127 let 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
133 let nest_dots starter ender
fn f d
=
134 mcode print_string starter
;
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
);
141 mcode print_string ender
143 (* --------------------------------------------------------------------- *)
145 let print_type keep info
= function
147 (* print_string "/* ";
148 print_string "keep:"; print_unitary keep;
149 print_string " inherited:"; print_bool inherited;
154 print_between (function _ -> print_string ", ") Type_cocci.typeC ty;(*
155 print_string "keep:"; print_unitary keep;
156 print_string " inherited:"; print_bool inherited;*)
159 (* --------------------------------------------------------------------- *)
160 (* Contraint on Identifier and Function *)
161 (* FIXME: Not called at the moment *)
163 let 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
170 and 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
"\""
176 (* --------------------------------------------------------------------- *)
180 match Ast.unwrap i
with
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
188 and print_unitary
= function
189 Type_cocci.Unitary
-> print_string
"unitary"
190 | Type_cocci.Nonunitary
-> print_string
"nonunitary"
191 | Type_cocci.Saved
-> print_string
"saved"
193 (* --------------------------------------------------------------------- *)
196 let print_disj_list fn l
=
197 if !print_newlines_disj
198 then (force_newline
(); print_string
"("; force_newline
())
199 else print_string
"(";
202 if !print_newlines_disj
203 then (force_newline
(); print_string
"|"; force_newline
())
204 else print_string
" | ")
206 if !print_newlines_disj
207 then (force_newline
(); print_string
")"; force_newline
())
208 else print_string
")"
210 let 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
" ";
231 | Ast.Nested
(left
,op
,right
) ->
232 expression left
; print_string
" "; mcode binaryOp op
; print_string
" ";
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
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
261 | Ast.NestExpr
(starter
,expr_dots
,ender
,Some whencode
,multi
) ->
262 nest_dots starter ender
expression
263 (function _
-> print_string
" when != "; expression whencode
)
265 | Ast.NestExpr
(starter
,expr_dots
,ender
,None
,multi
) ->
266 nest_dots starter ender
expression (function _
-> ()) expr_dots
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
277 and 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
"!"
285 and assignOp
= function
286 Ast.SimpleAssign
-> print_string
"="
287 | Ast.OpAssign
(aop
) -> arithOp aop
; print_string
"="
290 Ast.Dec
-> print_string
"--"
291 | Ast.Inc
-> print_string
"++"
293 and binaryOp
= function
294 Ast.Arith
(aop
) -> arithOp aop
295 | Ast.Logical
(lop
) -> logicalOp lop
297 and 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
"^"
309 and 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
"||"
319 and 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
325 (* --------------------------------------------------------------------- *)
328 and 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 "
334 (* --------------------------------------------------------------------- *)
338 match Ast.unwrap ft
with
340 print_option (function x
-> mcode const_vol x
; print_string
" ") cv
;
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
346 and 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
351 and 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
355 and 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
" "
362 match Ast.unwrap ty
with
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
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
)
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
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
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
" "
393 and 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 "
401 | Ast.LongLongType
-> print_string
"long long "
403 and structUnion
= function
404 Ast.Struct
-> print_string
"struct "
405 | Ast.Union
-> print_string
"union "
408 Ast.Signed
-> print_string
"signed "
409 | Ast.Unsigned
-> print_string
"unsigned "
411 and const_vol
= function
412 Ast.Const
-> print_string
"const"
413 | Ast.Volatile
-> print_string
"volatile"
415 (* --------------------------------------------------------------------- *)
416 (* Variable declaration *)
417 (* Even if the Cocci program specifies a list of declarations, they are
418 split out into multiple declarations of a single variable each. *)
420 and print_named_type ty id
=
421 match Ast.unwrap ty
with
422 Ast.Type
(None
,ty1
) ->
423 (match Ast.unwrap ty1
with
424 Ast.FunctionPointer
(ty
,lp1
,star
,rp1
,lp2
,params
,rp2
) ->
425 print_function_pointer
(ty
,lp1
,star
,rp1
,lp2
,params
,rp2
)
426 (function _
-> print_string
" "; ident id
)
427 | Ast.FunctionType
(_
,ty
,lp1
,params
,rp1
) ->
428 print_function_type
(ty
,lp1
,params
,rp1
)
429 (function _
-> print_string
" "; ident id
)
430 | Ast.Array
(ty
,lb
,size
,rb
) ->
432 match Ast.unwrap ty
with
433 Ast.Array
(ty
,lb
,size
,rb
) ->
434 (match Ast.unwrap ty
with
439 mcode print_string lb
;
440 print_option expression size
;
441 mcode print_string rb
)
442 | _
-> failwith
"complex array types not supported")
443 | _
-> typeC ty
; ident id
; k
() in
444 loop ty1
(function _
-> ())
445 | _
-> fullType ty
; ident id
)
446 | _
-> fullType ty
; ident id
449 match Ast.unwrap d
with
450 Ast.MetaDecl
(name,_
,_
) | Ast.MetaField
(name,_
,_
) -> mcode print_meta name
451 | Ast.Init
(stg
,ty
,id
,eq
,ini
,sem
) ->
452 print_option (mcode storage
) stg
; print_named_type ty id
;
453 print_string
" "; mcode print_string eq
;
454 print_string
" "; initialiser ini
; mcode print_string sem
455 | Ast.UnInit
(stg
,ty
,id
,sem
) ->
456 print_option (mcode storage
) stg
; print_named_type ty id
;
457 mcode print_string sem
458 | Ast.MacroDecl
(name,lp
,args
,rp
,sem
) ->
459 ident name; mcode print_string_box lp
;
460 dots (function _
-> ()) expression args
;
461 close_box
(); mcode print_string rp
; mcode print_string sem
462 | Ast.TyDecl
(ty
,sem
) -> fullType ty
; mcode print_string sem
463 | Ast.Typedef
(stg
,ty
,id
,sem
) ->
464 mcode print_string stg
; print_string
" "; fullType ty
; typeC id
;
465 mcode print_string sem
466 | Ast.DisjDecl
(decls
) -> print_disj_list declaration decls
467 | Ast.Ddots
(dots,Some whencode
) ->
468 mcode print_string
dots; print_string
" when != "; declaration whencode
469 | Ast.Ddots
(dots,None
) -> mcode print_string
dots
470 | Ast.OptDecl
(decl
) -> print_string
"?"; declaration decl
471 | Ast.UniqueDecl
(decl
) -> print_string
"!"; declaration decl
473 (* --------------------------------------------------------------------- *)
477 match Ast.unwrap i
with
478 Ast.MetaInit
(name,_
,_
) ->
479 mcode print_meta name; print_string
" "
480 | Ast.InitExpr
(exp
) -> expression exp
481 | Ast.ArInitList
(lb
,initlist
,rb
) ->
482 mcode print_string lb
; open_box
0;
483 dots force_newline initialiser initlist
; close_box
();
484 mcode print_string rb
485 | Ast.StrInitList
(allminus
,lb
,initlist
,rb
,whencode
) ->
486 mcode print_string lb
; open_box
0;
487 if not
(whencode
= [])
489 (print_string
" WHEN != ";
490 print_between (function _
-> print_string
" v ")
491 initialiser whencode
;
493 List.iter initialiser initlist
; close_box
();
494 mcode print_string rb
495 | Ast.InitGccExt
(designators
,eq
,ini
) ->
496 List.iter designator designators
; print_string
" ";
497 mcode print_string eq
; print_string
" "; initialiser ini
498 | Ast.InitGccName
(name,eq
,ini
) ->
499 ident name; mcode print_string eq
; initialiser ini
500 | Ast.IComma
(comma
) -> mcode print_string comma
; force_newline
()
501 | Ast.Idots
(dots,Some whencode
) ->
502 mcode print_string
dots; print_string
" when != "; initialiser whencode
503 | Ast.Idots
(dots,None
) -> mcode print_string
dots
504 | Ast.OptIni
(ini
) -> print_string
"?"; initialiser ini
505 | Ast.UniqueIni
(ini
) -> print_string
"!"; initialiser ini
507 and designator
= function
508 Ast.DesignatorField
(dot
,id
) -> mcode print_string dot
; ident id
509 | Ast.DesignatorIndex
(lb
,exp
,rb
) ->
510 mcode print_string lb
; expression exp
; mcode print_string rb
511 | Ast.DesignatorRange
(lb
,min
,dots,max
,rb
) ->
512 mcode print_string lb
; expression min
; mcode print_string
dots;
513 expression max
; mcode print_string rb
515 (* --------------------------------------------------------------------- *)
518 and parameterTypeDef p
=
519 match Ast.unwrap p
with
520 Ast.VoidParam
(ty
) -> fullType ty
521 | Ast.Param
(ty
,Some id
) -> print_named_type ty id
522 | Ast.Param
(ty
,None
) -> fullType ty
523 | Ast.MetaParam
(name,_
,_
) -> mcode print_meta name
524 | Ast.MetaParamList
(name,_
,_
,_
) -> mcode print_meta name
525 | Ast.PComma
(cm
) -> mcode print_string cm
; print_space
()
526 | Ast.Pdots
(dots) -> mcode print_string
dots
527 | Ast.Pcircles
(dots) -> mcode print_string
dots
528 | Ast.OptParam
(param
) -> print_string
"?"; parameterTypeDef param
529 | Ast.UniqueParam
(param
) -> print_string
"!"; parameterTypeDef param
531 and parameter_list l
= dots (function _
-> ()) parameterTypeDef l
533 (* --------------------------------------------------------------------- *)
536 let rec rule_elem arity re
=
537 match Ast.unwrap re
with
538 Ast.FunHeader
(bef
,allminus
,fninfo
,name,lp
,params
,rp
) ->
539 mcode (function _
-> ()) ((),Ast.no_info
,bef
,Ast.NoMetaPos
);
540 print_string arity
; List.iter print_fninfo fninfo
;
541 ident name; mcode print_string_box lp
;
542 parameter_list params
; close_box
(); mcode print_string rp
;
544 | Ast.Decl
(bef
,allminus
,decl
) ->
545 mcode (function _
-> ()) ((),Ast.no_info
,bef
,Ast.NoMetaPos
);
548 | Ast.SeqStart
(brace
) ->
549 print_string arity
; mcode print_string brace
;
550 if !print_newlines_disj then start_block()
551 | Ast.SeqEnd
(brace
) ->
552 if !print_newlines_disj then end_block();
553 print_string arity
; mcode print_string brace
554 | Ast.ExprStatement
(exp
,sem
) ->
555 print_string arity
; expression exp
; mcode print_string sem
556 | Ast.IfHeader
(iff
,lp
,exp
,rp
) ->
558 mcode print_string iff
; print_string
" "; mcode print_string_box lp
;
559 expression exp
; close_box
(); mcode print_string rp
; print_string
" "
561 print_string arity
; mcode print_string els
; print_string
" "
562 | Ast.WhileHeader
(whl
,lp
,exp
,rp
) ->
564 mcode print_string whl
; print_string
" "; mcode print_string_box lp
;
565 expression exp
; close_box
(); mcode print_string rp
; print_string
" "
567 print_string arity
; mcode print_string d
; print_string
" "
568 | Ast.WhileTail
(whl
,lp
,exp
,rp
,sem
) ->
570 mcode print_string whl
; print_string
" "; mcode print_string_box lp
;
571 expression exp
; close_box
(); mcode print_string rp
;
572 mcode print_string sem
573 | Ast.ForHeader
(fr
,lp
,e1
,sem1
,e2
,sem2
,e3
,rp
) ->
575 mcode print_string fr
; mcode print_string_box lp
;
576 print_option expression e1
; mcode print_string sem1
;
577 print_option expression e2
; mcode print_string sem2
;
578 print_option expression e3
; close_box
();
579 mcode print_string rp
; print_string
" "
580 | Ast.IteratorHeader
(nm
,lp
,args
,rp
) ->
582 ident nm
; print_string
" "; mcode print_string_box lp
;
583 dots (function _
-> ()) expression args
; close_box
();
584 mcode print_string rp
; print_string
" "
585 | Ast.SwitchHeader
(switch
,lp
,exp
,rp
) ->
587 mcode print_string switch
; print_string
" "; mcode print_string_box lp
;
588 expression exp
; close_box
(); mcode print_string rp
; print_string
" "
589 | Ast.Break
(br
,sem
) ->
590 print_string arity
; mcode print_string br
; mcode print_string sem
591 | Ast.Continue
(cont
,sem
) ->
592 print_string arity
; mcode print_string cont
; mcode print_string sem
593 | Ast.Label
(l
,dd
) -> ident l
; mcode print_string dd
594 | Ast.Goto
(goto
,l
,sem
) ->
595 mcode print_string goto
; ident l
; mcode print_string sem
596 | Ast.Return
(ret
,sem
) ->
597 print_string arity
; mcode print_string ret
; mcode print_string sem
598 | Ast.ReturnExpr
(ret
,exp
,sem
) ->
599 print_string arity
; mcode print_string ret
; print_string
" ";
600 expression exp
; mcode print_string sem
601 | Ast.MetaRuleElem
(name,_
,_
) ->
602 print_string arity
; mcode print_meta name
603 | Ast.MetaStmt
(name,_
,_
,_
) ->
604 print_string arity
; mcode print_meta name
605 | Ast.MetaStmtList
(name,_
,_
) ->
606 print_string arity
; mcode print_meta name
607 | Ast.Exp
(exp
) -> print_string arity
; expression exp
608 | Ast.TopExp
(exp
) -> print_string arity
; expression exp
609 | Ast.Ty
(ty
) -> print_string arity
; fullType ty
610 | Ast.TopInit
(init
) -> initialiser init
611 | Ast.Include
(inc
,s
) ->
612 mcode print_string inc
; print_string
" "; mcode inc_file s
613 | Ast.DefineHeader
(def
,id
,params
) ->
614 mcode print_string def
; print_string
" "; ident id
;
615 print_define_parameters params
616 | Ast.Default
(def
,colon
) ->
617 mcode print_string def
; mcode print_string colon
; print_string
" "
618 | Ast.Case
(case
,exp
,colon
) ->
619 mcode print_string case
; print_string
" "; expression exp
;
620 mcode print_string colon
; print_string
" "
621 | Ast.DisjRuleElem
(res
) ->
623 force_newline
(); print_string
"("; force_newline
();
625 (function _
-> force_newline
();print_string
"|"; force_newline
())
628 force_newline
(); print_string
")"
631 and print_define_parameters params
=
632 match Ast.unwrap params
with
634 | Ast.DParams
(lp
,params
,rp
) ->
635 mcode print_string lp
;
636 dots (function _
-> ()) print_define_param params
; mcode print_string rp
638 and print_define_param param
=
639 match Ast.unwrap param
with
640 Ast.DParam
(id
) -> ident id
641 | Ast.DPComma
(comma
) -> mcode print_string comma
642 | Ast.DPdots
(dots) -> mcode print_string
dots
643 | Ast.DPcircles
(circles
) -> mcode print_string circles
644 | Ast.OptDParam
(dp
) -> print_string
"?"; print_define_param dp
645 | Ast.UniqueDParam
(dp
) -> print_string
"!"; print_define_param dp
647 and statement arity s
=
648 match Ast.unwrap s
with
649 Ast.Seq
(lbrace
,body
,rbrace
) ->
650 rule_elem arity lbrace
;
651 dots force_newline
(statement arity
) body
;
652 rule_elem arity rbrace
653 | Ast.IfThen
(header
,branch
,(_
,_
,_
,aft
)) ->
654 rule_elem arity header
; statement arity branch
;
655 mcode (function _
-> ()) ((),Ast.no_info
,aft
,Ast.NoMetaPos
)
656 | Ast.IfThenElse
(header
,branch1
,els
,branch2
,(_
,_
,_
,aft
)) ->
657 rule_elem arity header
; statement arity branch1
; print_string
" ";
658 rule_elem arity els
; statement arity branch2
;
659 mcode (function _
-> ()) ((),Ast.no_info
,aft
,Ast.NoMetaPos
)
660 | Ast.While
(header
,body
,(_
,_
,_
,aft
)) ->
661 rule_elem arity header
; statement arity body
;
662 mcode (function _
-> ()) ((),Ast.no_info
,aft
,Ast.NoMetaPos
)
663 | Ast.Do
(header
,body
,tail
) ->
664 rule_elem arity header
; statement arity body
;
666 | Ast.For
(header
,body
,(_
,_
,_
,aft
)) ->
667 rule_elem arity header
; statement arity body
;
668 mcode (function _
-> ()) ((),Ast.no_info
,aft
,Ast.NoMetaPos
)
669 | Ast.Iterator
(header
,body
,(_
,_
,_
,aft
)) ->
670 rule_elem arity header
; statement arity body
;
671 mcode (function _
-> ()) ((),Ast.no_info
,aft
,Ast.NoMetaPos
)
672 | Ast.Switch
(header
,lb
,decls
,cases
,rb
) ->
673 rule_elem arity header
; rule_elem arity lb
;
674 dots force_newline
(statement arity
) decls
;
675 List.iter
(function x
-> case_line arity x
; force_newline
()) cases
;
677 | Ast.Atomic
(re
) -> rule_elem arity re
678 | Ast.FunDecl
(header
,lbrace
,body
,rbrace
) ->
679 rule_elem arity header
; rule_elem arity lbrace
;
680 dots force_newline
(statement arity
) body
;
681 rule_elem arity rbrace
682 | Ast.Disj
([stmt_dots
]) ->
684 dots (function _
-> if !print_newlines_disj then force_newline
())
685 (statement arity
) stmt_dots
686 | Ast.Disj
(stmt_dots_list
) -> (* ignores newline directive for readability *)
688 force_newline
(); print_string
"("; force_newline
();
690 (function _
-> force_newline
();print_string
"|"; force_newline
())
691 (dots force_newline
(statement arity
))
693 force_newline
(); print_string
")"
694 | Ast.Define
(header
,body
) ->
695 rule_elem arity header
; print_string
" ";
696 dots force_newline
(statement arity
) body
697 | Ast.Nest
(starter
,stmt_dots
,ender
,whn
,multi
,_
,_
) ->
699 nest_dots starter ender
(statement arity
)
702 print_between force_newline
703 (whencode
(dots force_newline
(statement
"")) (statement
"")) whn
;
704 close_box
(); force_newline
())
706 | Ast.Dots
(d
,whn
,_
,_
) | Ast.Circles
(d
,whn
,_
,_
) | Ast.Stars
(d
,whn
,_
,_
) ->
707 print_string arity
; mcode print_string d
;
709 print_between force_newline
710 (whencode
(dots force_newline
(statement
"")) (statement
"")) whn
;
711 close_box
(); force_newline
()
712 | Ast.OptStm
(s
) -> statement
"?" s
713 | Ast.UniqueStm
(s
) -> statement
"!" s
715 and print_statement_when whencode
=
716 print_string
" WHEN != ";
718 print_between (function _
-> print_string
" &"; force_newline
())
719 (dots force_newline
(statement
"")) whencode
;
723 and whencode notfn alwaysfn
= function
725 print_string
" WHEN != "; open_box
0; notfn a
; close_box
()
726 | Ast.WhenAlways a
->
727 print_string
" WHEN = "; open_box
0; alwaysfn a
; close_box
()
728 | Ast.WhenModifier x
-> print_string
" WHEN "; print_when_modif x
729 | Ast.WhenNotTrue a
->
730 print_string
" WHEN != TRUE "; open_box
0; rule_elem "" a
; close_box
()
731 | Ast.WhenNotFalse a
->
732 print_string
" WHEN != FALSE "; open_box
0; rule_elem "" a
; close_box
()
734 and print_when_modif
= function
735 | Ast.WhenAny
-> print_string
"ANY"
736 | Ast.WhenStrict
-> print_string
"STRICT"
737 | Ast.WhenForall
-> print_string
"FORALL"
738 | Ast.WhenExists
-> print_string
"EXISTS"
740 and case_line arity c
=
741 match Ast.unwrap c
with
742 Ast.CaseLine
(header
,code
) ->
743 rule_elem arity header
; print_string
" ";
744 dots force_newline
(statement arity
) code
745 | Ast.OptCase
(case
) -> case_line
"?" case
747 (* --------------------------------------------------------------------- *)
750 and inc_file
= function
753 print_between (function _
-> print_string
"/") inc_elem elems
;
755 | Ast.NonLocal
(elems
) ->
757 print_between (function _
-> print_string
"/") inc_elem elems
;
760 and inc_elem
= function
761 Ast.IncPath s
-> print_string s
762 | Ast.IncDots
-> print_string
"..."
764 (* for export only *)
765 let statement_dots l
= dots force_newline
(statement
"") l
768 match Ast.unwrap t
with
769 Ast.FILEINFO
(old_file
,new_file
) ->
770 print_string
"--- "; mcode print_string old_file
; force_newline
();
771 print_string
"+++ "; mcode print_string new_file
772 | Ast.DECL
(stmt
) -> statement
"" stmt
773 | Ast.CODE
(stmt_dots
) ->
774 dots force_newline
(statement
"") stmt_dots
775 | Ast.ERRORWORDS
(exps
) ->
776 print_string
"error words = [";
777 print_between (function _
-> print_string
", ") expression exps
;
781 print_between (function _
-> force_newline
(); force_newline
()) top_level
783 let pp_print_anything x
= !anything x
787 Ast.FullTypeTag
(x
) -> fullType x
788 | Ast.BaseTypeTag
(x
) -> baseType x
789 | Ast.StructUnionTag
(x
) -> structUnion x
790 | Ast.SignTag
(x
) -> sign x
791 | Ast.IdentTag
(x
) -> ident x
792 | Ast.ExpressionTag
(x
) -> expression x
793 | Ast.ConstantTag
(x
) -> constant x
794 | Ast.UnaryOpTag
(x
) -> unaryOp x
795 | Ast.AssignOpTag
(x
) -> assignOp x
796 | Ast.FixOpTag
(x
) -> fixOp x
797 | Ast.BinaryOpTag
(x
) -> binaryOp x
798 | Ast.ArithOpTag
(x
) -> arithOp x
799 | Ast.LogicalOpTag
(x
) -> logicalOp x
800 | Ast.InitTag
(x
) -> initialiser x
801 | Ast.DeclarationTag
(x
) -> declaration x
802 | Ast.StorageTag
(x
) -> storage x
803 | Ast.IncFileTag
(x
) -> inc_file x
804 | Ast.Rule_elemTag
(x
) -> rule_elem "" x
805 | Ast.StatementTag
(x
) -> statement
"" x
806 | Ast.CaseLineTag
(x
) -> case_line
"" x
807 | Ast.ConstVolTag
(x
) -> const_vol x
808 | Ast.Token
(x
,Some info
) -> print_string_befaft print_string x info
809 | Ast.Token
(x
,None
) -> print_string x
811 let print = function Ast.Noindent s
| Ast.Indent s
-> print_string s
in
812 print_between force_newline
print xs
813 | Ast.Code
(x
) -> let _ = top_level x
in ()
814 | Ast.ExprDotsTag
(x
) -> dots (function _ -> ()) expression x
815 | Ast.ParamDotsTag
(x
) -> parameter_list x
816 | Ast.StmtDotsTag
(x
) -> dots (function _ -> ()) (statement
"") x
817 | Ast.DeclDotsTag
(x
) -> dots (function _ -> ()) declaration x
818 | Ast.TypeCTag
(x
) -> typeC x
819 | Ast.ParamTag
(x
) -> parameterTypeDef x
820 | Ast.SgrepStartTag
(x
) -> print_string x
821 | Ast.SgrepEndTag
(x
) -> print_string x
823 let rec dep in_and
= function
824 Ast.Dep
(s
) -> print_string s
825 | Ast.AntiDep
(s
) -> print_string
"!"; print_string s
826 | Ast.EverDep
(s
) -> print_string
"ever "; print_string s
827 | Ast.NeverDep
(s
) -> print_string
"never "; print_string s
828 | Ast.AndDep
(s1
,s2
) ->
829 let print_and _ = dep true s1
; print_string
" && "; dep true s2
in
832 else (print_string
"("; print_and(); print_string
")")
833 | Ast.OrDep
(s1
,s2
) ->
834 let print_or _ = dep false s1
; print_string
" || "; dep false s2
in
837 else (print_string
"("; print_or(); print_string
")")
838 | Ast.NoDep
-> print_string
"no_dep"
839 | Ast.FailDep
-> print_string
"fail_dep"
841 let script_header str lang deps code
=
844 print_string
(str ^
":" ^ lang
);
847 | _ -> print_string
" depends on "; dep true deps
);
856 Ast.InitialScriptRule
(name,lang
,deps
,code
) ->
857 script_header "initialize" lang deps code
858 | Ast.FinalScriptRule
(name,lang
,deps
,code
) ->
859 script_header "finalize" lang deps code
860 | Ast.ScriptRule
(name,lang
,deps
,bindings
,script_vars
,code
) ->
861 script_header "script" lang deps code
862 | Ast.CocciRule
(nm
, (deps
, drops
, exists
), x
, _, _) ->
868 | _ -> print_string
" depends on "; dep true deps
);
870 print_string "line ";
871 print_int (Ast.get_line (List.hd x));
875 print_newlines_disj := true;
881 let rule_elem_to_string x
=
882 print_newlines_disj := true;
883 Common.format_to_string
(function _ -> rule_elem "" x
)
885 let ident_to_string x
=
886 print_newlines_disj := true;
887 Common.format_to_string
(function _ -> ident x
)
889 let unparse_to_string x
=
890 print_newlines_disj := true;
891 Common.format_to_string
(function _ -> unparse x
)
893 let print_rule_elem re
=
894 let nl = !print_newlines_disj in
895 print_newlines_disj := false;
897 print_newlines_disj := nl