Release coccinelle-0.1.1
[bpt/coccinelle.git] / parsing_cocci / visitor_ast.ml
CommitLineData
34e49164
C
1(*
2* Copyright 2005-2008, 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
23module 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
33type '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
51type ('mc,'a) cmcode = 'a combiner -> 'mc Ast_cocci.mcode -> 'a
52type ('cd,'a) ccode = 'a combiner -> ('cd -> 'a) -> 'cd -> 'a
53
54
55let combiner bind option_default
56 meta_mcodefn string_mcodefn const_mcodefn assign_mcodefn fix_mcodefn
57 unary_mcodefn binary_mcodefn
58 cv_mcodefn base_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 base_mcode x = base_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,sgn) ->
211 bind (get_option sign_mcode sgn) (base_mcode ty)
212 | Ast.ImplicitInt(sgn) -> sign_mcode sgn
213 | Ast.Pointer(ty,star) ->
214 bind (fullType ty) (string_mcode star)
215 | Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
216 function_pointer (ty,lp1,star,rp1,lp2,params,rp2) []
217 | Ast.FunctionType (_,ty,lp1,params,rp1) ->
218 function_type (ty,lp1,params,rp1) []
219 | Ast.Array(ty,lb,size,rb) -> array_type (ty,lb,size,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.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.InitExpr(exp) -> expression exp
273 | Ast.InitList(lb,initlist,rb,whencode) ->
274 multibind
275 [string_mcode lb;
276 multibind (List.map initialiser initlist);
277 string_mcode rb;
278 multibind (List.map initialiser whencode)]
279 | Ast.InitGccDotName(dot,name,eq,ini) ->
280 multibind
281 [string_mcode dot; ident name; string_mcode eq; initialiser ini]
282 | Ast.InitGccName(name,eq,ini) ->
283 multibind [ident name; string_mcode eq; initialiser ini]
284 | Ast.InitGccIndex(lb,exp,rb,eq,ini) ->
285 multibind
286 [string_mcode lb; expression exp; string_mcode rb;
287 string_mcode eq; initialiser ini]
288 | Ast.InitGccRange(lb,exp1,dots,exp2,rb,eq,ini) ->
289 multibind
290 [string_mcode lb; expression exp1; string_mcode dots;
291 expression exp2; string_mcode rb; string_mcode eq;
292 initialiser ini]
293 | Ast.IComma(cm) -> string_mcode cm
294 | Ast.OptIni(i) -> initialiser i
295 | Ast.UniqueIni(i) -> initialiser i in
296 initfn all_functions k i
297
298 and parameterTypeDef p =
299 let k p =
300 match Ast.unwrap p with
301 Ast.VoidParam(ty) -> fullType ty
302 | Ast.Param(ty,Some id) -> named_type ty id
303 | Ast.Param(ty,None) -> fullType ty
304 | Ast.MetaParam(name,_,_) -> meta_mcode name
305 | Ast.MetaParamList(name,_,_,_) -> meta_mcode name
306 | Ast.PComma(cm) -> string_mcode cm
307 | Ast.Pdots(dots) -> string_mcode dots
308 | Ast.Pcircles(dots) -> string_mcode dots
309 | Ast.OptParam(param) -> parameterTypeDef param
310 | Ast.UniqueParam(param) -> parameterTypeDef param in
311 paramfn all_functions k p
312
313 and rule_elem re =
314 let k re =
315 match Ast.unwrap re with
316 Ast.FunHeader(_,_,fi,name,lp,params,rp) ->
317 multibind
318 ((List.map fninfo fi) @
319 [ident name;string_mcode lp;parameter_dots params;
320 string_mcode rp])
321 | Ast.Decl(_,_,decl) -> declaration decl
322 | Ast.SeqStart(brace) -> string_mcode brace
323 | Ast.SeqEnd(brace) -> string_mcode brace
324 | Ast.ExprStatement(exp,sem) ->
325 bind (expression exp) (string_mcode sem)
326 | Ast.IfHeader(iff,lp,exp,rp) ->
327 multibind [string_mcode iff; string_mcode lp; expression exp;
328 string_mcode rp]
329 | Ast.Else(els) -> string_mcode els
330 | Ast.WhileHeader(whl,lp,exp,rp) ->
331 multibind [string_mcode whl; string_mcode lp; expression exp;
332 string_mcode rp]
333 | Ast.DoHeader(d) -> string_mcode d
334 | Ast.WhileTail(whl,lp,exp,rp,sem) ->
335 multibind [string_mcode whl; string_mcode lp; expression exp;
336 string_mcode rp; string_mcode sem]
337 | Ast.ForHeader(fr,lp,e1,sem1,e2,sem2,e3,rp) ->
338 multibind [string_mcode fr; string_mcode lp;
339 get_option expression e1; string_mcode sem1;
340 get_option expression e2; string_mcode sem2;
341 get_option expression e3; string_mcode rp]
342 | Ast.IteratorHeader(nm,lp,args,rp) ->
343 multibind [ident nm; string_mcode lp;
344 expression_dots args; string_mcode rp]
345 | Ast.SwitchHeader(switch,lp,exp,rp) ->
346 multibind [string_mcode switch; string_mcode lp; expression exp;
347 string_mcode rp]
348 | Ast.Break(br,sem) -> bind (string_mcode br) (string_mcode sem)
349 | Ast.Continue(cont,sem) -> bind (string_mcode cont) (string_mcode sem)
350 | Ast.Label(l,dd) -> bind (ident l) (string_mcode dd)
351 | Ast.Goto(goto,l,sem) ->
352 bind (string_mcode goto) (bind (ident l) (string_mcode sem))
353 | Ast.Return(ret,sem) -> bind (string_mcode ret) (string_mcode sem)
354 | Ast.ReturnExpr(ret,exp,sem) ->
355 multibind [string_mcode ret; expression exp; string_mcode sem]
356 | Ast.MetaStmt(name,_,_,_) -> meta_mcode name
357 | Ast.MetaStmtList(name,_,_) -> meta_mcode name
358 | Ast.MetaRuleElem(name,_,_) -> meta_mcode name
359 | Ast.Exp(exp) -> expression exp
360 | Ast.TopExp(exp) -> expression exp
361 | Ast.Ty(ty) -> fullType ty
1be43e12 362 | Ast.TopInit(init) -> initialiser init
34e49164
C
363 | Ast.Include(inc,name) -> bind (string_mcode inc) (inc_file_mcode name)
364 | Ast.DefineHeader(def,id,params) ->
365 multibind [string_mcode def; ident id; define_parameters params]
366 | Ast.Default(def,colon) -> bind (string_mcode def) (string_mcode colon)
367 | Ast.Case(case,exp,colon) ->
368 multibind [string_mcode case; expression exp; string_mcode colon]
369 | Ast.DisjRuleElem(res) -> multibind (List.map rule_elem res) in
370 rulefn all_functions k re
371
372 (* not parameterizable for now... *)
373 and define_parameters p =
374 let k p =
375 match Ast.unwrap p with
376 Ast.NoParams -> option_default
377 | Ast.DParams(lp,params,rp) ->
378 multibind
379 [string_mcode lp; define_param_dots params; string_mcode rp] in
380 k p
381
382 and define_param_dots d =
383 let k d =
384 match Ast.unwrap d with
385 Ast.DOTS(l) | Ast.CIRCLES(l) | Ast.STARS(l) ->
386 multibind (List.map define_param l) in
387 k d
388
389 and define_param p =
390 let k p =
391 match Ast.unwrap p with
392 Ast.DParam(id) -> ident id
393 | Ast.DPComma(comma) -> string_mcode comma
394 | Ast.DPdots(d) -> string_mcode d
395 | Ast.DPcircles(c) -> string_mcode c
396 | Ast.OptDParam(dp) -> define_param dp
397 | Ast.UniqueDParam(dp) -> define_param dp in
398 k p
399
400 (* discard the result, because the statement is assumed to be already
401 represented elsewhere in the code *)
402 and process_bef_aft s =
403 match Ast.get_dots_bef_aft s with
404 Ast.NoDots -> ()
405 | Ast.DroppingBetweenDots(stm,ind) -> let _ = statement stm in ()
406 | Ast.AddingBetweenDots(stm,ind) -> let _ = statement stm in ()
407
408 and statement s =
409 process_bef_aft s;
410 let k s =
411 match Ast.unwrap s with
412 Ast.Seq(lbrace,decls,body,rbrace) ->
413 multibind [rule_elem lbrace; statement_dots decls;
414 statement_dots body; rule_elem rbrace]
415 | Ast.IfThen(header,branch,_) ->
416 multibind [rule_elem header; statement branch]
417 | Ast.IfThenElse(header,branch1,els,branch2,_) ->
418 multibind [rule_elem header; statement branch1; rule_elem els;
419 statement branch2]
420 | Ast.While(header,body,_) ->
421 multibind [rule_elem header; statement body]
422 | Ast.Do(header,body,tail) ->
423 multibind [rule_elem header; statement body; rule_elem tail]
424 | Ast.For(header,body,_) -> multibind [rule_elem header; statement body]
425 | Ast.Iterator(header,body,_) ->
426 multibind [rule_elem header; statement body]
427 | Ast.Switch(header,lb,cases,rb) ->
428 multibind [rule_elem header;rule_elem lb;
429 multibind (List.map case_line cases);
430 rule_elem rb]
431 | Ast.Atomic(re) -> rule_elem re
432 | Ast.Disj(stmt_dots_list) ->
433 multibind (List.map statement_dots stmt_dots_list)
434 | Ast.Nest(stmt_dots,whn,_,_,_) ->
435 bind (statement_dots stmt_dots)
436 (multibind (List.map (whencode statement_dots statement) whn))
437 | Ast.FunDecl(header,lbrace,decls,body,rbrace) ->
438 multibind [rule_elem header; rule_elem lbrace;
439 statement_dots decls; statement_dots body;
440 rule_elem rbrace]
441 | Ast.Define(header,body) ->
442 bind (rule_elem header) (statement_dots body)
443 | Ast.Dots(d,whn,_,_) | Ast.Circles(d,whn,_,_) | Ast.Stars(d,whn,_,_) ->
444 bind (string_mcode d)
445 (multibind (List.map (whencode statement_dots statement) whn))
446 | Ast.OptStm(stmt) | Ast.UniqueStm(stmt) ->
447 statement stmt in
448 stmtfn all_functions k s
449
450 and fninfo = function
451 Ast.FStorage(stg) -> storage_mcode stg
452 | Ast.FType(ty) -> fullType ty
453 | Ast.FInline(inline) -> string_mcode inline
454 | Ast.FAttr(attr) -> string_mcode attr
455
456 and whencode notfn alwaysfn = function
457 Ast.WhenNot a -> notfn a
458 | Ast.WhenAlways a -> alwaysfn a
459 | Ast.WhenModifier(_) -> option_default
1be43e12
C
460 | Ast.WhenNotTrue(e) -> rule_elem e
461 | Ast.WhenNotFalse(e) -> rule_elem e
34e49164
C
462
463 and case_line c =
464 let k c =
465 match Ast.unwrap c with
466 Ast.CaseLine(header,code) ->
467 bind (rule_elem header) (statement_dots code)
468 | Ast.OptCase(case) -> case_line case in
469 casefn all_functions k c
470
471 and top_level t =
472 let k t =
473 match Ast.unwrap t with
474 Ast.FILEINFO(old_file,new_file) ->
475 bind (string_mcode old_file) (string_mcode new_file)
476 | Ast.DECL(stmt) -> statement stmt
477 | Ast.CODE(stmt_dots) -> statement_dots stmt_dots
478 | Ast.ERRORWORDS(exps) -> multibind (List.map expression exps) in
479 topfn all_functions k t
480
481 and anything a =
482 let k = function
483 (*in many cases below, the thing is not even mcode, so we do nothing*)
484 Ast.FullTypeTag(ft) -> fullType ft
485 | Ast.BaseTypeTag(bt) -> option_default
486 | Ast.StructUnionTag(su) -> option_default
487 | Ast.SignTag(sgn) -> option_default
488 | Ast.IdentTag(id) -> ident id
489 | Ast.ExpressionTag(exp) -> expression exp
490 | Ast.ConstantTag(cst) -> option_default
491 | Ast.UnaryOpTag(unop) -> option_default
492 | Ast.AssignOpTag(asgnop) -> option_default
493 | Ast.FixOpTag(fixop) -> option_default
494 | Ast.BinaryOpTag(binop) -> option_default
495 | Ast.ArithOpTag(arithop) -> option_default
496 | Ast.LogicalOpTag(logop) -> option_default
497 | Ast.DeclarationTag(decl) -> declaration decl
498 | Ast.InitTag(ini) -> initialiser ini
499 | Ast.StorageTag(stg) -> option_default
500 | Ast.IncFileTag(stg) -> option_default
501 | Ast.Rule_elemTag(rule) -> rule_elem rule
502 | Ast.StatementTag(rule) -> statement rule
503 | Ast.CaseLineTag(case) -> case_line case
504 | Ast.ConstVolTag(cv) -> option_default
505 | Ast.Token(tok,info) -> option_default
506 | Ast.Code(cd) -> top_level cd
507 | Ast.ExprDotsTag(ed) -> expression_dots ed
508 | Ast.ParamDotsTag(pd) -> parameter_dots pd
509 | Ast.StmtDotsTag(sd) -> statement_dots sd
510 | Ast.DeclDotsTag(sd) -> declaration_dots sd
511 | Ast.TypeCTag(ty) -> typeC ty
512 | Ast.ParamTag(param) -> parameterTypeDef param
513 | Ast.SgrepStartTag(tok) -> option_default
514 | Ast.SgrepEndTag(tok) -> option_default in
515 anyfn all_functions k a
516
517 and all_functions =
518 {combiner_ident = ident;
519 combiner_expression = expression;
520 combiner_fullType = fullType;
521 combiner_typeC = typeC;
522 combiner_declaration = declaration;
523 combiner_initialiser = initialiser;
524 combiner_parameter = parameterTypeDef;
525 combiner_parameter_list = parameter_dots;
526 combiner_rule_elem = rule_elem;
527 combiner_statement = statement;
528 combiner_case_line = case_line;
529 combiner_top_level = top_level;
530 combiner_anything = anything;
531 combiner_expression_dots = expression_dots;
532 combiner_statement_dots = statement_dots;
533 combiner_declaration_dots = declaration_dots} in
534 all_functions
535
536(* ---------------------------------------------------------------------- *)
537
538type 'a inout = 'a -> 'a (* for specifying the type of rebuilder *)
539
540type rebuilder =
541 {rebuilder_ident : Ast.ident inout;
542 rebuilder_expression : Ast.expression inout;
543 rebuilder_fullType : Ast.fullType inout;
544 rebuilder_typeC : Ast.typeC inout;
545 rebuilder_declaration : Ast.declaration inout;
546 rebuilder_initialiser : Ast.initialiser inout;
547 rebuilder_parameter : Ast.parameterTypeDef inout;
548 rebuilder_parameter_list : Ast.parameter_list inout;
549 rebuilder_statement : Ast.statement inout;
550 rebuilder_case_line : Ast.case_line inout;
551 rebuilder_rule_elem : Ast.rule_elem inout;
552 rebuilder_top_level : Ast.top_level inout;
553 rebuilder_expression_dots : Ast.expression Ast.dots inout;
554 rebuilder_statement_dots : Ast.statement Ast.dots inout;
555 rebuilder_declaration_dots : Ast.declaration Ast.dots inout;
556 rebuilder_define_param_dots : Ast.define_param Ast.dots inout;
557 rebuilder_define_param : Ast.define_param inout;
558 rebuilder_define_parameters : Ast.define_parameters inout;
559 rebuilder_anything : Ast.anything inout}
560
561type 'mc rmcode = 'mc Ast.mcode inout
562type 'cd rcode = rebuilder -> ('cd inout) -> 'cd inout
563
564
565let rebuilder
566 meta_mcode string_mcode const_mcode assign_mcode fix_mcode unary_mcode
567 binary_mcode cv_mcode base_mcode sign_mcode struct_mcode storage_mcode
568 inc_file_mcode
569 expdotsfn paramdotsfn stmtdotsfn decldotsfn
570 identfn exprfn ftfn tyfn initfn paramfn declfn rulefn stmtfn casefn
571 topfn anyfn =
572 let get_option f = function
573 Some x -> Some (f x)
574 | None -> None in
575 let rec expression_dots d =
576 let k d =
577 Ast.rewrap d
578 (match Ast.unwrap d with
579 Ast.DOTS(l) -> Ast.DOTS(List.map expression l)
580 | Ast.CIRCLES(l) -> Ast.CIRCLES(List.map expression l)
581 | Ast.STARS(l) -> Ast.STARS(List.map expression l)) in
582 expdotsfn all_functions k d
583
584 and parameter_dots d =
585 let k d =
586 Ast.rewrap d
587 (match Ast.unwrap d with
588 Ast.DOTS(l) -> Ast.DOTS(List.map parameterTypeDef l)
589 | Ast.CIRCLES(l) -> Ast.CIRCLES(List.map parameterTypeDef l)
590 | Ast.STARS(l) -> Ast.STARS(List.map parameterTypeDef l)) in
591 paramdotsfn all_functions k d
592
593 and statement_dots d =
594 let k d =
595 Ast.rewrap d
596 (match Ast.unwrap d with
597 Ast.DOTS(l) -> Ast.DOTS(List.map statement l)
598 | Ast.CIRCLES(l) -> Ast.CIRCLES(List.map statement l)
599 | Ast.STARS(l) -> Ast.STARS(List.map statement l)) in
600 stmtdotsfn all_functions k d
601
602 and declaration_dots d =
603 let k d =
604 Ast.rewrap d
605 (match Ast.unwrap d with
606 Ast.DOTS(l) -> Ast.DOTS(List.map declaration l)
607 | Ast.CIRCLES(l) -> Ast.CIRCLES(List.map declaration l)
608 | Ast.STARS(l) -> Ast.STARS(List.map declaration l)) in
609 decldotsfn all_functions k d
610
611 and ident i =
612 let k i =
613 Ast.rewrap i
614 (match Ast.unwrap i with
615 Ast.Id(name) -> Ast.Id(string_mcode name)
616 | Ast.MetaId(name,constraints,keep,inherited) ->
617 Ast.MetaId(meta_mcode name,constraints,keep,inherited)
618 | Ast.MetaFunc(name,constraints,keep,inherited) ->
619 Ast.MetaFunc(meta_mcode name,constraints,keep,inherited)
620 | Ast.MetaLocalFunc(name,constraints,keep,inherited) ->
621 Ast.MetaLocalFunc(meta_mcode name,constraints,keep,inherited)
622 | Ast.OptIdent(id) -> Ast.OptIdent(ident id)
623 | Ast.UniqueIdent(id) -> Ast.UniqueIdent(ident id)) in
624 identfn all_functions k i
625
626 and expression e =
627 let k e =
628 Ast.rewrap e
629 (match Ast.unwrap e with
630 Ast.Ident(id) -> Ast.Ident(ident id)
631 | Ast.Constant(const) -> Ast.Constant(const_mcode const)
632 | Ast.FunCall(fn,lp,args,rp) ->
633 Ast.FunCall(expression fn, string_mcode lp, expression_dots args,
634 string_mcode rp)
635 | Ast.Assignment(left,op,right,simple) ->
636 Ast.Assignment(expression left, assign_mcode op, expression right,
637 simple)
638 | Ast.CondExpr(exp1,why,exp2,colon,exp3) ->
639 Ast.CondExpr(expression exp1, string_mcode why,
640 get_option expression exp2, string_mcode colon,
641 expression exp3)
642 | Ast.Postfix(exp,op) -> Ast.Postfix(expression exp,fix_mcode op)
643 | Ast.Infix(exp,op) -> Ast.Infix(expression exp,fix_mcode op)
644 | Ast.Unary(exp,op) -> Ast.Unary(expression exp,unary_mcode op)
645 | Ast.Binary(left,op,right) ->
646 Ast.Binary(expression left, binary_mcode op, expression right)
647 | Ast.Nested(left,op,right) ->
648 Ast.Nested(expression left, binary_mcode op, expression right)
649 | Ast.Paren(lp,exp,rp) ->
650 Ast.Paren(string_mcode lp, expression exp, string_mcode rp)
651 | Ast.ArrayAccess(exp1,lb,exp2,rb) ->
652 Ast.ArrayAccess(expression exp1, string_mcode lb, expression exp2,
653 string_mcode rb)
654 | Ast.RecordAccess(exp,pt,field) ->
655 Ast.RecordAccess(expression exp, string_mcode pt, ident field)
656 | Ast.RecordPtAccess(exp,ar,field) ->
657 Ast.RecordPtAccess(expression exp, string_mcode ar, ident field)
658 | Ast.Cast(lp,ty,rp,exp) ->
659 Ast.Cast(string_mcode lp, fullType ty, string_mcode rp,
660 expression exp)
661 | Ast.SizeOfExpr(szf,exp) ->
662 Ast.SizeOfExpr(string_mcode szf, expression exp)
663 | Ast.SizeOfType(szf,lp,ty,rp) ->
664 Ast.SizeOfType(string_mcode szf,string_mcode lp, fullType ty,
665 string_mcode rp)
666 | Ast.TypeExp(ty) -> Ast.TypeExp(fullType ty)
667 | Ast.MetaErr(name,constraints,keep,inherited) ->
668 Ast.MetaErr(meta_mcode name,constraints,keep,inherited)
669 | Ast.MetaExpr(name,constraints,keep,ty,form,inherited) ->
670 Ast.MetaExpr(meta_mcode name,constraints,keep,ty,form,inherited)
671 | Ast.MetaExprList(name,lenname_inh,keep,inherited) ->
672 Ast.MetaExprList(meta_mcode name,lenname_inh,keep,inherited)
673 | Ast.EComma(cm) -> Ast.EComma(string_mcode cm)
674 | Ast.DisjExpr(exp_list) -> Ast.DisjExpr(List.map expression exp_list)
675 | Ast.NestExpr(expr_dots,whencode,multi) ->
676 Ast.NestExpr(expression_dots expr_dots,
677 get_option expression whencode,multi)
678 | Ast.Edots(dots,whencode) ->
679 Ast.Edots(string_mcode dots,get_option expression whencode)
680 | Ast.Ecircles(dots,whencode) ->
681 Ast.Ecircles(string_mcode dots,get_option expression whencode)
682 | Ast.Estars(dots,whencode) ->
683 Ast.Estars(string_mcode dots,get_option expression whencode)
684 | Ast.OptExp(exp) -> Ast.OptExp(expression exp)
685 | Ast.UniqueExp(exp) -> Ast.UniqueExp(expression exp)) in
686 exprfn all_functions k e
687
688 and fullType ft =
689 let k ft =
690 Ast.rewrap ft
691 (match Ast.unwrap ft with
692 Ast.Type(cv,ty) -> Ast.Type (get_option cv_mcode cv, typeC ty)
693 | Ast.DisjType(types) -> Ast.DisjType(List.map fullType types)
694 | Ast.OptType(ty) -> Ast.OptType(fullType ty)
695 | Ast.UniqueType(ty) -> Ast.UniqueType(fullType ty)) in
696 ftfn all_functions k ft
697
698 and typeC ty =
699 let k ty =
700 Ast.rewrap ty
701 (match Ast.unwrap ty with
702 Ast.BaseType(ty,sgn) ->
703 Ast.BaseType (base_mcode ty,get_option sign_mcode sgn)
704 | Ast.ImplicitInt(sgn) -> Ast.ImplicitInt (sign_mcode sgn)
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.StructUnionName(kind,name) ->
719 Ast.StructUnionName (struct_mcode kind, get_option ident name)
720 | Ast.StructUnionDef(ty,lb,decls,rb) ->
721 Ast.StructUnionDef (fullType ty,
722 string_mcode lb, declaration_dots decls,
723 string_mcode rb)
724 | Ast.TypeName(name) -> Ast.TypeName(string_mcode name)
725 | Ast.MetaType(name,keep,inherited) ->
726 Ast.MetaType(meta_mcode name,keep,inherited)) in
727 tyfn all_functions k ty
728
729 and declaration d =
730 let k d =
731 Ast.rewrap d
732 (match Ast.unwrap d with
733 Ast.Init(stg,ty,id,eq,ini,sem) ->
734 Ast.Init(get_option storage_mcode stg, fullType ty, ident id,
735 string_mcode eq, initialiser ini, string_mcode sem)
736 | Ast.UnInit(stg,ty,id,sem) ->
737 Ast.UnInit(get_option storage_mcode stg, fullType ty, ident id,
738 string_mcode sem)
739 | Ast.MacroDecl(name,lp,args,rp,sem) ->
740 Ast.MacroDecl(ident name, string_mcode lp, expression_dots args,
741 string_mcode rp,string_mcode sem)
742 | Ast.TyDecl(ty,sem) -> Ast.TyDecl(fullType ty, string_mcode sem)
743 | Ast.Typedef(stg,ty,id,sem) ->
744 Ast.Typedef(string_mcode stg, fullType ty, typeC id,
745 string_mcode sem)
746 | Ast.DisjDecl(decls) -> Ast.DisjDecl(List.map declaration decls)
747 | Ast.Ddots(dots,whencode) ->
748 Ast.Ddots(string_mcode dots, get_option declaration whencode)
749 | Ast.MetaDecl(name,keep,inherited) ->
750 Ast.MetaDecl(meta_mcode name,keep,inherited)
751 | Ast.OptDecl(decl) -> Ast.OptDecl(declaration decl)
752 | Ast.UniqueDecl(decl) -> Ast.UniqueDecl(declaration decl)) in
753 declfn all_functions k d
754
755 and initialiser i =
756 let k i =
757 Ast.rewrap i
758 (match Ast.unwrap i with
759 Ast.InitExpr(exp) -> Ast.InitExpr(expression exp)
760 | Ast.InitList(lb,initlist,rb,whencode) ->
761 Ast.InitList(string_mcode lb, List.map initialiser initlist,
762 string_mcode rb, List.map initialiser whencode)
763 | Ast.InitGccDotName(dot,name,eq,ini) ->
764 Ast.InitGccDotName
765 (string_mcode dot, ident name, string_mcode eq, initialiser ini)
766 | Ast.InitGccName(name,eq,ini) ->
767 Ast.InitGccName(ident name, string_mcode eq, initialiser ini)
768 | Ast.InitGccIndex(lb,exp,rb,eq,ini) ->
769 Ast.InitGccIndex
770 (string_mcode lb, expression exp, string_mcode rb,
771 string_mcode eq, initialiser ini)
772 | Ast.InitGccRange(lb,exp1,dots,exp2,rb,eq,ini) ->
773 Ast.InitGccRange
774 (string_mcode lb, expression exp1, string_mcode dots,
775 expression exp2, string_mcode rb, 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 parameterTypeDef p =
783 let k p =
784 Ast.rewrap p
785 (match Ast.unwrap p with
786 Ast.VoidParam(ty) -> Ast.VoidParam(fullType ty)
787 | Ast.Param(ty,id) -> Ast.Param(fullType ty, get_option ident id)
788 | Ast.MetaParam(name,keep,inherited) ->
789 Ast.MetaParam(meta_mcode name,keep,inherited)
790 | Ast.MetaParamList(name,lenname_inh,keep,inherited) ->
791 Ast.MetaParamList(meta_mcode name,lenname_inh,keep,inherited)
792 | Ast.PComma(cm) -> Ast.PComma(string_mcode cm)
793 | Ast.Pdots(dots) -> Ast.Pdots(string_mcode dots)
794 | Ast.Pcircles(dots) -> Ast.Pcircles(string_mcode dots)
795 | Ast.OptParam(param) -> Ast.OptParam(parameterTypeDef param)
796 | Ast.UniqueParam(param) -> Ast.UniqueParam(parameterTypeDef param)) in
797 paramfn all_functions k p
798
799 and rule_elem re =
800 let k re =
801 Ast.rewrap re
802 (match Ast.unwrap re with
803 Ast.FunHeader(bef,allminus,fi,name,lp,params,rp) ->
804 Ast.FunHeader(bef,allminus,List.map fninfo fi,ident name,
805 string_mcode lp, parameter_dots params,
806 string_mcode rp)
807 | Ast.Decl(bef,allminus,decl) ->
808 Ast.Decl(bef,allminus,declaration decl)
809 | Ast.SeqStart(brace) -> Ast.SeqStart(string_mcode brace)
810 | Ast.SeqEnd(brace) -> Ast.SeqEnd(string_mcode brace)
811 | Ast.ExprStatement(exp,sem) ->
812 Ast.ExprStatement (expression exp, string_mcode sem)
813 | Ast.IfHeader(iff,lp,exp,rp) ->
814 Ast.IfHeader(string_mcode iff, string_mcode lp, expression exp,
815 string_mcode rp)
816 | Ast.Else(els) -> Ast.Else(string_mcode els)
817 | Ast.WhileHeader(whl,lp,exp,rp) ->
818 Ast.WhileHeader(string_mcode whl, string_mcode lp, expression exp,
819 string_mcode rp)
820 | Ast.DoHeader(d) -> Ast.DoHeader(string_mcode d)
821 | Ast.WhileTail(whl,lp,exp,rp,sem) ->
822 Ast.WhileTail(string_mcode whl, string_mcode lp, expression exp,
823 string_mcode rp, string_mcode sem)
824 | Ast.ForHeader(fr,lp,e1,sem1,e2,sem2,e3,rp) ->
825 Ast.ForHeader(string_mcode fr, string_mcode lp,
826 get_option expression e1, string_mcode sem1,
827 get_option expression e2, string_mcode sem2,
828 get_option expression e3, string_mcode rp)
829 | Ast.IteratorHeader(whl,lp,args,rp) ->
830 Ast.IteratorHeader(ident whl, string_mcode lp,
831 expression_dots args, string_mcode rp)
832 | Ast.SwitchHeader(switch,lp,exp,rp) ->
833 Ast.SwitchHeader(string_mcode switch, string_mcode lp,
834 expression exp, string_mcode rp)
835 | Ast.Break(br,sem) ->
836 Ast.Break(string_mcode br, string_mcode sem)
837 | Ast.Continue(cont,sem) ->
838 Ast.Continue(string_mcode cont, string_mcode sem)
839 | Ast.Label(l,dd) -> Ast.Label(ident l, string_mcode dd)
840 | Ast.Goto(goto,l,sem) ->
841 Ast.Goto(string_mcode goto,ident l,string_mcode sem)
842 | Ast.Return(ret,sem) ->
843 Ast.Return(string_mcode ret, string_mcode sem)
844 | Ast.ReturnExpr(ret,exp,sem) ->
845 Ast.ReturnExpr(string_mcode ret, expression exp, string_mcode sem)
846 | Ast.MetaStmt(name,keep,seqible,inherited) ->
847 Ast.MetaStmt(meta_mcode name,keep,seqible,inherited)
848 | Ast.MetaStmtList(name,keep,inherited) ->
849 Ast.MetaStmtList(meta_mcode name,keep,inherited)
850 | Ast.MetaRuleElem(name,keep,inherited) ->
851 Ast.MetaRuleElem(meta_mcode name,keep,inherited)
852 | Ast.Exp(exp) -> Ast.Exp(expression exp)
853 | Ast.TopExp(exp) -> Ast.TopExp(expression exp)
854 | Ast.Ty(ty) -> Ast.Ty(fullType ty)
1be43e12 855 | Ast.TopInit(init) -> Ast.TopInit(initialiser init)
34e49164
C
856 | Ast.Include(inc,name) ->
857 Ast.Include(string_mcode inc,inc_file_mcode name)
858 | Ast.DefineHeader(def,id,params) ->
859 Ast.DefineHeader(string_mcode def,ident id,
860 define_parameters params)
861 | Ast.Default(def,colon) ->
862 Ast.Default(string_mcode def,string_mcode colon)
863 | Ast.Case(case,exp,colon) ->
864 Ast.Case(string_mcode case,expression exp,string_mcode colon)
865 | Ast.DisjRuleElem(res) -> Ast.DisjRuleElem(List.map rule_elem res)) in
866 rulefn all_functions k re
867
868 (* not parameterizable for now... *)
869 and define_parameters p =
870 let k p =
871 Ast.rewrap p
872 (match Ast.unwrap p with
873 Ast.NoParams -> Ast.NoParams
874 | Ast.DParams(lp,params,rp) ->
875 Ast.DParams(string_mcode lp,define_param_dots params,
876 string_mcode rp)) in
877 k p
878
879 and define_param_dots d =
880 let k d =
881 Ast.rewrap d
882 (match Ast.unwrap d with
883 Ast.DOTS(l) -> Ast.DOTS(List.map define_param l)
884 | Ast.CIRCLES(l) -> Ast.CIRCLES(List.map define_param l)
885 | Ast.STARS(l) -> Ast.STARS(List.map define_param l)) in
886 k d
887
888 and define_param p =
889 let k p =
890 Ast.rewrap p
891 (match Ast.unwrap p with
892 Ast.DParam(id) -> Ast.DParam(ident id)
893 | Ast.DPComma(comma) -> Ast.DPComma(string_mcode comma)
894 | Ast.DPdots(d) -> Ast.DPdots(string_mcode d)
895 | Ast.DPcircles(c) -> Ast.DPcircles(string_mcode c)
896 | Ast.OptDParam(dp) -> Ast.OptDParam(define_param dp)
897 | Ast.UniqueDParam(dp) -> Ast.UniqueDParam(define_param dp)) in
898 k p
899
900 and process_bef_aft s =
901 Ast.set_dots_bef_aft
902 (match Ast.get_dots_bef_aft s with
903 Ast.NoDots -> Ast.NoDots
904 | Ast.DroppingBetweenDots(stm,ind) ->
905 Ast.DroppingBetweenDots(statement stm,ind)
906 | Ast.AddingBetweenDots(stm,ind) ->
907 Ast.AddingBetweenDots(statement stm,ind))
908 s
909
910 and statement s =
911 let k s =
912 Ast.rewrap s
913 (match Ast.unwrap s with
914 Ast.Seq(lbrace,decls,body,rbrace) ->
915 Ast.Seq(rule_elem lbrace, statement_dots decls,
916 statement_dots body, rule_elem rbrace)
917 | Ast.IfThen(header,branch,aft) ->
918 Ast.IfThen(rule_elem header, statement branch,aft)
919 | Ast.IfThenElse(header,branch1,els,branch2,aft) ->
920 Ast.IfThenElse(rule_elem header, statement branch1, rule_elem els,
921 statement branch2, aft)
922 | Ast.While(header,body,aft) ->
923 Ast.While(rule_elem header, statement body, aft)
924 | Ast.Do(header,body,tail) ->
925 Ast.Do(rule_elem header, statement body, rule_elem tail)
926 | Ast.For(header,body,aft) ->
927 Ast.For(rule_elem header, statement body, aft)
928 | Ast.Iterator(header,body,aft) ->
929 Ast.Iterator(rule_elem header, statement body, aft)
930 | Ast.Switch(header,lb,cases,rb) ->
931 Ast.Switch(rule_elem header,rule_elem lb,
932 List.map case_line cases,rule_elem rb)
933 | Ast.Atomic(re) -> Ast.Atomic(rule_elem re)
934 | Ast.Disj(stmt_dots_list) ->
935 Ast.Disj (List.map statement_dots stmt_dots_list)
936 | Ast.Nest(stmt_dots,whn,multi,bef,aft) ->
937 Ast.Nest(statement_dots stmt_dots,
938 List.map (whencode statement_dots statement) whn,
939 multi,bef,aft)
940 | Ast.FunDecl(header,lbrace,decls,body,rbrace) ->
941 Ast.FunDecl(rule_elem header,rule_elem lbrace,
942 statement_dots decls,
943 statement_dots body, rule_elem rbrace)
944 | Ast.Define(header,body) ->
945 Ast.Define(rule_elem header,statement_dots body)
946 | Ast.Dots(d,whn,bef,aft) ->
947 Ast.Dots(string_mcode d,
948 List.map (whencode statement_dots statement) whn,bef,aft)
949 | Ast.Circles(d,whn,bef,aft) ->
950 Ast.Circles(string_mcode d,
951 List.map (whencode statement_dots statement) whn,
952 bef,aft)
953 | Ast.Stars(d,whn,bef,aft) ->
954 Ast.Stars(string_mcode d,
955 List.map (whencode statement_dots statement) whn,bef,aft)
956 | Ast.OptStm(stmt) -> Ast.OptStm(statement stmt)
957 | Ast.UniqueStm(stmt) -> Ast.UniqueStm(statement stmt)) in
958 let s = stmtfn all_functions k s in
959 (* better to do this after, in case there is an equality test on the whole
960 statement, eg in free_vars. equality test would require that this
961 subterm not already be changed *)
962 process_bef_aft s
963
964 and fninfo = function
965 Ast.FStorage(stg) -> Ast.FStorage(storage_mcode stg)
966 | Ast.FType(ty) -> Ast.FType(fullType ty)
967 | Ast.FInline(inline) -> Ast.FInline(string_mcode inline)
968 | Ast.FAttr(attr) -> Ast.FAttr(string_mcode attr)
969
970 and whencode notfn alwaysfn = function
971 Ast.WhenNot a -> Ast.WhenNot (notfn a)
972 | Ast.WhenAlways a -> Ast.WhenAlways (alwaysfn a)
973 | Ast.WhenModifier(x) -> Ast.WhenModifier(x)
1be43e12
C
974 | Ast.WhenNotTrue(e) -> Ast.WhenNotTrue(rule_elem e)
975 | Ast.WhenNotFalse(e) -> Ast.WhenNotFalse(rule_elem e)
34e49164
C
976
977 and case_line c =
978 let k c =
979 Ast.rewrap c
980 (match Ast.unwrap c with
981 Ast.CaseLine(header,code) ->
982 Ast.CaseLine(rule_elem header,statement_dots code)
983 | Ast.OptCase(case) -> Ast.OptCase(case_line case)) in
984 casefn all_functions k c
985
986 and top_level t =
987 let k t =
988 Ast.rewrap t
989 (match Ast.unwrap t with
990 Ast.FILEINFO(old_file,new_file) ->
991 Ast.FILEINFO (string_mcode old_file, string_mcode new_file)
992 | Ast.DECL(stmt) -> Ast.DECL(statement stmt)
993 | Ast.CODE(stmt_dots) -> Ast.CODE(statement_dots stmt_dots)
994 | Ast.ERRORWORDS(exps) -> Ast.ERRORWORDS (List.map expression exps)) in
995 topfn all_functions k t
996
997 and anything a =
998 let k = function
999 (*in many cases below, the thing is not even mcode, so we do nothing*)
1000 Ast.FullTypeTag(ft) -> Ast.FullTypeTag(fullType ft)
1001 | Ast.BaseTypeTag(bt) as x -> x
1002 | Ast.StructUnionTag(su) as x -> x
1003 | Ast.SignTag(sgn) as x -> x
1004 | Ast.IdentTag(id) -> Ast.IdentTag(ident id)
1005 | Ast.ExpressionTag(exp) -> Ast.ExpressionTag(expression exp)
1006 | Ast.ConstantTag(cst) as x -> x
1007 | Ast.UnaryOpTag(unop) as x -> x
1008 | Ast.AssignOpTag(asgnop) as x -> x
1009 | Ast.FixOpTag(fixop) as x -> x
1010 | Ast.BinaryOpTag(binop) as x -> x
1011 | Ast.ArithOpTag(arithop) as x -> x
1012 | Ast.LogicalOpTag(logop) as x -> x
1013 | Ast.InitTag(decl) -> Ast.InitTag(initialiser decl)
1014 | Ast.DeclarationTag(decl) -> Ast.DeclarationTag(declaration decl)
1015 | Ast.StorageTag(stg) as x -> x
1016 | Ast.IncFileTag(stg) as x -> x
1017 | Ast.Rule_elemTag(rule) -> Ast.Rule_elemTag(rule_elem rule)
1018 | Ast.StatementTag(rule) -> Ast.StatementTag(statement rule)
1019 | Ast.CaseLineTag(case) -> Ast.CaseLineTag(case_line case)
1020 | Ast.ConstVolTag(cv) as x -> x
1021 | Ast.Token(tok,info) as x -> x
1022 | Ast.Code(cd) -> Ast.Code(top_level cd)
1023 | Ast.ExprDotsTag(ed) -> Ast.ExprDotsTag(expression_dots ed)
1024 | Ast.ParamDotsTag(pd) -> Ast.ParamDotsTag(parameter_dots pd)
1025 | Ast.StmtDotsTag(sd) -> Ast.StmtDotsTag(statement_dots sd)
1026 | Ast.DeclDotsTag(sd) -> Ast.DeclDotsTag(declaration_dots sd)
1027 | Ast.TypeCTag(ty) -> Ast.TypeCTag(typeC ty)
1028 | Ast.ParamTag(param) -> Ast.ParamTag(parameterTypeDef param)
1029 | Ast.SgrepStartTag(tok) as x -> x
1030 | Ast.SgrepEndTag(tok) as x -> x in
1031 anyfn all_functions k a
1032
1033 and all_functions =
1034 {rebuilder_ident = ident;
1035 rebuilder_expression = expression;
1036 rebuilder_fullType= fullType;
1037 rebuilder_typeC = typeC;
1038 rebuilder_declaration = declaration;
1039 rebuilder_initialiser = initialiser;
1040 rebuilder_parameter = parameterTypeDef;
1041 rebuilder_parameter_list = parameter_dots;
1042 rebuilder_rule_elem = rule_elem;
1043 rebuilder_statement = statement;
1044 rebuilder_case_line = case_line;
1045 rebuilder_top_level = top_level;
1046 rebuilder_expression_dots = expression_dots;
1047 rebuilder_statement_dots = statement_dots;
1048 rebuilder_declaration_dots = declaration_dots;
1049 rebuilder_define_param_dots = define_param_dots;
1050 rebuilder_define_param = define_param;
1051 rebuilder_define_parameters = define_parameters;
1052 rebuilder_anything = anything} in
1053 all_functions
1054