2 * Copyright 2005-2010, 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
38 let get_option fn
= function
40 | Some x
-> Some
(fn x
)
42 let make_info line logical_line offset col strbef straft
=
44 {Ast0.line_start
= line
; Ast0.line_end
= line
;
45 Ast0.logical_start
= logical_line
; Ast0.logical_end
= logical_line
;
46 Ast0.column
= col
; Ast0.offset
= offset
; } in
47 { Ast0.pos_info
= new_pos_info;
48 Ast0.attachable_start
= true; Ast0.attachable_end
= true;
49 Ast0.mcode_start
= []; Ast0.mcode_end
= [];
50 Ast0.strings_before
= strbef
; Ast0.strings_after
= straft
; }
52 let clt2info (_
,line
,logical_line
,offset
,col
,strbef
,straft
,pos
) =
53 make_info line logical_line offset col strbef straft
55 let drop_bef (arity
,line
,lline
,offset
,col
,strbef
,straft
,pos
) =
56 (arity
,line
,lline
,offset
,col
,[],straft
,pos
)
58 let drop_aft (arity
,line
,lline
,offset
,col
,strbef
,straft
,pos
) =
59 (arity
,line
,lline
,offset
,col
,strbef
,[],pos
)
61 let drop_pos (arity
,line
,lline
,offset
,col
,strbef
,straft
,pos
) =
62 (arity
,line
,lline
,offset
,col
,strbef
,straft
,Ast0.NoMetaPos
)
64 let clt2mcode str
= function
65 (Data.MINUS
,line
,lline
,offset
,col
,strbef
,straft
,pos
) ->
66 (str
,Ast0.NONE
,make_info line lline offset col strbef straft
,
67 Ast0.MINUS
(ref([],Ast0.default_token_info
)),ref pos
,-1)
68 | (Data.OPTMINUS
,line
,lline
,offset
,col
,strbef
,straft
,pos
) ->
69 (str
,Ast0.OPT
,make_info line lline offset col strbef straft
,
70 Ast0.MINUS
(ref([],Ast0.default_token_info
)),ref pos
,-1)
71 | (Data.UNIQUEMINUS
,line
,lline
,offset
,col
,strbef
,straft
,pos
) ->
72 (str
,Ast0.UNIQUE
,make_info line lline offset col strbef straft
,
73 Ast0.MINUS
(ref([],Ast0.default_token_info
)),ref pos
,-1)
74 | (Data.PLUS
,line
,lline
,offset
,col
,strbef
,straft
,pos
) ->
75 (str
,Ast0.NONE
,make_info line lline offset col strbef straft
,
76 Ast0.PLUS
(Ast.ONE
),ref pos
,-1)
77 | (Data.PLUSPLUS
,line
,lline
,offset
,col
,strbef
,straft
,pos
) ->
78 (str
,Ast0.NONE
,make_info line lline offset col strbef straft
,
79 Ast0.PLUS
(Ast.MANY
),ref pos
,-1)
80 | (Data.CONTEXT
,line
,lline
,offset
,col
,strbef
,straft
,pos
) ->
81 (str
,Ast0.NONE
,make_info line lline offset col strbef straft
,
82 Ast0.CONTEXT
(ref(Ast.NOTHING
,
83 Ast0.default_token_info
,Ast0.default_token_info
)),
85 | (Data.OPT
,line
,lline
,offset
,col
,strbef
,straft
,pos
) ->
86 (str
,Ast0.OPT
,make_info line lline offset col strbef straft
,
87 Ast0.CONTEXT
(ref(Ast.NOTHING
,
88 Ast0.default_token_info
,Ast0.default_token_info
)),
90 | (Data.UNIQUE
,line
,lline
,offset
,col
,strbef
,straft
,pos
) ->
91 (str
,Ast0.UNIQUE
,make_info line lline offset col strbef straft
,
92 Ast0.CONTEXT
(ref(Ast.NOTHING
,
93 Ast0.default_token_info
,Ast0.default_token_info
)),
96 let id2name (name
, clt
) = name
97 let id2clt (name
, clt
) = clt
98 let id2mcode (name
, clt
) = clt2mcode name clt
100 let mkdots str
(dot
,whencode
) =
102 "..." -> Ast0.wrap
(Ast0.Dots
(clt2mcode str dot
, whencode
))
103 | "ooo" -> Ast0.wrap
(Ast0.Circles
(clt2mcode str dot
, whencode
))
104 | "***" -> Ast0.wrap
(Ast0.Stars
(clt2mcode str dot
, whencode
))
105 | _
-> failwith
"cannot happen"
107 let mkedots str
(dot
,whencode
) =
109 "..." -> Ast0.wrap
(Ast0.Edots
(clt2mcode str dot
, whencode
))
110 | "ooo" -> Ast0.wrap
(Ast0.Ecircles
(clt2mcode str dot
, whencode
))
111 | "***" -> Ast0.wrap
(Ast0.Estars
(clt2mcode str dot
, whencode
))
112 | _
-> failwith
"cannot happen"
114 let mkdpdots str dot
=
116 "..." -> Ast0.wrap
(Ast0.DPdots
(clt2mcode str dot
))
117 | "ooo" -> Ast0.wrap
(Ast0.DPcircles
(clt2mcode str dot
))
118 | _
-> failwith
"cannot happen"
120 let mkidots str
(dot
,whencode
) =
122 "..." -> Ast0.wrap
(Ast0.Idots
(clt2mcode str dot
, whencode
))
123 | _
-> failwith
"cannot happen"
125 let mkddots str
(dot
,whencode
) =
126 match (str
,whencode
) with
127 ("...",None
) -> Ast0.wrap
(Ast0.Ddots
(clt2mcode str dot
, None
))
128 | ("...",Some
[w
]) -> Ast0.wrap
(Ast0.Ddots
(clt2mcode str dot
, Some w
))
129 | _
-> failwith
"cannot happen"
131 let mkpdots str dot
=
133 "..." -> Ast0.wrap
(Ast0.Pdots
(clt2mcode str dot
))
134 | "ooo" -> Ast0.wrap
(Ast0.Pcircles
(clt2mcode str dot
))
135 | _
-> failwith
"cannot happen"
137 let arith_op ast_op left op right
=
139 (Ast0.Binary
(left
, clt2mcode (Ast.Arith ast_op
) op
, right
))
141 let logic_op ast_op left op right
=
143 (Ast0.Binary
(left
, clt2mcode (Ast.Logical ast_op
) op
, right
))
146 match cv
with None
-> ty
| Some x
-> Ast0.wrap
(Ast0.ConstVol
(x
,ty
))
150 match Ast0.unwrap x
with Ast0.Circles
(_
) -> true | _
-> false in
152 match Ast0.unwrap x
with Ast0.Stars
(_
) -> true | _
-> false in
153 if List.exists
circle l
154 then Ast0.wrap
(Ast0.CIRCLES
(l
))
156 if List.exists
star l
157 then Ast0.wrap
(Ast0.STARS
(l
))
158 else Ast0.wrap
(Ast0.DOTS
(l
))
160 (* here the offset is that of the first in the sequence of *s, not that of
161 each * individually *)
162 let pointerify ty m
=
166 Ast0.wrap
(Ast0.Pointer
(inner
,clt2mcode "*" cur
)))
169 let ty_pointerify ty m
=
171 (function inner
-> function cur
-> Type_cocci.Pointer
(inner
))
174 (* Left is <=>, Right is =>. Collect <=>s. *)
175 (* The parser should have done this, with precedences. But whatever... *)
176 let iso_adjust first_fn fn first rest
=
177 let rec loop = function
179 | (Common.Left x
)::rest
->
180 (match loop rest
with
181 front
::after
-> (fn x
::front
)::after
182 | _
-> failwith
"not possible")
183 | (Common.Right x
)::rest
->
184 (match loop rest
with
185 front
::after
-> []::(fn x
::front
)::after
186 | _
-> failwith
"not possible") in
188 front
::after
-> (first_fn first
::front
)::after
189 | _
-> failwith
"not possible"
192 let lookup rule name
=
194 let info = Hashtbl.find
Data.all_metadecls rule
in
195 List.find
(function mv
-> Ast.get_meta_name mv
= (rule
,name
)) info
199 (Semantic_cocci.Semantic
200 ("bad rule "^rule^
" or bad variable "^name
)) in
202 Ast.MetaIdDecl
(Ast.NONE
,(rule
,name
)) ->
203 (match lookup rule name
with
204 Ast.MetaIdDecl
(_
,_
) | Ast.MetaFreshIdDecl
(_
,_
) -> ()
207 (Semantic_cocci.Semantic
208 ("incompatible inheritance declaration "^name
)))
209 | Ast.MetaFreshIdDecl
((rule
,name
),seed
) ->
211 (Semantic_cocci.Semantic
212 "can't inherit the freshness of an identifier")
213 | Ast.MetaListlenDecl
((rule
,name
)) ->
214 (match lookup rule name
with
215 Ast.MetaListlenDecl
(_
) -> ()
218 (Semantic_cocci.Semantic
219 ("incompatible inheritance declaration "^name
)))
220 | Ast.MetaTypeDecl
(Ast.NONE
,(rule
,name
)) ->
221 (match lookup rule name
with
222 Ast.MetaTypeDecl
(_
,_
) -> ()
225 (Semantic_cocci.Semantic
226 ("incompatible inheritance declaration "^name
)))
227 | Ast.MetaInitDecl
(Ast.NONE
,(rule
,name
)) ->
228 (match lookup rule name
with
229 Ast.MetaInitDecl
(_
,_
) -> ()
232 (Semantic_cocci.Semantic
233 ("incompatible inheritance declaration "^name
)))
234 | Ast.MetaParamDecl
(Ast.NONE
,(rule
,name
)) ->
235 (match lookup rule name
with
236 Ast.MetaParamDecl
(_
,_
) -> ()
239 (Semantic_cocci.Semantic
240 ("incompatible inheritance declaration "^name
)))
241 | Ast.MetaParamListDecl
(Ast.NONE
,(rule
,name
),len_name
) ->
242 (match lookup rule name
with
243 Ast.MetaParamListDecl
(_
,_
,_
) -> ()
246 (Semantic_cocci.Semantic
247 ("incompatible inheritance declaration "^name
)))
248 | Ast.MetaErrDecl
(Ast.NONE
,(rule
,name
)) ->
249 (match lookup rule name
with
250 Ast.MetaErrDecl
(_
,_
) -> ()
253 (Semantic_cocci.Semantic
254 ("incompatible inheritance declaration "^name
)))
255 | Ast.MetaExpDecl
(Ast.NONE
,(rule
,name
),ty
) ->
256 (match lookup rule name
with
257 Ast.MetaExpDecl
(_
,_
,ty1
) when ty
= ty1
-> ()
260 (Semantic_cocci.Semantic
261 ("incompatible inheritance declaration "^name
)))
262 | Ast.MetaIdExpDecl
(Ast.NONE
,(rule
,name
),ty
) ->
263 (match lookup rule name
with
264 Ast.MetaIdExpDecl
(_
,_
,ty1
) when ty
= ty1
-> ()
267 (Semantic_cocci.Semantic
268 ("incompatible inheritance declaration "^name
)))
269 | Ast.MetaLocalIdExpDecl
(Ast.NONE
,(rule
,name
),ty
) ->
270 (match lookup rule name
with
271 Ast.MetaLocalIdExpDecl
(_
,_
,ty1
) when ty
= ty1
-> ()
274 (Semantic_cocci.Semantic
275 ("incompatible inheritance declaration "^name
)))
276 | Ast.MetaExpListDecl
(Ast.NONE
,(rule
,name
),len_name
) ->
277 (match lookup rule name
with
278 Ast.MetaExpListDecl
(_
,_
,_
) -> ()
279 | Ast.MetaParamListDecl
(_
,_
,_
) when not
(!Flag.make_hrule
= None
) -> ()
282 (Semantic_cocci.Semantic
283 ("incompatible inheritance declaration "^name
)))
284 | Ast.MetaStmDecl
(Ast.NONE
,(rule
,name
)) ->
285 (match lookup rule name
with
286 Ast.MetaStmDecl
(_
,_
) -> ()
289 (Semantic_cocci.Semantic
290 ("incompatible inheritance declaration "^name
)))
291 | Ast.MetaStmListDecl
(Ast.NONE
,(rule
,name
)) ->
292 (match lookup rule name
with
293 Ast.MetaStmListDecl
(_
,_
) -> ()
296 (Semantic_cocci.Semantic
297 ("incompatible inheritance declaration "^name
)))
298 | Ast.MetaFuncDecl
(Ast.NONE
,(rule
,name
)) ->
299 (match lookup rule name
with
300 Ast.MetaFuncDecl
(_
,_
) -> ()
303 (Semantic_cocci.Semantic
304 ("incompatible inheritance declaration "^name
)))
305 | Ast.MetaLocalFuncDecl
(Ast.NONE
,(rule
,name
)) ->
306 (match lookup rule name
with
307 Ast.MetaLocalFuncDecl
(_
,_
) -> ()
310 (Semantic_cocci.Semantic
311 ("incompatible inheritance declaration "^name
)))
312 | Ast.MetaConstDecl
(Ast.NONE
,(rule
,name
),ty
) ->
313 (match lookup rule name
with
314 Ast.MetaConstDecl
(_
,_
,ty1
) when ty
= ty1
-> ()
317 (Semantic_cocci.Semantic
318 ("incompatible inheritance declaration "^name
)))
319 | Ast.MetaPosDecl
(Ast.NONE
,(rule
,name
)) ->
320 (match lookup rule name
with
321 Ast.MetaPosDecl
(_
,_
) ->
322 if not
(List.mem rule
!Data.inheritable_positions
)
325 (Semantic_cocci.Semantic
326 ("position cannot be inherited over modifications: "^name
))
329 (Semantic_cocci.Semantic
330 ("incompatible inheritance declaration "^name
)))
333 (Semantic_cocci.Semantic
("arity not allowed on imported declaration"))
335 let create_metadec ar ispure kindfn ids current_rule
=
338 (function (rule
,nm
) ->
341 None
-> ((current_rule
,nm
),function x
-> [Common.Left x
])
344 function x
-> check_meta x
; [Common.Right x
]) in
345 kindfn ar rule ispure checker
)
349 let create_metadec_virt ar ispure kindfn ids current_rule
=
353 let checker = function x
-> [Common.Right x
] in
354 kindfn ar nm ispure
checker !Flag.defined_virtual_env
)
357 let create_fresh_metadec kindfn ids current_rule
=
360 (function ((rule
,nm
),seed
) ->
363 None
-> ((current_rule
,nm
),function x
-> [Common.Left x
])
366 function x
-> check_meta x
; [Common.Right x
]) in
367 kindfn rule
checker seed
)
370 let create_metadec_with_constraints ar ispure kindfn ids current_rule
=
373 (function ((rule
,nm
),constraints
) ->
376 None
-> ((current_rule
,nm
),function x
-> [Common.Left x
])
379 function x
-> check_meta x
; [Common.Right x
]) in
380 kindfn ar rule ispure
checker constraints
)
383 let create_metadec_ty ar ispure kindfn ids current_rule
=
386 (function ((rule
,nm
),constraints
) ->
389 None
-> ((current_rule
,nm
),function x
-> [Common.Left x
])
392 function x
-> check_meta x
; [Common.Right x
]) in
393 kindfn ar rule ispure
checker constraints
)
396 let create_len_metadec ar ispure kindfn lenid ids current_rule
=
398 create_metadec Ast.NONE
Ast0.Impure
399 (fun _ name _
check_meta -> check_meta(Ast.MetaListlenDecl
(name
)))
400 [lenid
] current_rule
in
403 [Common.Left
(Ast.MetaListlenDecl
(x
))] -> x
404 | [Common.Right
(Ast.MetaListlenDecl
(x
))] -> x
405 | _
-> failwith
"unexpected length declaration" in
406 lendec@(create_metadec ar ispure
(kindfn
lenname) ids current_rule
)
408 (* ---------------------------------------------------------------------- *)
411 let elements = Str.split
(Str.regexp
"/") s
in
412 List.map
(function "..." -> Ast.IncDots
| s
-> Ast.IncPath s
) elements
414 (* ---------------------------------------------------------------------- *)
418 let (nm
,pure
,clt
) = name
in
419 Ast0.wrap
(Ast0.MetaStmt
(clt2mcode nm clt
,pure
))
422 Ast0.wrap
(Ast0.ExprStatement
(exp
, clt2mcode ";" pv
))
424 let ifthen iff lp tst rp thn
=
425 Ast0.wrap
(Ast0.IfThen
(clt2mcode "if" iff
,
426 clt2mcode "(" lp
,tst
,clt2mcode ")" rp
,thn
,
427 (Ast0.default_info
(),Ast0.context_befaft
())))
429 let ifthenelse iff lp tst rp thn e els
=
430 Ast0.wrap
(Ast0.IfThenElse
(clt2mcode "if" iff
,
431 clt2mcode "(" lp
,tst
,clt2mcode ")" rp
,thn
,
432 clt2mcode "else" e
,els
,
433 (Ast0.default_info
(),Ast0.context_befaft
())))
435 let forloop fr lp e1 sc1 e2 sc2 e3 rp s
=
436 Ast0.wrap
(Ast0.For
(clt2mcode "for" fr
,clt2mcode "(" lp
,e1
,
437 clt2mcode ";" sc1
,e2
,
438 clt2mcode ";" sc2
,e3
,clt2mcode ")" rp
,s
,
439 (Ast0.default_info
(),Ast0.context_befaft
())))
441 let whileloop w lp e rp s
=
442 Ast0.wrap
(Ast0.While
(clt2mcode "while" w
,clt2mcode "(" lp
,
443 e
,clt2mcode ")" rp
,s
,
444 (Ast0.default_info
(),Ast0.context_befaft
())))
446 let doloop d s w lp e rp pv
=
447 Ast0.wrap
(Ast0.Do
(clt2mcode "do" d
,s
,clt2mcode "while" w
,
448 clt2mcode "(" lp
,e
,clt2mcode ")" rp
,
451 let iterator i lp e rp s
=
452 Ast0.wrap
(Ast0.Iterator
(i
,clt2mcode "(" lp
,e
,clt2mcode ")" rp
,s
,
453 (Ast0.default_info
(),Ast0.context_befaft
())))
455 let switch s lp e rp lb d c rb
=
459 Ast0.wrap
(Ast0.Decl
((Ast0.default_info
(),Ast0.context_befaft
()),d)))
461 Ast0.wrap
(Ast0.Switch
(clt2mcode "switch" s
,clt2mcode "(" lp
,e
,
462 clt2mcode ")" rp
,clt2mcode "{" lb
,
463 Ast0.wrap
(Ast0.DOTS
(d)),
464 Ast0.wrap
(Ast0.DOTS
(c
)),clt2mcode "}" rb
))
467 Ast0.wrap
(Ast0.ReturnExpr
(clt2mcode "return" r
,e
,clt2mcode ";" pv
))
470 Ast0.wrap
(Ast0.Return
(clt2mcode "return" r
,clt2mcode ";" pv
))
473 Ast0.wrap
(Ast0.Break
(clt2mcode "break" b
,clt2mcode ";" pv
))
476 Ast0.wrap
(Ast0.Continue
(clt2mcode "continue" c
,clt2mcode ";" pv
))
479 Ast0.wrap
(Ast0.Label
(i
,clt2mcode ":" dd
))
482 Ast0.wrap
(Ast0.Goto
(clt2mcode "goto" g
,i
,clt2mcode ";" pv
))
485 Ast0.wrap
(Ast0.Seq
(clt2mcode "{" lb
,s
,clt2mcode "}" rb
))
487 (* ---------------------------------------------------------------------- *)
489 let make_iso_rule_name_result n
=
490 (try let _ = Hashtbl.find
Data.all_metadecls n
in
491 raise
(Semantic_cocci.Semantic
("repeated rule name"))
492 with Not_found
-> ());
493 Ast.CocciRulename
(Some n
,Ast.NoDep
,[],[],Ast.Undetermined
,false (*discarded*))
495 let make_cocci_rule_name_result nm
d i a e ee
=
498 let n = id2name nm
in
499 (try let _ = Hashtbl.find
Data.all_metadecls
n in
500 raise
(Semantic_cocci.Semantic
("repeated rule name"))
501 with Not_found
-> ());
502 Ast.CocciRulename
(Some
n,d,i
,a
,e
,ee
)
503 | None
-> Ast.CocciRulename
(None
,d,i
,a
,e
,ee
)
505 let make_generated_rule_name_result nm
d i a e ee
=
508 let n = id2name nm
in
509 (try let _ = Hashtbl.find
Data.all_metadecls
n in
510 raise
(Semantic_cocci.Semantic
("repeated rule name"))
511 with Not_found
-> ());
512 Ast.GeneratedRulename
(Some
n,d,i
,a
,e
,ee
)
513 | None
-> Ast.GeneratedRulename
(None
,d,i
,a
,e
,ee
)
515 let make_script_rule_name_result lang deps
=
516 let l = id2name lang
in
517 Ast.ScriptRulename
(l,deps
)
519 let make_initial_script_rule_name_result lang deps
=
520 let l = id2name lang
in
521 Ast.InitialScriptRulename
(l,deps
)
523 let make_final_script_rule_name_result lang deps
=
524 let l = id2name lang
in
525 Ast.FinalScriptRulename
(l,deps
)
527 (* Allows type alone only when it is void and only when there is only one
528 parameter. This avoids ambiguity problems in the parser. *)
529 let verify_parameter_declarations = function
532 (match Ast0.unwrap x
with
533 Ast0.Param
(t
, None
) ->
534 (match Ast0.unwrap t
with
535 Ast0.BaseType
(Ast.VoidType
,_) -> ()
539 "%d: only void can be a parameter without an identifier"
545 match Ast0.unwrap x
with
546 Ast0.Param
(t
, None
) ->
549 "%d: only void alone can be a parameter without an identifier"