2 * Copyright 2012, INRIA
3 * Julia Lawall, Gilles Muller
4 * Copyright 2010-2011, INRIA, University of Copenhagen
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.
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.
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.
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/>.
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
27 #
0 "./visitor_ast.ml"
28 module Ast0
= Ast0_cocci
29 module Ast
= Ast_cocci
31 (* --------------------------------------------------------------------- *)
32 (* Generic traversal: combiner *)
35 treatment of: mcode, identifiers, expressions, fullTypes, types,
36 declarations, statements, toplevels
37 default value for options *)
40 {combiner_ident
: Ast.ident
-> 'a
;
41 combiner_expression
: Ast.expression
-> 'a
;
42 combiner_fullType
: Ast.fullType
-> 'a
;
43 combiner_typeC
: Ast.typeC
-> 'a
;
44 combiner_declaration
: Ast.declaration
-> 'a
;
45 combiner_initialiser
: Ast.initialiser
-> 'a
;
46 combiner_parameter
: Ast.parameterTypeDef
-> 'a
;
47 combiner_parameter_list
: Ast.parameter_list
-> 'a
;
48 combiner_rule_elem
: Ast.rule_elem
-> 'a
;
49 combiner_statement
: Ast.statement
-> 'a
;
50 combiner_case_line
: Ast.case_line
-> 'a
;
51 combiner_top_level
: Ast.top_level
-> 'a
;
52 combiner_anything
: Ast.anything
-> 'a
;
53 combiner_expression_dots
: Ast.expression
Ast.dots
-> 'a
;
54 combiner_statement_dots
: Ast.statement
Ast.dots
-> 'a
;
55 combiner_declaration_dots
: Ast.declaration
Ast.dots
-> 'a
;
56 combiner_initialiser_dots
: Ast.initialiser
Ast.dots
-> 'a
}
58 type ('mc
,'a
) cmcode
= 'a combiner
-> 'mc
Ast_cocci.mcode
-> 'a
59 type ('cd
,'a
) ccode
= 'a combiner
-> ('cd
-> 'a
) -> 'cd
-> 'a
62 let combiner bind option_default
63 meta_mcodefn string_mcodefn const_mcodefn assign_mcodefn fix_mcodefn
64 unary_mcodefn binary_mcodefn
65 cv_mcodefn sign_mcodefn struct_mcodefn storage_mcodefn
67 expdotsfn paramdotsfn stmtdotsfn decldotsfn initdotsfn
68 identfn exprfn ftfn tyfn initfn paramfn declfn rulefn stmtfn casefn
71 let rec loop = function
74 | x
::xs
-> bind x
(loop xs
) in
76 let get_option f
= function
78 | None
-> option_default
in
80 let dotsfn param default all_functions arg
=
82 match Ast.unwrap d
with
83 Ast.DOTS
(l
) | Ast.CIRCLES
(l
) | Ast.STARS
(l
) ->
84 multibind (List.map default l
) in
85 param all_functions
k arg
in
87 let rec meta_mcode x
= meta_mcodefn all_functions x
88 and string_mcode x
= string_mcodefn all_functions x
89 and const_mcode x
= const_mcodefn all_functions x
90 and assign_mcode x
= assign_mcodefn all_functions x
91 and fix_mcode x
= fix_mcodefn all_functions x
92 and unary_mcode x
= unary_mcodefn all_functions x
93 and binary_mcode x
= binary_mcodefn all_functions x
94 and cv_mcode x
= cv_mcodefn all_functions x
95 and sign_mcode x
= sign_mcodefn all_functions x
96 and struct_mcode x
= struct_mcodefn all_functions x
97 and storage_mcode x
= storage_mcodefn all_functions x
98 and inc_file_mcode x
= inc_file_mcodefn all_functions x
100 and expression_dots d
= dotsfn expdotsfn expression all_functions d
101 and parameter_dots d
= dotsfn paramdotsfn parameterTypeDef all_functions d
102 and statement_dots d
= dotsfn stmtdotsfn statement all_functions d
103 and declaration_dots d
= dotsfn decldotsfn declaration all_functions d
104 and initialiser_dots d
= dotsfn initdotsfn initialiser all_functions d
108 match Ast.unwrap i
with
109 Ast.Id
(name
) -> string_mcode name
110 | Ast.MetaId
(name
,_
,_
,_
) -> meta_mcode name
111 | Ast.MetaFunc
(name
,_
,_
,_
) -> meta_mcode name
112 | Ast.MetaLocalFunc
(name
,_
,_
,_
) -> meta_mcode name
113 | Ast.AsIdent
(id
,asid
) -> bind
(ident id
) (ident asid
)
114 | Ast.DisjId
(id_list
) -> multibind (List.map ident id_list
)
115 | Ast.OptIdent
(id
) -> ident id
116 | Ast.UniqueIdent
(id
) -> ident id
in
117 identfn all_functions
k i
121 match Ast.unwrap e
with
122 Ast.Ident
(id
) -> ident id
123 | Ast.Constant
(const
) -> const_mcode const
124 | Ast.FunCall
(fn
,lp
,args
,rp
) ->
125 multibind [expression fn
; string_mcode lp
; expression_dots args
;
127 | Ast.Assignment
(left
,op
,right
,simple
) ->
128 multibind [expression left
; assign_mcode op
; expression right
]
129 | Ast.Sequence
(left
,op
,right
) ->
130 multibind [expression left
; string_mcode op
; expression right
]
131 | Ast.CondExpr
(exp1
,why
,exp2
,colon
,exp3
) ->
132 multibind [expression exp1
; string_mcode why
;
133 get_option expression exp2
; string_mcode colon
;
135 | Ast.Postfix
(exp
,op
) -> bind
(expression exp
) (fix_mcode op
)
136 | Ast.Infix
(exp
,op
) -> bind
(fix_mcode op
) (expression exp
)
137 | Ast.Unary
(exp
,op
) -> bind
(unary_mcode op
) (expression exp
)
138 | Ast.Binary
(left
,op
,right
) ->
139 multibind [expression left
; binary_mcode op
; expression right
]
140 | Ast.Nested
(left
,op
,right
) ->
141 multibind [expression left
; binary_mcode op
; expression right
]
142 | Ast.Paren
(lp
,exp
,rp
) ->
143 multibind [string_mcode lp
; expression exp
; string_mcode rp
]
144 | Ast.ArrayAccess
(exp1
,lb
,exp2
,rb
) ->
146 [expression exp1
; string_mcode lb
; expression exp2
;
148 | Ast.RecordAccess
(exp
,pt
,field
) ->
149 multibind [expression exp
; string_mcode pt
; ident field
]
150 | Ast.RecordPtAccess
(exp
,ar
,field
) ->
151 multibind [expression exp
; string_mcode ar
; ident field
]
152 | Ast.Cast
(lp
,ty
,rp
,exp
) ->
154 [string_mcode lp
; fullType ty
; string_mcode rp
; expression exp
]
155 | Ast.SizeOfExpr
(szf
,exp
) ->
156 multibind [string_mcode szf
; expression exp
]
157 | Ast.SizeOfType
(szf
,lp
,ty
,rp
) ->
159 [string_mcode szf
; string_mcode lp
; fullType ty
; string_mcode rp
]
160 | Ast.TypeExp
(ty
) -> fullType ty
161 | Ast.Constructor
(lp
,ty
,rp
,init
) ->
163 [string_mcode lp
; fullType ty
; string_mcode rp
; initialiser init
]
164 | Ast.MetaErr
(name
,_
,_
,_
)
165 | Ast.MetaExpr
(name
,_
,_
,_
,_
,_
)
166 | Ast.MetaExprList
(name
,_
,_
,_
) -> meta_mcode name
167 | Ast.AsExpr
(exp
,asexp
) -> bind
(expression exp
) (expression asexp
)
168 | Ast.EComma
(cm
) -> string_mcode cm
169 | Ast.DisjExpr
(exp_list
) -> multibind (List.map expression exp_list
)
170 | Ast.NestExpr
(starter
,expr_dots
,ender
,whencode
,multi
) ->
171 bind
(string_mcode starter
)
172 (bind
(expression_dots expr_dots
)
173 (bind
(string_mcode ender
)
174 (get_option expression whencode
)))
175 | Ast.Edots
(dots
,whencode
) | Ast.Ecircles
(dots
,whencode
)
176 | Ast.Estars
(dots
,whencode
) ->
177 bind
(string_mcode dots
) (get_option expression whencode
)
178 | Ast.OptExp
(exp
) | Ast.UniqueExp
(exp
) ->
180 exprfn all_functions
k e
184 match Ast.unwrap ft
with
185 Ast.Type
(_
,cv
,ty
) -> bind
(get_option cv_mcode cv
) (typeC ty
)
186 | Ast.AsType
(ty
,asty
) -> bind
(fullType ty
) (fullType asty
)
187 | Ast.DisjType
(types
) -> multibind (List.map fullType types
)
188 | Ast.OptType
(ty
) -> fullType ty
189 | Ast.UniqueType
(ty
) -> fullType ty
in
190 ftfn all_functions
k ft
192 and function_pointer
(ty
,lp1
,star
,rp1
,lp2
,params
,rp2
) extra
=
193 (* have to put the treatment of the identifier into the right position *)
195 ([fullType ty
; string_mcode lp1
; string_mcode star
] @ extra
@
197 string_mcode lp2
; parameter_dots params
; string_mcode rp2
])
199 and function_type
(ty
,lp1
,params
,rp1
) extra
=
200 (* have to put the treatment of the identifier into the right position *)
202 ([get_option fullType ty
] @ extra
@
203 [string_mcode lp1
; parameter_dots params
; string_mcode rp1
])
205 and array_type
(ty
,lb
,size
,rb
) extra
=
207 ([fullType ty
] @ extra
@
208 [string_mcode lb
; get_option expression size
; string_mcode rb
])
212 match Ast.unwrap ty
with
213 Ast.BaseType
(ty
,strings
) -> multibind (List.map string_mcode strings
)
214 | Ast.SignedT
(sgn
,ty
) -> bind
(sign_mcode sgn
) (get_option typeC ty
)
215 | Ast.Pointer
(ty
,star
) ->
216 bind
(fullType ty
) (string_mcode star
)
217 | Ast.FunctionPointer
(ty
,lp1
,star
,rp1
,lp2
,params
,rp2
) ->
218 function_pointer
(ty
,lp1
,star
,rp1
,lp2
,params
,rp2
) []
219 | Ast.FunctionType
(_
,ty
,lp1
,params
,rp1
) ->
220 function_type
(ty
,lp1
,params
,rp1
) []
221 | Ast.Array
(ty
,lb
,size
,rb
) -> array_type
(ty
,lb
,size
,rb
) []
222 | Ast.EnumName
(kind
,name
) ->
223 bind
(string_mcode kind
) (get_option ident name
)
224 | Ast.EnumDef
(ty
,lb
,ids
,rb
) ->
226 [fullType ty
; string_mcode lb
; expression_dots ids
;
228 | Ast.StructUnionName
(kind
,name
) ->
229 bind
(struct_mcode kind
) (get_option ident name
)
230 | Ast.StructUnionDef
(ty
,lb
,decls
,rb
) ->
232 [fullType ty
; string_mcode lb
; declaration_dots decls
;
234 | Ast.TypeName
(name
) -> string_mcode name
235 | Ast.MetaType
(name
,_
,_
) -> meta_mcode name
in
236 tyfn all_functions
k ty
238 and named_type ty id
=
239 match Ast.unwrap ty
with
240 Ast.Type
(_
,None
,ty1
) ->
241 (match Ast.unwrap ty1
with
242 Ast.FunctionPointer
(ty
,lp1
,star
,rp1
,lp2
,params
,rp2
) ->
243 function_pointer
(ty
,lp1
,star
,rp1
,lp2
,params
,rp2
) [ident id
]
244 | Ast.FunctionType
(_
,ty
,lp1
,params
,rp1
) ->
245 function_type
(ty
,lp1
,params
,rp1
) [ident id
]
246 | Ast.Array
(ty
,lb
,size
,rb
) -> array_type
(ty
,lb
,size
,rb
) [ident id
]
247 | _
-> bind
(fullType ty
) (ident id
))
248 | _
-> bind
(fullType ty
) (ident id
)
252 match Ast.unwrap d
with
253 Ast.MetaDecl
(name
,_
,_
) | Ast.MetaField
(name
,_
,_
)
254 | Ast.MetaFieldList
(name
,_
,_
,_
) ->
256 | Ast.AsDecl
(decl
,asdecl
) ->
257 bind
(declaration decl
) (declaration asdecl
)
258 | Ast.Init
(stg
,ty
,id
,eq
,ini
,sem
) ->
259 bind
(get_option storage_mcode stg
)
260 (bind
(named_type ty id
)
262 [string_mcode eq
; initialiser ini
; string_mcode sem
]))
263 | Ast.UnInit
(stg
,ty
,id
,sem
) ->
264 bind
(get_option storage_mcode stg
)
265 (bind
(named_type ty id
) (string_mcode sem
))
266 | Ast.MacroDecl
(name
,lp
,args
,rp
,sem
) ->
268 [ident name
; string_mcode lp
; expression_dots args
;
269 string_mcode rp
; string_mcode sem
]
270 | Ast.MacroDeclInit
(name
,lp
,args
,rp
,eq
,ini
,sem
) ->
272 [ident name
; string_mcode lp
; expression_dots args
;
273 string_mcode rp
; string_mcode eq
; initialiser ini
;
275 | Ast.TyDecl
(ty
,sem
) -> bind
(fullType ty
) (string_mcode sem
)
276 | Ast.Typedef
(stg
,ty
,id
,sem
) ->
277 bind
(string_mcode stg
)
278 (bind
(fullType ty
) (bind
(typeC id
) (string_mcode sem
)))
279 | Ast.DisjDecl
(decls
) -> multibind (List.map declaration decls
)
280 | Ast.Ddots
(dots
,whencode
) ->
281 bind
(string_mcode dots
) (get_option declaration whencode
)
282 | Ast.OptDecl
(decl
) -> declaration decl
283 | Ast.UniqueDecl
(decl
) -> declaration decl
in
284 declfn all_functions
k d
288 match Ast.unwrap i
with
289 Ast.MetaInit
(name
,_
,_
) -> meta_mcode name
290 | Ast.MetaInitList
(name
,_
,_
,_
) -> meta_mcode name
291 | Ast.AsInit
(init
,asinit
) ->
292 bind
(initialiser init
) (initialiser asinit
)
293 | Ast.InitExpr
(exp
) -> expression exp
294 | Ast.ArInitList
(lb
,initlist
,rb
) ->
296 [string_mcode lb
; initialiser_dots initlist
; string_mcode rb
]
297 | Ast.StrInitList
(allminus
,lb
,initlist
,rb
,whencode
) ->
300 multibind (List.map initialiser initlist
);
302 multibind (List.map initialiser whencode
)]
303 | Ast.InitGccName
(name
,eq
,ini
) ->
304 multibind [ident name
; string_mcode eq
; initialiser ini
]
305 | Ast.InitGccExt
(designators
,eq
,ini
) ->
307 ((List.map designator designators
) @
308 [string_mcode eq
; initialiser ini
])
309 | Ast.IComma
(cm
) -> string_mcode cm
310 | Ast.Idots
(dots
,whencode
) ->
311 bind
(string_mcode dots
) (get_option initialiser whencode
)
312 | Ast.OptIni
(i
) -> initialiser i
313 | Ast.UniqueIni
(i
) -> initialiser i
in
314 initfn all_functions
k i
316 and designator
= function
317 Ast.DesignatorField
(dot
,id
) -> bind
(string_mcode dot
) (ident id
)
318 | Ast.DesignatorIndex
(lb
,exp
,rb
) ->
319 bind
(string_mcode lb
) (bind
(expression exp
) (string_mcode rb
))
320 | Ast.DesignatorRange
(lb
,min
,dots
,max
,rb
) ->
322 [string_mcode lb
; expression min
; string_mcode dots
;
323 expression max
; string_mcode rb
]
325 and parameterTypeDef p
=
327 match Ast.unwrap p
with
328 Ast.VoidParam
(ty
) -> fullType ty
329 | Ast.Param
(ty
,Some id
) -> named_type ty id
330 | Ast.Param
(ty
,None
) -> fullType ty
331 | Ast.MetaParam
(name
,_
,_
) -> meta_mcode name
332 | Ast.MetaParamList
(name
,_
,_
,_
) -> meta_mcode name
333 | Ast.PComma
(cm
) -> string_mcode cm
334 | Ast.Pdots
(dots
) -> string_mcode dots
335 | Ast.Pcircles
(dots
) -> string_mcode dots
336 | Ast.OptParam
(param
) -> parameterTypeDef param
337 | Ast.UniqueParam
(param
) -> parameterTypeDef param
in
338 paramfn all_functions
k p
342 match Ast.unwrap re
with
343 Ast.FunHeader
(_
,_
,fi
,name
,lp
,params
,rp
) ->
345 ((List.map fninfo fi
) @
346 [ident name
;string_mcode lp
;parameter_dots params
;
348 | Ast.Decl
(_
,_
,decl
) -> declaration decl
349 | Ast.SeqStart
(brace
) -> string_mcode brace
350 | Ast.SeqEnd
(brace
) -> string_mcode brace
351 | Ast.ExprStatement
(exp
,sem
) ->
352 bind
(get_option expression exp
) (string_mcode sem
)
353 | Ast.IfHeader
(iff
,lp
,exp
,rp
) ->
354 multibind [string_mcode iff
; string_mcode lp
; expression exp
;
356 | Ast.Else
(els
) -> string_mcode els
357 | Ast.WhileHeader
(whl
,lp
,exp
,rp
) ->
358 multibind [string_mcode whl
; string_mcode lp
; expression exp
;
360 | Ast.DoHeader
(d
) -> string_mcode d
361 | Ast.WhileTail
(whl
,lp
,exp
,rp
,sem
) ->
362 multibind [string_mcode whl
; string_mcode lp
; expression exp
;
363 string_mcode rp
; string_mcode sem
]
364 | Ast.ForHeader
(fr
,lp
,e1
,sem1
,e2
,sem2
,e3
,rp
) ->
365 multibind [string_mcode fr
; string_mcode lp
;
366 get_option expression e1
; string_mcode sem1
;
367 get_option expression e2
; string_mcode sem2
;
368 get_option expression e3
; string_mcode rp
]
369 | Ast.IteratorHeader
(nm
,lp
,args
,rp
) ->
370 multibind [ident nm
; string_mcode lp
;
371 expression_dots args
; string_mcode rp
]
372 | Ast.SwitchHeader
(switch
,lp
,exp
,rp
) ->
373 multibind [string_mcode switch
; string_mcode lp
; expression exp
;
375 | Ast.Break
(br
,sem
) -> bind
(string_mcode br
) (string_mcode sem
)
376 | Ast.Continue
(cont
,sem
) -> bind
(string_mcode cont
) (string_mcode sem
)
377 | Ast.Label
(l
,dd
) -> bind
(ident l
) (string_mcode dd
)
378 | Ast.Goto
(goto
,l
,sem
) ->
379 bind
(string_mcode goto
) (bind
(ident l
) (string_mcode sem
))
380 | Ast.Return
(ret
,sem
) -> bind
(string_mcode ret
) (string_mcode sem
)
381 | Ast.ReturnExpr
(ret
,exp
,sem
) ->
382 multibind [string_mcode ret
; expression exp
; string_mcode sem
]
383 | Ast.MetaStmt
(name
,_
,_
,_
) -> meta_mcode name
384 | Ast.MetaStmtList
(name
,_
,_
) -> meta_mcode name
385 | Ast.MetaRuleElem
(name
,_
,_
) -> meta_mcode name
386 | Ast.Exp
(exp
) -> expression exp
387 | Ast.TopExp
(exp
) -> expression exp
388 | Ast.Ty
(ty
) -> fullType ty
389 | Ast.TopInit
(init
) -> initialiser init
390 | Ast.Include
(inc
,name
) -> bind
(string_mcode inc
) (inc_file_mcode name
)
391 | Ast.Undef
(def
,id
) ->
392 multibind [string_mcode def
; ident id
]
393 | Ast.DefineHeader
(def
,id
,params
) ->
394 multibind [string_mcode def
; ident id
; define_parameters params
]
395 | Ast.Default
(def
,colon
) -> bind
(string_mcode def
) (string_mcode colon
)
396 | Ast.Case
(case
,exp
,colon
) ->
397 multibind [string_mcode case
; expression exp
; string_mcode colon
]
398 | Ast.DisjRuleElem
(res
) -> multibind (List.map rule_elem res
) in
399 rulefn all_functions
k re
401 (* not parameterizable for now... *)
402 and define_parameters p
=
404 match Ast.unwrap p
with
405 Ast.NoParams
-> option_default
406 | Ast.DParams
(lp
,params
,rp
) ->
408 [string_mcode lp
; define_param_dots params
; string_mcode rp
] in
411 and define_param_dots d
=
413 match Ast.unwrap d
with
414 Ast.DOTS
(l
) | Ast.CIRCLES
(l
) | Ast.STARS
(l
) ->
415 multibind (List.map define_param l
) in
420 match Ast.unwrap p
with
421 Ast.DParam
(id
) -> ident id
422 | Ast.DPComma
(comma
) -> string_mcode comma
423 | Ast.DPdots
(d
) -> string_mcode d
424 | Ast.DPcircles
(c
) -> string_mcode c
425 | Ast.OptDParam
(dp
) -> define_param dp
426 | Ast.UniqueDParam
(dp
) -> define_param dp
in
429 (* discard the result, because the statement is assumed to be already
430 represented elsewhere in the code *)
431 and process_bef_aft s
=
432 match Ast.get_dots_bef_aft s
with
434 | Ast.DroppingBetweenDots
(stm
,ind
) -> let _ = statement stm
in ()
435 | Ast.AddingBetweenDots
(stm
,ind
) -> let _ = statement stm
in ()
440 match Ast.unwrap s
with
441 Ast.Seq
(lbrace
,body
,rbrace
) ->
442 multibind [rule_elem lbrace
;
443 statement_dots body
; rule_elem rbrace
]
444 | Ast.IfThen
(header
,branch
,_) ->
445 multibind [rule_elem header
; statement branch
]
446 | Ast.IfThenElse
(header
,branch1
,els
,branch2
,_) ->
447 multibind [rule_elem header
; statement branch1
; rule_elem els
;
449 | Ast.While
(header
,body
,_) ->
450 multibind [rule_elem header
; statement body
]
451 | Ast.Do
(header
,body
,tail
) ->
452 multibind [rule_elem header
; statement body
; rule_elem tail
]
453 | Ast.For
(header
,body
,_) -> multibind [rule_elem header
; statement body
]
454 | Ast.Iterator
(header
,body
,_) ->
455 multibind [rule_elem header
; statement body
]
456 | Ast.Switch
(header
,lb
,decls
,cases
,rb
) ->
457 multibind [rule_elem header
;rule_elem lb
;
458 statement_dots decls
;
459 multibind (List.map case_line cases
);
461 | Ast.Atomic
(re
) -> rule_elem re
462 | Ast.Disj
(stmt_dots_list
) ->
463 multibind (List.map statement_dots stmt_dots_list
)
464 | Ast.Nest
(starter
,stmt_dots
,ender
,whn
,_,_,_) ->
465 bind
(string_mcode starter
)
466 (bind
(statement_dots stmt_dots
)
467 (bind
(string_mcode ender
)
469 (List.map
(whencode statement_dots statement
) whn
))))
470 | Ast.FunDecl
(header
,lbrace
,body
,rbrace
) ->
471 multibind [rule_elem header
; rule_elem lbrace
;
472 statement_dots body
; rule_elem rbrace
]
473 | Ast.Define
(header
,body
) ->
474 bind
(rule_elem header
) (statement_dots body
)
475 | Ast.AsStmt
(stm
,asstm
) ->
476 bind
(statement stm
) (statement asstm
)
477 | Ast.Dots
(d
,whn
,_,_) | Ast.Circles
(d
,whn
,_,_) | Ast.Stars
(d
,whn
,_,_) ->
478 bind
(string_mcode d
)
479 (multibind (List.map
(whencode statement_dots statement
) whn
))
480 | Ast.OptStm
(stmt
) | Ast.UniqueStm
(stmt
) ->
482 stmtfn all_functions
k s
484 and fninfo
= function
485 Ast.FStorage
(stg
) -> storage_mcode stg
486 | Ast.FType
(ty
) -> fullType ty
487 | Ast.FInline
(inline
) -> string_mcode inline
488 | Ast.FAttr
(attr
) -> string_mcode attr
490 and whencode notfn alwaysfn
= function
491 Ast.WhenNot a
-> notfn a
492 | Ast.WhenAlways a
-> alwaysfn a
493 | Ast.WhenModifier
(_) -> option_default
494 | Ast.WhenNotTrue
(e
) -> rule_elem e
495 | Ast.WhenNotFalse
(e
) -> rule_elem e
499 match Ast.unwrap c
with
500 Ast.CaseLine
(header
,code
) ->
501 bind
(rule_elem header
) (statement_dots code
)
502 | Ast.OptCase
(case
) -> case_line case
in
503 casefn all_functions
k c
507 match Ast.unwrap t
with
508 Ast.FILEINFO
(old_file
,new_file
) ->
509 bind
(string_mcode old_file
) (string_mcode new_file
)
510 | Ast.NONDECL
(stmt
) -> statement stmt
511 | Ast.CODE
(stmt_dots
) -> statement_dots stmt_dots
512 | Ast.ERRORWORDS
(exps
) -> multibind (List.map expression exps
) in
513 topfn all_functions
k t
517 (*in many cases below, the thing is not even mcode, so we do nothing*)
518 Ast.FullTypeTag
(ft
) -> fullType ft
519 | Ast.BaseTypeTag
(bt
) -> option_default
520 | Ast.StructUnionTag
(su
) -> option_default
521 | Ast.SignTag
(sgn
) -> option_default
522 | Ast.IdentTag
(id
) -> ident id
523 | Ast.ExpressionTag
(exp
) -> expression exp
524 | Ast.ConstantTag
(cst
) -> option_default
525 | Ast.UnaryOpTag
(unop
) -> option_default
526 | Ast.AssignOpTag
(asgnop
) -> option_default
527 | Ast.FixOpTag
(fixop
) -> option_default
528 | Ast.BinaryOpTag
(binop
) -> option_default
529 | Ast.ArithOpTag
(arithop
) -> option_default
530 | Ast.LogicalOpTag
(logop
) -> option_default
531 | Ast.DeclarationTag
(decl
) -> declaration decl
532 | Ast.InitTag
(ini
) -> initialiser ini
533 | Ast.StorageTag
(stg
) -> option_default
534 | Ast.IncFileTag
(stg
) -> option_default
535 | Ast.Rule_elemTag
(rule
) -> rule_elem rule
536 | Ast.StatementTag
(rule
) -> statement rule
537 | Ast.CaseLineTag
(case
) -> case_line case
538 | Ast.ConstVolTag
(cv
) -> option_default
539 | Ast.Token
(tok
,info
) -> option_default
540 | Ast.Pragma
(str
) -> option_default
541 | Ast.Code
(cd
) -> top_level cd
542 | Ast.ExprDotsTag
(ed
) -> expression_dots ed
543 | Ast.ParamDotsTag
(pd
) -> parameter_dots pd
544 | Ast.StmtDotsTag
(sd
) -> statement_dots sd
545 | Ast.DeclDotsTag
(sd
) -> declaration_dots sd
546 | Ast.TypeCTag
(ty
) -> typeC ty
547 | Ast.ParamTag
(param
) -> parameterTypeDef param
548 | Ast.SgrepStartTag
(tok
) -> option_default
549 | Ast.SgrepEndTag
(tok
) -> option_default
in
550 anyfn all_functions
k a
553 {combiner_ident
= ident
;
554 combiner_expression
= expression
;
555 combiner_fullType
= fullType
;
556 combiner_typeC
= typeC
;
557 combiner_declaration
= declaration
;
558 combiner_initialiser
= initialiser
;
559 combiner_parameter
= parameterTypeDef
;
560 combiner_parameter_list
= parameter_dots
;
561 combiner_rule_elem
= rule_elem
;
562 combiner_statement
= statement
;
563 combiner_case_line
= case_line
;
564 combiner_top_level
= top_level
;
565 combiner_anything
= anything
;
566 combiner_expression_dots
= expression_dots
;
567 combiner_statement_dots
= statement_dots
;
568 combiner_declaration_dots
= declaration_dots
;
569 combiner_initialiser_dots
= initialiser_dots
} in
572 (* ---------------------------------------------------------------------- *)
574 type 'a inout
= 'a
-> 'a
(* for specifying the type of rebuilder *)
577 {rebuilder_ident
: Ast.ident inout
;
578 rebuilder_expression
: Ast.expression inout
;
579 rebuilder_fullType
: Ast.fullType inout
;
580 rebuilder_typeC
: Ast.typeC inout
;
581 rebuilder_declaration
: Ast.declaration inout
;
582 rebuilder_initialiser
: Ast.initialiser inout
;
583 rebuilder_parameter
: Ast.parameterTypeDef inout
;
584 rebuilder_parameter_list
: Ast.parameter_list inout
;
585 rebuilder_statement
: Ast.statement inout
;
586 rebuilder_case_line
: Ast.case_line inout
;
587 rebuilder_rule_elem
: Ast.rule_elem inout
;
588 rebuilder_top_level
: Ast.top_level inout
;
589 rebuilder_expression_dots
: Ast.expression
Ast.dots inout
;
590 rebuilder_statement_dots
: Ast.statement
Ast.dots inout
;
591 rebuilder_declaration_dots
: Ast.declaration
Ast.dots inout
;
592 rebuilder_initialiser_dots
: Ast.initialiser
Ast.dots inout
;
593 rebuilder_define_param_dots
: Ast.define_param
Ast.dots inout
;
594 rebuilder_define_param
: Ast.define_param inout
;
595 rebuilder_define_parameters
: Ast.define_parameters inout
;
596 rebuilder_anything
: Ast.anything inout
}
598 type 'mc rmcode
= 'mc
Ast.mcode inout
599 type 'cd rcode
= rebuilder
-> ('cd inout
) -> 'cd inout
603 meta_mcode string_mcode const_mcode assign_mcode fix_mcode unary_mcode
604 binary_mcode cv_mcode sign_mcode struct_mcode storage_mcode
606 expdotsfn paramdotsfn stmtdotsfn decldotsfn initdotsfn
607 identfn exprfn ftfn tyfn initfn paramfn declfn rulefn stmtfn casefn
609 let get_option f
= function
613 let dotsfn param default all_functions arg
=
616 (match Ast.unwrap d
with
617 Ast.DOTS
(l
) -> Ast.DOTS
(List.map default l
)
618 | Ast.CIRCLES
(l
) -> Ast.CIRCLES
(List.map default l
)
619 | Ast.STARS
(l
) -> Ast.STARS
(List.map default l
)) in
620 param all_functions
k arg
in
622 let rec expression_dots d
= dotsfn expdotsfn expression all_functions d
623 and parameter_dots d
= dotsfn paramdotsfn parameterTypeDef all_functions d
624 and statement_dots d
= dotsfn stmtdotsfn statement all_functions d
625 and declaration_dots d
= dotsfn decldotsfn declaration all_functions d
626 and initialiser_dots d
= dotsfn initdotsfn initialiser all_functions d
631 (match Ast.unwrap i
with
632 Ast.Id
(name
) -> Ast.Id
(string_mcode name
)
633 | Ast.MetaId
(name
,constraints
,keep
,inherited
) ->
634 Ast.MetaId
(meta_mcode name
,constraints
,keep
,inherited
)
635 | Ast.MetaFunc
(name
,constraints
,keep
,inherited
) ->
636 Ast.MetaFunc
(meta_mcode name
,constraints
,keep
,inherited
)
637 | Ast.MetaLocalFunc
(name
,constraints
,keep
,inherited
) ->
638 Ast.MetaLocalFunc
(meta_mcode name
,constraints
,keep
,inherited
)
639 | Ast.AsIdent
(id
,asid
) -> Ast.AsIdent
(ident id
,ident asid
)
640 | Ast.DisjId
(id_list
) -> Ast.DisjId
(List.map ident id_list
)
641 | Ast.OptIdent
(id
) -> Ast.OptIdent
(ident id
)
642 | Ast.UniqueIdent
(id
) -> Ast.UniqueIdent
(ident id
)) in
643 identfn all_functions
k i
648 (match Ast.unwrap e
with
649 Ast.Ident
(id
) -> Ast.Ident
(ident id
)
650 | Ast.Constant
(const
) -> Ast.Constant
(const_mcode const
)
651 | Ast.FunCall
(fn
,lp
,args
,rp
) ->
652 Ast.FunCall
(expression fn
, string_mcode lp
, expression_dots args
,
654 | Ast.Assignment
(left
,op
,right
,simple
) ->
655 Ast.Assignment
(expression left
, assign_mcode op
, expression right
,
657 | Ast.Sequence
(left
,op
,right
) ->
658 Ast.Sequence
(expression left
, string_mcode op
, expression right
)
659 | Ast.CondExpr
(exp1
,why
,exp2
,colon
,exp3
) ->
660 Ast.CondExpr
(expression exp1
, string_mcode why
,
661 get_option expression exp2
, string_mcode colon
,
663 | Ast.Postfix
(exp
,op
) -> Ast.Postfix
(expression exp
,fix_mcode op
)
664 | Ast.Infix
(exp
,op
) -> Ast.Infix
(expression exp
,fix_mcode op
)
665 | Ast.Unary
(exp
,op
) -> Ast.Unary
(expression exp
,unary_mcode op
)
666 | Ast.Binary
(left
,op
,right
) ->
667 Ast.Binary
(expression left
, binary_mcode op
, expression right
)
668 | Ast.Nested
(left
,op
,right
) ->
669 Ast.Nested
(expression left
, binary_mcode op
, expression right
)
670 | Ast.Paren
(lp
,exp
,rp
) ->
671 Ast.Paren
(string_mcode lp
, expression exp
, string_mcode rp
)
672 | Ast.ArrayAccess
(exp1
,lb
,exp2
,rb
) ->
673 Ast.ArrayAccess
(expression exp1
, string_mcode lb
, expression exp2
,
675 | Ast.RecordAccess
(exp
,pt
,field
) ->
676 Ast.RecordAccess
(expression exp
, string_mcode pt
, ident field
)
677 | Ast.RecordPtAccess
(exp
,ar
,field
) ->
678 Ast.RecordPtAccess
(expression exp
, string_mcode ar
, ident field
)
679 | Ast.Cast
(lp
,ty
,rp
,exp
) ->
680 Ast.Cast
(string_mcode lp
, fullType ty
, string_mcode rp
,
682 | Ast.SizeOfExpr
(szf
,exp
) ->
683 Ast.SizeOfExpr
(string_mcode szf
, expression exp
)
684 | Ast.SizeOfType
(szf
,lp
,ty
,rp
) ->
685 Ast.SizeOfType
(string_mcode szf
,string_mcode lp
, fullType ty
,
687 | Ast.TypeExp
(ty
) -> Ast.TypeExp
(fullType ty
)
688 | Ast.Constructor
(lp
,ty
,rp
,init
) ->
689 Ast.Constructor
(string_mcode lp
, fullType ty
, string_mcode rp
,
691 | Ast.MetaErr
(name
,constraints
,keep
,inherited
) ->
692 Ast.MetaErr
(meta_mcode name
,constraints
,keep
,inherited
)
693 | Ast.MetaExpr
(name
,constraints
,keep
,ty
,form
,inherited
) ->
694 Ast.MetaExpr
(meta_mcode name
,constraints
,keep
,ty
,form
,inherited
)
695 | Ast.MetaExprList
(name
,lenname_inh
,keep
,inherited
) ->
696 Ast.MetaExprList
(meta_mcode name
,lenname_inh
,keep
,inherited
)
697 | Ast.AsExpr
(exp
,asexp
) -> Ast.AsExpr
(expression exp
,expression asexp
)
698 | Ast.EComma
(cm
) -> Ast.EComma
(string_mcode cm
)
699 | Ast.DisjExpr
(exp_list
) -> Ast.DisjExpr
(List.map expression exp_list
)
700 | Ast.NestExpr
(starter
,expr_dots
,ender
,whencode
,multi
) ->
701 Ast.NestExpr
(string_mcode starter
,expression_dots expr_dots
,
703 get_option expression whencode
,multi
)
704 | Ast.Edots
(dots
,whencode
) ->
705 Ast.Edots
(string_mcode dots
,get_option expression whencode
)
706 | Ast.Ecircles
(dots
,whencode
) ->
707 Ast.Ecircles
(string_mcode dots
,get_option expression whencode
)
708 | Ast.Estars
(dots
,whencode
) ->
709 Ast.Estars
(string_mcode dots
,get_option expression whencode
)
710 | Ast.OptExp
(exp
) -> Ast.OptExp
(expression exp
)
711 | Ast.UniqueExp
(exp
) -> Ast.UniqueExp
(expression exp
)) in
712 exprfn all_functions
k e
717 (match Ast.unwrap ft
with
718 Ast.Type
(allminus
,cv
,ty
) ->
719 Ast.Type
(allminus
,get_option cv_mcode cv
, typeC ty
)
720 | Ast.AsType
(ty
,asty
) -> Ast.AsType
(fullType ty
,fullType asty
)
721 | Ast.DisjType
(types
) -> Ast.DisjType
(List.map fullType types
)
722 | Ast.OptType
(ty
) -> Ast.OptType
(fullType ty
)
723 | Ast.UniqueType
(ty
) -> Ast.UniqueType
(fullType ty
)) in
724 ftfn all_functions
k ft
729 (match Ast.unwrap ty
with
730 Ast.BaseType
(ty
,strings
) ->
731 Ast.BaseType
(ty
, List.map string_mcode strings
)
732 | Ast.SignedT
(sgn
,ty
) ->
733 Ast.SignedT
(sign_mcode sgn
,get_option typeC ty
)
734 | Ast.Pointer
(ty
,star
) ->
735 Ast.Pointer
(fullType ty
, string_mcode star
)
736 | Ast.FunctionPointer
(ty
,lp1
,star
,rp1
,lp2
,params
,rp2
) ->
737 Ast.FunctionPointer
(fullType ty
,string_mcode lp1
,string_mcode star
,
738 string_mcode rp1
,string_mcode lp2
,
739 parameter_dots params
,
741 | Ast.FunctionType
(allminus
,ty
,lp
,params
,rp
) ->
742 Ast.FunctionType
(allminus
,get_option fullType ty
,string_mcode lp
,
743 parameter_dots params
,string_mcode rp
)
744 | Ast.Array
(ty
,lb
,size
,rb
) ->
745 Ast.Array
(fullType ty
, string_mcode lb
,
746 get_option expression size
, string_mcode rb
)
747 | Ast.EnumName
(kind
,name
) ->
748 Ast.EnumName
(string_mcode kind
, get_option ident name
)
749 | Ast.EnumDef
(ty
,lb
,ids
,rb
) ->
750 Ast.EnumDef
(fullType ty
, string_mcode lb
, expression_dots ids
,
752 | Ast.StructUnionName
(kind
,name
) ->
753 Ast.StructUnionName
(struct_mcode kind
, get_option ident name
)
754 | Ast.StructUnionDef
(ty
,lb
,decls
,rb
) ->
755 Ast.StructUnionDef
(fullType ty
,
756 string_mcode lb
, declaration_dots decls
,
758 | Ast.TypeName
(name
) -> Ast.TypeName
(string_mcode name
)
759 | Ast.MetaType
(name
,keep
,inherited
) ->
760 Ast.MetaType
(meta_mcode name
,keep
,inherited
)) in
761 tyfn all_functions
k ty
766 (match Ast.unwrap d
with
767 Ast.MetaDecl
(name
,keep
,inherited
) ->
768 Ast.MetaDecl
(meta_mcode name
,keep
,inherited
)
769 | Ast.MetaField
(name
,keep
,inherited
) ->
770 Ast.MetaField
(meta_mcode name
,keep
,inherited
)
771 | Ast.MetaFieldList
(name
,lenname_inh
,keep
,inherited
) ->
772 Ast.MetaFieldList
(meta_mcode name
,lenname_inh
,keep
,inherited
)
773 | Ast.AsDecl
(decl
,asdecl
) ->
774 Ast.AsDecl
(declaration decl
,declaration asdecl
)
775 | Ast.Init
(stg
,ty
,id
,eq
,ini
,sem
) ->
776 Ast.Init
(get_option storage_mcode stg
, fullType ty
, ident id
,
777 string_mcode eq
, initialiser ini
, string_mcode sem
)
778 | Ast.UnInit
(stg
,ty
,id
,sem
) ->
779 Ast.UnInit
(get_option storage_mcode stg
, fullType ty
, ident id
,
781 | Ast.MacroDecl
(name
,lp
,args
,rp
,sem
) ->
782 Ast.MacroDecl
(ident name
, string_mcode lp
, expression_dots args
,
783 string_mcode rp
,string_mcode sem
)
784 | Ast.MacroDeclInit
(name
,lp
,args
,rp
,eq
,ini
,sem
) ->
786 (ident name
, string_mcode lp
, expression_dots args
,
787 string_mcode rp
,string_mcode eq
,initialiser ini
,
789 | Ast.TyDecl
(ty
,sem
) -> Ast.TyDecl
(fullType ty
, string_mcode sem
)
790 | Ast.Typedef
(stg
,ty
,id
,sem
) ->
791 Ast.Typedef
(string_mcode stg
, fullType ty
, typeC id
,
793 | Ast.DisjDecl
(decls
) -> Ast.DisjDecl
(List.map declaration decls
)
794 | Ast.Ddots
(dots
,whencode
) ->
795 Ast.Ddots
(string_mcode dots
, get_option declaration whencode
)
796 | Ast.OptDecl
(decl
) -> Ast.OptDecl
(declaration decl
)
797 | Ast.UniqueDecl
(decl
) -> Ast.UniqueDecl
(declaration decl
)) in
798 declfn all_functions
k d
803 (match Ast.unwrap i
with
804 Ast.MetaInit
(name
,keep
,inherited
) ->
805 Ast.MetaInit
(meta_mcode name
,keep
,inherited
)
806 | Ast.MetaInitList
(name
,lenname_inh
,keep
,inherited
) ->
807 Ast.MetaInitList
(meta_mcode name
,lenname_inh
,keep
,inherited
)
808 | Ast.AsInit
(ini
,asini
) ->
809 Ast.AsInit
(initialiser ini
,initialiser asini
)
810 | Ast.InitExpr
(exp
) -> Ast.InitExpr
(expression exp
)
811 | Ast.ArInitList
(lb
,initlist
,rb
) ->
812 Ast.ArInitList
(string_mcode lb
, initialiser_dots initlist
,
814 | Ast.StrInitList
(allminus
,lb
,initlist
,rb
,whencode
) ->
815 Ast.StrInitList
(allminus
,
816 string_mcode lb
, List.map initialiser initlist
,
817 string_mcode rb
, List.map initialiser whencode
)
818 | Ast.InitGccName
(name
,eq
,ini
) ->
819 Ast.InitGccName
(ident name
, string_mcode eq
, initialiser ini
)
820 | Ast.InitGccExt
(designators
,eq
,ini
) ->
822 (List.map designator designators
, string_mcode eq
,
824 | Ast.IComma
(cm
) -> Ast.IComma
(string_mcode cm
)
825 | Ast.Idots
(dots
,whencode
) ->
826 Ast.Idots
(string_mcode dots
,get_option initialiser whencode
)
827 | Ast.OptIni
(i
) -> Ast.OptIni
(initialiser i
)
828 | Ast.UniqueIni
(i
) -> Ast.UniqueIni
(initialiser i
)) in
829 initfn all_functions
k i
831 and designator
= function
832 Ast.DesignatorField
(dot
,id
) ->
833 Ast.DesignatorField
(string_mcode dot
,ident id
)
834 | Ast.DesignatorIndex
(lb
,exp
,rb
) ->
835 Ast.DesignatorIndex
(string_mcode lb
,expression exp
,string_mcode rb
)
836 | Ast.DesignatorRange
(lb
,min
,dots
,max
,rb
) ->
837 Ast.DesignatorRange
(string_mcode lb
,expression min
,string_mcode dots
,
838 expression max
,string_mcode rb
)
840 and parameterTypeDef p
=
843 (match Ast.unwrap p
with
844 Ast.VoidParam
(ty
) -> Ast.VoidParam
(fullType ty
)
845 | Ast.Param
(ty
,id
) -> Ast.Param
(fullType ty
, get_option ident id
)
846 | Ast.MetaParam
(name
,keep
,inherited
) ->
847 Ast.MetaParam
(meta_mcode name
,keep
,inherited
)
848 | Ast.MetaParamList
(name
,lenname_inh
,keep
,inherited
) ->
849 Ast.MetaParamList
(meta_mcode name
,lenname_inh
,keep
,inherited
)
850 | Ast.PComma
(cm
) -> Ast.PComma
(string_mcode cm
)
851 | Ast.Pdots
(dots
) -> Ast.Pdots
(string_mcode dots
)
852 | Ast.Pcircles
(dots
) -> Ast.Pcircles
(string_mcode dots
)
853 | Ast.OptParam
(param
) -> Ast.OptParam
(parameterTypeDef param
)
854 | Ast.UniqueParam
(param
) -> Ast.UniqueParam
(parameterTypeDef param
)) in
855 paramfn all_functions
k p
860 (match Ast.unwrap re
with
861 Ast.FunHeader
(bef
,allminus
,fi
,name
,lp
,params
,rp
) ->
862 Ast.FunHeader
(bef
,allminus
,List.map fninfo fi
,ident name
,
863 string_mcode lp
, parameter_dots params
,
865 | Ast.Decl
(bef
,allminus
,decl
) ->
866 Ast.Decl
(bef
,allminus
,declaration decl
)
867 | Ast.SeqStart
(brace
) -> Ast.SeqStart
(string_mcode brace
)
868 | Ast.SeqEnd
(brace
) -> Ast.SeqEnd
(string_mcode brace
)
869 | Ast.ExprStatement
(exp
,sem
) ->
870 Ast.ExprStatement
(get_option expression exp
, string_mcode sem
)
871 | Ast.IfHeader
(iff
,lp
,exp
,rp
) ->
872 Ast.IfHeader
(string_mcode iff
, string_mcode lp
, expression exp
,
874 | Ast.Else
(els
) -> Ast.Else
(string_mcode els
)
875 | Ast.WhileHeader
(whl
,lp
,exp
,rp
) ->
876 Ast.WhileHeader
(string_mcode whl
, string_mcode lp
, expression exp
,
878 | Ast.DoHeader
(d
) -> Ast.DoHeader
(string_mcode d
)
879 | Ast.WhileTail
(whl
,lp
,exp
,rp
,sem
) ->
880 Ast.WhileTail
(string_mcode whl
, string_mcode lp
, expression exp
,
881 string_mcode rp
, string_mcode sem
)
882 | Ast.ForHeader
(fr
,lp
,e1
,sem1
,e2
,sem2
,e3
,rp
) ->
883 Ast.ForHeader
(string_mcode fr
, string_mcode lp
,
884 get_option expression e1
, string_mcode sem1
,
885 get_option expression e2
, string_mcode sem2
,
886 get_option expression e3
, string_mcode rp
)
887 | Ast.IteratorHeader
(whl
,lp
,args
,rp
) ->
888 Ast.IteratorHeader
(ident whl
, string_mcode lp
,
889 expression_dots args
, string_mcode rp
)
890 | Ast.SwitchHeader
(switch
,lp
,exp
,rp
) ->
891 Ast.SwitchHeader
(string_mcode switch
, string_mcode lp
,
892 expression exp
, string_mcode rp
)
893 | Ast.Break
(br
,sem
) ->
894 Ast.Break
(string_mcode br
, string_mcode sem
)
895 | Ast.Continue
(cont
,sem
) ->
896 Ast.Continue
(string_mcode cont
, string_mcode sem
)
897 | Ast.Label
(l
,dd
) -> Ast.Label
(ident l
, string_mcode dd
)
898 | Ast.Goto
(goto
,l
,sem
) ->
899 Ast.Goto
(string_mcode goto
,ident l
,string_mcode sem
)
900 | Ast.Return
(ret
,sem
) ->
901 Ast.Return
(string_mcode ret
, string_mcode sem
)
902 | Ast.ReturnExpr
(ret
,exp
,sem
) ->
903 Ast.ReturnExpr
(string_mcode ret
, expression exp
, string_mcode sem
)
904 | Ast.MetaStmt
(name
,keep
,seqible
,inherited
) ->
905 Ast.MetaStmt
(meta_mcode name
,keep
,seqible
,inherited
)
906 | Ast.MetaStmtList
(name
,keep
,inherited
) ->
907 Ast.MetaStmtList
(meta_mcode name
,keep
,inherited
)
908 | Ast.MetaRuleElem
(name
,keep
,inherited
) ->
909 Ast.MetaRuleElem
(meta_mcode name
,keep
,inherited
)
910 | Ast.Exp
(exp
) -> Ast.Exp
(expression exp
)
911 | Ast.TopExp
(exp
) -> Ast.TopExp
(expression exp
)
912 | Ast.Ty
(ty
) -> Ast.Ty
(fullType ty
)
913 | Ast.TopInit
(init
) -> Ast.TopInit
(initialiser init
)
914 | Ast.Include
(inc
,name
) ->
915 Ast.Include
(string_mcode inc
,inc_file_mcode name
)
916 | Ast.Undef
(def
,id
) ->
917 Ast.Undef
(string_mcode def
,ident id
)
918 | Ast.DefineHeader
(def
,id
,params
) ->
919 Ast.DefineHeader
(string_mcode def
,ident id
,
920 define_parameters params
)
921 | Ast.Default
(def
,colon
) ->
922 Ast.Default
(string_mcode def
,string_mcode colon
)
923 | Ast.Case
(case
,exp
,colon
) ->
924 Ast.Case
(string_mcode case
,expression exp
,string_mcode colon
)
925 | Ast.DisjRuleElem
(res
) -> Ast.DisjRuleElem
(List.map rule_elem res
)) in
926 rulefn all_functions
k re
928 (* not parameterizable for now... *)
929 and define_parameters p
=
932 (match Ast.unwrap p
with
933 Ast.NoParams
-> Ast.NoParams
934 | Ast.DParams
(lp
,params
,rp
) ->
935 Ast.DParams
(string_mcode lp
,define_param_dots params
,
939 and define_param_dots d
=
942 (match Ast.unwrap d
with
943 Ast.DOTS
(l
) -> Ast.DOTS
(List.map define_param l
)
944 | Ast.CIRCLES
(l
) -> Ast.CIRCLES
(List.map define_param l
)
945 | Ast.STARS
(l
) -> Ast.STARS
(List.map define_param l
)) in
951 (match Ast.unwrap p
with
952 Ast.DParam
(id
) -> Ast.DParam
(ident id
)
953 | Ast.DPComma
(comma
) -> Ast.DPComma
(string_mcode comma
)
954 | Ast.DPdots
(d
) -> Ast.DPdots
(string_mcode d
)
955 | Ast.DPcircles
(c
) -> Ast.DPcircles
(string_mcode c
)
956 | Ast.OptDParam
(dp
) -> Ast.OptDParam
(define_param dp
)
957 | Ast.UniqueDParam
(dp
) -> Ast.UniqueDParam
(define_param dp
)) in
960 and process_bef_aft s
=
962 (match Ast.get_dots_bef_aft s
with
963 Ast.NoDots
-> Ast.NoDots
964 | Ast.DroppingBetweenDots
(stm
,ind
) ->
965 Ast.DroppingBetweenDots
(statement stm
,ind
)
966 | Ast.AddingBetweenDots
(stm
,ind
) ->
967 Ast.AddingBetweenDots
(statement stm
,ind
))
973 (match Ast.unwrap s
with
974 Ast.Seq
(lbrace
,body
,rbrace
) ->
975 Ast.Seq
(rule_elem lbrace
,
976 statement_dots body
, rule_elem rbrace
)
977 | Ast.IfThen
(header
,branch
,aft
) ->
978 Ast.IfThen
(rule_elem header
, statement branch
,aft
)
979 | Ast.IfThenElse
(header
,branch1
,els
,branch2
,aft
) ->
980 Ast.IfThenElse
(rule_elem header
, statement branch1
, rule_elem els
,
981 statement branch2
, aft
)
982 | Ast.While
(header
,body
,aft
) ->
983 Ast.While
(rule_elem header
, statement body
, aft
)
984 | Ast.Do
(header
,body
,tail
) ->
985 Ast.Do
(rule_elem header
, statement body
, rule_elem tail
)
986 | Ast.For
(header
,body
,aft
) ->
987 Ast.For
(rule_elem header
, statement body
, aft
)
988 | Ast.Iterator
(header
,body
,aft
) ->
989 Ast.Iterator
(rule_elem header
, statement body
, aft
)
990 | Ast.Switch
(header
,lb
,decls
,cases
,rb
) ->
991 Ast.Switch
(rule_elem header
,rule_elem lb
,
992 statement_dots decls
,
993 List.map case_line cases
,rule_elem rb
)
994 | Ast.Atomic
(re
) -> Ast.Atomic
(rule_elem re
)
995 | Ast.Disj
(stmt_dots_list
) ->
996 Ast.Disj
(List.map statement_dots stmt_dots_list
)
997 | Ast.Nest
(starter
,stmt_dots
,ender
,whn
,multi
,bef
,aft
) ->
998 Ast.Nest
(string_mcode starter
,statement_dots stmt_dots
,
1000 List.map
(whencode statement_dots statement
) whn
,
1002 | Ast.FunDecl
(header
,lbrace
,body
,rbrace
) ->
1003 Ast.FunDecl
(rule_elem header
,rule_elem lbrace
,
1004 statement_dots body
, rule_elem rbrace
)
1005 | Ast.Define
(header
,body
) ->
1006 Ast.Define
(rule_elem header
,statement_dots body
)
1007 | Ast.AsStmt
(stm
,asstm
) -> Ast.AsStmt
(statement stm
,statement asstm
)
1008 | Ast.Dots
(d
,whn
,bef
,aft
) ->
1009 Ast.Dots
(string_mcode d
,
1010 List.map
(whencode statement_dots statement
) whn
,bef
,aft
)
1011 | Ast.Circles
(d
,whn
,bef
,aft
) ->
1012 Ast.Circles
(string_mcode d
,
1013 List.map
(whencode statement_dots statement
) whn
,
1015 | Ast.Stars
(d
,whn
,bef
,aft
) ->
1016 Ast.Stars
(string_mcode d
,
1017 List.map
(whencode statement_dots statement
) whn
,bef
,aft
)
1018 | Ast.OptStm
(stmt
) -> Ast.OptStm
(statement stmt
)
1019 | Ast.UniqueStm
(stmt
) -> Ast.UniqueStm
(statement stmt
)) in
1020 let s = stmtfn all_functions
k s in
1021 (* better to do this after, in case there is an equality test on the whole
1022 statement, eg in free_vars. equality test would require that this
1023 subterm not already be changed *)
1026 and fninfo
= function
1027 Ast.FStorage
(stg
) -> Ast.FStorage
(storage_mcode stg
)
1028 | Ast.FType
(ty
) -> Ast.FType
(fullType ty
)
1029 | Ast.FInline
(inline
) -> Ast.FInline
(string_mcode inline
)
1030 | Ast.FAttr
(attr
) -> Ast.FAttr
(string_mcode attr
)
1032 and whencode notfn alwaysfn
= function
1033 Ast.WhenNot a
-> Ast.WhenNot
(notfn a
)
1034 | Ast.WhenAlways a
-> Ast.WhenAlways
(alwaysfn a
)
1035 | Ast.WhenModifier
(x
) -> Ast.WhenModifier
(x
)
1036 | Ast.WhenNotTrue
(e
) -> Ast.WhenNotTrue
(rule_elem e
)
1037 | Ast.WhenNotFalse
(e
) -> Ast.WhenNotFalse
(rule_elem e
)
1042 (match Ast.unwrap c
with
1043 Ast.CaseLine
(header
,code
) ->
1044 Ast.CaseLine
(rule_elem header
,statement_dots code
)
1045 | Ast.OptCase
(case
) -> Ast.OptCase
(case_line case
)) in
1046 casefn all_functions
k c
1051 (match Ast.unwrap t
with
1052 Ast.FILEINFO
(old_file
,new_file
) ->
1053 Ast.FILEINFO
(string_mcode old_file
, string_mcode new_file
)
1054 | Ast.NONDECL
(stmt
) -> Ast.NONDECL
(statement stmt
)
1055 | Ast.CODE
(stmt_dots
) -> Ast.CODE
(statement_dots stmt_dots
)
1056 | Ast.ERRORWORDS
(exps
) -> Ast.ERRORWORDS
(List.map expression exps
)) in
1057 topfn all_functions
k t
1061 (*in many cases below, the thing is not even mcode, so we do nothing*)
1062 Ast.FullTypeTag
(ft
) -> Ast.FullTypeTag
(fullType ft
)
1063 | Ast.BaseTypeTag
(bt
) as x
-> x
1064 | Ast.StructUnionTag
(su
) as x
-> x
1065 | Ast.SignTag
(sgn
) as x
-> x
1066 | Ast.IdentTag
(id
) -> Ast.IdentTag
(ident id
)
1067 | Ast.ExpressionTag
(exp
) -> Ast.ExpressionTag
(expression exp
)
1068 | Ast.ConstantTag
(cst
) as x
-> x
1069 | Ast.UnaryOpTag
(unop
) as x
-> x
1070 | Ast.AssignOpTag
(asgnop
) as x
-> x
1071 | Ast.FixOpTag
(fixop
) as x
-> x
1072 | Ast.BinaryOpTag
(binop
) as x
-> x
1073 | Ast.ArithOpTag
(arithop
) as x
-> x
1074 | Ast.LogicalOpTag
(logop
) as x
-> x
1075 | Ast.InitTag
(decl
) -> Ast.InitTag
(initialiser decl
)
1076 | Ast.DeclarationTag
(decl
) -> Ast.DeclarationTag
(declaration decl
)
1077 | Ast.StorageTag
(stg
) as x
-> x
1078 | Ast.IncFileTag
(stg
) as x
-> x
1079 | Ast.Rule_elemTag
(rule
) -> Ast.Rule_elemTag
(rule_elem rule
)
1080 | Ast.StatementTag
(rule
) -> Ast.StatementTag
(statement rule
)
1081 | Ast.CaseLineTag
(case
) -> Ast.CaseLineTag
(case_line case
)
1082 | Ast.ConstVolTag
(cv
) as x
-> x
1083 | Ast.Token
(tok
,info
) as x
-> x
1084 | Ast.Pragma
(str
) as x
-> x
1085 | Ast.Code
(cd
) -> Ast.Code
(top_level cd
)
1086 | Ast.ExprDotsTag
(ed
) -> Ast.ExprDotsTag
(expression_dots ed
)
1087 | Ast.ParamDotsTag
(pd
) -> Ast.ParamDotsTag
(parameter_dots pd
)
1088 | Ast.StmtDotsTag
(sd
) -> Ast.StmtDotsTag
(statement_dots sd
)
1089 | Ast.DeclDotsTag
(sd
) -> Ast.DeclDotsTag
(declaration_dots sd
)
1090 | Ast.TypeCTag
(ty
) -> Ast.TypeCTag
(typeC ty
)
1091 | Ast.ParamTag
(param
) -> Ast.ParamTag
(parameterTypeDef param
)
1092 | Ast.SgrepStartTag
(tok
) as x
-> x
1093 | Ast.SgrepEndTag
(tok
) as x
-> x
in
1094 anyfn all_functions
k a
1097 {rebuilder_ident
= ident
;
1098 rebuilder_expression
= expression
;
1099 rebuilder_fullType
= fullType
;
1100 rebuilder_typeC
= typeC
;
1101 rebuilder_declaration
= declaration
;
1102 rebuilder_initialiser
= initialiser
;
1103 rebuilder_parameter
= parameterTypeDef
;
1104 rebuilder_parameter_list
= parameter_dots
;
1105 rebuilder_rule_elem
= rule_elem
;
1106 rebuilder_statement
= statement
;
1107 rebuilder_case_line
= case_line
;
1108 rebuilder_top_level
= top_level
;
1109 rebuilder_expression_dots
= expression_dots;
1110 rebuilder_statement_dots
= statement_dots
;
1111 rebuilder_declaration_dots
= declaration_dots
;
1112 rebuilder_initialiser_dots
= initialiser_dots
;
1113 rebuilder_define_param_dots
= define_param_dots
;
1114 rebuilder_define_param
= define_param
;
1115 rebuilder_define_parameters
= define_parameters
;
1116 rebuilder_anything
= anything
} in