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