Commit | Line | Data |
---|---|---|
5636bb2c C |
1 | (* |
2 | * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen | |
3 | * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix | |
4 | * This file is part of Coccinelle. | |
5 | * | |
6 | * Coccinelle is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, according to version 2 of the License. | |
9 | * | |
10 | * Coccinelle is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>. | |
17 | * | |
18 | * The authors reserve the right to distribute this or future versions of | |
19 | * Coccinelle under other licenses. | |
20 | *) | |
21 | ||
22 | ||
9f8e26f4 | 23 | (* |
ae4735db | 24 | * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen |
9f8e26f4 C |
25 | * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix |
26 | * This file is part of Coccinelle. | |
27 | * | |
28 | * Coccinelle is free software: you can redistribute it and/or modify | |
29 | * it under the terms of the GNU General Public License as published by | |
30 | * the Free Software Foundation, according to version 2 of the License. | |
31 | * | |
32 | * Coccinelle is distributed in the hope that it will be useful, | |
33 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
34 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
35 | * GNU General Public License for more details. | |
36 | * | |
37 | * You should have received a copy of the GNU General Public License | |
38 | * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>. | |
39 | * | |
40 | * The authors reserve the right to distribute this or future versions of | |
41 | * Coccinelle under other licenses. | |
42 | *) | |
43 | ||
44 | ||
34e49164 | 45 | open Format |
ae4735db | 46 | module Ast = Ast_cocci |
34e49164 C |
47 | module Ast0 = Ast0_cocci |
48 | module U = Pretty_print_cocci | |
49 | ||
50 | let quiet = ref true (* true = no decoration on - context, etc *) | |
51 | ||
708f4980 C |
52 | let full_ids = ref false (* true = print rule name as well *) |
53 | ||
34e49164 C |
54 | let start_block str = |
55 | force_newline(); print_string " "; open_box 0 | |
56 | ||
57 | let end_block str = | |
58 | close_box(); force_newline () | |
59 | ||
60 | let print_option = Common.do_option | |
61 | let print_between = Common.print_between | |
62 | ||
63 | (* --------------------------------------------------------------------- *) | |
64 | (* Positions *) | |
65 | ||
66 | let meta_pos = function | |
67 | Ast0.MetaPos(name,_,_) -> | |
68 | print_string "@"; | |
69 | let (_,name) = Ast0.unwrap_mcode name in | |
70 | print_string name | |
71 | | Ast0.NoMetaPos -> () | |
72 | ||
73 | (* --------------------------------------------------------------------- *) | |
74 | (* Modified code *) | |
75 | ||
0708f913 | 76 | let mcodekind brackets fn x info mc = |
c3e37e97 C |
77 | let print = function Ast.Noindent s | Ast.Indent s -> print_string s in |
78 | List.iter (function (s,_) -> print s) info.Ast0.strings_before; | |
0708f913 | 79 | (match mc with |
34e49164 C |
80 | Ast0.MINUS(plus_stream) -> |
81 | let (lb,rb) = | |
82 | if !quiet | |
83 | then ("","") | |
84 | else | |
85 | match brackets with | |
86 | Some x -> ("[","]^"^(string_of_int x)) | |
87 | | None -> ("","") in | |
88 | let (plus_stream,_) = !plus_stream in | |
89 | if !quiet | |
90 | then fn x | |
91 | else (print_string "-"; | |
92 | print_string lb; fn x; print_string rb); | |
93 | U.print_anything ">>> " plus_stream | |
94 | | Ast0.CONTEXT(plus_streams) -> | |
95 | let (lb,rb) = | |
96 | if !quiet | |
97 | then ("","") | |
98 | else | |
99 | match brackets with | |
100 | Some x -> ("[",("]^"^(string_of_int x))) | None -> ("","") in | |
101 | let (plus_streams,t1,t2) = !plus_streams in | |
102 | U.print_around | |
103 | (function x -> | |
104 | print_string lb; fn x; print_string rb) | |
105 | x plus_streams | |
951c7801 | 106 | | Ast0.PLUS _ -> print_int (info.Ast0.pos_info.Ast0.column); fn x |
34e49164 C |
107 | | Ast0.MIXED(plus_streams) -> |
108 | let (lb,rb) = | |
109 | if !quiet | |
110 | then ("","") | |
111 | else | |
faf9a90c | 112 | let n = |
34e49164 C |
113 | match brackets with Some x -> "^"^(string_of_int x) | None -> "" in |
114 | ("§","½"^n) in | |
115 | let (plus_streams,_,_) = !plus_streams in | |
116 | U.print_around (function x -> print_string lb; fn x; print_string rb) | |
0708f913 | 117 | x plus_streams); |
c3e37e97 | 118 | List.iter (function (s,_) -> print s) info.Ast0.strings_after |
34e49164 | 119 | |
708f4980 | 120 | let mcode fn (x,_,info,mc,pos,adj) = |
34e49164 | 121 | let fn x = fn x; meta_pos !pos in |
0708f913 | 122 | mcodekind (Some info.Ast0.pos_info.Ast0.line_start)(*None*) fn x info mc |
34e49164 C |
123 | |
124 | let print_context x fn = | |
125 | mcodekind (Some (Ast0.get_line x)) fn () (Ast0.get_info x) | |
126 | (Ast0.get_mcodekind x) | |
127 | ||
708f4980 | 128 | let print_meta (ctx,name) = |
ae4735db C |
129 | (if !full_ids |
130 | then (print_string ctx; print_string ":")); | |
708f4980 | 131 | print_string name |
34e49164 C |
132 | |
133 | (* --------------------------------------------------------------------- *) | |
134 | (* --------------------------------------------------------------------- *) | |
135 | (* Dots *) | |
136 | ||
137 | let dots between fn d = | |
138 | print_context d | |
139 | (function _ -> | |
140 | match Ast0.unwrap d with | |
141 | Ast0.DOTS(l) -> print_between between fn l | |
142 | | Ast0.CIRCLES(l) -> print_between between fn l | |
143 | | Ast0.STARS(l) -> print_between between fn l) | |
144 | ||
145 | (* --------------------------------------------------------------------- *) | |
146 | ||
147 | let print_types = function | |
148 | None -> () | |
149 | | Some ty -> | |
150 | print_string "/* "; | |
151 | Format.print_flush(); | |
152 | print_between (function _ -> print_string ", ") Type_cocci.typeC ty; | |
153 | Format.print_flush(); | |
154 | print_string " */" | |
155 | ||
156 | (* --------------------------------------------------------------------- *) | |
157 | (* Identifier *) | |
158 | ||
159 | let rec ident i = | |
160 | print_context i | |
161 | (function _ -> | |
162 | match Ast0.unwrap i with | |
163 | Ast0.Id(name) -> mcode print_string name | |
164 | | Ast0.MetaId(name,_,_) -> mcode print_meta name | |
165 | | Ast0.MetaFunc(name,_,_) -> mcode print_meta name | |
166 | | Ast0.MetaLocalFunc(name,_,_) -> mcode print_meta name | |
167 | | Ast0.OptIdent(id) -> print_string "?"; ident id | |
168 | | Ast0.UniqueIdent(id) -> print_string "!"; ident id) | |
169 | ||
170 | (* --------------------------------------------------------------------- *) | |
171 | (* Expression *) | |
172 | ||
173 | let print_string_box s = print_string s; open_box 0 | |
174 | ||
175 | let rec expression e = | |
176 | print_option Type_cocci.typeC (Ast0.get_type e); | |
177 | print_context e | |
178 | (function _ -> | |
179 | match Ast0.unwrap e with | |
180 | Ast0.Ident(id) -> ident id | |
181 | | Ast0.Constant(const) -> mcode U.constant const | |
182 | | Ast0.FunCall(fn,lp,args,rp) -> | |
183 | expression fn; mcode print_string_box lp; | |
184 | let _ = dots (function _ -> ()) expression args in | |
185 | close_box(); mcode print_string rp | |
186 | | Ast0.Assignment(left,op,right,_) -> | |
187 | expression left; print_string " "; mcode U.assignOp op; | |
188 | print_string " "; expression right | |
189 | | Ast0.CondExpr(exp1,why,exp2,colon,exp3) -> | |
190 | expression exp1; print_string " "; mcode print_string why; | |
191 | print_option (function e -> print_string " "; expression e) exp2; | |
192 | print_string " "; mcode print_string colon; expression exp3 | |
193 | | Ast0.Postfix(exp,op) -> expression exp; mcode U.fixOp op | |
194 | | Ast0.Infix(exp,op) -> mcode U.fixOp op; expression exp | |
195 | | Ast0.Unary(exp,op) -> mcode U.unaryOp op; expression exp | |
196 | | Ast0.Binary(left,op,right) -> | |
197 | print_string "("; | |
198 | expression left; print_string " "; mcode U.binaryOp op; | |
199 | print_string " "; expression right; | |
200 | print_string ")" | |
201 | | Ast0.Nested(left,op,right) -> | |
202 | print_string "("; | |
203 | expression left; print_string " "; mcode U.binaryOp op; | |
204 | print_string " "; expression right; | |
205 | print_string ")" | |
206 | | Ast0.Paren(lp,exp,rp) -> | |
207 | mcode print_string_box lp; expression exp; close_box(); | |
208 | mcode print_string rp | |
209 | | Ast0.ArrayAccess(exp1,lb,exp2,rb) -> | |
210 | expression exp1; mcode print_string_box lb; expression exp2; | |
211 | close_box(); mcode print_string rb | |
212 | | Ast0.RecordAccess(exp,pt,field) -> | |
213 | expression exp; mcode print_string pt; ident field | |
214 | | Ast0.RecordPtAccess(exp,ar,field) -> | |
215 | expression exp; mcode print_string ar; ident field | |
216 | | Ast0.Cast(lp,ty,rp,exp) -> | |
217 | mcode print_string_box lp; typeC ty; close_box(); | |
218 | mcode print_string rp; expression exp | |
219 | | Ast0.SizeOfExpr(szf,exp) -> | |
220 | mcode print_string szf; expression exp | |
221 | | Ast0.SizeOfType(szf,lp,ty,rp) -> | |
222 | mcode print_string szf; | |
223 | mcode print_string_box lp; typeC ty; close_box(); | |
224 | mcode print_string rp | |
225 | | Ast0.TypeExp(ty) -> typeC ty | |
226 | | Ast0.MetaErr(name,_,_) -> mcode print_meta name | |
485bce71 C |
227 | | Ast0.MetaExpr(name,_,ty,_,pure) -> |
228 | mcode print_meta name; print_types ty(*; | |
229 | print_string "^"; | |
230 | (match pure with | |
231 | Ast0.Pure -> print_string "pure" | |
232 | | Ast0.Impure -> print_string "impure" | |
233 | | Ast0.Context -> print_string "context" | |
234 | | Ast0.PureContext -> print_string "pure_context")*) | |
34e49164 C |
235 | | Ast0.MetaExprList(name,_,_) -> mcode print_meta name |
236 | | Ast0.EComma(cm) -> mcode print_string cm; print_space() | |
237 | | Ast0.DisjExpr(_,exp_list,_,_) -> | |
238 | print_string "\n("; force_newline(); | |
239 | print_between | |
240 | (function _ -> print_string "\n|"; force_newline()) | |
241 | expression exp_list; | |
242 | print_string "\n)" | |
243 | | Ast0.NestExpr(starter,expr_dots,ender,None,multi) -> | |
244 | mcode print_string starter; | |
245 | start_block(); dots force_newline expression expr_dots; end_block(); | |
246 | mcode print_string ender | |
247 | | Ast0.NestExpr(starter,expr_dots,ender,Some whencode,multi) -> | |
248 | mcode print_string starter; print_string " WHEN != "; | |
249 | expression whencode; | |
250 | start_block(); dots force_newline expression expr_dots; end_block(); | |
251 | mcode print_string ender | |
252 | | Ast0.Edots(dots,Some whencode) | |
253 | | Ast0.Ecircles(dots,Some whencode) | |
254 | | Ast0.Estars(dots,Some whencode) -> | |
255 | mcode print_string dots; print_string " WHEN != "; | |
256 | expression whencode | |
257 | | Ast0.Edots(dots,None) | |
258 | | Ast0.Ecircles(dots,None) | |
259 | | Ast0.Estars(dots,None) -> mcode print_string dots | |
260 | | Ast0.OptExp(exp) -> print_string "?"; expression exp | |
261 | | Ast0.UniqueExp(exp) -> print_string "!"; expression exp) | |
262 | ||
263 | and expression_dots x = dots (function _ -> ()) expression x | |
264 | ||
265 | (* --------------------------------------------------------------------- *) | |
266 | (* Types *) | |
267 | ||
268 | and print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2) fn = | |
269 | typeC ty; mcode print_string lp1; mcode print_string star; fn(); | |
270 | mcode print_string rp1; mcode print_string lp2; | |
271 | parameter_list params; mcode print_string rp2 | |
272 | ||
273 | and print_function_type (ty,lp1,params,rp1) fn = | |
274 | print_option typeC ty; fn(); mcode print_string lp1; | |
275 | parameter_list params; mcode print_string rp1 | |
276 | ||
277 | and typeC t = | |
278 | print_context t | |
279 | (function _ -> | |
280 | match Ast0.unwrap t with | |
281 | Ast0.ConstVol(cv,ty) -> | |
282 | mcode U.const_vol cv; print_string " "; typeC ty | |
faf9a90c C |
283 | | Ast0.BaseType(ty,strings) -> |
284 | List.iter (function s -> mcode print_string s; print_string " ") | |
285 | strings | |
286 | | Ast0.Signed(sgn,ty) -> mcode U.sign sgn; print_option typeC ty | |
34e49164 C |
287 | | Ast0.Pointer(ty,star) -> typeC ty; mcode print_string star |
288 | | Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) -> | |
289 | print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2) | |
290 | (function _ -> ()) | |
291 | | Ast0.FunctionType(ty,lp1,params,rp1) -> | |
292 | print_function_type (ty,lp1,params,rp1) (function _ -> ()) | |
293 | | Ast0.Array(ty,lb,size,rb) -> | |
294 | typeC ty; mcode print_string lb; print_option expression size; | |
295 | mcode print_string rb | |
faf9a90c C |
296 | | Ast0.EnumName(kind,name) -> mcode print_string kind; print_string " "; |
297 | ident name | |
34e49164 C |
298 | | Ast0.StructUnionName(kind,name) -> |
299 | mcode U.structUnion kind; | |
300 | print_option (function x -> ident x; print_string " ") name | |
301 | | Ast0.StructUnionDef(ty,lb,decls,rb) -> | |
302 | typeC ty; mcode print_string lb; | |
303 | dots force_newline declaration decls; | |
304 | mcode print_string rb | |
305 | | Ast0.TypeName(name)-> mcode print_string name; print_string " " | |
306 | | Ast0.MetaType(name,_)-> mcode print_meta name; print_string " " | |
307 | | Ast0.DisjType(lp,types,mids,rp) -> | |
308 | print_string "\n"; mcode print_string lp; force_newline(); | |
309 | print_between | |
310 | (function _ -> print_string "\n|"; force_newline()) | |
311 | typeC types; | |
312 | print_string "\n"; mcode print_string rp | |
313 | | Ast0.OptType(ty) -> print_string "?"; typeC ty | |
314 | | Ast0.UniqueType(ty) -> print_string "!"; typeC ty) | |
315 | ||
316 | (* --------------------------------------------------------------------- *) | |
317 | (* Variable declaration *) | |
318 | (* Even if the Cocci program specifies a list of declarations, they are | |
319 | split out into multiple declarations of a single variable each. *) | |
320 | ||
321 | and print_named_type ty id = | |
322 | match Ast0.unwrap ty with | |
323 | Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) -> | |
324 | print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2) | |
325 | (function _ -> print_string " "; ident id) | |
326 | | Ast0.FunctionType(ty,lp1,params,rp1) -> | |
327 | print_function_type (ty,lp1,params,rp1) | |
328 | (function _ -> print_string " "; ident id) | |
329 | | Ast0.Array(ty,lb,size,rb) -> | |
330 | let rec loop ty k = | |
331 | match Ast0.unwrap ty with | |
332 | Ast0.Array(ty,lb,size,rb) -> | |
333 | loop ty | |
334 | (function _ -> | |
335 | k (); | |
336 | mcode print_string lb; | |
337 | print_option expression size; | |
338 | mcode print_string rb) | |
339 | | _ -> typeC ty; ident id; k () in | |
340 | loop ty (function _ -> ()) | |
341 | | _ -> typeC ty; ident id | |
342 | ||
343 | and declaration d = | |
344 | print_context d | |
345 | (function _ -> | |
346 | match Ast0.unwrap d with | |
347 | Ast0.Init(stg,ty,id,eq,ini,sem) -> | |
348 | print_option (mcode U.storage) stg; | |
349 | print_named_type ty id; | |
350 | print_string " "; | |
351 | mcode print_string eq; print_string " "; initialiser ini; | |
352 | mcode print_string sem | |
353 | | Ast0.UnInit(stg,ty,id,sem) -> | |
354 | print_option (mcode U.storage) stg; print_named_type ty id; | |
355 | mcode print_string sem | |
356 | | Ast0.MacroDecl(name,lp,args,rp,sem) -> | |
357 | ident name; mcode print_string_box lp; | |
358 | let _ = dots (function _ -> ()) expression args in | |
359 | close_box(); mcode print_string rp; mcode print_string sem | |
360 | | Ast0.TyDecl(ty,sem) -> typeC ty; mcode print_string sem | |
361 | | Ast0.Typedef(stg,ty,id,sem) -> | |
362 | mcode print_string stg; typeC ty; typeC id; | |
363 | mcode print_string sem | |
364 | | Ast0.DisjDecl(_,decls,_,_) -> | |
365 | print_string "\n("; force_newline(); | |
366 | print_between | |
367 | (function _ -> print_string "\n|"; force_newline()) | |
368 | declaration decls; | |
369 | print_string "\n)" | |
faf9a90c | 370 | | Ast0.Ddots(dots,Some whencode) -> |
34e49164 C |
371 | mcode print_string dots; print_string " when != "; |
372 | declaration whencode | |
373 | | Ast0.Ddots(dots,None) -> mcode print_string dots | |
374 | | Ast0.OptDecl(decl) -> print_string "?"; declaration decl | |
375 | | Ast0.UniqueDecl(decl) -> print_string "!"; declaration decl) | |
376 | ||
377 | and declaration_dots l = dots (function _ -> ()) declaration l | |
378 | ||
379 | (* --------------------------------------------------------------------- *) | |
380 | (* Initialiser *) | |
381 | ||
382 | and initialiser i = | |
383 | print_context i | |
384 | (function _ -> | |
385 | match Ast0.unwrap i with | |
113803cf C |
386 | Ast0.MetaInit(name,_)-> mcode print_meta name; print_string " " |
387 | | Ast0.InitExpr(exp) -> expression exp | |
34e49164 C |
388 | | Ast0.InitList(lb,initlist,rb) -> |
389 | mcode print_string lb; open_box 0; | |
390 | let _ = dots (function _ -> ()) initialiser initlist in | |
391 | close_box(); mcode print_string rb | |
113803cf C |
392 | | Ast0.InitGccExt(designators,eq,ini) -> |
393 | List.iter designator designators; print_string " "; | |
34e49164 C |
394 | mcode print_string eq; print_string " "; initialiser ini |
395 | | Ast0.InitGccName(name,eq,ini) -> | |
396 | ident name; mcode print_string eq; initialiser ini | |
34e49164 C |
397 | | Ast0.IComma(cm) -> mcode print_string cm; force_newline() |
398 | | Ast0.Idots(d,Some whencode) -> | |
399 | mcode print_string d; print_string " WHEN != "; | |
400 | initialiser whencode | |
401 | | Ast0.Idots(d,None) -> mcode print_string d | |
402 | | Ast0.OptIni(ini) -> print_string "?"; initialiser ini | |
403 | | Ast0.UniqueIni(ini) -> print_string "!"; initialiser ini) | |
404 | ||
113803cf C |
405 | and designator = function |
406 | Ast0.DesignatorField(dot,id) -> mcode print_string dot; ident id | |
407 | | Ast0.DesignatorIndex(lb,exp,rb) -> | |
408 | mcode print_string lb; expression exp; mcode print_string rb | |
409 | | Ast0.DesignatorRange(lb,min,dots,max,rb) -> | |
410 | mcode print_string lb; expression min; mcode print_string dots; | |
411 | expression max; mcode print_string rb | |
412 | ||
34e49164 C |
413 | and initialiser_list l = dots (function _ -> ()) initialiser l |
414 | ||
415 | (* --------------------------------------------------------------------- *) | |
416 | (* Parameter *) | |
417 | ||
418 | and parameterTypeDef p = | |
419 | print_context p | |
420 | (function _ -> | |
421 | match Ast0.unwrap p with | |
422 | Ast0.VoidParam(ty) -> typeC ty | |
423 | | Ast0.Param(ty,Some id) -> print_named_type ty id | |
424 | | Ast0.Param(ty,None) -> typeC ty | |
425 | | Ast0.MetaParam(name,_) -> mcode print_meta name | |
426 | | Ast0.MetaParamList(name,_,_) -> mcode print_meta name | |
427 | | Ast0.PComma(cm) -> mcode print_string cm; print_space() | |
428 | | Ast0.Pdots(dots) -> mcode print_string dots | |
429 | | Ast0.Pcircles(dots) -> mcode print_string dots | |
430 | | Ast0.OptParam(param) -> print_string "?"; parameterTypeDef param | |
431 | | Ast0.UniqueParam(param) -> print_string "!"; parameterTypeDef param) | |
432 | ||
433 | and parameter_list l = dots (function _ -> ()) parameterTypeDef l | |
434 | ||
435 | (* --------------------------------------------------------------------- *) | |
436 | (* Top-level code *) | |
437 | ||
438 | and statement arity s = | |
439 | print_context s | |
440 | (function _ -> | |
441 | match Ast0.unwrap s with | |
442 | Ast0.FunDecl(_,fninfo,name,lp,params,rp,lbrace,body,rbrace) -> | |
443 | print_string arity; | |
444 | List.iter print_fninfo fninfo; | |
445 | ident name; mcode print_string_box lp; | |
446 | parameter_list params; close_box(); mcode print_string rp; | |
447 | print_string " "; | |
448 | print_string arity; mcode print_string lbrace; start_block(); | |
449 | dots force_newline (statement arity) body; | |
450 | end_block(); print_string arity; mcode print_string rbrace | |
451 | | Ast0.Decl(_,decl) -> print_string arity; declaration decl | |
452 | | Ast0.Seq(lbrace,body,rbrace) -> | |
453 | print_string arity; mcode print_string lbrace; start_block(); | |
454 | dots force_newline (statement arity) body; | |
455 | end_block(); print_string arity; mcode print_string rbrace | |
456 | | Ast0.ExprStatement(exp,sem) -> | |
457 | print_string arity; expression exp; mcode print_string sem | |
458 | | Ast0.IfThen(iff,lp,exp,rp,branch1,(info,aft)) -> | |
459 | print_string arity; | |
460 | mcode print_string iff; print_string " "; mcode print_string_box lp; | |
461 | expression exp; close_box(); mcode print_string rp; print_string " "; | |
462 | statement arity branch1; | |
708f4980 | 463 | mcode (function _ -> ()) ((),(),info,aft,ref Ast0.NoMetaPos,-1) |
34e49164 C |
464 | | Ast0.IfThenElse(iff,lp,exp,rp,branch1,els,branch2,(info,aft)) -> |
465 | print_string arity; | |
466 | mcode print_string iff; print_string " "; mcode print_string_box lp; | |
467 | expression exp; close_box(); mcode print_string rp; print_string " "; | |
468 | statement arity branch1; | |
469 | print_string arity; mcode print_string els; print_string " "; | |
470 | statement arity branch2; | |
708f4980 | 471 | mcode (function _ -> ()) ((),(),info,aft,ref Ast0.NoMetaPos,-1) |
34e49164 C |
472 | | Ast0.While(whl,lp,exp,rp,body,(info,aft)) -> |
473 | print_string arity; | |
474 | mcode print_string whl; print_string " "; mcode print_string_box lp; | |
475 | expression exp; close_box(); mcode print_string rp; print_string " "; | |
476 | statement arity body; | |
708f4980 | 477 | mcode (function _ -> ()) ((),(),info,aft,ref Ast0.NoMetaPos,-1) |
34e49164 C |
478 | | Ast0.Do(d,body,whl,lp,exp,rp,sem) -> |
479 | print_string arity; mcode print_string d; print_string " "; | |
480 | statement arity body; | |
481 | print_string arity; | |
482 | mcode print_string whl; print_string " "; mcode print_string_box lp; | |
483 | expression exp; close_box(); mcode print_string rp; | |
484 | mcode print_string sem | |
485 | | Ast0.For(fr,lp,e1,sem1,e2,sem2,e3,rp,body,(info,aft)) -> | |
486 | print_string arity; | |
487 | mcode print_string fr; mcode print_string_box lp; | |
488 | print_option expression e1; mcode print_string sem1; | |
489 | print_option expression e2; mcode print_string sem2; | |
490 | print_option expression e3; close_box(); | |
491 | mcode print_string rp; print_string " "; statement arity body; | |
708f4980 | 492 | mcode (function _ -> ()) ((),(),info,aft,ref Ast0.NoMetaPos,-1) |
34e49164 C |
493 | | Ast0.Iterator(nm,lp,args,rp,body,(info,aft)) -> |
494 | print_string arity; | |
495 | ident nm; print_string " "; mcode print_string_box lp; | |
496 | let _ = dots (function _ -> ()) expression args in | |
497 | close_box(); mcode print_string rp; print_string " "; | |
498 | statement arity body; | |
708f4980 | 499 | mcode (function _ -> ()) ((),(),info,aft,ref Ast0.NoMetaPos,-1) |
fc1ad971 | 500 | | Ast0.Switch(switch,lp,exp,rp,lb,decls,cases,rb) -> |
34e49164 C |
501 | print_string arity; |
502 | mcode print_string switch; print_string " "; | |
503 | mcode print_string_box lp; expression exp; close_box(); | |
504 | mcode print_string rp; print_string " "; mcode print_string lb; | |
fc1ad971 | 505 | dots force_newline (statement arity) decls; |
34e49164 C |
506 | dots force_newline (case_line arity) cases; |
507 | mcode print_string rb | |
508 | | Ast0.Break(br,sem) -> | |
509 | print_string arity; mcode print_string br; mcode print_string sem | |
510 | | Ast0.Continue(cont,sem) -> | |
511 | print_string arity; mcode print_string cont; mcode print_string sem | |
512 | | Ast0.Label(l,dd) -> ident l; print_string ":" | |
513 | | Ast0.Goto(goto,l,sem) -> | |
514 | mcode print_string goto; ident l; mcode print_string sem | |
515 | | Ast0.Return(ret,sem) -> | |
516 | print_string arity; mcode print_string ret; mcode print_string sem | |
517 | | Ast0.ReturnExpr(ret,exp,sem) -> | |
518 | print_string arity; mcode print_string ret; print_string " "; | |
519 | expression exp; mcode print_string sem | |
520 | | Ast0.MetaStmt(name,pure) -> | |
521 | print_string arity; mcode print_meta name;(* | |
522 | print_string "^"; | |
523 | (match pure with | |
524 | Ast0.Pure -> print_string "pure" | |
525 | | Ast0.Impure -> print_string "impure" | |
526 | | Ast0.Context -> print_string "context" | |
527 | | Ast0.PureContext -> print_string "pure_context")*) | |
528 | | Ast0.MetaStmtList(name,_) -> | |
529 | print_string arity; mcode print_meta name | |
978fd7e5 | 530 | | Ast0.Disj(starter,statement_dots_list,_,ender) -> |
34e49164 | 531 | print_string arity; |
978fd7e5 | 532 | print_string "\n"; mcode print_string starter; force_newline(); |
34e49164 C |
533 | print_between |
534 | (function _ -> print_string "\n|"; force_newline()) | |
535 | (dots force_newline (statement arity)) | |
536 | statement_dots_list; | |
978fd7e5 | 537 | print_string "\n"; mcode print_string ender |
34e49164 C |
538 | | Ast0.Nest(starter,stmt_dots,ender,whn,multi) -> |
539 | print_string arity; | |
540 | mcode print_string starter; | |
541 | open_box 0; | |
542 | List.iter | |
543 | (whencode (dots force_newline (statement "")) (statement "")) | |
544 | whn; | |
545 | close_box(); | |
546 | start_block(); | |
547 | dots force_newline (statement arity) stmt_dots; | |
548 | end_block(); | |
549 | mcode print_string ender | |
550 | | Ast0.Exp(exp) -> print_string arity; expression exp | |
551 | | Ast0.TopExp(exp) -> print_string arity; expression exp | |
552 | | Ast0.Ty(ty) -> print_string arity; typeC ty | |
1be43e12 | 553 | | Ast0.TopInit(init) -> initialiser init |
34e49164 C |
554 | | Ast0.Dots(d,whn) | Ast0.Circles(d,whn) | Ast0.Stars(d,whn) -> |
555 | print_string arity; mcode print_string d; | |
556 | List.iter | |
557 | (whencode (dots force_newline (statement "")) (statement "")) | |
558 | whn | |
559 | | Ast0.Include(inc,s) -> | |
560 | mcode print_string inc; print_string " "; mcode U.inc_file s | |
561 | | Ast0.Define(def,id,params,body) -> | |
562 | mcode print_string def; print_string " "; ident id; | |
563 | print_define_parameters params; | |
564 | print_string " "; | |
565 | dots force_newline (statement arity) body | |
566 | | Ast0.OptStm(re) -> statement "?" re | |
567 | | Ast0.UniqueStm(re) -> statement "!" re) | |
568 | ||
569 | and print_define_parameters params = | |
570 | match Ast0.unwrap params with | |
571 | Ast0.NoParams -> () | |
572 | | Ast0.DParams(lp,params,rp) -> | |
573 | mcode print_string lp; | |
574 | dots (function _ -> ()) print_define_param params; mcode print_string rp | |
575 | ||
576 | and print_define_param param = | |
577 | match Ast0.unwrap param with | |
578 | Ast0.DParam(id) -> ident id | |
579 | | Ast0.DPComma(comma) -> mcode print_string comma | |
580 | | Ast0.DPdots(dots) -> mcode print_string dots | |
581 | | Ast0.DPcircles(circles) -> mcode print_string circles | |
582 | | Ast0.OptDParam(dp) -> print_string "?"; print_define_param dp | |
583 | | Ast0.UniqueDParam(dp) -> print_string "!"; print_define_param dp | |
584 | ||
585 | and print_fninfo = function | |
586 | Ast0.FStorage(stg) -> mcode U.storage stg | |
587 | | Ast0.FType(ty) -> typeC ty | |
588 | | Ast0.FInline(inline) -> mcode print_string inline | |
589 | | Ast0.FAttr(attr) -> mcode print_string attr | |
590 | ||
591 | and whencode notfn alwaysfn = function | |
592 | Ast0.WhenNot a -> | |
593 | print_string " WHEN != "; open_box 0; notfn a; close_box() | |
594 | | Ast0.WhenAlways a -> | |
595 | print_string " WHEN = "; open_box 0; alwaysfn a; close_box() | |
596 | | Ast0.WhenModifier x -> print_string " WHEN "; U.print_when_modif x | |
1be43e12 C |
597 | | Ast0.WhenNotTrue a -> |
598 | print_string " WHEN != TRUE "; open_box 0; expression a; close_box() | |
599 | | Ast0.WhenNotFalse a -> | |
600 | print_string " WHEN != FALSE "; open_box 0; expression a; close_box() | |
34e49164 C |
601 | |
602 | and case_line arity c = | |
603 | print_context c | |
604 | (function _ -> | |
605 | match Ast0.unwrap c with | |
606 | Ast0.Default(def,colon,code) -> | |
607 | print_string arity; | |
608 | mcode print_string def; mcode print_string colon; print_string " "; | |
609 | dots force_newline (statement arity) code | |
610 | | Ast0.Case(case,exp,colon,code) -> | |
611 | print_string arity; | |
612 | mcode print_string case; print_string " "; expression exp; | |
613 | mcode print_string colon; print_string " "; | |
614 | dots force_newline (statement arity) code | |
fc1ad971 C |
615 | | Ast0.DisjCase(starter,case_lines,mids,ender) -> |
616 | print_string "\n("; force_newline(); | |
617 | print_between | |
618 | (function _ -> print_string "\n|"; force_newline()) | |
619 | (case_line arity) case_lines; | |
620 | print_string "\n)" | |
34e49164 C |
621 | | Ast0.OptCase(case) -> case_line "?" case) |
622 | ||
623 | and statement_dots l = dots (function _ -> ()) (statement "") l | |
624 | and case_dots l = dots (function _ -> ()) (case_line "") l | |
625 | ||
626 | (* --------------------------------------------------------------------- *) | |
627 | (* Top level code *) | |
628 | ||
629 | let top_level t = | |
630 | print_context t | |
631 | (function _ -> | |
632 | match Ast0.unwrap t with | |
633 | Ast0.FILEINFO(old_file,new_file) -> | |
634 | print_string "--- "; mcode print_string old_file; force_newline(); | |
635 | print_string "+++ "; mcode print_string new_file | |
636 | | Ast0.DECL(stmt) -> statement "" stmt | |
637 | | Ast0.CODE(stmt_dots) -> | |
638 | dots force_newline (statement "") stmt_dots | |
639 | | Ast0.ERRORWORDS(exps) -> | |
640 | print_string "error words = ["; | |
641 | print_between (function _ -> print_string ", ") expression exps; | |
642 | print_string "]" | |
643 | | Ast0.OTHER(s) -> | |
644 | print_string "OTHER("; statement "" s; print_string ")") | |
645 | ||
646 | let rule = | |
647 | print_between (function _ -> force_newline(); force_newline()) top_level | |
648 | ||
649 | let unparse_anything x = | |
650 | let q = !quiet in | |
651 | quiet := true; | |
652 | (match x with | |
653 | Ast0.DotsExprTag(d) -> | |
654 | print_string "ExpDots:"; force_newline(); | |
655 | expression_dots d | |
656 | | Ast0.DotsParamTag(d) -> | |
657 | parameter_list d | |
658 | | Ast0.DotsInitTag(d) -> | |
659 | initialiser_list d | |
660 | | Ast0.DotsStmtTag(d) -> | |
661 | print_string "StmDots:"; force_newline(); | |
662 | statement_dots d | |
663 | | Ast0.DotsDeclTag(d) -> | |
664 | declaration_dots d | |
665 | | Ast0.DotsCaseTag(d) -> | |
666 | case_dots d | |
667 | | Ast0.IdentTag(d) -> | |
668 | ident d | |
669 | | Ast0.ExprTag(d) | Ast0.ArgExprTag(d) | Ast0.TestExprTag(d) -> | |
670 | print_string "Exp:"; force_newline(); | |
671 | expression d | |
672 | | Ast0.TypeCTag(d) -> | |
673 | typeC d | |
674 | | Ast0.ParamTag(d) -> | |
675 | parameterTypeDef d | |
676 | | Ast0.InitTag(d) -> | |
677 | initialiser d | |
678 | | Ast0.DeclTag(d) -> | |
679 | declaration d | |
680 | | Ast0.StmtTag(d) -> | |
681 | print_string "Stm:"; force_newline(); | |
682 | statement "" d | |
683 | | Ast0.CaseLineTag(d) -> | |
684 | case_line "" d | |
685 | | Ast0.TopTag(d) -> | |
686 | top_level d | |
687 | | Ast0.IsoWhenTag(x) -> U.print_when_modif x | |
1be43e12 C |
688 | | Ast0.IsoWhenTTag(e) -> expression e |
689 | | Ast0.IsoWhenFTag(e) -> expression e | |
34e49164 C |
690 | | Ast0.MetaPosTag(var) -> meta_pos var); |
691 | quiet := q; | |
692 | print_newline() | |
693 | ||
694 | let unparse x = | |
695 | print_string "\n@@\n@@"; | |
696 | force_newline(); | |
697 | force_newline(); | |
698 | rule x; | |
699 | print_newline() | |
700 | ||
701 | let unparse_to_string x = Common.format_to_string (function _ -> unparse x) |