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