2 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
3 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
4 * This file is part of Coccinelle.
6 * Coccinelle is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, according to version 2 of the License.
10 * Coccinelle is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
18 * The authors reserve the right to distribute this or future versions of
19 * Coccinelle under other licenses.
23 (* Arities matter for the minus slice, but not for the plus slice. *)
25 (* ? only allowed on rule_elems, and on subterms if the context is ? also. *)
27 module Ast0
= Ast0_cocci
28 module Ast
= Ast_cocci
30 (* --------------------------------------------------------------------- *)
32 let warning s
= Printf.printf
"warning: %s\n" s
36 (Printf.sprintf
"cocci line %d: %s"
37 ((Ast0.get_info w
).Ast0.pos_info
.Ast0.line_start
)
40 let make_opt_unique optfn uniquefn info tgt arity term
=
41 let term = Ast0.rewrap info
term in
44 else (* tgt must be NONE *)
46 Ast0.OPT
-> Ast0.copywrap info
(optfn
term)
47 | Ast0.UNIQUE
-> Ast0.copywrap info
(uniquefn
term)
48 | Ast0.NONE
-> failwith
"tgt must be NONE"
50 let all_same opt_allowed tgt line arities
=
54 (match List.hd arities
with
55 Ast0.OPT
when not opt_allowed
->
56 failwith
"opt only allowed for the elements of a statement list"
59 if not
(List.for_all
(function x
-> x
= tgt) arities
)
60 then warning (Printf.sprintf
"incompatible arity found on line %d" line
);
63 let get_option fn
= function
65 | Some x
-> Some
(fn x
)
67 let anyopt l fn
= List.exists
(function w
-> fn
(Ast0.unwrap w
)) l
70 let rec loop = function
73 match fn
(Ast0.unwrap x
) with
74 Some x
-> x
:: (loop xs
)
77 if List.length
res = List.length l
then Some
res else None
79 (* --------------------------------------------------------------------- *)
80 (* --------------------------------------------------------------------- *)
83 let mcode2line (_
,_
,info
,_
,_
,_
) = info
.Ast0.pos_info
.Ast0.line_start
84 let mcode2arity (_
,arity
,_
,_
,_
,_
) = arity
86 let mcode x
= x
(* nothing to do ... *)
88 (* --------------------------------------------------------------------- *)
93 (match Ast0.unwrap d
with
94 Ast0.DOTS
(x
) -> Ast0.DOTS
(List.map fn x
)
95 | Ast0.CIRCLES
(x
) -> Ast0.CIRCLES
(List.map fn x
)
96 | Ast0.STARS
(x
) -> Ast0.STARS
(List.map fn x
))
102 match Ast0.unwrap x
with
103 Ast0.Circles
(_
,_
) | Ast0.Stars
(_
,_
) -> true
110 match Ast0.unwrap x
with
111 Ast0.Dots
(_
,_
) | Ast0.Stars
(_
,_
) -> true
118 match Ast0.unwrap x
with
119 Ast0.Dots
(_
,_
) | Ast0.Circles
(_
,_
) -> true
123 let concat_dots fn d
=
125 (match Ast0.unwrap d
with
127 let l = List.map fn x
in
130 else fail d
"inconsistent dots usage"
132 let l = List.map fn x
in
135 else fail d
"inconsistent dots usage"
137 let l = List.map fn x
in
140 else fail d
"inconsistent dots usage")
142 let flat_concat_dots fn d
=
143 match Ast0.unwrap d
with
144 Ast0.DOTS
(x
) -> List.map fn x
145 | Ast0.CIRCLES
(x
) -> List.map fn x
146 | Ast0.STARS
(x
) -> List.map fn x
148 (* --------------------------------------------------------------------- *)
153 (function x
-> Ast0.OptIdent x
)
154 (function x
-> Ast0.UniqueIdent x
)
156 let ident opt_allowed
tgt i
=
157 match Ast0.unwrap i
with
160 all_same opt_allowed
tgt (mcode2line name
)
161 [mcode2arity name
] in
162 let name = mcode name in
163 make_id i
tgt arity (Ast0.Id
(name))
164 | Ast0.MetaId
(name,constraints
,pure
) ->
166 all_same opt_allowed
tgt (mcode2line name)
167 [mcode2arity name] in
168 let name = mcode name in
169 make_id i
tgt arity (Ast0.MetaId
(name,constraints
,pure
))
170 | Ast0.MetaFunc
(name,constraints
,pure
) ->
172 all_same opt_allowed
tgt (mcode2line name)
173 [mcode2arity name] in
174 let name = mcode name in
175 make_id i
tgt arity (Ast0.MetaFunc
(name,constraints
,pure
))
176 | Ast0.MetaLocalFunc
(name,constraints
,pure
) ->
178 all_same opt_allowed
tgt (mcode2line name)
179 [mcode2arity name] in
180 let name = mcode name in
181 make_id i
tgt arity (Ast0.MetaLocalFunc
(name,constraints
,pure
))
182 | Ast0.OptIdent
(_
) | Ast0.UniqueIdent
(_
) ->
183 failwith
"unexpected code"
185 (* --------------------------------------------------------------------- *)
190 (function x
-> Ast0.OptExp x
)
191 (function x
-> Ast0.UniqueExp x
)
193 let rec top_expression opt_allowed
tgt expr
=
194 let exp_same = all_same opt_allowed
tgt in
195 match Ast0.unwrap expr
with
197 let new_id = ident opt_allowed
tgt id
in
199 (match Ast0.unwrap
new_id with
201 Ast0.OptExp
(Ast0.rewrap expr
(Ast0.Ident
(id
)))
202 | Ast0.UniqueIdent
(id
) ->
203 Ast0.UniqueExp
(Ast0.rewrap expr
(Ast0.Ident
(id
)))
204 | _
-> Ast0.Ident
(new_id))
205 | Ast0.Constant
(const
) ->
206 let arity = exp_same (mcode2line const
) [mcode2arity const
] in
207 let const = mcode const in
208 make_exp expr
tgt arity (Ast0.Constant
(const))
209 | Ast0.FunCall
(fn
,lp
,args
,rp
) ->
210 let arity = exp_same (mcode2line lp
) [mcode2arity lp
;mcode2arity rp
] in
211 let fn = expression
arity fn in
213 let args = dots (expression
arity) args in
215 make_exp expr
tgt arity (Ast0.FunCall
(fn,lp,args,rp))
216 | Ast0.Assignment
(left
,op
,right
,simple
) ->
217 let arity = exp_same (mcode2line op
) [mcode2arity op
] in
218 let left = expression
arity left in
220 let right = expression
arity right in
221 make_exp expr
tgt arity (Ast0.Assignment
(left,op,right,simple
))
222 | Ast0.CondExpr
(exp1
,why
,exp2
,colon
,exp3
) ->
224 exp_same (mcode2line why
) [mcode2arity why
; mcode2arity colon
] in
225 let exp1 = expression
arity exp1 in
226 let why = mcode why in
227 let exp2 = get_option (expression
arity) exp2 in
228 let colon = mcode colon in
229 let exp3 = expression
arity exp3 in
230 make_exp expr
tgt arity (Ast0.CondExpr
(exp1,why,exp2,colon,exp3))
231 | Ast0.Postfix
(exp
,op) ->
232 let arity = exp_same (mcode2line op) [mcode2arity op] in
233 let exp = expression
arity exp in
235 make_exp expr
tgt arity (Ast0.Postfix
(exp,op))
236 | Ast0.Infix
(exp,op) ->
237 let arity = exp_same (mcode2line op) [mcode2arity op] in
238 let exp = expression
arity exp in
240 make_exp expr
tgt arity (Ast0.Infix
(exp,op))
241 | Ast0.Unary
(exp,op) ->
242 let arity = exp_same (mcode2line op) [mcode2arity op] in
243 let exp = expression
arity exp in
245 make_exp expr
tgt arity (Ast0.Unary
(exp,op))
246 | Ast0.Binary
(left,op,right) ->
247 let arity = exp_same (mcode2line op) [mcode2arity op] in
248 let left = expression
arity left in
250 let right = expression
arity right in
251 make_exp expr
tgt arity (Ast0.Binary
(left,op,right))
252 | Ast0.Nested
(left,op,right) -> failwith
"nested in arity not possible"
253 | Ast0.Paren
(lp,exp,rp) ->
254 let arity = exp_same (mcode2line lp) [mcode2arity lp;mcode2arity rp] in
256 let exp = expression
arity exp in
258 make_exp expr
tgt arity (Ast0.Paren
(lp,exp,rp))
259 | Ast0.ArrayAccess
(exp1,lb
,exp2,rb
) ->
260 let arity = exp_same (mcode2line lb
) [mcode2arity lb
; mcode2arity rb
] in
261 let exp1 = expression
arity exp1 in
263 let exp2 = expression
arity exp2 in
265 make_exp expr
tgt arity (Ast0.ArrayAccess
(exp1,lb,exp2,rb))
266 | Ast0.RecordAccess
(exp,pt
,field
) ->
267 let arity = exp_same (mcode2line pt
) [mcode2arity pt
] in
268 let exp = expression
arity exp in
270 let field = ident false arity field in
271 make_exp expr
tgt arity (Ast0.RecordAccess
(exp,pt,field))
272 | Ast0.RecordPtAccess
(exp,ar
,field) ->
273 let arity = exp_same (mcode2line ar
) [mcode2arity ar
] in
274 let exp = expression
arity exp in
276 let field = ident false arity field in
277 make_exp expr
tgt arity (Ast0.RecordPtAccess
(exp,ar,field))
278 | Ast0.Cast
(lp,ty
,rp,exp) ->
279 let arity = exp_same (mcode2line lp) [mcode2arity lp;mcode2arity rp] in
281 let ty = typeC
arity ty in
283 let exp = expression
arity exp in
284 make_exp expr
tgt arity (Ast0.Cast
(lp,ty,rp,exp))
285 | Ast0.SizeOfExpr
(szf
,exp) ->
286 let arity = exp_same (mcode2line szf
) [mcode2arity szf
] in
287 let szf = mcode szf in
288 let exp = expression
arity exp in
289 make_exp expr
tgt arity (Ast0.SizeOfExpr
(szf,exp))
290 | Ast0.SizeOfType
(szf,lp,ty,rp) ->
292 exp_same (mcode2line szf) (List.map
mcode2arity [szf;lp;rp]) in
293 let szf = mcode szf in
295 let ty = typeC
arity ty in
297 make_exp expr
tgt arity (Ast0.SizeOfType
(szf,lp,ty,rp))
298 | Ast0.TypeExp
(ty) -> Ast0.rewrap expr
(Ast0.TypeExp
(typeC
tgt ty))
299 | Ast0.MetaErr
(name,constraints
,pure
) ->
300 let arity = exp_same (mcode2line name) [mcode2arity name] in
301 let name = mcode name in
302 make_exp expr
tgt arity (Ast0.MetaErr
(name,constraints
,pure
))
303 | Ast0.MetaExpr
(name,constraints
,ty,form
,pure
) ->
304 let arity = exp_same (mcode2line name) [mcode2arity name] in
305 let name = mcode name in
306 make_exp expr
tgt arity (Ast0.MetaExpr
(name,constraints
,ty,form
,pure
))
307 | Ast0.MetaExprList
(name,lenname
,pure
) ->
308 let arity = exp_same (mcode2line name) [mcode2arity name] in
309 let name = mcode name in
310 make_exp expr
tgt arity (Ast0.MetaExprList
(name,lenname
,pure
))
312 let arity = exp_same (mcode2line cm
) [mcode2arity cm
] in
314 make_exp expr
tgt arity (Ast0.EComma
(cm))
315 | Ast0.DisjExpr
(starter
,exps
,mids
,ender
) ->
316 let exps = List.map
(top_expression opt_allowed
tgt) exps in
317 (match List.rev
exps with
319 if anyopt xs
(function Ast0.OptExp
(_
) -> true | _
-> false)
320 then fail expr
"opt only allowed in the last disjunct"
322 Ast0.rewrap expr
(Ast0.DisjExpr
(starter
,exps,mids
,ender
))
323 | Ast0.NestExpr
(starter
,exp_dots
,ender
,whencode
,multi
) ->
325 Ast0.NestExpr
(starter
,
326 dots (top_expression true Ast0.NONE
) exp_dots
,
327 ender
,whencode
,multi
) in
329 | Ast0.Edots
(dots,whencode
) ->
330 let arity = exp_same (mcode2line dots) [mcode2arity dots] in
331 let dots = mcode dots in
332 let whencode = get_option (expression
Ast0.NONE
) whencode in
333 make_exp expr
tgt arity (Ast0.Edots
(dots,whencode))
334 | Ast0.Ecircles
(dots,whencode) ->
335 let arity = exp_same (mcode2line dots) [mcode2arity dots] in
336 let dots = mcode dots in
337 let whencode = get_option (expression
Ast0.NONE
) whencode in
338 make_exp expr
tgt arity (Ast0.Ecircles
(dots,whencode))
339 | Ast0.Estars
(dots,whencode) ->
340 let arity = exp_same (mcode2line dots) [mcode2arity dots] in
341 let dots = mcode dots in
342 let whencode = get_option (expression
Ast0.NONE
) whencode in
343 make_exp expr
tgt arity (Ast0.Estars
(dots,whencode))
344 (* why does optexp exist???? *)
345 | Ast0.OptExp
(_
) | Ast0.UniqueExp
(_
) ->
346 failwith
"unexpected code"
348 and expression
tgt exp = top_expression false tgt exp
350 (* --------------------------------------------------------------------- *)
355 (function x
-> Ast0.OptType x
)
356 (function x
-> Ast0.UniqueType x
)
358 and top_typeC
tgt opt_allowed typ
=
359 match Ast0.unwrap typ
with
360 Ast0.ConstVol
(cv
,ty) ->
361 let arity = all_same opt_allowed
tgt (mcode2line cv
)
364 let ty = typeC
arity ty in
365 make_typeC typ
tgt arity (Ast0.ConstVol
(cv,ty))
366 | Ast0.BaseType
(ty,strings
) ->
368 all_same opt_allowed
tgt (mcode2line (List.hd strings
))
369 (List.map
mcode2arity strings
) in
370 let strings = List.map
mcode strings in
371 make_typeC typ
tgt arity (Ast0.BaseType
(ty,strings))
372 | Ast0.Signed
(sign
,ty) ->
374 all_same opt_allowed
tgt (mcode2line sign
) [mcode2arity sign
] in
375 let sign = mcode sign in
376 let ty = get_option (typeC
arity) ty in
377 make_typeC typ
tgt arity (Ast0.Signed
(sign,ty))
378 | Ast0.Pointer
(ty,star
) ->
380 all_same opt_allowed
tgt (mcode2line star
) [mcode2arity star
] in
381 let ty = typeC
arity ty in
382 let star = mcode star in
383 make_typeC typ
tgt arity (Ast0.Pointer
(ty,star))
384 | Ast0.FunctionPointer
(ty,lp1
,star,rp1
,lp2
,params
,rp2
) ->
386 all_same opt_allowed
tgt (mcode2line lp1
)
387 (List.map
mcode2arity [lp1
;star;rp1
;lp2
;rp2
]) in
388 let ty = typeC
arity ty in
389 let params = parameter_list
tgt params in
390 make_typeC typ
tgt arity
391 (Ast0.FunctionPointer
(ty,lp1
,star,rp1
,lp2
,params,rp2
))
392 | Ast0.FunctionType
(ty,lp1
,params,rp1
) ->
394 all_same opt_allowed
tgt (mcode2line lp1
)
395 (List.map
mcode2arity [lp1
;rp1
]) in
396 let ty = get_option (typeC
arity) ty in
397 let params = parameter_list
tgt params in
398 make_typeC typ
tgt arity (Ast0.FunctionType
(ty,lp1
,params,rp1
))
399 | Ast0.Array
(ty,lb,size
,rb) ->
401 all_same opt_allowed
tgt (mcode2line lb)
402 [mcode2arity lb;mcode2arity rb] in
403 let ty = typeC
arity ty in
405 let size = get_option (expression
arity) size in
407 make_typeC typ
tgt arity (Ast0.Array
(ty,lb,size,rb))
408 | Ast0.EnumName
(kind
,name) ->
410 all_same opt_allowed
tgt (mcode2line kind
) [mcode2arity kind
] in
411 let kind = mcode kind in
412 let name = ident false arity name in
413 make_typeC typ
tgt arity (Ast0.EnumName
(kind,name))
414 | Ast0.StructUnionName
(kind,name) ->
416 all_same opt_allowed
tgt (mcode2line kind)
417 [mcode2arity kind] in
418 let kind = mcode kind in
419 let name = get_option (ident false arity) name in
420 make_typeC typ
tgt arity (Ast0.StructUnionName
(kind,name))
421 | Ast0.StructUnionDef
(ty,lb,decls
,rb) ->
423 all_same opt_allowed
tgt (mcode2line lb)
424 (List.map
mcode2arity [lb;rb]) in
425 let ty = typeC
arity ty in
427 let decls = dots (declaration
tgt) decls in
429 make_typeC typ
tgt arity (Ast0.StructUnionDef
(ty,lb,decls,rb))
430 | Ast0.TypeName
(name) ->
432 all_same opt_allowed
tgt (mcode2line name) [mcode2arity name] in
433 let name = mcode name in
434 make_typeC typ
tgt arity (Ast0.TypeName
(name))
435 | Ast0.MetaType
(name,pure
) ->
437 all_same opt_allowed
tgt (mcode2line name) [mcode2arity name] in
438 let name = mcode name in
439 make_typeC typ
tgt arity (Ast0.MetaType
(name,pure
))
440 | Ast0.DisjType
(starter
,types
,mids
,ender
) ->
441 let types = List.map
(typeC
tgt) types in
442 (match List.rev
types with
444 if anyopt xs
(function Ast0.OptType
(_
) -> true | _
-> false)
445 then fail typ
"opt only allowed in the last disjunct"
447 let res = Ast0.DisjType
(starter
,types,mids
,ender
) in
449 | Ast0.OptType
(_
) | Ast0.UniqueType
(_
) ->
450 failwith
"unexpected code"
452 and typeC
tgt ty = top_typeC
tgt false ty
454 (* --------------------------------------------------------------------- *)
455 (* Variable declaration *)
456 (* Even if the Cocci program specifies a list of declarations, they are
457 split out into multiple declarations of a single variable each. *)
461 (function x
-> Ast0.OptDecl x
)
462 (function x
-> Ast0.UniqueDecl x
)
464 and declaration
tgt decl
=
465 match Ast0.unwrap decl
with
466 Ast0.Init
(stg
,ty,id
,eq
,exp,sem
) ->
468 all_same true tgt (mcode2line eq
)
469 ((match stg
with None
-> [] | Some x
-> [mcode2arity x
]) @
470 (List.map
mcode2arity [eq
;sem
])) in
471 let stg = get_option mcode stg in
472 let ty = typeC
arity ty in
473 let id = ident false arity id in
475 let exp = initialiser
arity exp in
476 let sem = mcode sem in
477 make_decl decl
tgt arity (Ast0.Init
(stg,ty,id,eq,exp,sem))
478 | Ast0.UnInit
(stg,ty,id,sem) ->
480 all_same true tgt (mcode2line sem)
481 ((match stg with None
-> [] | Some x
-> [mcode2arity x
]) @
482 [mcode2arity sem]) in
483 let stg = get_option mcode stg in
484 let ty = typeC
arity ty in
485 let id = ident false arity id in
486 let sem = mcode sem in
487 make_decl decl
tgt arity (Ast0.UnInit
(stg,ty,id,sem))
488 | Ast0.MacroDecl
(name,lp,args,rp,sem) ->
490 all_same true tgt (mcode2line lp) (List.map
mcode2arity [lp;rp;sem]) in
491 let name = ident false arity name in
493 let args = dots (expression
arity) args in
495 let sem = mcode sem in
496 make_decl decl
tgt arity (Ast0.MacroDecl
(name,lp,args,rp,sem))
497 | Ast0.TyDecl
(ty,sem) ->
499 all_same true tgt (mcode2line sem) [mcode2arity sem] in
500 let ty = typeC
arity ty in
501 let sem = mcode sem in
502 make_decl decl
tgt arity (Ast0.TyDecl
(ty,sem))
503 | Ast0.Typedef
(stg,ty,id,sem) ->
505 all_same true tgt (mcode2line sem)
506 [mcode2arity stg;mcode2arity sem] in
507 let stg = mcode stg in
508 let ty = typeC
arity ty in
509 let id = typeC
arity id in
510 let sem = mcode sem in
511 make_decl decl
tgt arity (Ast0.Typedef
(stg,ty,id,sem))
512 | Ast0.DisjDecl
(starter
,decls,mids
,ender
) ->
513 let decls = List.map
(declaration
tgt) decls in
514 (match List.rev
decls with
516 if anyopt xs
(function Ast0.OptDecl
(_
) -> true | _
-> false)
517 then fail decl
"opt only allowed in the last disjunct"
519 let res = Ast0.DisjDecl
(starter
,decls,mids
,ender
) in
521 | Ast0.Ddots
(dots,whencode) ->
522 let arity = all_same true tgt (mcode2line dots) [mcode2arity dots] in
523 let dots = mcode dots in
524 let whencode = get_option (declaration
Ast0.NONE
) whencode in
525 make_decl decl
tgt arity (Ast0.Ddots
(dots,whencode))
526 | Ast0.OptDecl
(_
) | Ast0.UniqueDecl
(_
) ->
527 failwith
"unexpected code"
529 (* --------------------------------------------------------------------- *)
534 (function x
-> Ast0.OptIni x
)
535 (function x
-> Ast0.UniqueIni x
)
537 and initialiser
tgt i
=
538 let init_same = all_same true tgt in
539 match Ast0.unwrap i
with
540 Ast0.MetaInit
(name,pure
) ->
541 let arity = init_same (mcode2line name) [mcode2arity name] in
542 let name = mcode name in
543 make_init i
tgt arity (Ast0.MetaInit
(name,pure
))
544 | Ast0.InitExpr
(exp) ->
545 Ast0.rewrap i
(Ast0.InitExpr
(expression
tgt exp))
546 | Ast0.InitList
(lb,initlist
,rb) ->
547 let arity = init_same (mcode2line lb) [mcode2arity lb; mcode2arity rb] in
549 let initlist = dots (initialiser
arity) initlist in
551 make_init i
tgt arity (Ast0.InitList
(lb,initlist,rb))
552 | Ast0.InitGccExt
(designators
,eq,ini
) ->
553 let arity = init_same (mcode2line eq) [mcode2arity eq] in
554 let designators = List.map
(designator
arity) designators in
556 let ini = initialiser
arity ini in
557 make_init i
tgt arity (Ast0.InitGccExt
(designators,eq,ini))
558 | Ast0.InitGccName
(name,eq,ini) ->
559 let arity = init_same (mcode2line eq) [mcode2arity eq] in
560 let name = ident true arity name in
562 let ini = initialiser
arity ini in
563 make_init i
tgt arity (Ast0.InitGccName
(name,eq,ini))
565 let arity = init_same (mcode2line cm) [mcode2arity cm] in
567 make_init i
tgt arity (Ast0.IComma
(cm))
568 | Ast0.Idots
(dots,whencode) ->
569 let arity = init_same (mcode2line dots) [mcode2arity dots] in
570 let dots = mcode dots in
571 let whencode = get_option (initialiser
Ast0.NONE
) whencode in
572 make_init i
tgt arity (Ast0.Idots
(dots,whencode))
573 | Ast0.OptIni
(_
) | Ast0.UniqueIni
(_
) ->
574 failwith
"unexpected code"
576 and designator
tgt d
=
577 let dsame = all_same false tgt in
579 Ast0.DesignatorField
(dot
,id) ->
580 let arity = dsame (mcode2line dot
) [mcode2arity dot
] in
581 let dot = mcode dot in
582 let id = ident false arity id in
583 Ast0.DesignatorField
(dot,id)
584 | Ast0.DesignatorIndex
(lb,exp,rb) ->
585 let arity = dsame (mcode2line lb) [mcode2arity lb;mcode2arity rb] in
587 let exp = top_expression false arity exp in
589 Ast0.DesignatorIndex
(lb,exp,rb)
590 | Ast0.DesignatorRange
(lb,min
,dots,max
,rb) ->
592 dsame (mcode2line lb)
593 [mcode2arity lb;mcode2arity dots;mcode2arity rb] in
595 let min = top_expression false arity min in
596 let dots = mcode dots in
597 let max = top_expression false arity max in
599 Ast0.DesignatorRange
(lb,min,dots,max,rb)
601 (* --------------------------------------------------------------------- *)
606 (function x
-> Ast0.OptParam x
)
607 (function x
-> Ast0.UniqueParam x
)
609 and parameterTypeDef
tgt param
=
610 let param_same = all_same true tgt in
611 match Ast0.unwrap param
with
612 Ast0.VoidParam
(ty) -> Ast0.rewrap param
(Ast0.VoidParam
(typeC
tgt ty))
613 | Ast0.Param
(ty,Some
id) ->
614 let ty = top_typeC
tgt true ty in
615 let id = ident true tgt id in
617 (match (Ast0.unwrap
ty,Ast0.unwrap
id) with
618 (Ast0.OptType
(ty),Ast0.OptIdent
(id)) ->
619 Ast0.OptParam
(Ast0.rewrap param
(Ast0.Param
(ty,Some
id)))
620 | (Ast0.UniqueType
(ty),Ast0.UniqueIdent
(id)) ->
621 Ast0.UniqueParam
(Ast0.rewrap param
(Ast0.Param
(ty,Some
id)))
622 | (Ast0.OptType
(ty),_
) ->
623 fail param
"arity mismatch in param declaration"
624 | (_
,Ast0.OptIdent
(id)) ->
625 fail param
"arity mismatch in param declaration"
626 | _
-> Ast0.Param
(ty,Some
id))
627 | Ast0.Param
(ty,None
) ->
628 let ty = top_typeC
tgt true ty in
630 (match Ast0.unwrap
ty with
632 Ast0.OptParam
(Ast0.rewrap param
(Ast0.Param
(ty,None
)))
633 | Ast0.UniqueType
(ty) ->
634 Ast0.UniqueParam
(Ast0.rewrap param
(Ast0.Param
(ty,None
)))
635 | _
-> Ast0.Param
(ty,None
))
636 | Ast0.MetaParam
(name,pure
) ->
637 let arity = param_same (mcode2line name) [mcode2arity name] in
638 let name = mcode name in
639 make_param param
tgt arity (Ast0.MetaParam
(name,pure
))
640 | Ast0.MetaParamList
(name,lenname
,pure
) ->
641 let arity = param_same (mcode2line name) [mcode2arity name] in
642 let name = mcode name in
643 make_param param
tgt arity (Ast0.MetaParamList
(name,lenname
,pure
))
645 let arity = param_same (mcode2line cm) [mcode2arity cm] in
647 make_param param
tgt arity (Ast0.PComma
(cm))
648 | Ast0.Pdots
(dots) ->
649 let arity = param_same (mcode2line dots) [mcode2arity dots] in
650 let dots = mcode dots in
651 make_param param
tgt arity (Ast0.Pdots
(dots))
652 | Ast0.Pcircles
(dots) ->
653 let arity = param_same (mcode2line dots) [mcode2arity dots] in
654 let dots = mcode dots in
655 make_param param
tgt arity (Ast0.Pcircles
(dots))
656 | Ast0.OptParam
(_
) | Ast0.UniqueParam
(_
) ->
657 failwith
"unexpected code"
659 and parameter_list
tgt = dots (parameterTypeDef
tgt)
661 (* --------------------------------------------------------------------- *)
664 and make_rule_elem x
=
666 (function x
-> Ast0.OptStm x
)
667 (function x
-> Ast0.UniqueStm x
)
670 and statement
tgt stm
=
671 let stm_same = all_same true tgt in
672 match Ast0.unwrap stm
with
673 Ast0.Decl
(bef
,decl
) ->
674 let new_decl = declaration
tgt decl
in
676 (match Ast0.unwrap
new_decl with
677 Ast0.OptDecl
(decl
) ->
678 Ast0.OptStm
(Ast0.rewrap stm
(Ast0.Decl
(bef
,decl
)))
679 | Ast0.UniqueDecl
(decl
) ->
680 Ast0.UniqueStm
(Ast0.rewrap stm
(Ast0.Decl
(bef
,decl
)))
681 | _
-> Ast0.Decl
(bef
,new_decl))
682 | Ast0.Seq
(lbrace
,body
,rbrace
) ->
684 stm_same (mcode2line lbrace
)
685 [mcode2arity lbrace
; mcode2arity rbrace
] in
686 let lbrace = mcode lbrace in
687 let body = dots (statement
arity) body in
688 let rbrace = mcode rbrace in
689 make_rule_elem stm
tgt arity (Ast0.Seq
(lbrace,body,rbrace))
690 | Ast0.ExprStatement
(exp,sem) ->
691 let arity = stm_same (mcode2line sem) [mcode2arity sem] in
692 let exp = expression
arity exp in
693 let sem = mcode sem in
694 make_rule_elem stm
tgt arity (Ast0.ExprStatement
(exp,sem))
695 | Ast0.IfThen
(iff
,lp,exp,rp,branch
,aft
) ->
697 stm_same (mcode2line iff
) (List.map
mcode2arity [iff
;lp;rp]) in
698 let iff = mcode iff in
700 let exp = expression
arity exp in
702 let branch = statement
arity branch in
703 make_rule_elem stm
tgt arity (Ast0.IfThen
(iff,lp,exp,rp,branch,aft
))
704 | Ast0.IfThenElse
(iff,lp,exp,rp,branch1
,els
,branch2
,aft
) ->
706 stm_same (mcode2line iff) (List.map
mcode2arity [iff;lp;rp;els
]) in
707 let iff = mcode iff in
709 let exp = expression
arity exp in
711 let branch1 = statement
arity branch1 in
712 let els = mcode els in
713 let branch2 = statement
arity branch2 in
714 make_rule_elem stm
tgt arity
715 (Ast0.IfThenElse
(iff,lp,exp,rp,branch1,els,branch2,aft
))
716 | Ast0.While
(wh
,lp,exp,rp,body,aft
) ->
718 stm_same (mcode2line wh
)
719 (List.map
mcode2arity [wh
;lp;rp]) in
722 let exp = expression
arity exp in
724 let body = statement
arity body in
725 make_rule_elem stm
tgt arity (Ast0.While
(wh,lp,exp,rp,body,aft
))
726 | Ast0.Do
(d
,body,wh,lp,exp,rp,sem) ->
728 stm_same (mcode2line wh) (List.map
mcode2arity [d
;wh;lp;rp;sem]) in
730 let body = statement
arity body in
733 let exp = expression
arity exp in
735 let sem = mcode sem in
736 make_rule_elem stm
tgt arity (Ast0.Do
(d,body,wh,lp,exp,rp,sem))
737 | Ast0.For
(fr
,lp,exp1,sem1
,exp2,sem2
,exp3,rp,body,aft
) ->
739 stm_same (mcode2line fr
) (List.map
mcode2arity [fr
;lp;sem1
;sem2
;rp]) in
742 let exp1 = get_option (expression
arity) exp1 in
743 let sem1 = mcode sem1 in
744 let exp2 = get_option (expression
arity) exp2 in
745 let sem2= mcode sem2 in
746 let exp3 = get_option (expression
arity) exp3 in
748 let body = statement
arity body in
749 make_rule_elem stm
tgt arity
750 (Ast0.For
(fr,lp,exp1,sem1,exp2,sem2,exp3,rp,body,aft
))
751 | Ast0.Iterator
(nm
,lp,args,rp,body,aft
) ->
752 let arity = stm_same (mcode2line lp) (List.map
mcode2arity [lp;rp]) in
753 let nm = ident false arity nm in
755 let args = dots (expression
arity) args in
757 let body = statement
arity body in
758 make_rule_elem stm
tgt arity (Ast0.Iterator
(nm,lp,args,rp,body,aft
))
759 | Ast0.Switch
(switch
,lp,exp,rp,lb,decls,cases
,rb) ->
761 stm_same (mcode2line switch
)
762 (List.map
mcode2arity [switch
;lp;rp;lb;rb]) in
763 let switch = mcode switch in
765 let exp = expression
arity exp in
768 let decls = dots (statement
arity) decls in
769 let cases = dots (case_line
arity) cases in
771 make_rule_elem stm
tgt arity
772 (Ast0.Switch
(switch,lp,exp,rp,lb,decls,cases,rb))
773 | Ast0.Break
(br
,sem) ->
774 let arity = stm_same (mcode2line br
) (List.map
mcode2arity [br
;sem]) in
776 let sem = mcode sem in
777 make_rule_elem stm
tgt arity (Ast0.Break
(br,sem))
778 | Ast0.Continue
(cont
,sem) ->
780 stm_same (mcode2line cont
) (List.map
mcode2arity [cont
;sem]) in
781 let cont = mcode cont in
782 let sem = mcode sem in
783 make_rule_elem stm
tgt arity (Ast0.Continue
(cont,sem))
784 | Ast0.Label
(l,dd
) ->
785 let arity = mcode2arity dd
in
786 let l = ident false tgt l in
788 make_rule_elem stm
tgt arity (Ast0.Label
(l,dd))
789 | Ast0.Goto
(goto
,l,sem) ->
791 stm_same (mcode2line goto
) (List.map
mcode2arity [goto
;sem]) in
792 let goto = mcode goto in
793 let l = ident false tgt l in
794 let sem = mcode sem in
795 make_rule_elem stm
tgt arity (Ast0.Goto
(goto,l,sem))
796 | Ast0.Return
(ret
,sem) ->
797 let arity = stm_same (mcode2line ret
) (List.map
mcode2arity [ret
;sem]) in
798 let ret = mcode ret in
799 let sem = mcode sem in
800 make_rule_elem stm
tgt arity (Ast0.Return
(ret,sem))
801 | Ast0.ReturnExpr
(ret,exp,sem) ->
802 let arity = stm_same (mcode2line ret) (List.map
mcode2arity [ret;sem]) in
803 let ret = mcode ret in
804 let exp = expression
arity exp in
805 let sem = mcode sem in
806 make_rule_elem stm
tgt arity (Ast0.ReturnExpr
(ret,exp,sem))
807 | Ast0.MetaStmt
(name,pure
) ->
808 let arity = stm_same (mcode2line name) [mcode2arity name] in
809 let name = mcode name in
810 make_rule_elem stm
tgt arity (Ast0.MetaStmt
(name,pure
))
811 | Ast0.MetaStmtList
(name,pure
) ->
812 let arity = stm_same (mcode2line name) [mcode2arity name] in
813 let name = mcode name in
814 make_rule_elem stm
tgt arity (Ast0.MetaStmtList
(name,pure
))
816 let new_exp = top_expression true tgt exp in
818 (match Ast0.unwrap
new_exp with
820 Ast0.OptStm
(Ast0.rewrap stm
(Ast0.Exp
(exp)))
821 | Ast0.UniqueExp
(exp) ->
822 Ast0.UniqueStm
(Ast0.rewrap stm
(Ast0.Exp
(exp)))
823 | _
-> Ast0.Exp
(new_exp))
824 | Ast0.TopExp
(exp) ->
825 let new_exp = top_expression true tgt exp in
827 (match Ast0.unwrap
new_exp with
829 Ast0.OptStm
(Ast0.rewrap stm
(Ast0.TopExp
(exp)))
830 | Ast0.UniqueExp
(exp) ->
831 Ast0.UniqueStm
(Ast0.rewrap stm
(Ast0.TopExp
(exp)))
832 | _
-> Ast0.TopExp
(new_exp))
834 let new_ty = typeC
tgt ty in (* opt makes no sense alone at top level *)
836 (match Ast0.unwrap
new_ty with
838 Ast0.OptStm
(Ast0.rewrap stm
(Ast0.Ty
(ty)))
839 | Ast0.UniqueType
(ty) ->
840 Ast0.UniqueStm
(Ast0.rewrap stm
(Ast0.Ty
(ty)))
841 | _
-> Ast0.Ty
(new_ty))
842 | Ast0.TopInit
(init
) ->
843 let new_init = initialiser
tgt init
in
845 (match Ast0.unwrap
new_init with
847 Ast0.OptStm
(Ast0.rewrap stm
(Ast0.TopInit
(init
)))
848 | Ast0.UniqueIni
(init
) ->
849 Ast0.UniqueStm
(Ast0.rewrap stm
(Ast0.TopInit
(init
)))
850 | _
-> Ast0.TopInit
(new_init))
851 | Ast0.Disj
(starter
,rule_elem_dots_list
,mids
,ender
) ->
853 List.map
(function x
-> concat_dots (statement
tgt) x
)
854 rule_elem_dots_list
in
855 let (found_opt
,unopt
) =
857 (function (found_opt
,lines
) ->
860 (* previously just checked the last thing in the list,
861 but everything should be optional for the whole thing to
864 match Ast0.unwrap x
with
865 Ast0.OptStm
(x
) -> true
868 match Ast0.unwrap x
with
871 if List.for_all
is_opt l
872 then (true,List.map
unopt l)
875 match Ast0.unwrap x
with
877 (l,function l -> Ast0.rewrap x
(Ast0.DOTS
l))
879 (l,function l -> Ast0.rewrap x
(Ast0.CIRCLES
l))
881 (l,function l -> Ast0.rewrap x
(Ast0.STARS
l)) in
882 let (found_opt
,l) = rebuild l in
883 (found_opt
,(k
l)::lines
))
885 let unopt = List.rev
unopt in
888 make_rule_elem stm
tgt Ast0.OPT
(Ast0.Disj
(starter
,unopt,mids
,ender
))
889 else Ast0.rewrap stm
(Ast0.Disj
(starter
,stms,mids
,ender
))
890 | Ast0.Nest
(starter
,rule_elem_dots
,ender
,whn
,multi
) ->
891 let new_rule_elem_dots =
892 concat_dots (statement
Ast0.NONE
) rule_elem_dots
in
895 (whencode (concat_dots (statement
Ast0.NONE
)) (statement
Ast0.NONE
)
896 (expression
Ast0.NONE
))
899 (Ast0.Nest
(starter
,new_rule_elem_dots,ender
,whn,multi
))
900 | Ast0.Dots
(dots,whn) ->
901 let arity = stm_same (mcode2line dots) [mcode2arity dots] in
902 let dots = mcode dots in
905 (whencode (concat_dots (statement
Ast0.NONE
)) (statement
Ast0.NONE
)
906 (expression
Ast0.NONE
))
908 make_rule_elem stm
tgt arity (Ast0.Dots
(dots,whn))
909 | Ast0.Circles
(dots,whn) ->
910 let arity = stm_same (mcode2line dots) [mcode2arity dots] in
911 let dots = mcode dots in
914 (whencode (concat_dots (statement
Ast0.NONE
)) (statement
Ast0.NONE
)
915 (expression
Ast0.NONE
))
917 make_rule_elem stm
tgt arity (Ast0.Circles
(dots,whn))
918 | Ast0.Stars
(dots,whn) ->
919 let arity = stm_same (mcode2line dots) [mcode2arity dots] in
920 let dots = mcode dots in
923 (whencode (concat_dots (statement
Ast0.NONE
)) (statement
Ast0.NONE
)
924 (expression
Ast0.NONE
))
926 make_rule_elem stm
tgt arity (Ast0.Stars
(dots,whn))
927 | Ast0.FunDecl
(bef
,fi
,name,lp,params,rp,lbrace,body,rbrace) ->
929 all_same true tgt (mcode2line lp)
930 ((List.map
mcode2arity [lp;rp;lbrace;rbrace]) @ (fninfo2arity fi
)) in
931 let fi = List.map
(fninfo
arity) fi in
932 let name = ident false arity name in
934 let params = parameter_list
arity params in
936 let lbrace = mcode lbrace in
937 let body = dots (statement
arity) body in
938 let rbrace = mcode rbrace in
939 make_rule_elem stm
tgt arity
940 (Ast0.FunDecl
(bef
,fi,name,lp,params,rp,lbrace,body,rbrace))
941 | Ast0.Include
(inc
,s
) ->
943 all_same true tgt (mcode2line inc
) [mcode2arity inc
; mcode2arity s
] in
944 let inc = mcode inc in
946 make_rule_elem stm
tgt arity (Ast0.Include
(inc,s))
947 | Ast0.Define
(def
,id,params,body) ->
948 let arity = all_same true tgt (mcode2line def
) [mcode2arity def
] in
949 let def = mcode def in
950 let id = ident false arity id in
951 let params = define_parameters
arity params in
952 let body = dots (statement
arity) body in
953 make_rule_elem stm
tgt arity (Ast0.Define
(def,id,params,body))
954 | Ast0.OptStm
(_
) | Ast0.UniqueStm
(_
) ->
955 failwith
"unexpected code"
957 and define_parameters
tgt params =
958 match Ast0.unwrap
params with
959 Ast0.NoParams
-> params
960 | Ast0.DParams
(lp,params,rp) ->
962 all_same true tgt (mcode2line lp) [mcode2arity lp;mcode2arity rp] in
964 let params = dots (define_param
arity) params in
966 Ast0.rewrap
params (Ast0.DParams
(lp,params,rp))
968 and make_define_param x
=
970 (function x
-> Ast0.OptDParam x
)
971 (function x
-> Ast0.UniqueDParam x
)
974 and define_param
tgt param
=
975 match Ast0.unwrap param
with
977 let new_id = ident true tgt id in
979 (match Ast0.unwrap
new_id with
981 Ast0.OptDParam
(Ast0.rewrap param
(Ast0.DParam
(id)))
982 | Ast0.UniqueIdent
(decl
) ->
983 Ast0.UniqueDParam
(Ast0.rewrap param
(Ast0.DParam
(id)))
984 | _
-> Ast0.DParam
(new_id))
985 | Ast0.DPComma
(cm) ->
987 all_same true tgt (mcode2line cm) [mcode2arity cm] in
989 make_define_param param
tgt arity (Ast0.DPComma
(cm))
990 | Ast0.DPdots
(dots) ->
992 all_same true tgt (mcode2line dots) [mcode2arity dots] in
993 let dots = mcode dots in
994 make_define_param param
tgt arity (Ast0.DPdots
(dots))
995 | Ast0.DPcircles
(circles
) ->
997 all_same true tgt (mcode2line circles
) [mcode2arity circles
] in
998 let circles = mcode circles in
999 make_define_param param
tgt arity (Ast0.DPcircles
(circles))
1000 | Ast0.OptDParam
(dp
) | Ast0.UniqueDParam
(dp
) ->
1001 failwith
"unexpected code"
1003 and fninfo
arity = function
1004 Ast0.FStorage
(stg) -> Ast0.FStorage
(mcode stg)
1005 | Ast0.FType
(ty) -> Ast0.FType
(typeC
arity ty)
1006 | Ast0.FInline
(inline
) -> Ast0.FInline
(mcode inline
)
1007 | Ast0.FAttr
(attr
) -> Ast0.FAttr
(mcode attr
)
1009 and fninfo2arity fninfo
=
1013 Ast0.FStorage
(stg) -> [mcode2arity stg]
1014 | Ast0.FType
(ty) -> []
1015 | Ast0.FInline
(inline
) -> [mcode2arity inline
]
1016 | Ast0.FAttr
(attr
) -> [mcode2arity attr
])
1019 and whencode notfn alwaysfn expression
= function
1020 Ast0.WhenNot a
-> Ast0.WhenNot
(notfn a
)
1021 | Ast0.WhenAlways a
-> Ast0.WhenAlways
(alwaysfn a
)
1022 | Ast0.WhenModifier
(x
) -> Ast0.WhenModifier
(x
)
1023 | Ast0.WhenNotTrue a
-> Ast0.WhenNotTrue
(expression a
)
1024 | Ast0.WhenNotFalse a
-> Ast0.WhenNotFalse
(expression a
)
1026 and make_case_line
=
1028 (function x
-> Ast0.OptCase x
)
1029 (function x
-> failwith
"unique not allowed for case_line")
1031 and case_line
tgt c
=
1032 match Ast0.unwrap c
with
1033 Ast0.Default
(def,colon,code
) ->
1035 all_same true tgt (mcode2line def)
1036 [mcode2arity def; mcode2arity colon] in
1037 let def = mcode def in
1038 let colon = mcode colon in
1039 let code = dots (statement
arity) code in
1040 make_case_line c
tgt arity (Ast0.Default
(def,colon,code))
1041 | Ast0.Case
(case
,exp,colon,code) ->
1043 all_same true tgt (mcode2line case
)
1044 [mcode2arity case
; mcode2arity colon] in
1045 let case = mcode case in
1046 let exp = expression
arity exp in
1047 let colon = mcode colon in
1048 let code = dots (statement
arity) code in
1049 make_case_line c
tgt arity (Ast0.Case
(case,exp,colon,code))
1050 | Ast0.DisjCase
(starter
,case_lines
,mids
,ender
) ->
1051 let case_lines = List.map
(case_line
tgt) case_lines in
1052 (match List.rev
case_lines with
1054 if anyopt xs
(function Ast0.OptCase
(_
) -> true | _
-> false)
1055 then fail c
"opt only allowed in the last disjunct"
1057 Ast0.rewrap c
(Ast0.DisjCase
(starter
,case_lines,mids
,ender
))
1058 | Ast0.OptCase
(_
) -> failwith
"unexpected OptCase"
1060 (* --------------------------------------------------------------------- *)
1061 (* Function declaration *)
1062 (* Haven't thought much about arity here... *)
1064 let top_level tgt t
=
1066 (match Ast0.unwrap t
with
1067 Ast0.FILEINFO
(old_file
,new_file
) ->
1068 if mcode2arity old_file
= Ast0.NONE
&& mcode2arity new_file
= Ast0.NONE
1069 then Ast0.FILEINFO
(mcode old_file
,mcode new_file
)
1070 else fail t
"unexpected arity for file info"
1071 | Ast0.DECL
(stmt
) ->
1072 Ast0.DECL
(statement
tgt stmt
)
1073 | Ast0.CODE
(rule_elem_dots
) ->
1074 Ast0.CODE
(concat_dots (statement
tgt) rule_elem_dots
)
1075 | Ast0.ERRORWORDS
(exps) ->
1076 Ast0.ERRORWORDS
(List.map
(top_expression false Ast0.NONE
) exps)
1077 | Ast0.OTHER
(_
) -> fail t
"eliminated by top_level")
1079 let rule tgt = List.map
(top_level tgt)
1081 (* --------------------------------------------------------------------- *)
1084 let minus_arity code =