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
27 module Ast0
= Ast0_cocci
28 module U
= Pretty_print_cocci
30 let quiet = ref true (* true = no decoration on - context, etc *)
32 let full_ids = ref false (* true = print rule name as well *)
35 force_newline
(); print_string
" "; open_box
0
38 close_box
(); force_newline
()
40 let print_option = Common.do_option
41 let print_between = Common.print_between
43 (* --------------------------------------------------------------------- *)
46 let meta_pos = function
47 Ast0.MetaPos
(name
,_
,_
) ->
49 let (_
,name
) = Ast0.unwrap_mcode name
in
51 | Ast0.NoMetaPos
-> ()
53 (* --------------------------------------------------------------------- *)
56 let mcodekind brackets fn x info mc
=
57 let print = function Ast.Noindent s
| Ast.Indent s
-> print_string s
in
58 List.iter
(function (s
,_
) -> print s
) info
.Ast0.strings_before
;
60 Ast0.MINUS
(plus_stream
) ->
66 Some x
-> ("[","]^"^
(string_of_int x
))
68 let (plus_stream
,_
) = !plus_stream
in
71 else (print_string
"-";
72 print_string lb
; fn x
; print_string rb
);
73 U.print_anything
">>> " plus_stream
74 | Ast0.CONTEXT
(plus_streams
) ->
80 Some x
-> ("[",("]^"^
(string_of_int x
))) | None
-> ("","") in
81 let (plus_streams
,t1
,t2
) = !plus_streams
in
84 print_string lb
; fn x
; print_string rb
)
86 | Ast0.PLUS _
-> print_int
(info
.Ast0.pos_info
.Ast0.column
); fn x
87 | Ast0.MIXED
(plus_streams
) ->
93 match brackets
with Some x
-> "^"^
(string_of_int x
) | None
-> "" in
95 let (plus_streams
,_
,_
) = !plus_streams
in
96 U.print_around
(function x
-> print_string lb
; fn x
; print_string rb
)
98 List.iter
(function (s
,_
) -> print s
) info
.Ast0.strings_after
100 let mcode fn
(x
,_
,info
,mc
,pos
,adj
) =
101 let fn x
= fn x
; meta_pos !pos
in
102 mcodekind (Some info
.Ast0.pos_info
.Ast0.line_start
)(*None*) fn x info mc
104 let print_context x
fn =
105 mcodekind (Some
(Ast0.get_line x
)) fn () (Ast0.get_info x
)
106 (Ast0.get_mcodekind x
)
108 let print_meta (ctx
,name
) =
110 then (print_string ctx
; print_string
":"));
113 (* --------------------------------------------------------------------- *)
114 (* --------------------------------------------------------------------- *)
117 let dots between
fn d
=
120 match Ast0.unwrap d
with
121 Ast0.DOTS
(l
) -> print_between between
fn l
122 | Ast0.CIRCLES
(l
) -> print_between between
fn l
123 | Ast0.STARS
(l
) -> print_between between
fn l
)
125 (* --------------------------------------------------------------------- *)
127 let print_types = function
131 Format.print_flush
();
132 print_between (function _
-> print_string
", ") Type_cocci.typeC ty
;
133 Format.print_flush
();
136 (* --------------------------------------------------------------------- *)
142 match Ast0.unwrap i
with
143 Ast0.Id
(name
) -> mcode print_string name
144 | Ast0.MetaId
(name
,_
,_
) -> mcode print_meta name
145 | Ast0.MetaFunc
(name
,_
,_
) -> mcode print_meta name
146 | Ast0.MetaLocalFunc
(name
,_
,_
) -> mcode print_meta name
147 | Ast0.OptIdent
(id
) -> print_string
"?"; ident id
148 | Ast0.UniqueIdent
(id
) -> print_string
"!"; ident id
)
150 (* --------------------------------------------------------------------- *)
153 let print_string_box s
= print_string s
; open_box
0
155 let rec expression e
=
156 print_option Type_cocci.typeC
(Ast0.get_type e
);
159 match Ast0.unwrap e
with
160 Ast0.Ident
(id
) -> ident id
161 | Ast0.Constant
(const
) -> mcode U.constant const
162 | Ast0.FunCall
(fn,lp
,args
,rp
) ->
163 expression fn; mcode print_string_box lp
;
164 let _ = dots (function _ -> ()) expression args
in
165 close_box
(); mcode print_string rp
166 | Ast0.Assignment
(left
,op
,right
,_) ->
167 expression left
; print_string
" "; mcode U.assignOp op
;
168 print_string
" "; expression right
169 | Ast0.CondExpr
(exp1
,why
,exp2
,colon
,exp3
) ->
170 expression exp1
; print_string
" "; mcode print_string why
;
171 print_option (function e
-> print_string
" "; expression e
) exp2
;
172 print_string
" "; mcode print_string colon
; expression exp3
173 | Ast0.Postfix
(exp
,op
) -> expression exp
; mcode U.fixOp op
174 | Ast0.Infix
(exp
,op
) -> mcode U.fixOp op
; expression exp
175 | Ast0.Unary
(exp
,op
) -> mcode U.unaryOp op
; expression exp
176 | Ast0.Binary
(left
,op
,right
) ->
178 expression left
; print_string
" "; mcode U.binaryOp op
;
179 print_string
" "; expression right
;
181 | Ast0.Nested
(left
,op
,right
) ->
183 expression left
; print_string
" "; mcode U.binaryOp op
;
184 print_string
" "; expression right
;
186 | Ast0.Paren
(lp
,exp
,rp
) ->
187 mcode print_string_box lp
; expression exp
; close_box
();
188 mcode print_string rp
189 | Ast0.ArrayAccess
(exp1
,lb
,exp2
,rb
) ->
190 expression exp1
; mcode print_string_box lb
; expression exp2
;
191 close_box
(); mcode print_string rb
192 | Ast0.RecordAccess
(exp
,pt
,field
) ->
193 expression exp
; mcode print_string pt
; ident field
194 | Ast0.RecordPtAccess
(exp
,ar
,field
) ->
195 expression exp
; mcode print_string ar
; ident field
196 | Ast0.Cast
(lp
,ty
,rp
,exp
) ->
197 mcode print_string_box lp
; typeC ty
; close_box
();
198 mcode print_string rp
; expression exp
199 | Ast0.SizeOfExpr
(szf
,exp
) ->
200 mcode print_string szf
; expression exp
201 | Ast0.SizeOfType
(szf
,lp
,ty
,rp
) ->
202 mcode print_string szf
;
203 mcode print_string_box lp
; typeC ty
; close_box
();
204 mcode print_string rp
205 | Ast0.TypeExp
(ty
) -> typeC ty
206 | Ast0.MetaErr
(name
,_,_) -> mcode print_meta name
207 | Ast0.MetaExpr
(name
,_,ty
,_,pure
) ->
208 mcode print_meta name
; print_types ty
(*;
211 Ast0.Pure -> print_string "pure"
212 | Ast0.Impure -> print_string "impure"
213 | Ast0.Context -> print_string "context"
214 | Ast0.PureContext -> print_string "pure_context")*)
215 | Ast0.MetaExprList
(name
,_,_) -> mcode print_meta name
216 | Ast0.EComma
(cm
) -> mcode print_string cm
; print_space
()
217 | Ast0.DisjExpr
(_,exp_list
,_,_) ->
218 print_string
"\n("; force_newline
();
220 (function _ -> print_string
"\n|"; force_newline
())
223 | Ast0.NestExpr
(starter
,expr_dots
,ender
,None
,multi
) ->
224 mcode print_string starter
;
225 start_block(); dots force_newline
expression expr_dots
; end_block();
226 mcode print_string ender
227 | Ast0.NestExpr
(starter
,expr_dots
,ender
,Some whencode
,multi
) ->
228 mcode print_string starter
; print_string
" WHEN != ";
230 start_block(); dots force_newline
expression expr_dots
; end_block();
231 mcode print_string ender
232 | Ast0.Edots
(dots,Some whencode
)
233 | Ast0.Ecircles
(dots,Some whencode
)
234 | Ast0.Estars
(dots,Some whencode
) ->
235 mcode print_string
dots; print_string
" WHEN != ";
237 | Ast0.Edots
(dots,None
)
238 | Ast0.Ecircles
(dots,None
)
239 | Ast0.Estars
(dots,None
) -> mcode print_string
dots
240 | Ast0.OptExp
(exp
) -> print_string
"?"; expression exp
241 | Ast0.UniqueExp
(exp
) -> print_string
"!"; expression exp
)
243 and expression_dots x
= dots (function _ -> ()) expression x
245 (* --------------------------------------------------------------------- *)
248 and print_function_pointer
(ty
,lp1
,star
,rp1
,lp2
,params
,rp2
) fn =
249 typeC ty
; mcode print_string lp1
; mcode print_string star
; fn();
250 mcode print_string rp1
; mcode print_string lp2
;
251 parameter_list params
; mcode print_string rp2
253 and print_function_type
(ty
,lp1
,params
,rp1
) fn =
254 print_option typeC ty
; fn(); mcode print_string lp1
;
255 parameter_list params
; mcode print_string rp1
260 match Ast0.unwrap t
with
261 Ast0.ConstVol
(cv
,ty
) ->
262 mcode U.const_vol cv
; print_string
" "; typeC ty
263 | Ast0.BaseType
(ty
,strings
) ->
264 List.iter
(function s
-> mcode print_string s
; print_string
" ")
266 | Ast0.Signed
(sgn
,ty
) -> mcode U.sign sgn
; print_option typeC ty
267 | Ast0.Pointer
(ty
,star
) -> typeC ty
; mcode print_string star
268 | Ast0.FunctionPointer
(ty
,lp1
,star
,rp1
,lp2
,params
,rp2
) ->
269 print_function_pointer
(ty
,lp1
,star
,rp1
,lp2
,params
,rp2
)
271 | Ast0.FunctionType
(ty
,lp1
,params
,rp1
) ->
272 print_function_type
(ty
,lp1
,params
,rp1
) (function _ -> ())
273 | Ast0.Array
(ty
,lb
,size
,rb
) ->
274 typeC ty
; mcode print_string lb
; print_option expression size
;
275 mcode print_string rb
276 | Ast0.EnumName
(kind
,name
) ->
277 mcode print_string kind
;
278 print_option (function x
-> ident x
; print_string
" ") name
279 | Ast0.EnumDef
(ty
,lb
,ids
,rb
) ->
280 typeC ty
; mcode print_string lb
;
281 dots force_newline
expression ids
;
282 mcode print_string rb
283 | Ast0.StructUnionName
(kind
,name
) ->
284 mcode U.structUnion kind
;
285 print_option (function x
-> ident x
; print_string
" ") name
286 | Ast0.StructUnionDef
(ty
,lb
,decls
,rb
) ->
287 typeC ty
; mcode print_string lb
;
288 dots force_newline declaration decls
;
289 mcode print_string rb
290 | Ast0.TypeName
(name
)-> mcode print_string name
; print_string
" "
291 | Ast0.MetaType
(name
,_)-> mcode print_meta name
; print_string
" "
292 | Ast0.DisjType
(lp
,types
,mids
,rp
) ->
293 print_string
"\n"; mcode print_string lp
; force_newline
();
295 (function _ -> print_string
"\n|"; force_newline
())
297 print_string
"\n"; mcode print_string rp
298 | Ast0.OptType
(ty
) -> print_string
"?"; typeC ty
299 | Ast0.UniqueType
(ty
) -> print_string
"!"; typeC ty
)
301 (* --------------------------------------------------------------------- *)
302 (* Variable declaration *)
303 (* Even if the Cocci program specifies a list of declarations, they are
304 split out into multiple declarations of a single variable each. *)
306 and print_named_type ty id
=
307 match Ast0.unwrap ty
with
308 Ast0.FunctionPointer
(ty
,lp1
,star
,rp1
,lp2
,params
,rp2
) ->
309 print_function_pointer
(ty
,lp1
,star
,rp1
,lp2
,params
,rp2
)
310 (function _ -> print_string
" "; ident id
)
311 | Ast0.FunctionType
(ty
,lp1
,params
,rp1
) ->
312 print_function_type
(ty
,lp1
,params
,rp1
)
313 (function _ -> print_string
" "; ident id
)
314 | Ast0.Array
(ty
,lb
,size
,rb
) ->
316 match Ast0.unwrap ty
with
317 Ast0.Array
(ty
,lb
,size
,rb
) ->
321 mcode print_string lb
;
322 print_option expression size
;
323 mcode print_string rb
)
324 | _ -> typeC ty
; ident id
; k
() in
325 loop ty
(function _ -> ())
326 | _ -> typeC ty
; ident id
331 match Ast0.unwrap d
with
332 Ast0.MetaDecl
(name
,_) | Ast0.MetaField
(name
,_) -> mcode print_meta name
333 | Ast0.Init
(stg
,ty
,id
,eq
,ini
,sem
) ->
334 print_option (mcode U.storage
) stg
;
335 print_named_type ty id
;
337 mcode print_string eq
; print_string
" "; initialiser ini
;
338 mcode print_string sem
339 | Ast0.UnInit
(stg
,ty
,id
,sem
) ->
340 print_option (mcode U.storage
) stg
; print_named_type ty id
;
341 mcode print_string sem
342 | Ast0.MacroDecl
(name
,lp
,args
,rp
,sem
) ->
343 ident name
; mcode print_string_box lp
;
344 let _ = dots (function _ -> ()) expression args
in
345 close_box
(); mcode print_string rp
; mcode print_string sem
346 | Ast0.TyDecl
(ty
,sem
) -> typeC ty
; mcode print_string sem
347 | Ast0.Typedef
(stg
,ty
,id
,sem
) ->
348 mcode print_string stg
; typeC ty
; typeC id
;
349 mcode print_string sem
350 | Ast0.DisjDecl
(_,decls
,_,_) ->
351 print_string
"\n("; force_newline
();
353 (function _ -> print_string
"\n|"; force_newline
())
356 | Ast0.Ddots
(dots,Some whencode
) ->
357 mcode print_string
dots; print_string
" when != ";
359 | Ast0.Ddots
(dots,None
) -> mcode print_string
dots
360 | Ast0.OptDecl
(decl
) -> print_string
"?"; declaration decl
361 | Ast0.UniqueDecl
(decl
) -> print_string
"!"; declaration decl
)
363 and declaration_dots l
= dots (function _ -> ()) declaration l
365 (* --------------------------------------------------------------------- *)
371 match Ast0.unwrap i
with
372 Ast0.MetaInit
(name
,_)-> mcode print_meta name
; print_string
" "
373 | Ast0.InitExpr
(exp
) -> expression exp
374 | Ast0.InitList
(lb
,initlist
,rb
,ordered
) ->
375 (*doesn't show commas dropped in unordered case*)
376 mcode print_string lb
; open_box
0;
377 let _ = dots (function _ -> ()) initialiser initlist
in
378 close_box
(); mcode print_string rb
379 | Ast0.InitGccExt
(designators
,eq
,ini
) ->
380 List.iter designator designators
; print_string
" ";
381 mcode print_string eq
; print_string
" "; initialiser ini
382 | Ast0.InitGccName
(name
,eq
,ini
) ->
383 ident name
; mcode print_string eq
; initialiser ini
384 | Ast0.IComma
(cm
) -> mcode print_string cm
; force_newline
()
385 | Ast0.Idots
(d
,Some whencode
) ->
386 mcode print_string d
; print_string
" WHEN != ";
388 | Ast0.Idots
(d
,None
) -> mcode print_string d
389 | Ast0.OptIni
(ini
) -> print_string
"?"; initialiser ini
390 | Ast0.UniqueIni
(ini
) -> print_string
"!"; initialiser ini
)
392 and designator
= function
393 Ast0.DesignatorField
(dot
,id
) -> mcode print_string dot
; ident id
394 | Ast0.DesignatorIndex
(lb
,exp
,rb
) ->
395 mcode print_string lb
; expression exp
; mcode print_string rb
396 | Ast0.DesignatorRange
(lb
,min
,dots,max
,rb
) ->
397 mcode print_string lb
; expression min
; mcode print_string
dots;
398 expression max
; mcode print_string rb
400 and initialiser_list l
= dots (function _ -> ()) initialiser l
402 (* --------------------------------------------------------------------- *)
405 and parameterTypeDef p
=
408 match Ast0.unwrap p
with
409 Ast0.VoidParam
(ty
) -> typeC ty
410 | Ast0.Param
(ty
,Some id
) -> print_named_type ty id
411 | Ast0.Param
(ty
,None
) -> typeC ty
412 | Ast0.MetaParam
(name
,_) -> mcode print_meta name
413 | Ast0.MetaParamList
(name
,_,_) -> mcode print_meta name
414 | Ast0.PComma
(cm
) -> mcode print_string cm
; print_space
()
415 | Ast0.Pdots
(dots) -> mcode print_string
dots
416 | Ast0.Pcircles
(dots) -> mcode print_string
dots
417 | Ast0.OptParam
(param
) -> print_string
"?"; parameterTypeDef param
418 | Ast0.UniqueParam
(param
) -> print_string
"!"; parameterTypeDef param
)
420 and parameter_list l
= dots (function _ -> ()) parameterTypeDef l
422 (* --------------------------------------------------------------------- *)
425 and statement arity s
=
428 match Ast0.unwrap s
with
429 Ast0.FunDecl
(_,fninfo
,name
,lp
,params
,rp
,lbrace
,body
,rbrace
) ->
431 List.iter print_fninfo fninfo
;
432 ident name
; mcode print_string_box lp
;
433 parameter_list params
; close_box
(); mcode print_string rp
;
435 print_string arity
; mcode print_string lbrace
; start_block();
436 dots force_newline
(statement arity
) body
;
437 end_block(); print_string arity
; mcode print_string rbrace
438 | Ast0.Decl
(_,decl
) -> print_string arity
; declaration decl
439 | Ast0.Seq
(lbrace
,body
,rbrace
) ->
440 print_string arity
; mcode print_string lbrace
; start_block();
441 dots force_newline
(statement arity
) body
;
442 end_block(); print_string arity
; mcode print_string rbrace
443 | Ast0.ExprStatement
(exp
,sem
) ->
444 print_string arity
; expression exp
; mcode print_string sem
445 | Ast0.IfThen
(iff
,lp
,exp
,rp
,branch1
,(info
,aft
)) ->
447 mcode print_string iff
; print_string
" "; mcode print_string_box lp
;
448 expression exp
; close_box
(); mcode print_string rp
; print_string
" ";
449 statement arity branch1
;
450 mcode (function _ -> ()) ((),(),info
,aft
,ref Ast0.NoMetaPos
,-1)
451 | Ast0.IfThenElse
(iff
,lp
,exp
,rp
,branch1
,els
,branch2
,(info
,aft
)) ->
453 mcode print_string iff
; print_string
" "; mcode print_string_box lp
;
454 expression exp
; close_box
(); mcode print_string rp
; print_string
" ";
455 statement arity branch1
;
456 print_string arity
; mcode print_string els
; print_string
" ";
457 statement arity branch2
;
458 mcode (function _ -> ()) ((),(),info
,aft
,ref Ast0.NoMetaPos
,-1)
459 | Ast0.While
(whl
,lp
,exp
,rp
,body
,(info
,aft
)) ->
461 mcode print_string whl
; print_string
" "; mcode print_string_box lp
;
462 expression exp
; close_box
(); mcode print_string rp
; print_string
" ";
463 statement arity body
;
464 mcode (function _ -> ()) ((),(),info
,aft
,ref Ast0.NoMetaPos
,-1)
465 | Ast0.Do
(d
,body
,whl
,lp
,exp
,rp
,sem
) ->
466 print_string arity
; mcode print_string d
; print_string
" ";
467 statement arity body
;
469 mcode print_string whl
; print_string
" "; mcode print_string_box lp
;
470 expression exp
; close_box
(); mcode print_string rp
;
471 mcode print_string sem
472 | Ast0.For
(fr
,lp
,e1
,sem1
,e2
,sem2
,e3
,rp
,body
,(info
,aft
)) ->
474 mcode print_string fr
; mcode print_string_box lp
;
475 print_option expression e1
; mcode print_string sem1
;
476 print_option expression e2
; mcode print_string sem2
;
477 print_option expression e3
; close_box
();
478 mcode print_string rp
; print_string
" "; statement arity body
;
479 mcode (function _ -> ()) ((),(),info
,aft
,ref Ast0.NoMetaPos
,-1)
480 | Ast0.Iterator
(nm
,lp
,args
,rp
,body
,(info
,aft
)) ->
482 ident nm
; print_string
" "; mcode print_string_box lp
;
483 let _ = dots (function _ -> ()) expression args
in
484 close_box
(); mcode print_string rp
; print_string
" ";
485 statement arity body
;
486 mcode (function _ -> ()) ((),(),info
,aft
,ref Ast0.NoMetaPos
,-1)
487 | Ast0.Switch
(switch
,lp
,exp
,rp
,lb
,decls
,cases
,rb
) ->
489 mcode print_string switch
; print_string
" ";
490 mcode print_string_box lp
; expression exp
; close_box
();
491 mcode print_string rp
; print_string
" "; mcode print_string lb
;
492 dots force_newline
(statement arity
) decls
;
493 dots force_newline
(case_line arity
) cases
;
494 mcode print_string rb
495 | Ast0.Break
(br
,sem
) ->
496 print_string arity
; mcode print_string br
; mcode print_string sem
497 | Ast0.Continue
(cont
,sem
) ->
498 print_string arity
; mcode print_string cont
; mcode print_string sem
499 | Ast0.Label
(l
,dd
) -> ident l
; print_string
":"
500 | Ast0.Goto
(goto
,l
,sem
) ->
501 mcode print_string goto
; ident l
; mcode print_string sem
502 | Ast0.Return
(ret
,sem
) ->
503 print_string arity
; mcode print_string ret
; mcode print_string sem
504 | Ast0.ReturnExpr
(ret
,exp
,sem
) ->
505 print_string arity
; mcode print_string ret
; print_string
" ";
506 expression exp
; mcode print_string sem
507 | Ast0.MetaStmt
(name
,pure
) ->
508 print_string arity
; mcode print_meta name
;(*
511 Ast0.Pure -> print_string "pure"
512 | Ast0.Impure -> print_string "impure"
513 | Ast0.Context -> print_string "context"
514 | Ast0.PureContext -> print_string "pure_context")*)
515 | Ast0.MetaStmtList
(name
,_) ->
516 print_string arity
; mcode print_meta name
517 | Ast0.Disj
(starter
,statement_dots_list
,_,ender
) ->
519 print_string
"\n"; mcode print_string starter
; force_newline
();
521 (function _ -> print_string
"\n|"; force_newline
())
522 (dots force_newline
(statement arity
))
524 print_string
"\n"; mcode print_string ender
525 | Ast0.Nest
(starter
,stmt_dots
,ender
,whn
,multi
) ->
527 mcode print_string starter
;
530 (whencode
(dots force_newline
(statement
"")) (statement
""))
534 dots force_newline
(statement arity
) stmt_dots
;
536 mcode print_string ender
537 | Ast0.Exp
(exp
) -> print_string arity
; expression exp
538 | Ast0.TopExp
(exp
) -> print_string arity
; expression exp
539 | Ast0.Ty
(ty
) -> print_string arity
; typeC ty
540 | Ast0.TopInit
(init
) -> initialiser init
541 | Ast0.Dots
(d
,whn
) | Ast0.Circles
(d
,whn
) | Ast0.Stars
(d
,whn
) ->
542 print_string arity
; mcode print_string d
;
544 (whencode
(dots force_newline
(statement
"")) (statement
""))
546 | Ast0.Include
(inc
,s
) ->
547 mcode print_string inc
; print_string
" "; mcode U.inc_file s
548 | Ast0.Define
(def
,id
,params
,body
) ->
549 mcode print_string def
; print_string
" "; ident id
;
550 print_define_parameters params
;
552 dots force_newline
(statement arity
) body
553 | Ast0.OptStm
(re
) -> statement
"?" re
554 | Ast0.UniqueStm
(re
) -> statement
"!" re
)
556 and print_define_parameters params
=
557 match Ast0.unwrap params
with
559 | Ast0.DParams
(lp
,params
,rp
) ->
560 mcode print_string lp
;
561 dots (function _ -> ()) print_define_param params
; mcode print_string rp
563 and print_define_param param
=
564 match Ast0.unwrap param
with
565 Ast0.DParam
(id
) -> ident id
566 | Ast0.DPComma
(comma
) -> mcode print_string comma
567 | Ast0.DPdots
(dots) -> mcode print_string
dots
568 | Ast0.DPcircles
(circles
) -> mcode print_string circles
569 | Ast0.OptDParam
(dp
) -> print_string
"?"; print_define_param dp
570 | Ast0.UniqueDParam
(dp
) -> print_string
"!"; print_define_param dp
572 and print_fninfo
= function
573 Ast0.FStorage
(stg
) -> mcode U.storage stg
574 | Ast0.FType
(ty
) -> typeC ty
575 | Ast0.FInline
(inline
) -> mcode print_string inline
576 | Ast0.FAttr
(attr
) -> mcode print_string attr
578 and whencode notfn alwaysfn
= function
580 print_string
" WHEN != "; open_box
0; notfn a
; close_box
()
581 | Ast0.WhenAlways a
->
582 print_string
" WHEN = "; open_box
0; alwaysfn a
; close_box
()
583 | Ast0.WhenModifier x
-> print_string
" WHEN "; U.print_when_modif x
584 | Ast0.WhenNotTrue a
->
585 print_string
" WHEN != TRUE "; open_box
0; expression a
; close_box
()
586 | Ast0.WhenNotFalse a
->
587 print_string
" WHEN != FALSE "; open_box
0; expression a
; close_box
()
589 and case_line arity c
=
592 match Ast0.unwrap c
with
593 Ast0.Default
(def
,colon
,code
) ->
595 mcode print_string def
; mcode print_string colon
; print_string
" ";
596 dots force_newline
(statement arity
) code
597 | Ast0.Case
(case
,exp
,colon
,code
) ->
599 mcode print_string case
; print_string
" "; expression exp
;
600 mcode print_string colon
; print_string
" ";
601 dots force_newline
(statement arity
) code
602 | Ast0.DisjCase
(starter
,case_lines
,mids
,ender
) ->
603 print_string
"\n("; force_newline
();
605 (function _ -> print_string
"\n|"; force_newline
())
606 (case_line arity
) case_lines
;
608 | Ast0.OptCase
(case
) -> case_line
"?" case
)
610 and statement_dots l
= dots (function _ -> ()) (statement
"") l
611 and case_dots l
= dots (function _ -> ()) (case_line
"") l
613 (* --------------------------------------------------------------------- *)
619 match Ast0.unwrap t
with
620 Ast0.FILEINFO
(old_file
,new_file
) ->
621 print_string
"--- "; mcode print_string old_file
; force_newline
();
622 print_string
"+++ "; mcode print_string new_file
623 | Ast0.DECL
(stmt
) -> statement
"" stmt
624 | Ast0.CODE
(stmt_dots
) ->
625 dots force_newline
(statement
"") stmt_dots
626 | Ast0.ERRORWORDS
(exps
) ->
627 print_string
"error words = [";
628 print_between (function _ -> print_string
", ") expression exps
;
631 print_string
"OTHER("; statement
"" s
; print_string
")")
634 print_between (function _ -> force_newline
(); force_newline
()) top_level
636 let unparse_anything x
=
640 Ast0.DotsExprTag
(d
) ->
641 print_string
"ExpDots:"; force_newline
();
643 | Ast0.DotsParamTag
(d
) ->
645 | Ast0.DotsInitTag
(d
) ->
647 | Ast0.DotsStmtTag
(d
) ->
648 print_string
"StmDots:"; force_newline
();
650 | Ast0.DotsDeclTag
(d
) -> declaration_dots d
651 | Ast0.DotsCaseTag
(d
) -> case_dots d
652 | Ast0.IdentTag
(d
) -> ident d
653 | Ast0.ExprTag
(d
) | Ast0.ArgExprTag
(d
) | Ast0.TestExprTag
(d
) ->
654 print_string
"Exp:"; force_newline
();
656 | Ast0.TypeCTag
(d
) -> typeC d
657 | Ast0.ParamTag
(d
) -> parameterTypeDef d
658 | Ast0.InitTag
(d
) -> initialiser d
659 | Ast0.DeclTag
(d
) -> declaration d
661 print_string
"Stm:"; force_newline
();
663 | Ast0.CaseLineTag
(d
) -> case_line
"" d
664 | Ast0.TopTag
(d
) -> top_level d
665 | Ast0.IsoWhenTag
(x
) -> U.print_when_modif x
666 | Ast0.IsoWhenTTag
(e
) -> expression e
667 | Ast0.IsoWhenFTag
(e
) -> expression e
668 | Ast0.MetaPosTag
(var
) -> meta_pos var
);
673 print_string
"\n@@\n@@";
679 let unparse_to_string x
= Common.format_to_string
(function _ -> unparse x
)