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.
27 (* For the moment I do only eq_type and not eq_expr, etc. The reason
28 * for eq_type is related to the typedef and struct isomorphism. Sometimes
29 * one use the typedef and sometimes the structname.
31 * TODO: should use the isomorphisms engine of julia.
32 * Maybe I can transform my ast_c in ast_cocci, and use julia's code ?
33 * Maybe I can add some Disj in my ast_c ?
42 type 'a matcher
= 'a
-> 'a
-> tin
-> 'a tout
46 ('a
-> (tin
-> 'b tout
)) ->
49 val (>&&>) : bool -> (tin
-> 'x tout
) -> (tin
-> 'x tout
)
51 val return
: 'a
-> tin
-> 'a tout
52 val fail
: tin
-> 'a tout
57 functor (X
: PARAM
) ->
60 type 'a matcher
= 'a
-> 'a
-> X.tin
-> 'a
X.tout
67 let (option: 'a matcher
-> ('a
option matcher
)) = fun f t1 t2
->
69 | (Some t1
, Some t2
) ->
73 | (None
, None
) -> return None
77 let same_s saopt sbopt
=
78 match saopt
, sbopt
with
80 | Some namea
, Some nameb
->
81 let sa = Ast_c.str_of_name namea
in
82 let sb = Ast_c.str_of_name nameb
in
87 let rec fullType a b
=
88 let ((qua
,iiqa
), tya
) = a
in
89 let ((qub
,iiqb
), tyb
) = b
in
90 (qua
.const
=:= qub
.const
&& qua
.volatile
=:= qub
.volatile
) >&&>
92 let (qu
,iiq
) = (qua
, iiqa
) in
93 typeC tya tyb
>>= (fun ty
->
104 | BaseType a
, BaseType b
->
105 a
=*= b
>&&> return (BaseType a
, iix)
106 | Pointer a
, Pointer b
->
107 fullType a b
>>= (fun x
-> return (Pointer x
, iix))
109 | StructUnionName
(sua
, sa), StructUnionName
(sub
, sb) ->
110 (sua
=*= sub
&& sa =$
= sb) >&&>
111 return (StructUnionName
(sua
, sa), iix)
113 | TypeName
(namea
, opta
), TypeName
(nameb
, optb
) ->
114 let sa = Ast_c.str_of_name namea
in
115 let sb = Ast_c.str_of_name nameb
in
117 (* assert compatible opta optb ? *)
118 (*option fullType opta optb*)
121 (match opta
, optb
with
130 return (TypeName
(namea
, opt), iix)
133 | Array
(ea
, a
), Array
(eb
,b
) ->
134 let get_option f
= function Some x
-> Some
(f x
) | None
-> None
in
135 let ea = get_option Lib_parsing_c.al_expr
ea in
136 let eb = get_option Lib_parsing_c.al_expr
eb in
137 ea =*= eb >&&> fullType a b
>>= (fun x
-> return (Array
(ea, x
), iix))
139 | FunctionType
(returna
, paramsa
), FunctionType
(returnb
, paramsb
) ->
140 let (tsa
, (ba
,iihas3dotsa
)) = paramsa
in
141 let (tsb
, (bb
,iihas3dotsb
)) = paramsb
in
144 let iihas3dotsx = iihas3dotsa
in
146 (ba
=:= bb
&& List.length tsa
=|= List.length tsb
) >&&>
147 fullType returna returnb
>>= (fun returnx
->
149 Common.zip tsa tsb
+> List.fold_left
150 (fun acc
((parama
,iia
),(paramb
,iib
))->
154 let {p_register
= (ba
,iiba
); p_namei
= saopt
; p_type
= ta
} =
156 let {p_register
= (bb
,iibb
); p_namei
= sbopt
; p_type
= tb
} =
165 (* todo? iso on name or argument ? *)
166 (ba
=:= bb
&& same_s saopt sbopt
) >&&>
167 fullType ta tb
>>= (fun tx
->
168 let paramx = { p_register
= (bx, iibx);
171 return ((paramx,iix)::xs
)
176 let paramsx = (List.rev tsx
, (bx, iihas3dotsx)) in
177 return (FunctionType
(returnx
, paramsx), iix)
180 | Enum
(saopt
, enuma
), Enum
(sbopt
, enumb
) ->
182 List.length enuma
=|= List.length enumb
&&
183 Common.zip enuma enumb
+> List.for_all
(fun
184 (((namesa
,eopta
), iicommaa
), ((namesb
,eoptb
),iicommab
))
186 let sa = str_of_name namesa
in
187 let sb = str_of_name namesb
in
189 (* todo ? eopta and b can have some info so ok to use =*= ? *)
193 return (Enum
(saopt
, enuma
), iix)
195 | EnumName
sa, EnumName
sb -> sa =$
= sb >&&> return (EnumName
sa, iix)
197 | ParenType a
, ParenType b
->
199 fullType a b
>>= (fun x
->
200 return (ParenType x
, iix)
203 | TypeOfExpr
ea, TypeOfExpr
eb ->
204 let ea = Lib_parsing_c.al_expr
ea in
205 let eb = Lib_parsing_c.al_expr
eb in
206 ea =*= eb >&&> return (TypeOfExpr
ea, iix)
208 | TypeOfType a
, TypeOfType b
->
209 fullType a b
>>= (fun x
-> return (TypeOfType x
, iix))
211 (* | TypeOfType a, b ->
216 | StructUnion
(sua
, saopt
, sta
), StructUnion
(sub
, sbopt
, stb
) ->
217 (sua
=*= sub
&& saopt
=*= sbopt
&& List.length sta
=|= List.length stb
)
219 Common.zip sta stb
+> List.fold_left
220 (fun acc
((fielda
), (fieldb
)) ->
222 match fielda
, fieldb
with
223 | EmptyField iia
, EmptyField iib
->
225 return ((EmptyField
iix)::xs
)
227 | DeclarationField
(FieldDeclList
(fa
, iipta
)),
228 DeclarationField
(FieldDeclList
(fb
, iiptb
)) ->
229 let iipt = iipta
in (* TODO ?*)
230 (List.length fa
=|= List.length fb
) >&&>
232 Common.zip fa fb
+> List.fold_left
233 (fun acc2
((fielda
,iia
),(fieldb
,iib
))->
236 match fielda
, fieldb
with
237 | Simple
(nameaopt
, ta
), Simple
(namebopt
, tb
) ->
240 same_s nameaopt namebopt
>&&>
241 fullType ta tb
>>= (fun tx
->
242 return (((Simple
(nameaopt
, tx
)), iix)::xs
)
245 | BitField
(nameopta
, ta
, infoa
, ea),
246 BitField
(nameoptb
, tb
, infob
, eb) ->
248 (same_s nameopta nameoptb
&& ea =*= eb) >&&>
249 fullType ta tb
>>= (fun tx
->
250 return (((BitField
(nameopta
,tx
,infox,ea)), iix)::xs
)
256 return (((DeclarationField
257 (FieldDeclList
(List.rev fx
,iipt))))::xs
)
265 return (StructUnion
(sua
, saopt
, List.rev stx
), iix)
271 * subtil: in the return must put iia, not iix, and in following case
272 * must put iib and not iix, because we want the token corresponding
275 | TypeName
(name
, Some a
), _
->
276 fullType a
(Ast_c.nQ
, tyb
) >>= (fun x
->
277 return (TypeName
(name
, Some x
), iia
)
280 | _
, TypeName
(name
, Some b
) ->
281 fullType b
(Ast_c.nQ
, tya
) >>= (fun x
->
282 return (TypeName
(name
, Some x
), iib
) (* subtil: *)
293 type 'a tout
= 'a
option
295 type 'a matcher
= 'a
-> 'a
-> tin
-> 'a tout
297 let return x
= fun tin
-> Some x
298 let fail = fun tin
-> None
300 let (>>=) m f
= fun tin
->
305 let (>&&>) b m
= fun tin
->
311 module EQ
= C_VS_C
(XEQ
)
314 let eq_type2 a b
= EQ.fullType a b
() <> None
315 let merge_type2 a b
= Common.some
(EQ.fullType a b
())
318 Common.profile_code
"C_vs_c" (fun () -> eq_type2 a b
)
321 Common.profile_code
"C_vs_c" (fun () -> merge_type2 a b
)