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