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