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