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