Release of coccinelle 1.0.0-rc9
[bpt/coccinelle.git] / parsing_cocci / visitor_ast.ml
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
25 module Ast0 = Ast0_cocci
26 module Ast = Ast_cocci
27
28 (* --------------------------------------------------------------------- *)
29 (* Generic traversal: combiner *)
30 (* parameters:
31 combining function
32 treatment of: mcode, identifiers, expressions, fullTypes, types,
33 declarations, statements, toplevels
34 default value for options *)
35
36 type 'a combiner =
37 {combiner_ident : Ast.ident -> 'a;
38 combiner_expression : Ast.expression -> 'a;
39 combiner_fullType : Ast.fullType -> 'a;
40 combiner_typeC : Ast.typeC -> 'a;
41 combiner_declaration : Ast.declaration -> 'a;
42 combiner_initialiser : Ast.initialiser -> 'a;
43 combiner_parameter : Ast.parameterTypeDef -> 'a;
44 combiner_parameter_list : Ast.parameter_list -> 'a;
45 combiner_rule_elem : Ast.rule_elem -> 'a;
46 combiner_statement : Ast.statement -> 'a;
47 combiner_case_line : Ast.case_line -> 'a;
48 combiner_top_level : Ast.top_level -> 'a;
49 combiner_anything : Ast.anything -> 'a;
50 combiner_expression_dots : Ast.expression Ast.dots -> 'a;
51 combiner_statement_dots : Ast.statement Ast.dots -> 'a;
52 combiner_declaration_dots : Ast.declaration Ast.dots -> 'a;
53 combiner_initialiser_dots : Ast.initialiser Ast.dots -> 'a}
54
55 type ('mc,'a) cmcode = 'a combiner -> 'mc Ast_cocci.mcode -> 'a
56 type ('cd,'a) ccode = 'a combiner -> ('cd -> 'a) -> 'cd -> 'a
57
58
59 let combiner bind option_default
60 meta_mcodefn string_mcodefn const_mcodefn assign_mcodefn fix_mcodefn
61 unary_mcodefn binary_mcodefn
62 cv_mcodefn sign_mcodefn struct_mcodefn storage_mcodefn
63 inc_file_mcodefn
64 expdotsfn paramdotsfn stmtdotsfn decldotsfn initdotsfn
65 identfn exprfn ftfn tyfn initfn paramfn declfn rulefn stmtfn casefn
66 topfn anyfn =
67 let multibind l =
68 let rec loop = function
69 [] -> option_default
70 | [x] -> x
71 | x::xs -> bind x (loop xs) in
72 loop l in
73 let get_option f = function
74 Some x -> f x
75 | None -> option_default in
76
77 let dotsfn param default all_functions arg =
78 let k d =
79 match Ast.unwrap d with
80 Ast.DOTS(l) | Ast.CIRCLES(l) | Ast.STARS(l) ->
81 multibind (List.map default l) in
82 param all_functions k arg in
83
84 let rec meta_mcode x = meta_mcodefn all_functions x
85 and string_mcode x = string_mcodefn all_functions x
86 and const_mcode x = const_mcodefn all_functions x
87 and assign_mcode x = assign_mcodefn all_functions x
88 and fix_mcode x = fix_mcodefn all_functions x
89 and unary_mcode x = unary_mcodefn all_functions x
90 and binary_mcode x = binary_mcodefn all_functions x
91 and cv_mcode x = cv_mcodefn all_functions x
92 and sign_mcode x = sign_mcodefn all_functions x
93 and struct_mcode x = struct_mcodefn all_functions x
94 and storage_mcode x = storage_mcodefn all_functions x
95 and inc_file_mcode x = inc_file_mcodefn all_functions x
96
97 and expression_dots d = dotsfn expdotsfn expression all_functions d
98 and parameter_dots d = dotsfn paramdotsfn parameterTypeDef all_functions d
99 and statement_dots d = dotsfn stmtdotsfn statement all_functions d
100 and declaration_dots d = dotsfn decldotsfn declaration all_functions d
101 and initialiser_dots d = dotsfn initdotsfn initialiser all_functions d
102
103 and ident i =
104 let k i =
105 match Ast.unwrap i with
106 Ast.Id(name) -> string_mcode name
107 | Ast.MetaId(name,_,_,_) -> meta_mcode name
108 | Ast.MetaFunc(name,_,_,_) -> meta_mcode name
109 | Ast.MetaLocalFunc(name,_,_,_) -> meta_mcode name
110 | Ast.DisjId(id_list) -> multibind (List.map ident id_list)
111 | Ast.OptIdent(id) -> ident id
112 | Ast.UniqueIdent(id) -> ident id in
113 identfn all_functions k i
114
115 and expression e =
116 let k e =
117 match Ast.unwrap e with
118 Ast.Ident(id) -> ident id
119 | Ast.Constant(const) -> const_mcode const
120 | Ast.FunCall(fn,lp,args,rp) ->
121 multibind [expression fn; string_mcode lp; expression_dots args;
122 string_mcode rp]
123 | Ast.Assignment(left,op,right,simple) ->
124 multibind [expression left; assign_mcode op; expression right]
125 | Ast.CondExpr(exp1,why,exp2,colon,exp3) ->
126 multibind [expression exp1; string_mcode why;
127 get_option expression exp2; string_mcode colon;
128 expression exp3]
129 | Ast.Postfix(exp,op) -> bind (expression exp) (fix_mcode op)
130 | Ast.Infix(exp,op) -> bind (fix_mcode op) (expression exp)
131 | Ast.Unary(exp,op) -> bind (unary_mcode op) (expression exp)
132 | Ast.Binary(left,op,right) ->
133 multibind [expression left; binary_mcode op; expression right]
134 | Ast.Nested(left,op,right) ->
135 multibind [expression left; binary_mcode op; expression right]
136 | Ast.Paren(lp,exp,rp) ->
137 multibind [string_mcode lp; expression exp; string_mcode rp]
138 | Ast.ArrayAccess(exp1,lb,exp2,rb) ->
139 multibind
140 [expression exp1; string_mcode lb; expression exp2;
141 string_mcode rb]
142 | Ast.RecordAccess(exp,pt,field) ->
143 multibind [expression exp; string_mcode pt; ident field]
144 | Ast.RecordPtAccess(exp,ar,field) ->
145 multibind [expression exp; string_mcode ar; ident field]
146 | Ast.Cast(lp,ty,rp,exp) ->
147 multibind
148 [string_mcode lp; fullType ty; string_mcode rp; expression exp]
149 | Ast.SizeOfExpr(szf,exp) ->
150 multibind [string_mcode szf; expression exp]
151 | Ast.SizeOfType(szf,lp,ty,rp) ->
152 multibind
153 [string_mcode szf; string_mcode lp; fullType ty; string_mcode rp]
154 | Ast.TypeExp(ty) -> fullType ty
155 | Ast.Constructor(lp,ty,rp,init) ->
156 multibind
157 [string_mcode lp; fullType ty; string_mcode rp; initialiser init]
158 | Ast.MetaErr(name,_,_,_)
159 | Ast.MetaExpr(name,_,_,_,_,_)
160 | Ast.MetaExprList(name,_,_,_) -> meta_mcode name
161 | Ast.EComma(cm) -> string_mcode cm
162 | Ast.DisjExpr(exp_list) -> multibind (List.map expression exp_list)
163 | Ast.NestExpr(starter,expr_dots,ender,whencode,multi) ->
164 bind (string_mcode starter)
165 (bind (expression_dots expr_dots)
166 (bind (string_mcode ender)
167 (get_option expression whencode)))
168 | Ast.Edots(dots,whencode) | Ast.Ecircles(dots,whencode)
169 | Ast.Estars(dots,whencode) ->
170 bind (string_mcode dots) (get_option expression whencode)
171 | Ast.OptExp(exp) | Ast.UniqueExp(exp) ->
172 expression exp in
173 exprfn all_functions k e
174
175 and fullType ft =
176 let k ft =
177 match Ast.unwrap ft with
178 Ast.Type(cv,ty) -> bind (get_option cv_mcode cv) (typeC ty)
179 | Ast.DisjType(types) -> multibind (List.map fullType types)
180 | Ast.OptType(ty) -> fullType ty
181 | Ast.UniqueType(ty) -> fullType ty in
182 ftfn all_functions k ft
183
184 and function_pointer (ty,lp1,star,rp1,lp2,params,rp2) extra =
185 (* have to put the treatment of the identifier into the right position *)
186 multibind
187 ([fullType ty; string_mcode lp1; string_mcode star] @ extra @
188 [string_mcode rp1;
189 string_mcode lp2; parameter_dots params; string_mcode rp2])
190
191 and function_type (ty,lp1,params,rp1) extra =
192 (* have to put the treatment of the identifier into the right position *)
193 multibind
194 ([get_option fullType ty] @ extra @
195 [string_mcode lp1; parameter_dots params; string_mcode rp1])
196
197 and array_type (ty,lb,size,rb) extra =
198 multibind
199 ([fullType ty] @ extra @
200 [string_mcode lb; get_option expression size; string_mcode rb])
201
202 and typeC ty =
203 let k ty =
204 match Ast.unwrap ty with
205 Ast.BaseType(ty,strings) -> multibind (List.map string_mcode strings)
206 | Ast.SignedT(sgn,ty) -> bind (sign_mcode sgn) (get_option typeC ty)
207 | Ast.Pointer(ty,star) ->
208 bind (fullType ty) (string_mcode star)
209 | Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
210 function_pointer (ty,lp1,star,rp1,lp2,params,rp2) []
211 | Ast.FunctionType (_,ty,lp1,params,rp1) ->
212 function_type (ty,lp1,params,rp1) []
213 | Ast.Array(ty,lb,size,rb) -> array_type (ty,lb,size,rb) []
214 | Ast.EnumName(kind,name) ->
215 bind (string_mcode kind) (get_option ident name)
216 | Ast.EnumDef(ty,lb,ids,rb) ->
217 multibind
218 [fullType ty; string_mcode lb; expression_dots ids;
219 string_mcode rb]
220 | Ast.StructUnionName(kind,name) ->
221 bind (struct_mcode kind) (get_option ident name)
222 | Ast.StructUnionDef(ty,lb,decls,rb) ->
223 multibind
224 [fullType ty; string_mcode lb; declaration_dots decls;
225 string_mcode rb]
226 | Ast.TypeName(name) -> string_mcode name
227 | Ast.MetaType(name,_,_) -> meta_mcode name in
228 tyfn all_functions k ty
229
230 and named_type ty id =
231 match Ast.unwrap ty with
232 Ast.Type(None,ty1) ->
233 (match Ast.unwrap ty1 with
234 Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
235 function_pointer (ty,lp1,star,rp1,lp2,params,rp2) [ident id]
236 | Ast.FunctionType(_,ty,lp1,params,rp1) ->
237 function_type (ty,lp1,params,rp1) [ident id]
238 | Ast.Array(ty,lb,size,rb) -> array_type (ty,lb,size,rb) [ident id]
239 | _ -> bind (fullType ty) (ident id))
240 | _ -> bind (fullType ty) (ident id)
241
242 and declaration d =
243 let k d =
244 match Ast.unwrap d with
245 Ast.MetaDecl(name,_,_) | Ast.MetaField(name,_,_)
246 | Ast.MetaFieldList(name,_,_,_) ->
247 meta_mcode name
248 | Ast.Init(stg,ty,id,eq,ini,sem) ->
249 bind (get_option storage_mcode stg)
250 (bind (named_type ty id)
251 (multibind
252 [string_mcode eq; initialiser ini; string_mcode sem]))
253 | Ast.UnInit(stg,ty,id,sem) ->
254 bind (get_option storage_mcode stg)
255 (bind (named_type ty id) (string_mcode sem))
256 | Ast.MacroDecl(name,lp,args,rp,sem) ->
257 multibind
258 [ident name; string_mcode lp; expression_dots args;
259 string_mcode rp; string_mcode sem]
260 | Ast.TyDecl(ty,sem) -> bind (fullType ty) (string_mcode sem)
261 | Ast.Typedef(stg,ty,id,sem) ->
262 bind (string_mcode stg)
263 (bind (fullType ty) (bind (typeC id) (string_mcode sem)))
264 | Ast.DisjDecl(decls) -> multibind (List.map declaration decls)
265 | Ast.Ddots(dots,whencode) ->
266 bind (string_mcode dots) (get_option declaration whencode)
267 | Ast.OptDecl(decl) -> declaration decl
268 | Ast.UniqueDecl(decl) -> declaration decl in
269 declfn all_functions k d
270
271 and initialiser i =
272 let k i =
273 match Ast.unwrap i with
274 Ast.MetaInit(name,_,_) -> meta_mcode name
275 | Ast.MetaInitList(name,_,_,_) -> meta_mcode name
276 | Ast.InitExpr(exp) -> expression exp
277 | Ast.ArInitList(lb,initlist,rb) ->
278 multibind
279 [string_mcode lb; initialiser_dots initlist; string_mcode rb]
280 | Ast.StrInitList(allminus,lb,initlist,rb,whencode) ->
281 multibind
282 [string_mcode lb;
283 multibind (List.map initialiser initlist);
284 string_mcode rb;
285 multibind (List.map initialiser whencode)]
286 | Ast.InitGccName(name,eq,ini) ->
287 multibind [ident name; string_mcode eq; initialiser ini]
288 | Ast.InitGccExt(designators,eq,ini) ->
289 multibind
290 ((List.map designator designators) @
291 [string_mcode eq; initialiser ini])
292 | Ast.IComma(cm) -> string_mcode cm
293 | Ast.Idots(dots,whencode) ->
294 bind (string_mcode dots) (get_option initialiser whencode)
295 | Ast.OptIni(i) -> initialiser i
296 | Ast.UniqueIni(i) -> initialiser i in
297 initfn all_functions k i
298
299 and designator = function
300 Ast.DesignatorField(dot,id) -> bind (string_mcode dot) (ident id)
301 | Ast.DesignatorIndex(lb,exp,rb) ->
302 bind (string_mcode lb) (bind (expression exp) (string_mcode rb))
303 | Ast.DesignatorRange(lb,min,dots,max,rb) ->
304 multibind
305 [string_mcode lb; expression min; string_mcode dots;
306 expression max; string_mcode rb]
307
308 and parameterTypeDef p =
309 let k p =
310 match Ast.unwrap p with
311 Ast.VoidParam(ty) -> fullType ty
312 | Ast.Param(ty,Some id) -> named_type ty id
313 | Ast.Param(ty,None) -> fullType ty
314 | Ast.MetaParam(name,_,_) -> meta_mcode name
315 | Ast.MetaParamList(name,_,_,_) -> meta_mcode name
316 | Ast.PComma(cm) -> string_mcode cm
317 | Ast.Pdots(dots) -> string_mcode dots
318 | Ast.Pcircles(dots) -> string_mcode dots
319 | Ast.OptParam(param) -> parameterTypeDef param
320 | Ast.UniqueParam(param) -> parameterTypeDef param in
321 paramfn all_functions k p
322
323 and rule_elem re =
324 let k re =
325 match Ast.unwrap re with
326 Ast.FunHeader(_,_,fi,name,lp,params,rp) ->
327 multibind
328 ((List.map fninfo fi) @
329 [ident name;string_mcode lp;parameter_dots params;
330 string_mcode rp])
331 | Ast.Decl(_,_,decl) -> declaration decl
332 | Ast.SeqStart(brace) -> string_mcode brace
333 | Ast.SeqEnd(brace) -> string_mcode brace
334 | Ast.ExprStatement(exp,sem) ->
335 bind (get_option expression exp) (string_mcode sem)
336 | Ast.IfHeader(iff,lp,exp,rp) ->
337 multibind [string_mcode iff; string_mcode lp; expression exp;
338 string_mcode rp]
339 | Ast.Else(els) -> string_mcode els
340 | Ast.WhileHeader(whl,lp,exp,rp) ->
341 multibind [string_mcode whl; string_mcode lp; expression exp;
342 string_mcode rp]
343 | Ast.DoHeader(d) -> string_mcode d
344 | Ast.WhileTail(whl,lp,exp,rp,sem) ->
345 multibind [string_mcode whl; string_mcode lp; expression exp;
346 string_mcode rp; string_mcode sem]
347 | Ast.ForHeader(fr,lp,e1,sem1,e2,sem2,e3,rp) ->
348 multibind [string_mcode fr; string_mcode lp;
349 get_option expression e1; string_mcode sem1;
350 get_option expression e2; string_mcode sem2;
351 get_option expression e3; string_mcode rp]
352 | Ast.IteratorHeader(nm,lp,args,rp) ->
353 multibind [ident nm; string_mcode lp;
354 expression_dots args; string_mcode rp]
355 | Ast.SwitchHeader(switch,lp,exp,rp) ->
356 multibind [string_mcode switch; string_mcode lp; expression exp;
357 string_mcode rp]
358 | Ast.Break(br,sem) -> bind (string_mcode br) (string_mcode sem)
359 | Ast.Continue(cont,sem) -> bind (string_mcode cont) (string_mcode sem)
360 | Ast.Label(l,dd) -> bind (ident l) (string_mcode dd)
361 | Ast.Goto(goto,l,sem) ->
362 bind (string_mcode goto) (bind (ident l) (string_mcode sem))
363 | Ast.Return(ret,sem) -> bind (string_mcode ret) (string_mcode sem)
364 | Ast.ReturnExpr(ret,exp,sem) ->
365 multibind [string_mcode ret; expression exp; string_mcode sem]
366 | Ast.MetaStmt(name,_,_,_) -> meta_mcode name
367 | Ast.MetaStmtList(name,_,_) -> meta_mcode name
368 | Ast.MetaRuleElem(name,_,_) -> meta_mcode name
369 | Ast.Exp(exp) -> expression exp
370 | Ast.TopExp(exp) -> expression exp
371 | Ast.Ty(ty) -> fullType ty
372 | Ast.TopInit(init) -> initialiser init
373 | Ast.Include(inc,name) -> bind (string_mcode inc) (inc_file_mcode name)
374 | Ast.Undef(def,id) ->
375 multibind [string_mcode def; ident id]
376 | Ast.DefineHeader(def,id,params) ->
377 multibind [string_mcode def; ident id; define_parameters params]
378 | Ast.Default(def,colon) -> bind (string_mcode def) (string_mcode colon)
379 | Ast.Case(case,exp,colon) ->
380 multibind [string_mcode case; expression exp; string_mcode colon]
381 | Ast.DisjRuleElem(res) -> multibind (List.map rule_elem res) in
382 rulefn all_functions k re
383
384 (* not parameterizable for now... *)
385 and define_parameters p =
386 let k p =
387 match Ast.unwrap p with
388 Ast.NoParams -> option_default
389 | Ast.DParams(lp,params,rp) ->
390 multibind
391 [string_mcode lp; define_param_dots params; string_mcode rp] in
392 k p
393
394 and define_param_dots d =
395 let k d =
396 match Ast.unwrap d with
397 Ast.DOTS(l) | Ast.CIRCLES(l) | Ast.STARS(l) ->
398 multibind (List.map define_param l) in
399 k d
400
401 and define_param p =
402 let k p =
403 match Ast.unwrap p with
404 Ast.DParam(id) -> ident id
405 | Ast.DPComma(comma) -> string_mcode comma
406 | Ast.DPdots(d) -> string_mcode d
407 | Ast.DPcircles(c) -> string_mcode c
408 | Ast.OptDParam(dp) -> define_param dp
409 | Ast.UniqueDParam(dp) -> define_param dp in
410 k p
411
412 (* discard the result, because the statement is assumed to be already
413 represented elsewhere in the code *)
414 and process_bef_aft s =
415 match Ast.get_dots_bef_aft s with
416 Ast.NoDots -> ()
417 | Ast.DroppingBetweenDots(stm,ind) -> let _ = statement stm in ()
418 | Ast.AddingBetweenDots(stm,ind) -> let _ = statement stm in ()
419
420 and statement s =
421 process_bef_aft s;
422 let k s =
423 match Ast.unwrap s with
424 Ast.Seq(lbrace,body,rbrace) ->
425 multibind [rule_elem lbrace;
426 statement_dots body; rule_elem rbrace]
427 | Ast.IfThen(header,branch,_) ->
428 multibind [rule_elem header; statement branch]
429 | Ast.IfThenElse(header,branch1,els,branch2,_) ->
430 multibind [rule_elem header; statement branch1; rule_elem els;
431 statement branch2]
432 | Ast.While(header,body,_) ->
433 multibind [rule_elem header; statement body]
434 | Ast.Do(header,body,tail) ->
435 multibind [rule_elem header; statement body; rule_elem tail]
436 | Ast.For(header,body,_) -> multibind [rule_elem header; statement body]
437 | Ast.Iterator(header,body,_) ->
438 multibind [rule_elem header; statement body]
439 | Ast.Switch(header,lb,decls,cases,rb) ->
440 multibind [rule_elem header;rule_elem lb;
441 statement_dots decls;
442 multibind (List.map case_line cases);
443 rule_elem rb]
444 | Ast.Atomic(re) -> rule_elem re
445 | Ast.Disj(stmt_dots_list) ->
446 multibind (List.map statement_dots stmt_dots_list)
447 | Ast.Nest(starter,stmt_dots,ender,whn,_,_,_) ->
448 bind (string_mcode starter)
449 (bind (statement_dots stmt_dots)
450 (bind (string_mcode ender)
451 (multibind
452 (List.map (whencode statement_dots statement) whn))))
453 | Ast.FunDecl(header,lbrace,body,rbrace) ->
454 multibind [rule_elem header; rule_elem lbrace;
455 statement_dots body; rule_elem rbrace]
456 | Ast.Define(header,body) ->
457 bind (rule_elem header) (statement_dots body)
458 | Ast.Dots(d,whn,_,_) | Ast.Circles(d,whn,_,_) | Ast.Stars(d,whn,_,_) ->
459 bind (string_mcode d)
460 (multibind (List.map (whencode statement_dots statement) whn))
461 | Ast.OptStm(stmt) | Ast.UniqueStm(stmt) ->
462 statement stmt in
463 stmtfn all_functions k s
464
465 and fninfo = function
466 Ast.FStorage(stg) -> storage_mcode stg
467 | Ast.FType(ty) -> fullType ty
468 | Ast.FInline(inline) -> string_mcode inline
469 | Ast.FAttr(attr) -> string_mcode attr
470
471 and whencode notfn alwaysfn = function
472 Ast.WhenNot a -> notfn a
473 | Ast.WhenAlways a -> alwaysfn a
474 | Ast.WhenModifier(_) -> option_default
475 | Ast.WhenNotTrue(e) -> rule_elem e
476 | Ast.WhenNotFalse(e) -> rule_elem e
477
478 and case_line c =
479 let k c =
480 match Ast.unwrap c with
481 Ast.CaseLine(header,code) ->
482 bind (rule_elem header) (statement_dots code)
483 | Ast.OptCase(case) -> case_line case in
484 casefn all_functions k c
485
486 and top_level t =
487 let k t =
488 match Ast.unwrap t with
489 Ast.FILEINFO(old_file,new_file) ->
490 bind (string_mcode old_file) (string_mcode new_file)
491 | Ast.NONDECL(stmt) -> statement stmt
492 | Ast.CODE(stmt_dots) -> statement_dots stmt_dots
493 | Ast.ERRORWORDS(exps) -> multibind (List.map expression exps) in
494 topfn all_functions k t
495
496 and anything a =
497 let k = function
498 (*in many cases below, the thing is not even mcode, so we do nothing*)
499 Ast.FullTypeTag(ft) -> fullType ft
500 | Ast.BaseTypeTag(bt) -> option_default
501 | Ast.StructUnionTag(su) -> option_default
502 | Ast.SignTag(sgn) -> option_default
503 | Ast.IdentTag(id) -> ident id
504 | Ast.ExpressionTag(exp) -> expression exp
505 | Ast.ConstantTag(cst) -> option_default
506 | Ast.UnaryOpTag(unop) -> option_default
507 | Ast.AssignOpTag(asgnop) -> option_default
508 | Ast.FixOpTag(fixop) -> option_default
509 | Ast.BinaryOpTag(binop) -> option_default
510 | Ast.ArithOpTag(arithop) -> option_default
511 | Ast.LogicalOpTag(logop) -> option_default
512 | Ast.DeclarationTag(decl) -> declaration decl
513 | Ast.InitTag(ini) -> initialiser ini
514 | Ast.StorageTag(stg) -> option_default
515 | Ast.IncFileTag(stg) -> option_default
516 | Ast.Rule_elemTag(rule) -> rule_elem rule
517 | Ast.StatementTag(rule) -> statement rule
518 | Ast.CaseLineTag(case) -> case_line case
519 | Ast.ConstVolTag(cv) -> option_default
520 | Ast.Token(tok,info) -> option_default
521 | Ast.Pragma(str) -> option_default
522 | Ast.Code(cd) -> top_level cd
523 | Ast.ExprDotsTag(ed) -> expression_dots ed
524 | Ast.ParamDotsTag(pd) -> parameter_dots pd
525 | Ast.StmtDotsTag(sd) -> statement_dots sd
526 | Ast.DeclDotsTag(sd) -> declaration_dots sd
527 | Ast.TypeCTag(ty) -> typeC ty
528 | Ast.ParamTag(param) -> parameterTypeDef param
529 | Ast.SgrepStartTag(tok) -> option_default
530 | Ast.SgrepEndTag(tok) -> option_default in
531 anyfn all_functions k a
532
533 and all_functions =
534 {combiner_ident = ident;
535 combiner_expression = expression;
536 combiner_fullType = fullType;
537 combiner_typeC = typeC;
538 combiner_declaration = declaration;
539 combiner_initialiser = initialiser;
540 combiner_parameter = parameterTypeDef;
541 combiner_parameter_list = parameter_dots;
542 combiner_rule_elem = rule_elem;
543 combiner_statement = statement;
544 combiner_case_line = case_line;
545 combiner_top_level = top_level;
546 combiner_anything = anything;
547 combiner_expression_dots = expression_dots;
548 combiner_statement_dots = statement_dots;
549 combiner_declaration_dots = declaration_dots;
550 combiner_initialiser_dots = initialiser_dots} in
551 all_functions
552
553 (* ---------------------------------------------------------------------- *)
554
555 type 'a inout = 'a -> 'a (* for specifying the type of rebuilder *)
556
557 type rebuilder =
558 {rebuilder_ident : Ast.ident inout;
559 rebuilder_expression : Ast.expression inout;
560 rebuilder_fullType : Ast.fullType inout;
561 rebuilder_typeC : Ast.typeC inout;
562 rebuilder_declaration : Ast.declaration inout;
563 rebuilder_initialiser : Ast.initialiser inout;
564 rebuilder_parameter : Ast.parameterTypeDef inout;
565 rebuilder_parameter_list : Ast.parameter_list inout;
566 rebuilder_statement : Ast.statement inout;
567 rebuilder_case_line : Ast.case_line inout;
568 rebuilder_rule_elem : Ast.rule_elem inout;
569 rebuilder_top_level : Ast.top_level inout;
570 rebuilder_expression_dots : Ast.expression Ast.dots inout;
571 rebuilder_statement_dots : Ast.statement Ast.dots inout;
572 rebuilder_declaration_dots : Ast.declaration Ast.dots inout;
573 rebuilder_initialiser_dots : Ast.initialiser Ast.dots inout;
574 rebuilder_define_param_dots : Ast.define_param Ast.dots inout;
575 rebuilder_define_param : Ast.define_param inout;
576 rebuilder_define_parameters : Ast.define_parameters inout;
577 rebuilder_anything : Ast.anything inout}
578
579 type 'mc rmcode = 'mc Ast.mcode inout
580 type 'cd rcode = rebuilder -> ('cd inout) -> 'cd inout
581
582
583 let rebuilder
584 meta_mcode string_mcode const_mcode assign_mcode fix_mcode unary_mcode
585 binary_mcode cv_mcode sign_mcode struct_mcode storage_mcode
586 inc_file_mcode
587 expdotsfn paramdotsfn stmtdotsfn decldotsfn initdotsfn
588 identfn exprfn ftfn tyfn initfn paramfn declfn rulefn stmtfn casefn
589 topfn anyfn =
590 let get_option f = function
591 Some x -> Some (f x)
592 | None -> None in
593
594 let dotsfn param default all_functions arg =
595 let k d =
596 Ast.rewrap d
597 (match Ast.unwrap d with
598 Ast.DOTS(l) -> Ast.DOTS(List.map default l)
599 | Ast.CIRCLES(l) -> Ast.CIRCLES(List.map default l)
600 | Ast.STARS(l) -> Ast.STARS(List.map default l)) in
601 param all_functions k arg in
602
603 let rec expression_dots d = dotsfn expdotsfn expression all_functions d
604 and parameter_dots d = dotsfn paramdotsfn parameterTypeDef all_functions d
605 and statement_dots d = dotsfn stmtdotsfn statement all_functions d
606 and declaration_dots d = dotsfn decldotsfn declaration all_functions d
607 and initialiser_dots d = dotsfn initdotsfn initialiser all_functions d
608
609 and ident i =
610 let k i =
611 Ast.rewrap i
612 (match Ast.unwrap i with
613 Ast.Id(name) -> Ast.Id(string_mcode name)
614 | Ast.MetaId(name,constraints,keep,inherited) ->
615 Ast.MetaId(meta_mcode name,constraints,keep,inherited)
616 | Ast.MetaFunc(name,constraints,keep,inherited) ->
617 Ast.MetaFunc(meta_mcode name,constraints,keep,inherited)
618 | Ast.MetaLocalFunc(name,constraints,keep,inherited) ->
619 Ast.MetaLocalFunc(meta_mcode name,constraints,keep,inherited)
620 | Ast.DisjId(id_list) -> Ast.DisjId(List.map ident id_list)
621 | Ast.OptIdent(id) -> Ast.OptIdent(ident id)
622 | Ast.UniqueIdent(id) -> Ast.UniqueIdent(ident id)) in
623 identfn all_functions k i
624
625 and expression e =
626 let k e =
627 Ast.rewrap e
628 (match Ast.unwrap e with
629 Ast.Ident(id) -> Ast.Ident(ident id)
630 | Ast.Constant(const) -> Ast.Constant(const_mcode const)
631 | Ast.FunCall(fn,lp,args,rp) ->
632 Ast.FunCall(expression fn, string_mcode lp, expression_dots args,
633 string_mcode rp)
634 | Ast.Assignment(left,op,right,simple) ->
635 Ast.Assignment(expression left, assign_mcode op, expression right,
636 simple)
637 | Ast.CondExpr(exp1,why,exp2,colon,exp3) ->
638 Ast.CondExpr(expression exp1, string_mcode why,
639 get_option expression exp2, string_mcode colon,
640 expression exp3)
641 | Ast.Postfix(exp,op) -> Ast.Postfix(expression exp,fix_mcode op)
642 | Ast.Infix(exp,op) -> Ast.Infix(expression exp,fix_mcode op)
643 | Ast.Unary(exp,op) -> Ast.Unary(expression exp,unary_mcode op)
644 | Ast.Binary(left,op,right) ->
645 Ast.Binary(expression left, binary_mcode op, expression right)
646 | Ast.Nested(left,op,right) ->
647 Ast.Nested(expression left, binary_mcode op, expression right)
648 | Ast.Paren(lp,exp,rp) ->
649 Ast.Paren(string_mcode lp, expression exp, string_mcode rp)
650 | Ast.ArrayAccess(exp1,lb,exp2,rb) ->
651 Ast.ArrayAccess(expression exp1, string_mcode lb, expression exp2,
652 string_mcode rb)
653 | Ast.RecordAccess(exp,pt,field) ->
654 Ast.RecordAccess(expression exp, string_mcode pt, ident field)
655 | Ast.RecordPtAccess(exp,ar,field) ->
656 Ast.RecordPtAccess(expression exp, string_mcode ar, ident field)
657 | Ast.Cast(lp,ty,rp,exp) ->
658 Ast.Cast(string_mcode lp, fullType ty, string_mcode rp,
659 expression exp)
660 | Ast.SizeOfExpr(szf,exp) ->
661 Ast.SizeOfExpr(string_mcode szf, expression exp)
662 | Ast.SizeOfType(szf,lp,ty,rp) ->
663 Ast.SizeOfType(string_mcode szf,string_mcode lp, fullType ty,
664 string_mcode rp)
665 | Ast.TypeExp(ty) -> Ast.TypeExp(fullType ty)
666 | Ast.Constructor(lp,ty,rp,init) ->
667 Ast.Constructor(string_mcode lp, fullType ty, string_mcode rp,
668 initialiser init)
669 | Ast.MetaErr(name,constraints,keep,inherited) ->
670 Ast.MetaErr(meta_mcode name,constraints,keep,inherited)
671 | Ast.MetaExpr(name,constraints,keep,ty,form,inherited) ->
672 Ast.MetaExpr(meta_mcode name,constraints,keep,ty,form,inherited)
673 | Ast.MetaExprList(name,lenname_inh,keep,inherited) ->
674 Ast.MetaExprList(meta_mcode name,lenname_inh,keep,inherited)
675 | Ast.EComma(cm) -> Ast.EComma(string_mcode cm)
676 | Ast.DisjExpr(exp_list) -> Ast.DisjExpr(List.map expression exp_list)
677 | Ast.NestExpr(starter,expr_dots,ender,whencode,multi) ->
678 Ast.NestExpr(string_mcode starter,expression_dots expr_dots,
679 string_mcode ender,
680 get_option expression whencode,multi)
681 | Ast.Edots(dots,whencode) ->
682 Ast.Edots(string_mcode dots,get_option expression whencode)
683 | Ast.Ecircles(dots,whencode) ->
684 Ast.Ecircles(string_mcode dots,get_option expression whencode)
685 | Ast.Estars(dots,whencode) ->
686 Ast.Estars(string_mcode dots,get_option expression whencode)
687 | Ast.OptExp(exp) -> Ast.OptExp(expression exp)
688 | Ast.UniqueExp(exp) -> Ast.UniqueExp(expression exp)) in
689 exprfn all_functions k e
690
691 and fullType ft =
692 let k ft =
693 Ast.rewrap ft
694 (match Ast.unwrap ft with
695 Ast.Type(cv,ty) -> Ast.Type (get_option cv_mcode cv, typeC ty)
696 | Ast.DisjType(types) -> Ast.DisjType(List.map fullType types)
697 | Ast.OptType(ty) -> Ast.OptType(fullType ty)
698 | Ast.UniqueType(ty) -> Ast.UniqueType(fullType ty)) in
699 ftfn all_functions k ft
700
701 and typeC ty =
702 let k ty =
703 Ast.rewrap ty
704 (match Ast.unwrap ty with
705 Ast.BaseType(ty,strings) ->
706 Ast.BaseType (ty, List.map string_mcode strings)
707 | Ast.SignedT(sgn,ty) ->
708 Ast.SignedT(sign_mcode sgn,get_option typeC ty)
709 | Ast.Pointer(ty,star) ->
710 Ast.Pointer (fullType ty, string_mcode star)
711 | Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
712 Ast.FunctionPointer(fullType ty,string_mcode lp1,string_mcode star,
713 string_mcode rp1,string_mcode lp2,
714 parameter_dots params,
715 string_mcode rp2)
716 | Ast.FunctionType(allminus,ty,lp,params,rp) ->
717 Ast.FunctionType(allminus,get_option fullType ty,string_mcode lp,
718 parameter_dots params,string_mcode rp)
719 | Ast.Array(ty,lb,size,rb) ->
720 Ast.Array(fullType ty, string_mcode lb,
721 get_option expression size, string_mcode rb)
722 | Ast.EnumName(kind,name) ->
723 Ast.EnumName(string_mcode kind, get_option ident name)
724 | Ast.EnumDef(ty,lb,ids,rb) ->
725 Ast.EnumDef (fullType ty, string_mcode lb, expression_dots ids,
726 string_mcode rb)
727 | Ast.StructUnionName(kind,name) ->
728 Ast.StructUnionName (struct_mcode kind, get_option ident name)
729 | Ast.StructUnionDef(ty,lb,decls,rb) ->
730 Ast.StructUnionDef (fullType ty,
731 string_mcode lb, declaration_dots decls,
732 string_mcode rb)
733 | Ast.TypeName(name) -> Ast.TypeName(string_mcode name)
734 | Ast.MetaType(name,keep,inherited) ->
735 Ast.MetaType(meta_mcode name,keep,inherited)) in
736 tyfn all_functions k ty
737
738 and declaration d =
739 let k d =
740 Ast.rewrap d
741 (match Ast.unwrap d with
742 Ast.MetaDecl(name,keep,inherited) ->
743 Ast.MetaDecl(meta_mcode name,keep,inherited)
744 | Ast.MetaField(name,keep,inherited) ->
745 Ast.MetaField(meta_mcode name,keep,inherited)
746 | Ast.MetaFieldList(name,lenname_inh,keep,inherited) ->
747 Ast.MetaFieldList(meta_mcode name,lenname_inh,keep,inherited)
748 | Ast.Init(stg,ty,id,eq,ini,sem) ->
749 Ast.Init(get_option storage_mcode stg, fullType ty, ident id,
750 string_mcode eq, initialiser ini, string_mcode sem)
751 | Ast.UnInit(stg,ty,id,sem) ->
752 Ast.UnInit(get_option storage_mcode stg, fullType ty, ident id,
753 string_mcode sem)
754 | Ast.MacroDecl(name,lp,args,rp,sem) ->
755 Ast.MacroDecl(ident name, string_mcode lp, expression_dots args,
756 string_mcode rp,string_mcode sem)
757 | Ast.TyDecl(ty,sem) -> Ast.TyDecl(fullType ty, string_mcode sem)
758 | Ast.Typedef(stg,ty,id,sem) ->
759 Ast.Typedef(string_mcode stg, fullType ty, typeC id,
760 string_mcode sem)
761 | Ast.DisjDecl(decls) -> Ast.DisjDecl(List.map declaration decls)
762 | Ast.Ddots(dots,whencode) ->
763 Ast.Ddots(string_mcode dots, get_option declaration whencode)
764 | Ast.OptDecl(decl) -> Ast.OptDecl(declaration decl)
765 | Ast.UniqueDecl(decl) -> Ast.UniqueDecl(declaration decl)) in
766 declfn all_functions k d
767
768 and initialiser i =
769 let k i =
770 Ast.rewrap i
771 (match Ast.unwrap i with
772 Ast.MetaInit(name,keep,inherited) ->
773 Ast.MetaInit(meta_mcode name,keep,inherited)
774 | Ast.MetaInitList(name,lenname_inh,keep,inherited) ->
775 Ast.MetaInitList(meta_mcode name,lenname_inh,keep,inherited)
776 | Ast.InitExpr(exp) -> Ast.InitExpr(expression exp)
777 | Ast.ArInitList(lb,initlist,rb) ->
778 Ast.ArInitList(string_mcode lb, initialiser_dots initlist,
779 string_mcode rb)
780 | Ast.StrInitList(allminus,lb,initlist,rb,whencode) ->
781 Ast.StrInitList(allminus,
782 string_mcode lb, List.map initialiser initlist,
783 string_mcode rb, List.map initialiser whencode)
784 | Ast.InitGccName(name,eq,ini) ->
785 Ast.InitGccName(ident name, string_mcode eq, initialiser ini)
786 | Ast.InitGccExt(designators,eq,ini) ->
787 Ast.InitGccExt
788 (List.map designator designators, string_mcode eq,
789 initialiser ini)
790 | Ast.IComma(cm) -> Ast.IComma(string_mcode cm)
791 | Ast.Idots(dots,whencode) ->
792 Ast.Idots(string_mcode dots,get_option initialiser whencode)
793 | Ast.OptIni(i) -> Ast.OptIni(initialiser i)
794 | Ast.UniqueIni(i) -> Ast.UniqueIni(initialiser i)) in
795 initfn all_functions k i
796
797 and designator = function
798 Ast.DesignatorField(dot,id) ->
799 Ast.DesignatorField(string_mcode dot,ident id)
800 | Ast.DesignatorIndex(lb,exp,rb) ->
801 Ast.DesignatorIndex(string_mcode lb,expression exp,string_mcode rb)
802 | Ast.DesignatorRange(lb,min,dots,max,rb) ->
803 Ast.DesignatorRange(string_mcode lb,expression min,string_mcode dots,
804 expression max,string_mcode rb)
805
806 and parameterTypeDef p =
807 let k p =
808 Ast.rewrap p
809 (match Ast.unwrap p with
810 Ast.VoidParam(ty) -> Ast.VoidParam(fullType ty)
811 | Ast.Param(ty,id) -> Ast.Param(fullType ty, get_option ident id)
812 | Ast.MetaParam(name,keep,inherited) ->
813 Ast.MetaParam(meta_mcode name,keep,inherited)
814 | Ast.MetaParamList(name,lenname_inh,keep,inherited) ->
815 Ast.MetaParamList(meta_mcode name,lenname_inh,keep,inherited)
816 | Ast.PComma(cm) -> Ast.PComma(string_mcode cm)
817 | Ast.Pdots(dots) -> Ast.Pdots(string_mcode dots)
818 | Ast.Pcircles(dots) -> Ast.Pcircles(string_mcode dots)
819 | Ast.OptParam(param) -> Ast.OptParam(parameterTypeDef param)
820 | Ast.UniqueParam(param) -> Ast.UniqueParam(parameterTypeDef param)) in
821 paramfn all_functions k p
822
823 and rule_elem re =
824 let k re =
825 Ast.rewrap re
826 (match Ast.unwrap re with
827 Ast.FunHeader(bef,allminus,fi,name,lp,params,rp) ->
828 Ast.FunHeader(bef,allminus,List.map fninfo fi,ident name,
829 string_mcode lp, parameter_dots params,
830 string_mcode rp)
831 | Ast.Decl(bef,allminus,decl) ->
832 Ast.Decl(bef,allminus,declaration decl)
833 | Ast.SeqStart(brace) -> Ast.SeqStart(string_mcode brace)
834 | Ast.SeqEnd(brace) -> Ast.SeqEnd(string_mcode brace)
835 | Ast.ExprStatement(exp,sem) ->
836 Ast.ExprStatement (get_option expression exp, string_mcode sem)
837 | Ast.IfHeader(iff,lp,exp,rp) ->
838 Ast.IfHeader(string_mcode iff, string_mcode lp, expression exp,
839 string_mcode rp)
840 | Ast.Else(els) -> Ast.Else(string_mcode els)
841 | Ast.WhileHeader(whl,lp,exp,rp) ->
842 Ast.WhileHeader(string_mcode whl, string_mcode lp, expression exp,
843 string_mcode rp)
844 | Ast.DoHeader(d) -> Ast.DoHeader(string_mcode d)
845 | Ast.WhileTail(whl,lp,exp,rp,sem) ->
846 Ast.WhileTail(string_mcode whl, string_mcode lp, expression exp,
847 string_mcode rp, string_mcode sem)
848 | Ast.ForHeader(fr,lp,e1,sem1,e2,sem2,e3,rp) ->
849 Ast.ForHeader(string_mcode fr, string_mcode lp,
850 get_option expression e1, string_mcode sem1,
851 get_option expression e2, string_mcode sem2,
852 get_option expression e3, string_mcode rp)
853 | Ast.IteratorHeader(whl,lp,args,rp) ->
854 Ast.IteratorHeader(ident whl, string_mcode lp,
855 expression_dots args, string_mcode rp)
856 | Ast.SwitchHeader(switch,lp,exp,rp) ->
857 Ast.SwitchHeader(string_mcode switch, string_mcode lp,
858 expression exp, string_mcode rp)
859 | Ast.Break(br,sem) ->
860 Ast.Break(string_mcode br, string_mcode sem)
861 | Ast.Continue(cont,sem) ->
862 Ast.Continue(string_mcode cont, string_mcode sem)
863 | Ast.Label(l,dd) -> Ast.Label(ident l, string_mcode dd)
864 | Ast.Goto(goto,l,sem) ->
865 Ast.Goto(string_mcode goto,ident l,string_mcode sem)
866 | Ast.Return(ret,sem) ->
867 Ast.Return(string_mcode ret, string_mcode sem)
868 | Ast.ReturnExpr(ret,exp,sem) ->
869 Ast.ReturnExpr(string_mcode ret, expression exp, string_mcode sem)
870 | Ast.MetaStmt(name,keep,seqible,inherited) ->
871 Ast.MetaStmt(meta_mcode name,keep,seqible,inherited)
872 | Ast.MetaStmtList(name,keep,inherited) ->
873 Ast.MetaStmtList(meta_mcode name,keep,inherited)
874 | Ast.MetaRuleElem(name,keep,inherited) ->
875 Ast.MetaRuleElem(meta_mcode name,keep,inherited)
876 | Ast.Exp(exp) -> Ast.Exp(expression exp)
877 | Ast.TopExp(exp) -> Ast.TopExp(expression exp)
878 | Ast.Ty(ty) -> Ast.Ty(fullType ty)
879 | Ast.TopInit(init) -> Ast.TopInit(initialiser init)
880 | Ast.Include(inc,name) ->
881 Ast.Include(string_mcode inc,inc_file_mcode name)
882 | Ast.Undef(def,id) ->
883 Ast.Undef(string_mcode def,ident id)
884 | Ast.DefineHeader(def,id,params) ->
885 Ast.DefineHeader(string_mcode def,ident id,
886 define_parameters params)
887 | Ast.Default(def,colon) ->
888 Ast.Default(string_mcode def,string_mcode colon)
889 | Ast.Case(case,exp,colon) ->
890 Ast.Case(string_mcode case,expression exp,string_mcode colon)
891 | Ast.DisjRuleElem(res) -> Ast.DisjRuleElem(List.map rule_elem res)) in
892 rulefn all_functions k re
893
894 (* not parameterizable for now... *)
895 and define_parameters p =
896 let k p =
897 Ast.rewrap p
898 (match Ast.unwrap p with
899 Ast.NoParams -> Ast.NoParams
900 | Ast.DParams(lp,params,rp) ->
901 Ast.DParams(string_mcode lp,define_param_dots params,
902 string_mcode rp)) in
903 k p
904
905 and define_param_dots d =
906 let k d =
907 Ast.rewrap d
908 (match Ast.unwrap d with
909 Ast.DOTS(l) -> Ast.DOTS(List.map define_param l)
910 | Ast.CIRCLES(l) -> Ast.CIRCLES(List.map define_param l)
911 | Ast.STARS(l) -> Ast.STARS(List.map define_param l)) in
912 k d
913
914 and define_param p =
915 let k p =
916 Ast.rewrap p
917 (match Ast.unwrap p with
918 Ast.DParam(id) -> Ast.DParam(ident id)
919 | Ast.DPComma(comma) -> Ast.DPComma(string_mcode comma)
920 | Ast.DPdots(d) -> Ast.DPdots(string_mcode d)
921 | Ast.DPcircles(c) -> Ast.DPcircles(string_mcode c)
922 | Ast.OptDParam(dp) -> Ast.OptDParam(define_param dp)
923 | Ast.UniqueDParam(dp) -> Ast.UniqueDParam(define_param dp)) in
924 k p
925
926 and process_bef_aft s =
927 Ast.set_dots_bef_aft
928 (match Ast.get_dots_bef_aft s with
929 Ast.NoDots -> Ast.NoDots
930 | Ast.DroppingBetweenDots(stm,ind) ->
931 Ast.DroppingBetweenDots(statement stm,ind)
932 | Ast.AddingBetweenDots(stm,ind) ->
933 Ast.AddingBetweenDots(statement stm,ind))
934 s
935
936 and statement s =
937 let k s =
938 Ast.rewrap s
939 (match Ast.unwrap s with
940 Ast.Seq(lbrace,body,rbrace) ->
941 Ast.Seq(rule_elem lbrace,
942 statement_dots body, rule_elem rbrace)
943 | Ast.IfThen(header,branch,aft) ->
944 Ast.IfThen(rule_elem header, statement branch,aft)
945 | Ast.IfThenElse(header,branch1,els,branch2,aft) ->
946 Ast.IfThenElse(rule_elem header, statement branch1, rule_elem els,
947 statement branch2, aft)
948 | Ast.While(header,body,aft) ->
949 Ast.While(rule_elem header, statement body, aft)
950 | Ast.Do(header,body,tail) ->
951 Ast.Do(rule_elem header, statement body, rule_elem tail)
952 | Ast.For(header,body,aft) ->
953 Ast.For(rule_elem header, statement body, aft)
954 | Ast.Iterator(header,body,aft) ->
955 Ast.Iterator(rule_elem header, statement body, aft)
956 | Ast.Switch(header,lb,decls,cases,rb) ->
957 Ast.Switch(rule_elem header,rule_elem lb,
958 statement_dots decls,
959 List.map case_line cases,rule_elem rb)
960 | Ast.Atomic(re) -> Ast.Atomic(rule_elem re)
961 | Ast.Disj(stmt_dots_list) ->
962 Ast.Disj (List.map statement_dots stmt_dots_list)
963 | Ast.Nest(starter,stmt_dots,ender,whn,multi,bef,aft) ->
964 Ast.Nest(string_mcode starter,statement_dots stmt_dots,
965 string_mcode ender,
966 List.map (whencode statement_dots statement) whn,
967 multi,bef,aft)
968 | Ast.FunDecl(header,lbrace,body,rbrace) ->
969 Ast.FunDecl(rule_elem header,rule_elem lbrace,
970 statement_dots body, rule_elem rbrace)
971 | Ast.Define(header,body) ->
972 Ast.Define(rule_elem header,statement_dots body)
973 | Ast.Dots(d,whn,bef,aft) ->
974 Ast.Dots(string_mcode d,
975 List.map (whencode statement_dots statement) whn,bef,aft)
976 | Ast.Circles(d,whn,bef,aft) ->
977 Ast.Circles(string_mcode d,
978 List.map (whencode statement_dots statement) whn,
979 bef,aft)
980 | Ast.Stars(d,whn,bef,aft) ->
981 Ast.Stars(string_mcode d,
982 List.map (whencode statement_dots statement) whn,bef,aft)
983 | Ast.OptStm(stmt) -> Ast.OptStm(statement stmt)
984 | Ast.UniqueStm(stmt) -> Ast.UniqueStm(statement stmt)) in
985 let s = stmtfn all_functions k s in
986 (* better to do this after, in case there is an equality test on the whole
987 statement, eg in free_vars. equality test would require that this
988 subterm not already be changed *)
989 process_bef_aft s
990
991 and fninfo = function
992 Ast.FStorage(stg) -> Ast.FStorage(storage_mcode stg)
993 | Ast.FType(ty) -> Ast.FType(fullType ty)
994 | Ast.FInline(inline) -> Ast.FInline(string_mcode inline)
995 | Ast.FAttr(attr) -> Ast.FAttr(string_mcode attr)
996
997 and whencode notfn alwaysfn = function
998 Ast.WhenNot a -> Ast.WhenNot (notfn a)
999 | Ast.WhenAlways a -> Ast.WhenAlways (alwaysfn a)
1000 | Ast.WhenModifier(x) -> Ast.WhenModifier(x)
1001 | Ast.WhenNotTrue(e) -> Ast.WhenNotTrue(rule_elem e)
1002 | Ast.WhenNotFalse(e) -> Ast.WhenNotFalse(rule_elem e)
1003
1004 and case_line c =
1005 let k c =
1006 Ast.rewrap c
1007 (match Ast.unwrap c with
1008 Ast.CaseLine(header,code) ->
1009 Ast.CaseLine(rule_elem header,statement_dots code)
1010 | Ast.OptCase(case) -> Ast.OptCase(case_line case)) in
1011 casefn all_functions k c
1012
1013 and top_level t =
1014 let k t =
1015 Ast.rewrap t
1016 (match Ast.unwrap t with
1017 Ast.FILEINFO(old_file,new_file) ->
1018 Ast.FILEINFO (string_mcode old_file, string_mcode new_file)
1019 | Ast.NONDECL(stmt) -> Ast.NONDECL(statement stmt)
1020 | Ast.CODE(stmt_dots) -> Ast.CODE(statement_dots stmt_dots)
1021 | Ast.ERRORWORDS(exps) -> Ast.ERRORWORDS (List.map expression exps)) in
1022 topfn all_functions k t
1023
1024 and anything a =
1025 let k = function
1026 (*in many cases below, the thing is not even mcode, so we do nothing*)
1027 Ast.FullTypeTag(ft) -> Ast.FullTypeTag(fullType ft)
1028 | Ast.BaseTypeTag(bt) as x -> x
1029 | Ast.StructUnionTag(su) as x -> x
1030 | Ast.SignTag(sgn) as x -> x
1031 | Ast.IdentTag(id) -> Ast.IdentTag(ident id)
1032 | Ast.ExpressionTag(exp) -> Ast.ExpressionTag(expression exp)
1033 | Ast.ConstantTag(cst) as x -> x
1034 | Ast.UnaryOpTag(unop) as x -> x
1035 | Ast.AssignOpTag(asgnop) as x -> x
1036 | Ast.FixOpTag(fixop) as x -> x
1037 | Ast.BinaryOpTag(binop) as x -> x
1038 | Ast.ArithOpTag(arithop) as x -> x
1039 | Ast.LogicalOpTag(logop) as x -> x
1040 | Ast.InitTag(decl) -> Ast.InitTag(initialiser decl)
1041 | Ast.DeclarationTag(decl) -> Ast.DeclarationTag(declaration decl)
1042 | Ast.StorageTag(stg) as x -> x
1043 | Ast.IncFileTag(stg) as x -> x
1044 | Ast.Rule_elemTag(rule) -> Ast.Rule_elemTag(rule_elem rule)
1045 | Ast.StatementTag(rule) -> Ast.StatementTag(statement rule)
1046 | Ast.CaseLineTag(case) -> Ast.CaseLineTag(case_line case)
1047 | Ast.ConstVolTag(cv) as x -> x
1048 | Ast.Token(tok,info) as x -> x
1049 | Ast.Pragma(str) as x -> x
1050 | Ast.Code(cd) -> Ast.Code(top_level cd)
1051 | Ast.ExprDotsTag(ed) -> Ast.ExprDotsTag(expression_dots ed)
1052 | Ast.ParamDotsTag(pd) -> Ast.ParamDotsTag(parameter_dots pd)
1053 | Ast.StmtDotsTag(sd) -> Ast.StmtDotsTag(statement_dots sd)
1054 | Ast.DeclDotsTag(sd) -> Ast.DeclDotsTag(declaration_dots sd)
1055 | Ast.TypeCTag(ty) -> Ast.TypeCTag(typeC ty)
1056 | Ast.ParamTag(param) -> Ast.ParamTag(parameterTypeDef param)
1057 | Ast.SgrepStartTag(tok) as x -> x
1058 | Ast.SgrepEndTag(tok) as x -> x in
1059 anyfn all_functions k a
1060
1061 and all_functions =
1062 {rebuilder_ident = ident;
1063 rebuilder_expression = expression;
1064 rebuilder_fullType = fullType;
1065 rebuilder_typeC = typeC;
1066 rebuilder_declaration = declaration;
1067 rebuilder_initialiser = initialiser;
1068 rebuilder_parameter = parameterTypeDef;
1069 rebuilder_parameter_list = parameter_dots;
1070 rebuilder_rule_elem = rule_elem;
1071 rebuilder_statement = statement;
1072 rebuilder_case_line = case_line;
1073 rebuilder_top_level = top_level;
1074 rebuilder_expression_dots = expression_dots;
1075 rebuilder_statement_dots = statement_dots;
1076 rebuilder_declaration_dots = declaration_dots;
1077 rebuilder_initialiser_dots = initialiser_dots;
1078 rebuilder_define_param_dots = define_param_dots;
1079 rebuilder_define_param = define_param;
1080 rebuilder_define_parameters = define_parameters;
1081 rebuilder_anything = anything} in
1082 all_functions
1083