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 module V0
= Visitor_ast0
24 module VT0
= Visitor_ast0_types
25 module Ast0
= Ast0_cocci
26 module Ast
= Ast_cocci
28 (* Detects where position variables can be present in the match of an
29 isomorpshims. This is allowed if all elements of an isomorphism have only
30 one token or if we can somehow match up equal tokens of all of the
31 isomorphic variants. *)
35 (* sort of unpleasant to convert the token representation to a string
36 but we can't make a list of mcodes otherwise because the types are all
38 [(Common.dump
(Ast0.unwrap_mcode x
),Ast0.get_pos_ref x
)] in
39 let donothing r k e
= k e
in
40 let bind x y
= x
@ y
in
41 let option_default = [] in
42 V0.flat_combiner
bind option_default
43 mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
44 donothing donothing donothing donothing donothing donothing
46 donothing donothing donothing donothing donothing donothing donothing
48 (* In general, we will get a list of lists:
50 [[tokens1;tokens2;tokens3];[tokens4;tokens5;tokens6];[tokens7;tokens8]]
52 If all of the lists tokens contain only one element, we are done.
54 Otherwise, we focus on tokens1. For each of its elements, if they are
55 present in all of the others, then a position is assigned, and if not then
56 a position is not. The order of the elements in the other lists is
57 irrelevant; we just take the first unannotated element that matches. Once
58 we are done with the elements of tokens1, we skip to tokens 4 and repeat,
59 including considering the one-element special case. *)
65 let name = ("",Printf.sprintf
"p%d" c) in
66 Ast0.MetaPos
(Ast0.make_mcode
name,[],Ast.PER
)
69 let rec loop = function
71 | ((f
::r
)::xs
) as a
->
72 if List.for_all
(List.for_all
(function e
-> List.length e
= 1)) a
75 List.iter
(List.iter
(List.iter
(function (_
,pos
) -> pos
:= p))) a
77 let all = r
@ List.concat xs
in
78 let rec find_first_available a
= function
81 if str
= a
&& !pos
= Ast0.NoMetaPos
83 else find_first_available a xs
in
85 (function (str
,pos
) ->
89 let entries = List.map
(find_first_available str
) all in
92 List.iter
(function pos
-> pos
:= p) entries
94 | _
-> (* already have a variable *) ())
97 | _
-> failwith
"bad iso" in
102 let process (metavars
,alts
,name) =
104 List.map
(List.map
sequence_tokens.VT0.combiner_rec_anything
) alts
in