Commit | Line | Data |
---|---|---|
c491d8ee C |
1 | (* |
2 | * Copyright 2010, INRIA, University of Copenhagen | |
3 | * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix | |
4 | * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen | |
5 | * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix | |
6 | * This file is part of Coccinelle. | |
7 | * | |
8 | * Coccinelle is free software: you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation, according to version 2 of the License. | |
11 | * | |
12 | * Coccinelle is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>. | |
19 | * | |
20 | * The authors reserve the right to distribute this or future versions of | |
21 | * Coccinelle under other licenses. | |
22 | *) | |
23 | ||
24 | ||
34e49164 C |
25 | open Format |
26 | module Ast = Ast_cocci | |
27 | ||
28 | let print_plus_flag = ref true | |
29 | let print_minus_flag = ref true | |
30 | let print_newlines_disj = ref true | |
31 | ||
32 | let start_block str = | |
33 | force_newline(); print_string " "; open_box 0 | |
34 | ||
35 | let end_block str = | |
36 | close_box(); force_newline () | |
37 | ||
38 | let print_string_box s = print_string s; open_box 0 | |
39 | ||
40 | ||
41 | let print_option = Common.do_option | |
42 | let print_between = Common.print_between | |
43 | ||
44 | (* --------------------------------------------------------------------- *) | |
45 | (* Modified code *) | |
46 | ||
47 | (* avoid polyvariance problems *) | |
48 | let anything : (Ast.anything -> unit) ref = ref (function _ -> ()) | |
49 | ||
50 | let rec print_anything str = function | |
51 | [] -> () | |
52 | | stream -> | |
53 | start_block(); | |
54 | print_between force_newline | |
55 | (function x -> | |
56 | print_string str; open_box 0; print_anything_list x; close_box()) | |
57 | stream; | |
58 | end_block() | |
59 | ||
60 | and print_anything_list = function | |
61 | [] -> () | |
62 | | [x] -> !anything x | |
63 | | bef::((aft::_) as rest) -> | |
64 | !anything bef; | |
65 | let space = | |
66 | (match bef with | |
67 | Ast.Rule_elemTag(_) | Ast.AssignOpTag(_) | Ast.BinaryOpTag(_) | |
68 | | Ast.ArithOpTag(_) | Ast.LogicalOpTag(_) | |
69 | | Ast.Token("if",_) | Ast.Token("while",_) -> true | _ -> false) or | |
70 | (match aft with | |
71 | Ast.Rule_elemTag(_) | Ast.AssignOpTag(_) | Ast.BinaryOpTag(_) | |
72 | | Ast.ArithOpTag(_) | Ast.LogicalOpTag(_) | Ast.Token("{",_) -> true | |
73 | | _ -> false) in | |
74 | if space then print_string " "; | |
75 | print_anything_list rest | |
76 | ||
77 | let print_around printer term = function | |
78 | Ast.NOTHING -> printer term | |
951c7801 C |
79 | | Ast.BEFORE(bef,_) -> print_anything "<<< " bef; printer term |
80 | | Ast.AFTER(aft,_) -> printer term; print_anything ">>> " aft | |
81 | | Ast.BEFOREAFTER(bef,aft,_) -> | |
34e49164 C |
82 | print_anything "<<< " bef; printer term; print_anything ">>> " aft |
83 | ||
84 | let print_string_befaft fn x info = | |
c3e37e97 C |
85 | let print = function Ast.Noindent s | Ast.Indent s -> print_string s in |
86 | List.iter (function (s,_,_) -> print s; force_newline()) info.Ast.strbef; | |
34e49164 | 87 | fn x; |
c3e37e97 | 88 | List.iter (function (s,_,_) -> force_newline(); print s) info.Ast.straft |
34e49164 C |
89 | |
90 | let print_meta (r,x) = print_string r; print_string ":"; print_string x | |
91 | ||
92 | let print_pos = function | |
93 | Ast.MetaPos(name,_,_,_,_) -> | |
94 | let name = Ast.unwrap_mcode name in | |
95 | print_string "@"; print_meta name | |
96 | | _ -> () | |
97 | ||
98 | let mcode fn = function | |
708f4980 | 99 | (x, _, Ast.MINUS(_,_,adj,plus_stream), pos) -> |
34e49164 C |
100 | if !print_minus_flag |
101 | then print_string (if !Flag.sgrep_mode2 then "*" else "-"); | |
102 | fn x; print_pos pos; | |
faf9a90c | 103 | if !print_plus_flag |
34e49164 | 104 | then print_anything ">>> " plus_stream |
faf9a90c | 105 | | (x, _, Ast.CONTEXT(_,plus_streams), pos) -> |
34e49164 C |
106 | if !print_plus_flag |
107 | then | |
108 | let fn x = fn x; print_pos pos in | |
109 | print_around fn x plus_streams | |
110 | else (fn x; print_pos pos) | |
951c7801 | 111 | | (x, info, Ast.PLUS _, pos) -> |
34e49164 C |
112 | let fn x = fn x; print_pos pos in |
113 | print_string_befaft fn x info | |
114 | ||
faf9a90c | 115 | let print_mcodekind = function |
708f4980 | 116 | Ast.MINUS(_,_,_,plus_stream) -> |
34e49164 C |
117 | print_string "MINUS"; |
118 | print_anything ">>> " plus_stream | |
119 | | Ast.CONTEXT(_,plus_streams) -> | |
120 | print_around (function _ -> print_string "CONTEXT") () plus_streams | |
951c7801 | 121 | | Ast.PLUS _ -> print_string "PLUS" |
34e49164 C |
122 | |
123 | (* --------------------------------------------------------------------- *) | |
124 | (* --------------------------------------------------------------------- *) | |
125 | (* Dots *) | |
126 | ||
127 | let dots between fn d = | |
128 | match Ast.unwrap d with | |
129 | Ast.DOTS(l) -> print_between between fn l | |
130 | | Ast.CIRCLES(l) -> print_between between fn l | |
131 | | Ast.STARS(l) -> print_between between fn l | |
132 | ||
5636bb2c C |
133 | let nest_dots starter ender fn f d = |
134 | mcode print_string starter; | |
135 | f(); start_block(); | |
136 | (match Ast.unwrap d with | |
137 | Ast.DOTS(l) -> print_between force_newline fn l | |
138 | | Ast.CIRCLES(l) -> print_between force_newline fn l | |
139 | | Ast.STARS(l) -> print_between force_newline fn l); | |
140 | end_block(); | |
141 | mcode print_string ender | |
34e49164 C |
142 | |
143 | (* --------------------------------------------------------------------- *) | |
144 | ||
145 | let print_type keep info = function | |
146 | None -> () | |
147 | (* print_string "/* "; | |
148 | print_string "keep:"; print_unitary keep; | |
149 | print_string " inherited:"; print_bool inherited; | |
150 | print_string " */"*) | |
151 | | Some ty -> () | |
152 | (*; | |
153 | print_string "/* "; | |
154 | print_between (function _ -> print_string ", ") Type_cocci.typeC ty;(* | |
155 | print_string "keep:"; print_unitary keep; | |
156 | print_string " inherited:"; print_bool inherited;*) | |
157 | print_string " */"*) | |
158 | ||
951c7801 C |
159 | (* --------------------------------------------------------------------- *) |
160 | (* Contraint on Identifier and Function *) | |
161 | (* FIXME: Not called at the moment *) | |
162 | ||
5636bb2c C |
163 | let rec idconstraint = function |
164 | Ast.IdNoConstraint -> print_string "/* No constraint */" | |
165 | | Ast.IdNegIdSet (str,meta) -> | |
166 | List.iter (function s -> print_string (" "^s)) str; | |
167 | List.iter (function (r,n) -> print_string " "; print_meta(r,n)) meta | |
168 | | Ast.IdRegExpConstraint re -> regconstraint re | |
169 | ||
170 | and regconstraint = function | |
171 | Ast.IdRegExp (re,_) -> | |
172 | print_string "~= \""; print_string re; print_string "\"" | |
173 | | Ast.IdNotRegExp (re,_) -> | |
174 | print_string "~!= \""; print_string re; print_string "\"" | |
951c7801 | 175 | |
34e49164 C |
176 | (* --------------------------------------------------------------------- *) |
177 | (* Identifier *) | |
178 | ||
179 | let rec ident i = | |
180 | match Ast.unwrap i with | |
951c7801 C |
181 | Ast.Id(name) -> mcode print_string name |
182 | | Ast.MetaId(name,_,keep,inherited) -> mcode print_meta name | |
183 | | Ast.MetaFunc(name,_,_,_) -> mcode print_meta name | |
184 | | Ast.MetaLocalFunc(name,_,_,_) -> mcode print_meta name | |
185 | | Ast.OptIdent(id) -> print_string "?"; ident id | |
186 | | Ast.UniqueIdent(id) -> print_string "!"; ident id | |
34e49164 C |
187 | |
188 | and print_unitary = function | |
189 | Type_cocci.Unitary -> print_string "unitary" | |
190 | | Type_cocci.Nonunitary -> print_string "nonunitary" | |
191 | | Type_cocci.Saved -> print_string "saved" | |
192 | ||
193 | (* --------------------------------------------------------------------- *) | |
194 | (* Expression *) | |
195 | ||
196 | let print_disj_list fn l = | |
197 | if !print_newlines_disj | |
198 | then (force_newline(); print_string "("; force_newline()) | |
199 | else print_string "("; | |
200 | print_between | |
201 | (function _ -> | |
202 | if !print_newlines_disj | |
203 | then (force_newline(); print_string "|"; force_newline()) | |
204 | else print_string " | ") | |
205 | fn l; | |
206 | if !print_newlines_disj | |
207 | then (force_newline(); print_string ")"; force_newline()) | |
208 | else print_string ")" | |
209 | ||
210 | let rec expression e = | |
211 | match Ast.unwrap e with | |
212 | Ast.Ident(id) -> ident id | |
213 | | Ast.Constant(const) -> mcode constant const | |
214 | | Ast.FunCall(fn,lp,args,rp) -> | |
215 | expression fn; mcode print_string_box lp; | |
216 | dots (function _ -> ()) expression args; | |
217 | close_box(); mcode print_string rp | |
218 | | Ast.Assignment(left,op,right,simple) -> | |
219 | expression left; print_string " "; mcode assignOp op; | |
220 | print_string " "; expression right | |
221 | | Ast.CondExpr(exp1,why,exp2,colon,exp3) -> | |
222 | expression exp1; print_string " "; mcode print_string why; | |
223 | print_option (function e -> print_string " "; expression e) exp2; | |
224 | print_string " "; mcode print_string colon; expression exp3 | |
225 | | Ast.Postfix(exp,op) -> expression exp; mcode fixOp op | |
226 | | Ast.Infix(exp,op) -> mcode fixOp op; expression exp | |
227 | | Ast.Unary(exp,op) -> mcode unaryOp op; expression exp | |
228 | | Ast.Binary(left,op,right) -> | |
229 | expression left; print_string " "; mcode binaryOp op; print_string " "; | |
230 | expression right | |
231 | | Ast.Nested(left,op,right) -> | |
232 | expression left; print_string " "; mcode binaryOp op; print_string " "; | |
233 | expression right | |
234 | | Ast.Paren(lp,exp,rp) -> | |
235 | mcode print_string_box lp; expression exp; close_box(); | |
236 | mcode print_string rp | |
237 | | Ast.ArrayAccess(exp1,lb,exp2,rb) -> | |
238 | expression exp1; mcode print_string_box lb; expression exp2; close_box(); | |
239 | mcode print_string rb | |
240 | | Ast.RecordAccess(exp,pt,field) -> | |
241 | expression exp; mcode print_string pt; ident field | |
242 | | Ast.RecordPtAccess(exp,ar,field) -> | |
243 | expression exp; mcode print_string ar; ident field | |
244 | | Ast.Cast(lp,ty,rp,exp) -> | |
245 | mcode print_string_box lp; fullType ty; close_box(); | |
246 | mcode print_string rp; expression exp | |
247 | | Ast.SizeOfExpr(sizeof,exp) -> | |
248 | mcode print_string sizeof; expression exp | |
249 | | Ast.SizeOfType(sizeof,lp,ty,rp) -> | |
250 | mcode print_string sizeof; | |
251 | mcode print_string_box lp; fullType ty; close_box(); | |
252 | mcode print_string rp | |
253 | | Ast.TypeExp(ty) -> fullType ty | |
254 | ||
255 | | Ast.MetaErr(name,_,_,_) -> mcode print_meta name | |
256 | | Ast.MetaExpr(name,_,keep,ty,form,inherited) -> | |
257 | mcode print_meta name; print_type keep inherited ty | |
258 | | Ast.MetaExprList(name,_,_,_) -> mcode print_meta name | |
259 | | Ast.EComma(cm) -> mcode print_string cm; print_space() | |
260 | | Ast.DisjExpr(exp_list) -> print_disj_list expression exp_list | |
5636bb2c C |
261 | | Ast.NestExpr(starter,expr_dots,ender,Some whencode,multi) -> |
262 | nest_dots starter ender expression | |
34e49164 C |
263 | (function _ -> print_string " when != "; expression whencode) |
264 | expr_dots | |
5636bb2c C |
265 | | Ast.NestExpr(starter,expr_dots,ender,None,multi) -> |
266 | nest_dots starter ender expression (function _ -> ()) expr_dots | |
34e49164 C |
267 | | Ast.Edots(dots,Some whencode) |
268 | | Ast.Ecircles(dots,Some whencode) | |
269 | | Ast.Estars(dots,Some whencode) -> | |
270 | mcode print_string dots; print_string " when != "; expression whencode | |
271 | | Ast.Edots(dots,None) | |
272 | | Ast.Ecircles(dots,None) | |
273 | | Ast.Estars(dots,None) -> mcode print_string dots | |
274 | | Ast.OptExp(exp) -> print_string "?"; expression exp | |
275 | | Ast.UniqueExp(exp) -> print_string "!"; expression exp | |
276 | ||
277 | and unaryOp = function | |
278 | Ast.GetRef -> print_string "&" | |
279 | | Ast.DeRef -> print_string "*" | |
280 | | Ast.UnPlus -> print_string "+" | |
281 | | Ast.UnMinus -> print_string "-" | |
282 | | Ast.Tilde -> print_string "~" | |
283 | | Ast.Not -> print_string "!" | |
284 | ||
285 | and assignOp = function | |
286 | Ast.SimpleAssign -> print_string "=" | |
287 | | Ast.OpAssign(aop) -> arithOp aop; print_string "=" | |
288 | ||
289 | and fixOp = function | |
290 | Ast.Dec -> print_string "--" | |
291 | | Ast.Inc -> print_string "++" | |
292 | ||
293 | and binaryOp = function | |
294 | Ast.Arith(aop) -> arithOp aop | |
295 | | Ast.Logical(lop) -> logicalOp lop | |
296 | ||
297 | and arithOp = function | |
298 | Ast.Plus -> print_string "+" | |
299 | | Ast.Minus -> print_string "-" | |
300 | | Ast.Mul -> print_string "*" | |
301 | | Ast.Div -> print_string "/" | |
302 | | Ast.Mod -> print_string "%" | |
303 | | Ast.DecLeft -> print_string "<<" | |
304 | | Ast.DecRight -> print_string ">>" | |
305 | | Ast.And -> print_string "&" | |
306 | | Ast.Or -> print_string "|" | |
307 | | Ast.Xor -> print_string "^" | |
308 | ||
309 | and logicalOp = function | |
310 | Ast.Inf -> print_string "<" | |
311 | | Ast.Sup -> print_string ">" | |
312 | | Ast.InfEq -> print_string "<=" | |
313 | | Ast.SupEq -> print_string ">=" | |
314 | | Ast.Eq -> print_string "==" | |
315 | | Ast.NotEq -> print_string "!=" | |
316 | | Ast.AndLog -> print_string "&&" | |
317 | | Ast.OrLog -> print_string "||" | |
318 | ||
319 | and constant = function | |
320 | Ast.String(s) -> print_string "\""; print_string s; print_string "\"" | |
321 | | Ast.Char(s) -> print_string "'"; print_string s; print_string "'" | |
322 | | Ast.Int(s) -> print_string s | |
323 | | Ast.Float(s) -> print_string s | |
324 | ||
325 | (* --------------------------------------------------------------------- *) | |
326 | (* Declarations *) | |
327 | ||
328 | and storage = function | |
329 | Ast.Static -> print_string "static " | |
330 | | Ast.Auto -> print_string "auto " | |
331 | | Ast.Register -> print_string "register " | |
332 | | Ast.Extern -> print_string "extern " | |
333 | ||
334 | (* --------------------------------------------------------------------- *) | |
335 | (* Types *) | |
336 | ||
337 | and fullType ft = | |
338 | match Ast.unwrap ft with | |
339 | Ast.Type(cv,ty) -> | |
340 | print_option (function x -> mcode const_vol x; print_string " ") cv; | |
341 | typeC ty | |
342 | | Ast.DisjType(decls) -> print_disj_list fullType decls | |
343 | | Ast.OptType(ty) -> print_string "?"; fullType ty | |
344 | | Ast.UniqueType(ty) -> print_string "!"; fullType ty | |
345 | ||
346 | and print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2) fn = | |
347 | fullType ty; mcode print_string lp1; mcode print_string star; fn(); | |
348 | mcode print_string rp1; mcode print_string lp1; | |
349 | parameter_list params; mcode print_string rp2 | |
350 | ||
351 | and print_function_type (ty,lp1,params,rp1) fn = | |
352 | print_option fullType ty; fn(); mcode print_string lp1; | |
353 | parameter_list params; mcode print_string rp1 | |
354 | ||
355 | and print_fninfo = function | |
356 | Ast.FStorage(stg) -> mcode storage stg | |
357 | | Ast.FType(ty) -> fullType ty | |
358 | | Ast.FInline(inline) -> mcode print_string inline; print_string " " | |
359 | | Ast.FAttr(attr) -> mcode print_string attr; print_string " " | |
360 | ||
361 | and typeC ty = | |
362 | match Ast.unwrap ty with | |
faf9a90c C |
363 | Ast.BaseType(ty,strings) -> |
364 | List.iter (function s -> mcode print_string s; print_string " ") strings | |
365 | | Ast.SignedT(sgn,ty) -> mcode sign sgn; print_option typeC ty | |
34e49164 C |
366 | | Ast.Pointer(ty,star) -> fullType ty; mcode print_string star |
367 | | Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) -> | |
368 | print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2) | |
369 | (function _ -> ()) | |
370 | | Ast.FunctionType (_,ty,lp1,params,rp1) -> | |
371 | print_function_type (ty,lp1,params,rp1) (function _ -> ()) | |
372 | | Ast.Array(ty,lb,size,rb) -> | |
373 | fullType ty; mcode print_string lb; print_option expression size; | |
374 | mcode print_string rb | |
c491d8ee C |
375 | | Ast.EnumName(kind,name) -> |
376 | mcode print_string kind; | |
377 | print_option (function x -> ident x; print_string " ") name | |
378 | | Ast.EnumDef(ty,lb,ids,rb) -> | |
379 | fullType ty; mcode print_string lb; | |
380 | dots force_newline expression ids; | |
381 | mcode print_string rb | |
34e49164 C |
382 | | Ast.StructUnionName(kind,name) -> |
383 | mcode structUnion kind; | |
384 | print_option (function x -> ident x; print_string " ") name | |
385 | | Ast.StructUnionDef(ty,lb,decls,rb) -> | |
386 | fullType ty; mcode print_string lb; | |
387 | dots force_newline declaration decls; | |
388 | mcode print_string rb | |
389 | | Ast.TypeName(name) -> mcode print_string name; print_string " " | |
390 | | Ast.MetaType(name,_,_) -> | |
391 | mcode print_meta name; print_string " " | |
392 | ||
393 | and baseType = function | |
394 | Ast.VoidType -> print_string "void " | |
395 | | Ast.CharType -> print_string "char " | |
396 | | Ast.ShortType -> print_string "short " | |
397 | | Ast.IntType -> print_string "int " | |
398 | | Ast.DoubleType -> print_string "double " | |
399 | | Ast.FloatType -> print_string "float " | |
400 | | Ast.LongType -> print_string "long " | |
faf9a90c | 401 | | Ast.LongLongType -> print_string "long long " |
34e49164 C |
402 | |
403 | and structUnion = function | |
404 | Ast.Struct -> print_string "struct " | |
405 | | Ast.Union -> print_string "union " | |
406 | ||
407 | and sign = function | |
408 | Ast.Signed -> print_string "signed " | |
409 | | Ast.Unsigned -> print_string "unsigned " | |
410 | ||
411 | and const_vol = function | |
412 | Ast.Const -> print_string "const" | |
413 | | Ast.Volatile -> print_string "volatile" | |
414 | ||
415 | (* --------------------------------------------------------------------- *) | |
416 | (* Variable declaration *) | |
417 | (* Even if the Cocci program specifies a list of declarations, they are | |
418 | split out into multiple declarations of a single variable each. *) | |
419 | ||
420 | and print_named_type ty id = | |
421 | match Ast.unwrap ty with | |
422 | Ast.Type(None,ty1) -> | |
423 | (match Ast.unwrap ty1 with | |
424 | Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) -> | |
425 | print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2) | |
426 | (function _ -> print_string " "; ident id) | |
427 | | Ast.FunctionType(_,ty,lp1,params,rp1) -> | |
428 | print_function_type (ty,lp1,params,rp1) | |
429 | (function _ -> print_string " "; ident id) | |
430 | | Ast.Array(ty,lb,size,rb) -> | |
431 | let rec loop ty k = | |
432 | match Ast.unwrap ty with | |
433 | Ast.Array(ty,lb,size,rb) -> | |
434 | (match Ast.unwrap ty with | |
435 | Ast.Type(None,ty) -> | |
436 | loop ty | |
437 | (function _ -> | |
438 | k (); | |
439 | mcode print_string lb; | |
440 | print_option expression size; | |
441 | mcode print_string rb) | |
442 | | _ -> failwith "complex array types not supported") | |
443 | | _ -> typeC ty; ident id; k () in | |
444 | loop ty1 (function _ -> ()) | |
445 | | _ -> fullType ty; ident id) | |
446 | | _ -> fullType ty; ident id | |
447 | ||
448 | and declaration d = | |
449 | match Ast.unwrap d with | |
413ffc02 C |
450 | Ast.MetaDecl(name,_,_) | Ast.MetaField(name,_,_) -> mcode print_meta name |
451 | | Ast.Init(stg,ty,id,eq,ini,sem) -> | |
34e49164 C |
452 | print_option (mcode storage) stg; print_named_type ty id; |
453 | print_string " "; mcode print_string eq; | |
454 | print_string " "; initialiser ini; mcode print_string sem | |
455 | | Ast.UnInit(stg,ty,id,sem) -> | |
456 | print_option (mcode storage) stg; print_named_type ty id; | |
457 | mcode print_string sem | |
458 | | Ast.MacroDecl(name,lp,args,rp,sem) -> | |
459 | ident name; mcode print_string_box lp; | |
460 | dots (function _ -> ()) expression args; | |
461 | close_box(); mcode print_string rp; mcode print_string sem | |
462 | | Ast.TyDecl(ty,sem) -> fullType ty; mcode print_string sem | |
463 | | Ast.Typedef(stg,ty,id,sem) -> | |
464 | mcode print_string stg; print_string " "; fullType ty; typeC id; | |
465 | mcode print_string sem | |
466 | | Ast.DisjDecl(decls) -> print_disj_list declaration decls | |
faf9a90c | 467 | | Ast.Ddots(dots,Some whencode) -> |
34e49164 C |
468 | mcode print_string dots; print_string " when != "; declaration whencode |
469 | | Ast.Ddots(dots,None) -> mcode print_string dots | |
34e49164 C |
470 | | Ast.OptDecl(decl) -> print_string "?"; declaration decl |
471 | | Ast.UniqueDecl(decl) -> print_string "!"; declaration decl | |
472 | ||
473 | (* --------------------------------------------------------------------- *) | |
474 | (* Initialiser *) | |
475 | ||
476 | and initialiser i = | |
477 | match Ast.unwrap i with | |
113803cf C |
478 | Ast.MetaInit(name,_,_) -> |
479 | mcode print_meta name; print_string " " | |
480 | | Ast.InitExpr(exp) -> expression exp | |
c491d8ee C |
481 | | Ast.ArInitList(lb,initlist,rb) -> |
482 | mcode print_string lb; open_box 0; | |
483 | dots force_newline initialiser initlist; close_box(); | |
484 | mcode print_string rb | |
485 | | Ast.StrInitList(allminus,lb,initlist,rb,whencode) -> | |
34e49164 C |
486 | mcode print_string lb; open_box 0; |
487 | if not (whencode = []) | |
488 | then | |
489 | (print_string " WHEN != "; | |
490 | print_between (function _ -> print_string " v ") | |
491 | initialiser whencode; | |
492 | force_newline()); | |
493 | List.iter initialiser initlist; close_box(); | |
494 | mcode print_string rb | |
113803cf C |
495 | | Ast.InitGccExt(designators,eq,ini) -> |
496 | List.iter designator designators; print_string " "; | |
34e49164 C |
497 | mcode print_string eq; print_string " "; initialiser ini |
498 | | Ast.InitGccName(name,eq,ini) -> | |
499 | ident name; mcode print_string eq; initialiser ini | |
34e49164 | 500 | | Ast.IComma(comma) -> mcode print_string comma; force_newline() |
c491d8ee C |
501 | | Ast.Idots(dots,Some whencode) -> |
502 | mcode print_string dots; print_string " when != "; initialiser whencode | |
503 | | Ast.Idots(dots,None) -> mcode print_string dots | |
34e49164 C |
504 | | Ast.OptIni(ini) -> print_string "?"; initialiser ini |
505 | | Ast.UniqueIni(ini) -> print_string "!"; initialiser ini | |
506 | ||
113803cf C |
507 | and designator = function |
508 | Ast.DesignatorField(dot,id) -> mcode print_string dot; ident id | |
509 | | Ast.DesignatorIndex(lb,exp,rb) -> | |
510 | mcode print_string lb; expression exp; mcode print_string rb | |
511 | | Ast.DesignatorRange(lb,min,dots,max,rb) -> | |
512 | mcode print_string lb; expression min; mcode print_string dots; | |
513 | expression max; mcode print_string rb | |
514 | ||
34e49164 C |
515 | (* --------------------------------------------------------------------- *) |
516 | (* Parameter *) | |
517 | ||
518 | and parameterTypeDef p = | |
519 | match Ast.unwrap p with | |
520 | Ast.VoidParam(ty) -> fullType ty | |
521 | | Ast.Param(ty,Some id) -> print_named_type ty id | |
522 | | Ast.Param(ty,None) -> fullType ty | |
523 | | Ast.MetaParam(name,_,_) -> mcode print_meta name | |
524 | | Ast.MetaParamList(name,_,_,_) -> mcode print_meta name | |
525 | | Ast.PComma(cm) -> mcode print_string cm; print_space() | |
526 | | Ast.Pdots(dots) -> mcode print_string dots | |
527 | | Ast.Pcircles(dots) -> mcode print_string dots | |
528 | | Ast.OptParam(param) -> print_string "?"; parameterTypeDef param | |
529 | | Ast.UniqueParam(param) -> print_string "!"; parameterTypeDef param | |
530 | ||
531 | and parameter_list l = dots (function _ -> ()) parameterTypeDef l | |
532 | ||
533 | (* --------------------------------------------------------------------- *) | |
534 | (* Top-level code *) | |
535 | ||
536 | let rec rule_elem arity re = | |
537 | match Ast.unwrap re with | |
538 | Ast.FunHeader(bef,allminus,fninfo,name,lp,params,rp) -> | |
539 | mcode (function _ -> ()) ((),Ast.no_info,bef,Ast.NoMetaPos); | |
540 | print_string arity; List.iter print_fninfo fninfo; | |
541 | ident name; mcode print_string_box lp; | |
542 | parameter_list params; close_box(); mcode print_string rp; | |
543 | print_string " " | |
544 | | Ast.Decl(bef,allminus,decl) -> | |
545 | mcode (function _ -> ()) ((),Ast.no_info,bef,Ast.NoMetaPos); | |
546 | print_string arity; | |
547 | declaration decl | |
548 | | Ast.SeqStart(brace) -> | |
549 | print_string arity; mcode print_string brace; | |
550 | if !print_newlines_disj then start_block() | |
551 | | Ast.SeqEnd(brace) -> | |
552 | if !print_newlines_disj then end_block(); | |
553 | print_string arity; mcode print_string brace | |
554 | | Ast.ExprStatement(exp,sem) -> | |
555 | print_string arity; expression exp; mcode print_string sem | |
556 | | Ast.IfHeader(iff,lp,exp,rp) -> | |
557 | print_string arity; | |
558 | mcode print_string iff; print_string " "; mcode print_string_box lp; | |
559 | expression exp; close_box(); mcode print_string rp; print_string " " | |
560 | | Ast.Else(els) -> | |
561 | print_string arity; mcode print_string els; print_string " " | |
562 | | Ast.WhileHeader(whl,lp,exp,rp) -> | |
563 | print_string arity; | |
564 | mcode print_string whl; print_string " "; mcode print_string_box lp; | |
565 | expression exp; close_box(); mcode print_string rp; print_string " " | |
566 | | Ast.DoHeader(d) -> | |
567 | print_string arity; mcode print_string d; print_string " " | |
568 | | Ast.WhileTail(whl,lp,exp,rp,sem) -> | |
569 | print_string arity; | |
570 | mcode print_string whl; print_string " "; mcode print_string_box lp; | |
571 | expression exp; close_box(); mcode print_string rp; | |
572 | mcode print_string sem | |
573 | | Ast.ForHeader(fr,lp,e1,sem1,e2,sem2,e3,rp) -> | |
574 | print_string arity; | |
575 | mcode print_string fr; mcode print_string_box lp; | |
576 | print_option expression e1; mcode print_string sem1; | |
577 | print_option expression e2; mcode print_string sem2; | |
578 | print_option expression e3; close_box(); | |
579 | mcode print_string rp; print_string " " | |
580 | | Ast.IteratorHeader(nm,lp,args,rp) -> | |
581 | print_string arity; | |
582 | ident nm; print_string " "; mcode print_string_box lp; | |
583 | dots (function _ -> ()) expression args; close_box(); | |
584 | mcode print_string rp; print_string " " | |
585 | | Ast.SwitchHeader(switch,lp,exp,rp) -> | |
586 | print_string arity; | |
587 | mcode print_string switch; print_string " "; mcode print_string_box lp; | |
588 | expression exp; close_box(); mcode print_string rp; print_string " " | |
589 | | Ast.Break(br,sem) -> | |
590 | print_string arity; mcode print_string br; mcode print_string sem | |
591 | | Ast.Continue(cont,sem) -> | |
592 | print_string arity; mcode print_string cont; mcode print_string sem | |
593 | | Ast.Label(l,dd) -> ident l; mcode print_string dd | |
594 | | Ast.Goto(goto,l,sem) -> | |
595 | mcode print_string goto; ident l; mcode print_string sem | |
596 | | Ast.Return(ret,sem) -> | |
597 | print_string arity; mcode print_string ret; mcode print_string sem | |
598 | | Ast.ReturnExpr(ret,exp,sem) -> | |
599 | print_string arity; mcode print_string ret; print_string " "; | |
600 | expression exp; mcode print_string sem | |
601 | | Ast.MetaRuleElem(name,_,_) -> | |
602 | print_string arity; mcode print_meta name | |
603 | | Ast.MetaStmt(name,_,_,_) -> | |
604 | print_string arity; mcode print_meta name | |
605 | | Ast.MetaStmtList(name,_,_) -> | |
606 | print_string arity; mcode print_meta name | |
607 | | Ast.Exp(exp) -> print_string arity; expression exp | |
608 | | Ast.TopExp(exp) -> print_string arity; expression exp | |
609 | | Ast.Ty(ty) -> print_string arity; fullType ty | |
1be43e12 | 610 | | Ast.TopInit(init) -> initialiser init |
34e49164 C |
611 | | Ast.Include(inc,s) -> |
612 | mcode print_string inc; print_string " "; mcode inc_file s | |
613 | | Ast.DefineHeader(def,id,params) -> | |
614 | mcode print_string def; print_string " "; ident id; | |
615 | print_define_parameters params | |
616 | | Ast.Default(def,colon) -> | |
617 | mcode print_string def; mcode print_string colon; print_string " " | |
618 | | Ast.Case(case,exp,colon) -> | |
619 | mcode print_string case; print_string " "; expression exp; | |
620 | mcode print_string colon; print_string " " | |
621 | | Ast.DisjRuleElem(res) -> | |
622 | print_string arity; | |
623 | force_newline(); print_string "("; force_newline(); | |
624 | print_between | |
625 | (function _ -> force_newline();print_string "|"; force_newline()) | |
626 | (rule_elem arity) | |
627 | res; | |
628 | force_newline(); print_string ")" | |
629 | ||
630 | ||
631 | and print_define_parameters params = | |
632 | match Ast.unwrap params with | |
633 | Ast.NoParams -> () | |
634 | | Ast.DParams(lp,params,rp) -> | |
635 | mcode print_string lp; | |
636 | dots (function _ -> ()) print_define_param params; mcode print_string rp | |
637 | ||
638 | and print_define_param param = | |
639 | match Ast.unwrap param with | |
640 | Ast.DParam(id) -> ident id | |
641 | | Ast.DPComma(comma) -> mcode print_string comma | |
642 | | Ast.DPdots(dots) -> mcode print_string dots | |
643 | | Ast.DPcircles(circles) -> mcode print_string circles | |
644 | | Ast.OptDParam(dp) -> print_string "?"; print_define_param dp | |
645 | | Ast.UniqueDParam(dp) -> print_string "!"; print_define_param dp | |
646 | ||
647 | and statement arity s = | |
648 | match Ast.unwrap s with | |
708f4980 | 649 | Ast.Seq(lbrace,body,rbrace) -> |
34e49164 | 650 | rule_elem arity lbrace; |
34e49164 C |
651 | dots force_newline (statement arity) body; |
652 | rule_elem arity rbrace | |
653 | | Ast.IfThen(header,branch,(_,_,_,aft)) -> | |
654 | rule_elem arity header; statement arity branch; | |
655 | mcode (function _ -> ()) ((),Ast.no_info,aft,Ast.NoMetaPos) | |
656 | | Ast.IfThenElse(header,branch1,els,branch2,(_,_,_,aft)) -> | |
657 | rule_elem arity header; statement arity branch1; print_string " "; | |
658 | rule_elem arity els; statement arity branch2; | |
659 | mcode (function _ -> ()) ((),Ast.no_info,aft,Ast.NoMetaPos) | |
660 | | Ast.While(header,body,(_,_,_,aft)) -> | |
661 | rule_elem arity header; statement arity body; | |
662 | mcode (function _ -> ()) ((),Ast.no_info,aft,Ast.NoMetaPos) | |
663 | | Ast.Do(header,body,tail) -> | |
664 | rule_elem arity header; statement arity body; | |
665 | rule_elem arity tail | |
666 | | Ast.For(header,body,(_,_,_,aft)) -> | |
667 | rule_elem arity header; statement arity body; | |
668 | mcode (function _ -> ()) ((),Ast.no_info,aft,Ast.NoMetaPos) | |
669 | | Ast.Iterator(header,body,(_,_,_,aft)) -> | |
670 | rule_elem arity header; statement arity body; | |
671 | mcode (function _ -> ()) ((),Ast.no_info,aft,Ast.NoMetaPos) | |
fc1ad971 | 672 | | Ast.Switch(header,lb,decls,cases,rb) -> |
34e49164 | 673 | rule_elem arity header; rule_elem arity lb; |
fc1ad971 | 674 | dots force_newline (statement arity) decls; |
34e49164 C |
675 | List.iter (function x -> case_line arity x; force_newline()) cases; |
676 | rule_elem arity rb | |
677 | | Ast.Atomic(re) -> rule_elem arity re | |
708f4980 | 678 | | Ast.FunDecl(header,lbrace,body,rbrace) -> |
34e49164 | 679 | rule_elem arity header; rule_elem arity lbrace; |
34e49164 C |
680 | dots force_newline (statement arity) body; |
681 | rule_elem arity rbrace | |
682 | | Ast.Disj([stmt_dots]) -> | |
683 | print_string arity; | |
684 | dots (function _ -> if !print_newlines_disj then force_newline()) | |
685 | (statement arity) stmt_dots | |
686 | | Ast.Disj(stmt_dots_list) -> (* ignores newline directive for readability *) | |
687 | print_string arity; | |
688 | force_newline(); print_string "("; force_newline(); | |
689 | print_between | |
690 | (function _ -> force_newline();print_string "|"; force_newline()) | |
691 | (dots force_newline (statement arity)) | |
692 | stmt_dots_list; | |
693 | force_newline(); print_string ")" | |
694 | | Ast.Define(header,body) -> | |
695 | rule_elem arity header; print_string " "; | |
696 | dots force_newline (statement arity) body | |
5636bb2c | 697 | | Ast.Nest(starter,stmt_dots,ender,whn,multi,_,_) -> |
34e49164 | 698 | print_string arity; |
5636bb2c | 699 | nest_dots starter ender (statement arity) |
34e49164 C |
700 | (function _ -> |
701 | open_box 0; | |
702 | print_between force_newline | |
703 | (whencode (dots force_newline (statement "")) (statement "")) whn; | |
704 | close_box(); force_newline()) | |
705 | stmt_dots | |
706 | | Ast.Dots(d,whn,_,_) | Ast.Circles(d,whn,_,_) | Ast.Stars(d,whn,_,_) -> | |
707 | print_string arity; mcode print_string d; | |
708 | open_box 0; | |
709 | print_between force_newline | |
710 | (whencode (dots force_newline (statement "")) (statement "")) whn; | |
711 | close_box(); force_newline() | |
712 | | Ast.OptStm(s) -> statement "?" s | |
713 | | Ast.UniqueStm(s) -> statement "!" s | |
714 | ||
715 | and print_statement_when whencode = | |
716 | print_string " WHEN != "; | |
717 | open_box 0; | |
718 | print_between (function _ -> print_string " &"; force_newline()) | |
719 | (dots force_newline (statement "")) whencode; | |
720 | close_box() | |
721 | ||
722 | ||
723 | and whencode notfn alwaysfn = function | |
724 | Ast.WhenNot a -> | |
725 | print_string " WHEN != "; open_box 0; notfn a; close_box() | |
726 | | Ast.WhenAlways a -> | |
727 | print_string " WHEN = "; open_box 0; alwaysfn a; close_box() | |
728 | | Ast.WhenModifier x -> print_string " WHEN "; print_when_modif x | |
1be43e12 C |
729 | | Ast.WhenNotTrue a -> |
730 | print_string " WHEN != TRUE "; open_box 0; rule_elem "" a; close_box() | |
731 | | Ast.WhenNotFalse a -> | |
732 | print_string " WHEN != FALSE "; open_box 0; rule_elem "" a; close_box() | |
34e49164 C |
733 | |
734 | and print_when_modif = function | |
735 | | Ast.WhenAny -> print_string "ANY" | |
736 | | Ast.WhenStrict -> print_string "STRICT" | |
737 | | Ast.WhenForall -> print_string "FORALL" | |
738 | | Ast.WhenExists -> print_string "EXISTS" | |
739 | ||
740 | and case_line arity c = | |
741 | match Ast.unwrap c with | |
742 | Ast.CaseLine(header,code) -> | |
743 | rule_elem arity header; print_string " "; | |
744 | dots force_newline (statement arity) code | |
745 | | Ast.OptCase(case) -> case_line "?" case | |
746 | ||
747 | (* --------------------------------------------------------------------- *) | |
748 | (* CPP code *) | |
749 | ||
750 | and inc_file = function | |
751 | Ast.Local(elems) -> | |
752 | print_string "\""; | |
753 | print_between (function _ -> print_string "/") inc_elem elems; | |
754 | print_string "\"" | |
755 | | Ast.NonLocal(elems) -> | |
756 | print_string "<"; | |
757 | print_between (function _ -> print_string "/") inc_elem elems; | |
758 | print_string ">" | |
759 | ||
760 | and inc_elem = function | |
761 | Ast.IncPath s -> print_string s | |
762 | | Ast.IncDots -> print_string "..." | |
763 | ||
764 | (* for export only *) | |
765 | let statement_dots l = dots force_newline (statement "") l | |
766 | ||
767 | let top_level t = | |
768 | match Ast.unwrap t with | |
769 | Ast.FILEINFO(old_file,new_file) -> | |
770 | print_string "--- "; mcode print_string old_file; force_newline(); | |
771 | print_string "+++ "; mcode print_string new_file | |
772 | | Ast.DECL(stmt) -> statement "" stmt | |
773 | | Ast.CODE(stmt_dots) -> | |
774 | dots force_newline (statement "") stmt_dots | |
775 | | Ast.ERRORWORDS(exps) -> | |
776 | print_string "error words = ["; | |
777 | print_between (function _ -> print_string ", ") expression exps; | |
778 | print_string "]" | |
779 | ||
780 | let rule = | |
781 | print_between (function _ -> force_newline(); force_newline()) top_level | |
782 | ||
783 | let pp_print_anything x = !anything x | |
784 | ||
785 | let _ = | |
786 | anything := function | |
787 | Ast.FullTypeTag(x) -> fullType x | |
788 | | Ast.BaseTypeTag(x) -> baseType x | |
789 | | Ast.StructUnionTag(x) -> structUnion x | |
790 | | Ast.SignTag(x) -> sign x | |
791 | | Ast.IdentTag(x) -> ident x | |
792 | | Ast.ExpressionTag(x) -> expression x | |
793 | | Ast.ConstantTag(x) -> constant x | |
794 | | Ast.UnaryOpTag(x) -> unaryOp x | |
795 | | Ast.AssignOpTag(x) -> assignOp x | |
796 | | Ast.FixOpTag(x) -> fixOp x | |
797 | | Ast.BinaryOpTag(x) -> binaryOp x | |
798 | | Ast.ArithOpTag(x) -> arithOp x | |
799 | | Ast.LogicalOpTag(x) -> logicalOp x | |
800 | | Ast.InitTag(x) -> initialiser x | |
801 | | Ast.DeclarationTag(x) -> declaration x | |
802 | | Ast.StorageTag(x) -> storage x | |
803 | | Ast.IncFileTag(x) -> inc_file x | |
804 | | Ast.Rule_elemTag(x) -> rule_elem "" x | |
805 | | Ast.StatementTag(x) -> statement "" x | |
806 | | Ast.CaseLineTag(x) -> case_line "" x | |
807 | | Ast.ConstVolTag(x) -> const_vol x | |
808 | | Ast.Token(x,Some info) -> print_string_befaft print_string x info | |
809 | | Ast.Token(x,None) -> print_string x | |
c3e37e97 C |
810 | | Ast.Pragma(xs) -> |
811 | let print = function Ast.Noindent s | Ast.Indent s -> print_string s in | |
812 | print_between force_newline print xs | |
34e49164 C |
813 | | Ast.Code(x) -> let _ = top_level x in () |
814 | | Ast.ExprDotsTag(x) -> dots (function _ -> ()) expression x | |
815 | | Ast.ParamDotsTag(x) -> parameter_list x | |
816 | | Ast.StmtDotsTag(x) -> dots (function _ -> ()) (statement "") x | |
817 | | Ast.DeclDotsTag(x) -> dots (function _ -> ()) declaration x | |
818 | | Ast.TypeCTag(x) -> typeC x | |
819 | | Ast.ParamTag(x) -> parameterTypeDef x | |
820 | | Ast.SgrepStartTag(x) -> print_string x | |
821 | | Ast.SgrepEndTag(x) -> print_string x | |
822 | ||
823 | let rec dep in_and = function | |
824 | Ast.Dep(s) -> print_string s | |
825 | | Ast.AntiDep(s) -> print_string "!"; print_string s | |
826 | | Ast.EverDep(s) -> print_string "ever "; print_string s | |
827 | | Ast.NeverDep(s) -> print_string "never "; print_string s | |
828 | | Ast.AndDep(s1,s2) -> | |
829 | let print_and _ = dep true s1; print_string " && "; dep true s2 in | |
830 | if in_and | |
831 | then print_and () | |
832 | else (print_string "("; print_and(); print_string ")") | |
833 | | Ast.OrDep(s1,s2) -> | |
834 | let print_or _ = dep false s1; print_string " || "; dep false s2 in | |
835 | if not in_and | |
836 | then print_or () | |
837 | else (print_string "("; print_or(); print_string ")") | |
7f004419 C |
838 | | Ast.NoDep -> print_string "no_dep" |
839 | | Ast.FailDep -> print_string "fail_dep" | |
34e49164 | 840 | |
c3e37e97 C |
841 | let script_header str lang deps code = |
842 | print_string "@@"; | |
843 | force_newline(); | |
844 | print_string (str ^ ":" ^ lang); | |
845 | (match deps with | |
846 | Ast.NoDep -> () | |
847 | | _ -> print_string " depends on "; dep true deps); | |
848 | force_newline(); | |
849 | print_string "@@"; | |
850 | force_newline(); | |
851 | print_string code; | |
852 | force_newline() | |
853 | ||
34e49164 C |
854 | let unparse z = |
855 | match z with | |
174d1640 | 856 | Ast.InitialScriptRule (name,lang,deps,code) -> |
c3e37e97 | 857 | script_header "initialize" lang deps code |
174d1640 | 858 | | Ast.FinalScriptRule (name,lang,deps,code) -> |
c3e37e97 | 859 | script_header "finalize" lang deps code |
413ffc02 | 860 | | Ast.ScriptRule (name,lang,deps,bindings,script_vars,code) -> |
c3e37e97 | 861 | script_header "script" lang deps code |
faf9a90c | 862 | | Ast.CocciRule (nm, (deps, drops, exists), x, _, _) -> |
b1b2de81 C |
863 | print_string "@@"; |
864 | force_newline(); | |
865 | print_string nm; | |
866 | (match deps with | |
867 | Ast.NoDep -> () | |
868 | | _ -> print_string " depends on "; dep true deps); | |
34e49164 C |
869 | (* |
870 | print_string "line "; | |
871 | print_int (Ast.get_line (List.hd x)); | |
872 | *) | |
b1b2de81 C |
873 | force_newline(); |
874 | print_string "@@"; | |
875 | print_newlines_disj := true; | |
876 | force_newline(); | |
877 | force_newline(); | |
878 | rule x; | |
879 | force_newline() | |
34e49164 C |
880 | |
881 | let rule_elem_to_string x = | |
882 | print_newlines_disj := true; | |
883 | Common.format_to_string (function _ -> rule_elem "" x) | |
884 | ||
885 | let ident_to_string x = | |
886 | print_newlines_disj := true; | |
887 | Common.format_to_string (function _ -> ident x) | |
888 | ||
889 | let unparse_to_string x = | |
890 | print_newlines_disj := true; | |
891 | Common.format_to_string (function _ -> unparse x) | |
892 | ||
893 | let print_rule_elem re = | |
894 | let nl = !print_newlines_disj in | |
895 | print_newlines_disj := false; | |
896 | rule_elem "" re; | |
897 | print_newlines_disj := nl | |
898 |