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 (* exports everything, used only by parser_cocci_menhir.mly *)
24 module Ast0
= Ast0_cocci
25 module Ast
= Ast_cocci
27 (* types for metavariable tokens *)
28 type info
= Ast.meta_name
* Ast0.pure
* Data.clt
29 type idinfo
= Ast.meta_name
* Data.iconstraints
* Ast0.pure
* Data.clt
30 type expinfo
= Ast.meta_name
* Data.econstraints
* Ast0.pure
* Data.clt
31 type tyinfo
= Ast.meta_name
* Ast0.typeC list
* Ast0.pure
* Data.clt
32 type list_info
= Ast.meta_name
* Ast.meta_name
option * Ast0.pure
* Data.clt
34 Ast.meta_name
* Data.econstraints
* Ast0.pure
*
35 Type_cocci.typeC list
option * Data.clt
36 type pos_info
= Ast.meta_name
* Data.pconstraints
* Ast.meta_collect
* Data.clt
39 let get_option fn
= function
41 | Some x
-> Some
(fn x
)
43 let make_info line logical_line offset col strbef straft
=
45 {Ast0.line_start
= line
; Ast0.line_end
= line
;
46 Ast0.logical_start
= logical_line
; Ast0.logical_end
= logical_line
;
47 Ast0.column
= col
; Ast0.offset
= offset
; } in
48 { Ast0.pos_info
= new_pos_info;
49 Ast0.attachable_start
= true; Ast0.attachable_end
= true;
50 Ast0.mcode_start
= []; Ast0.mcode_end
= [];
51 Ast0.strings_before
= strbef
; Ast0.strings_after
= straft
; }
53 let clt2info (_
,line
,logical_line
,offset
,col
,strbef
,straft
,pos
) =
54 make_info line logical_line offset col strbef straft
56 let drop_bef (arity
,line
,lline
,offset
,col
,strbef
,straft
,pos
) =
57 (arity
,line
,lline
,offset
,col
,[],straft
,pos
)
59 let drop_aft (arity
,line
,lline
,offset
,col
,strbef
,straft
,pos
) =
60 (arity
,line
,lline
,offset
,col
,strbef
,[],pos
)
62 let drop_pos (arity
,line
,lline
,offset
,col
,strbef
,straft
,pos
) =
63 (arity
,line
,lline
,offset
,col
,strbef
,straft
,Ast0.NoMetaPos
)
65 let clt2mcode str
= function
66 (Data.MINUS
,line
,lline
,offset
,col
,strbef
,straft
,pos
) ->
67 (str
,Ast0.NONE
,make_info line lline offset col strbef straft
,
68 Ast0.MINUS
(ref([],Ast0.default_token_info
)),ref pos
,-1)
69 | (Data.OPTMINUS
,line
,lline
,offset
,col
,strbef
,straft
,pos
) ->
70 (str
,Ast0.OPT
,make_info line lline offset col strbef straft
,
71 Ast0.MINUS
(ref([],Ast0.default_token_info
)),ref pos
,-1)
72 | (Data.UNIQUEMINUS
,line
,lline
,offset
,col
,strbef
,straft
,pos
) ->
73 (str
,Ast0.UNIQUE
,make_info line lline offset col strbef straft
,
74 Ast0.MINUS
(ref([],Ast0.default_token_info
)),ref pos
,-1)
75 | (Data.PLUS
,line
,lline
,offset
,col
,strbef
,straft
,pos
) ->
76 (str
,Ast0.NONE
,make_info line lline offset col strbef straft
,
77 Ast0.PLUS
(Ast.ONE
),ref pos
,-1)
78 | (Data.PLUSPLUS
,line
,lline
,offset
,col
,strbef
,straft
,pos
) ->
79 (str
,Ast0.NONE
,make_info line lline offset col strbef straft
,
80 Ast0.PLUS
(Ast.MANY
),ref pos
,-1)
81 | (Data.CONTEXT
,line
,lline
,offset
,col
,strbef
,straft
,pos
) ->
82 (str
,Ast0.NONE
,make_info line lline offset col strbef straft
,
83 Ast0.CONTEXT
(ref(Ast.NOTHING
,
84 Ast0.default_token_info
,Ast0.default_token_info
)),
86 | (Data.OPT
,line
,lline
,offset
,col
,strbef
,straft
,pos
) ->
87 (str
,Ast0.OPT
,make_info line lline offset col strbef straft
,
88 Ast0.CONTEXT
(ref(Ast.NOTHING
,
89 Ast0.default_token_info
,Ast0.default_token_info
)),
91 | (Data.UNIQUE
,line
,lline
,offset
,col
,strbef
,straft
,pos
) ->
92 (str
,Ast0.UNIQUE
,make_info line lline offset col strbef straft
,
93 Ast0.CONTEXT
(ref(Ast.NOTHING
,
94 Ast0.default_token_info
,Ast0.default_token_info
)),
97 let id2name (name
, clt
) = name
98 let id2clt (name
, clt
) = clt
99 let id2mcode (name
, clt
) = clt2mcode name clt
101 let mkdots str
(dot
,whencode
) =
103 "..." -> Ast0.wrap
(Ast0.Dots
(clt2mcode str dot
, whencode
))
104 | "ooo" -> Ast0.wrap
(Ast0.Circles
(clt2mcode str dot
, whencode
))
105 | "***" -> Ast0.wrap
(Ast0.Stars
(clt2mcode str dot
, whencode
))
106 | _
-> failwith
"cannot happen"
108 let mkedots str
(dot
,whencode
) =
110 "..." -> Ast0.wrap
(Ast0.Edots
(clt2mcode str dot
, whencode
))
111 | "ooo" -> Ast0.wrap
(Ast0.Ecircles
(clt2mcode str dot
, whencode
))
112 | "***" -> Ast0.wrap
(Ast0.Estars
(clt2mcode str dot
, whencode
))
113 | _
-> failwith
"cannot happen"
115 let mkdpdots str dot
=
117 "..." -> Ast0.wrap
(Ast0.DPdots
(clt2mcode str dot
))
118 | "ooo" -> Ast0.wrap
(Ast0.DPcircles
(clt2mcode str dot
))
119 | _
-> failwith
"cannot happen"
121 let mkidots str
(dot
,whencode
) =
123 "..." -> Ast0.wrap
(Ast0.Idots
(clt2mcode str dot
, whencode
))
124 | _
-> failwith
"cannot happen"
126 let mkddots str
(dot
,whencode
) =
127 match (str
,whencode
) with
128 ("...",None
) -> Ast0.wrap
(Ast0.Ddots
(clt2mcode str dot
, None
))
129 | ("...",Some
[w
]) -> Ast0.wrap
(Ast0.Ddots
(clt2mcode str dot
, Some w
))
130 | _
-> failwith
"cannot happen"
132 let mkpdots str dot
=
134 "..." -> Ast0.wrap
(Ast0.Pdots
(clt2mcode str dot
))
135 | "ooo" -> Ast0.wrap
(Ast0.Pcircles
(clt2mcode str dot
))
136 | _
-> failwith
"cannot happen"
138 let arith_op ast_op left op right
=
140 (Ast0.Binary
(left
, clt2mcode (Ast.Arith ast_op
) op
, right
))
142 let logic_op ast_op left op right
=
144 (Ast0.Binary
(left
, clt2mcode (Ast.Logical ast_op
) op
, right
))
147 match cv
with None
-> ty
| Some x
-> Ast0.wrap
(Ast0.ConstVol
(x
,ty
))
151 match Ast0.unwrap x
with Ast0.Circles
(_
) -> true | _
-> false in
153 match Ast0.unwrap x
with Ast0.Stars
(_
) -> true | _
-> false in
154 if List.exists
circle l
155 then Ast0.wrap
(Ast0.CIRCLES
(l
))
157 if List.exists
star l
158 then Ast0.wrap
(Ast0.STARS
(l
))
159 else Ast0.wrap
(Ast0.DOTS
(l
))
161 (* here the offset is that of the first in the sequence of *s, not that of
162 each * individually *)
163 let pointerify ty m
=
167 Ast0.wrap
(Ast0.Pointer
(inner
,clt2mcode "*" cur
)))
170 let ty_pointerify ty m
=
172 (function inner
-> function cur
-> Type_cocci.Pointer
(inner
))
175 (* Left is <=>, Right is =>. Collect <=>s. *)
176 (* The parser should have done this, with precedences. But whatever... *)
177 let iso_adjust fn first rest
=
178 let rec loop = function
180 | (Common.Left x
)::rest
->
181 (match loop rest
with
182 front
::after
-> (fn x
::front
)::after
183 | _
-> failwith
"not possible")
184 | (Common.Right x
)::rest
->
185 (match loop rest
with
186 front
::after
-> []::(fn x
::front
)::after
187 | _
-> failwith
"not possible") in
189 front
::after
-> (fn first
::front
)::after
190 | _
-> failwith
"not possible"
193 let lookup rule name
=
195 let info = Hashtbl.find
Data.all_metadecls rule
in
196 List.find
(function mv
-> Ast.get_meta_name mv
= (rule
,name
)) info
200 (Semantic_cocci.Semantic
201 ("bad rule "^rule^
" or bad variable "^name
)) in
203 Ast.MetaIdDecl
(Ast.NONE
,(rule
,name
)) ->
204 (match lookup rule name
with
205 Ast.MetaIdDecl
(_
,_
) | Ast.MetaFreshIdDecl
(_
,_
) -> ()
208 (Semantic_cocci.Semantic
209 ("incompatible inheritance declaration "^name
)))
210 | Ast.MetaFreshIdDecl
((rule
,name
),seed
) ->
212 (Semantic_cocci.Semantic
213 "can't inherit the freshness of an identifier")
214 | Ast.MetaListlenDecl
((rule
,name
)) ->
215 (match lookup rule name
with
216 Ast.MetaListlenDecl
(_
) -> ()
219 (Semantic_cocci.Semantic
220 ("incompatible inheritance declaration "^name
)))
221 | Ast.MetaTypeDecl
(Ast.NONE
,(rule
,name
)) ->
222 (match lookup rule name
with
223 Ast.MetaTypeDecl
(_
,_
) -> ()
226 (Semantic_cocci.Semantic
227 ("incompatible inheritance declaration "^name
)))
228 | Ast.MetaInitDecl
(Ast.NONE
,(rule
,name
)) ->
229 (match lookup rule name
with
230 Ast.MetaInitDecl
(_
,_
) -> ()
233 (Semantic_cocci.Semantic
234 ("incompatible inheritance declaration "^name
)))
235 | Ast.MetaParamDecl
(Ast.NONE
,(rule
,name
)) ->
236 (match lookup rule name
with
237 Ast.MetaParamDecl
(_
,_
) -> ()
240 (Semantic_cocci.Semantic
241 ("incompatible inheritance declaration "^name
)))
242 | Ast.MetaParamListDecl
(Ast.NONE
,(rule
,name
),len_name
) ->
243 (match lookup rule name
with
244 Ast.MetaParamListDecl
(_
,_
,_
) -> ()
247 (Semantic_cocci.Semantic
248 ("incompatible inheritance declaration "^name
)))
249 | Ast.MetaErrDecl
(Ast.NONE
,(rule
,name
)) ->
250 (match lookup rule name
with
251 Ast.MetaErrDecl
(_
,_
) -> ()
254 (Semantic_cocci.Semantic
255 ("incompatible inheritance declaration "^name
)))
256 | Ast.MetaExpDecl
(Ast.NONE
,(rule
,name
),ty
) ->
257 (match lookup rule name
with
258 Ast.MetaExpDecl
(_
,_
,ty1
) when ty
= ty1
-> ()
261 (Semantic_cocci.Semantic
262 ("incompatible inheritance declaration "^name
)))
263 | Ast.MetaIdExpDecl
(Ast.NONE
,(rule
,name
),ty
) ->
264 (match lookup rule name
with
265 Ast.MetaIdExpDecl
(_
,_
,ty1
) when ty
= ty1
-> ()
268 (Semantic_cocci.Semantic
269 ("incompatible inheritance declaration "^name
)))
270 | Ast.MetaLocalIdExpDecl
(Ast.NONE
,(rule
,name
),ty
) ->
271 (match lookup rule name
with
272 Ast.MetaLocalIdExpDecl
(_
,_
,ty1
) when ty
= ty1
-> ()
275 (Semantic_cocci.Semantic
276 ("incompatible inheritance declaration "^name
)))
277 | Ast.MetaExpListDecl
(Ast.NONE
,(rule
,name
),len_name
) ->
278 (match lookup rule name
with
279 Ast.MetaExpListDecl
(_
,_
,_
) -> ()
280 | Ast.MetaParamListDecl
(_
,_
,_
) when not
(!Flag.make_hrule
= None
) -> ()
283 (Semantic_cocci.Semantic
284 ("incompatible inheritance declaration "^name
)))
285 | Ast.MetaStmDecl
(Ast.NONE
,(rule
,name
)) ->
286 (match lookup rule name
with
287 Ast.MetaStmDecl
(_
,_
) -> ()
290 (Semantic_cocci.Semantic
291 ("incompatible inheritance declaration "^name
)))
292 | Ast.MetaStmListDecl
(Ast.NONE
,(rule
,name
)) ->
293 (match lookup rule name
with
294 Ast.MetaStmListDecl
(_
,_
) -> ()
297 (Semantic_cocci.Semantic
298 ("incompatible inheritance declaration "^name
)))
299 | Ast.MetaFuncDecl
(Ast.NONE
,(rule
,name
)) ->
300 (match lookup rule name
with
301 Ast.MetaFuncDecl
(_
,_
) -> ()
304 (Semantic_cocci.Semantic
305 ("incompatible inheritance declaration "^name
)))
306 | Ast.MetaLocalFuncDecl
(Ast.NONE
,(rule
,name
)) ->
307 (match lookup rule name
with
308 Ast.MetaLocalFuncDecl
(_
,_
) -> ()
311 (Semantic_cocci.Semantic
312 ("incompatible inheritance declaration "^name
)))
313 | Ast.MetaConstDecl
(Ast.NONE
,(rule
,name
),ty
) ->
314 (match lookup rule name
with
315 Ast.MetaConstDecl
(_
,_
,ty1
) when ty
= ty1
-> ()
318 (Semantic_cocci.Semantic
319 ("incompatible inheritance declaration "^name
)))
320 | Ast.MetaPosDecl
(Ast.NONE
,(rule
,name
)) ->
321 (match lookup rule name
with
322 Ast.MetaPosDecl
(_
,_
) ->
323 if not
(List.mem rule
!Data.inheritable_positions
)
326 (Semantic_cocci.Semantic
327 ("position cannot be inherited over modifications: "^name
))
330 (Semantic_cocci.Semantic
331 ("incompatible inheritance declaration "^name
)))
334 (Semantic_cocci.Semantic
("arity not allowed on imported declaration"))
336 let create_metadec ar ispure kindfn ids current_rule
=
339 (function (rule
,nm
) ->
342 None
-> ((current_rule
,nm
),function x
-> [Common.Left x
])
345 function x
-> check_meta x
; [Common.Right x
]) in
346 kindfn ar rule ispure checker
)
349 let create_fresh_metadec kindfn ids current_rule
=
352 (function ((rule
,nm
),seed
) ->
355 None
-> ((current_rule
,nm
),function x
-> [Common.Left x
])
358 function x
-> check_meta x
; [Common.Right x
]) in
359 kindfn rule checker seed
)
362 let create_metadec_with_constraints ar ispure kindfn ids current_rule
=
365 (function ((rule
,nm
),constraints
) ->
368 None
-> ((current_rule
,nm
),function x
-> [Common.Left x
])
371 function x
-> check_meta x
; [Common.Right x
]) in
372 kindfn ar rule ispure checker constraints
)
375 let create_metadec_ty ar ispure kindfn ids current_rule
=
378 (function ((rule
,nm
),constraints
) ->
381 None
-> ((current_rule
,nm
),function x
-> [Common.Left x
])
384 function x
-> check_meta x
; [Common.Right x
]) in
385 kindfn ar rule ispure checker constraints
)
388 let create_len_metadec ar ispure kindfn lenid ids current_rule
=
390 create_metadec Ast.NONE
Ast0.Impure
391 (fun _ name _
check_meta -> check_meta(Ast.MetaListlenDecl
(name
)))
392 [lenid
] current_rule
in
395 [Common.Left
(Ast.MetaListlenDecl
(x
))] -> x
396 | [Common.Right
(Ast.MetaListlenDecl
(x
))] -> x
397 | _
-> failwith
"unexpected length declaration" in
398 lendec@(create_metadec ar ispure
(kindfn
lenname) ids current_rule
)
400 (* ---------------------------------------------------------------------- *)
403 let elements = Str.split
(Str.regexp
"/") s
in
404 List.map
(function "..." -> Ast.IncDots
| s
-> Ast.IncPath s
) elements
406 (* ---------------------------------------------------------------------- *)
410 let (nm
,pure
,clt
) = name
in
411 Ast0.wrap
(Ast0.MetaStmt
(clt2mcode nm clt
,pure
))
414 Ast0.wrap
(Ast0.ExprStatement
(exp
, clt2mcode ";" pv
))
416 let ifthen iff lp tst rp thn
=
417 Ast0.wrap
(Ast0.IfThen
(clt2mcode "if" iff
,
418 clt2mcode "(" lp
,tst
,clt2mcode ")" rp
,thn
,
419 (Ast0.default_info
(),Ast0.context_befaft
())))
421 let ifthenelse iff lp tst rp thn e els
=
422 Ast0.wrap
(Ast0.IfThenElse
(clt2mcode "if" iff
,
423 clt2mcode "(" lp
,tst
,clt2mcode ")" rp
,thn
,
424 clt2mcode "else" e
,els
,
425 (Ast0.default_info
(),Ast0.context_befaft
())))
427 let forloop fr lp e1 sc1 e2 sc2 e3 rp s
=
428 Ast0.wrap
(Ast0.For
(clt2mcode "for" fr
,clt2mcode "(" lp
,e1
,
429 clt2mcode ";" sc1
,e2
,
430 clt2mcode ";" sc2
,e3
,clt2mcode ")" rp
,s
,
431 (Ast0.default_info
(),Ast0.context_befaft
())))
433 let whileloop w lp e rp s
=
434 Ast0.wrap
(Ast0.While
(clt2mcode "while" w
,clt2mcode "(" lp
,
435 e
,clt2mcode ")" rp
,s
,
436 (Ast0.default_info
(),Ast0.context_befaft
())))
438 let doloop d s w lp e rp pv
=
439 Ast0.wrap
(Ast0.Do
(clt2mcode "do" d
,s
,clt2mcode "while" w
,
440 clt2mcode "(" lp
,e
,clt2mcode ")" rp
,
443 let iterator i lp e rp s
=
444 Ast0.wrap
(Ast0.Iterator
(i
,clt2mcode "(" lp
,e
,clt2mcode ")" rp
,s
,
445 (Ast0.default_info
(),Ast0.context_befaft
())))
447 let switch s lp e rp lb d c rb
=
451 Ast0.wrap
(Ast0.Decl
((Ast0.default_info
(),Ast0.context_befaft
()),d)))
453 Ast0.wrap
(Ast0.Switch
(clt2mcode "switch" s
,clt2mcode "(" lp
,e
,
454 clt2mcode ")" rp
,clt2mcode "{" lb
,
455 Ast0.wrap
(Ast0.DOTS
(d)),
456 Ast0.wrap
(Ast0.DOTS
(c
)),clt2mcode "}" rb
))
459 Ast0.wrap
(Ast0.ReturnExpr
(clt2mcode "return" r
,e
,clt2mcode ";" pv
))
462 Ast0.wrap
(Ast0.Return
(clt2mcode "return" r
,clt2mcode ";" pv
))
465 Ast0.wrap
(Ast0.Break
(clt2mcode "break" b
,clt2mcode ";" pv
))
468 Ast0.wrap
(Ast0.Continue
(clt2mcode "continue" c
,clt2mcode ";" pv
))
471 Ast0.wrap
(Ast0.Label
(i
,clt2mcode ":" dd
))
474 Ast0.wrap
(Ast0.Goto
(clt2mcode "goto" g
,i
,clt2mcode ";" pv
))
477 Ast0.wrap
(Ast0.Seq
(clt2mcode "{" lb
,s
,clt2mcode "}" rb
))
479 (* ---------------------------------------------------------------------- *)
481 let make_iso_rule_name_result n
=
482 (try let _ = Hashtbl.find
Data.all_metadecls n
in
483 raise
(Semantic_cocci.Semantic
("repeated rule name"))
484 with Not_found
-> ());
485 Ast.CocciRulename
(Some n
,Ast.NoDep
,[],[],Ast.Undetermined
,false (*discarded*))
487 let make_cocci_rule_name_result nm
d i a e ee
=
490 let n = id2name nm
in
491 (try let _ = Hashtbl.find
Data.all_metadecls
n in
492 raise
(Semantic_cocci.Semantic
("repeated rule name"))
493 with Not_found
-> ());
494 Ast.CocciRulename
(Some
n,d,i
,a
,e
,ee
)
495 | None
-> Ast.CocciRulename
(None
,d,i
,a
,e
,ee
)
497 let make_generated_rule_name_result nm
d i a e ee
=
500 let n = id2name nm
in
501 (try let _ = Hashtbl.find
Data.all_metadecls
n in
502 raise
(Semantic_cocci.Semantic
("repeated rule name"))
503 with Not_found
-> ());
504 Ast.GeneratedRulename
(Some
n,d,i
,a
,e
,ee
)
505 | None
-> Ast.GeneratedRulename
(None
,d,i
,a
,e
,ee
)
507 let make_script_rule_name_result lang deps
=
508 let l = id2name lang
in
509 Ast.ScriptRulename
(l,deps
)
511 let make_initial_script_rule_name_result lang
=
512 let l = id2name lang
in
513 Ast.InitialScriptRulename
(l)
515 let make_final_script_rule_name_result lang
=
516 let l = id2name lang
in
517 Ast.FinalScriptRulename
(l)
519 (* Allows type alone only when it is void and only when there is only one
520 parameter. This avoids ambiguity problems in the parser. *)
521 let verify_parameter_declarations = function
524 (match Ast0.unwrap x
with
525 Ast0.Param
(t
, None
) ->
526 (match Ast0.unwrap t
with
527 Ast0.BaseType
(Ast.VoidType
,_) -> ()
531 "%d: only void can be a parameter without an identifier"
537 match Ast0.unwrap x
with
538 Ast0.Param
(t
, None
) ->
541 "%d: only void alone can be a parameter without an identifier"