| 1 |
(* |
| 2 |
* This file is part of Coccinelle, licensed under the terms of the GPL v2. |
| 3 |
* See copyright.txt in the Coccinelle source code for more information. |
| 4 |
* The Coccinelle source code can be obtained at http://coccinelle.lip6.fr |
| 5 |
*) |
| 6 |
|
| 7 |
module Ast0 = Ast0_cocci |
| 8 |
module Snap = Snapshot |
| 9 |
module PG = Position_generator |
| 10 |
|
| 11 |
(* ------------------------------------------------------------------------- *) |
| 12 |
|
| 13 |
(* Returns snapshot that has both updated result (added rule with no stars) |
| 14 |
* and updated disj_result (rule with stars). |
| 15 |
*) |
| 16 |
|
| 17 |
(* ------------------------------------------------------------------------- *) |
| 18 |
(* TYPE HANDLER FUNCTIONS *) |
| 19 |
|
| 20 |
type statement_dots_fn = Ast0.statement Ast0.dots -> Snap.t -> Snap.t |
| 21 |
|
| 22 |
type string_fn = string Ast0.mcode -> Snap.t -> Snap.t |
| 23 |
|
| 24 |
type statement_fn = Ast0.statement -> Snap.t -> Snap.t |
| 25 |
|
| 26 |
type expression_fn = Ast0.expression -> Snap.t -> Snap.t |
| 27 |
|
| 28 |
type ident_fn = Ast0.ident -> Snap.t -> Snap.t |
| 29 |
|
| 30 |
type declaration_fn = Ast0.declaration -> Snap.t -> Snap.t |
| 31 |
|
| 32 |
type field_fn = Ast0.field -> Snap.t -> Snap.t |
| 33 |
|
| 34 |
(* ------------------------------------------------------------------------- *) |
| 35 |
(* DISJUNCTION HANDLER *) |
| 36 |
|
| 37 |
let ( >> ) f g x = g (f x) |
| 38 |
|
| 39 |
(* given the components of a disjunction + functions to handle them + snapshot: |
| 40 |
* returns new snapshot that has the disjunction added in the context rule. |
| 41 |
* this may include splitting the rule and/or adding stars where appropriate. |
| 42 |
*) |
| 43 |
let handle_disj |
| 44 |
~lp (* left parenthesis, string mcode *) |
| 45 |
~rp (* right parenthesis, string mcode *) |
| 46 |
~pipes (* separator pipes, string mcode list *) |
| 47 |
~cases (* disjunction cases, 'a list *) |
| 48 |
~casefn (* function to handle one disj case, 'a -> snapshot -> snapshot *) |
| 49 |
~singlefn (* casefn for only one patch, same type as casefn *) |
| 50 |
~strfn (* string mcode handler, string mcode -> snapshot -> snapshot *) |
| 51 |
~at_top (* true: disj is the only thing so don't add another rule, bool *) |
| 52 |
snapshot = |
| 53 |
|
| 54 |
let index = Ast0.get_mcode_line lp in |
| 55 |
let boollist = Snap.get_disj index snapshot in |
| 56 |
let combined = List.combine cases boollist in |
| 57 |
|
| 58 |
(* determine if all or none are patches *) |
| 59 |
let all_same = function [] -> true | x :: xs -> List.for_all (( = ) x) xs in |
| 60 |
|
| 61 |
(* true if multiple patch cases; ie. we want disjunction parentheses *) |
| 62 |
let mult_stmt = List.length (List.filter (fun x -> x) boollist) <> 1 in |
| 63 |
let casefn = if mult_stmt then casefn else singlefn in |
| 64 |
|
| 65 |
(* keep the same positions if several disjunctions *) |
| 66 |
let freeze_pos = if mult_stmt then Snap.do_freeze_pos else (fun x -> x) in |
| 67 |
|
| 68 |
(* handle each disjunction case one at a time |
| 69 |
* setmodefn is the function that sets the generation mode of the snapshot. |
| 70 |
* tblist is a list of (disj case, whether it is a patch) |
| 71 |
*) |
| 72 |
let handle_cases set_modefn tblist pipes = |
| 73 |
let rec handle_cases' tblist pipes fn = |
| 74 |
match tblist, pipes with |
| 75 |
| [(t,b)], [] -> |
| 76 |
fn >> set_modefn b >> casefn t |
| 77 |
| (t,b) :: ts, p :: ps -> |
| 78 |
handle_cases' ts ps (fn >> set_modefn b >> casefn t >> strfn p) |
| 79 |
| _ -> |
| 80 |
assert false (* should be exactly one more stmt than pipes *) in |
| 81 |
handle_cases' tblist pipes (fun x -> x) in |
| 82 |
|
| 83 |
let disj = |
| 84 |
|
| 85 |
(* CASE 1: all or none are patches or toplevel, no extra rule needed, |
| 86 |
* always generate positions (unless we're in no_gen mode). |
| 87 |
*) |
| 88 |
if at_top || all_same boollist then begin |
| 89 |
let handle_no_gen = handle_cases (fun _ y -> y) in |
| 90 |
strfn lp >> handle_no_gen combined pipes >> strfn rp |
| 91 |
end |
| 92 |
|
| 93 |
(* CASE 2: only some are patches, generate extra rule (disj result) *) |
| 94 |
else begin |
| 95 |
|
| 96 |
(* if b is true, DO generate positions/stars and add to disj result *) |
| 97 |
let set_add_disj b = Snap.set_no_gen (not b) >> Snap.set_disj_mode b in |
| 98 |
let handle_do_gen = handle_cases set_add_disj in |
| 99 |
|
| 100 |
Snap.init_disj_result |
| 101 |
>> set_add_disj mult_stmt >> strfn lp |
| 102 |
>> handle_do_gen combined pipes |
| 103 |
>> set_add_disj mult_stmt >> strfn rp |
| 104 |
>> set_add_disj true |
| 105 |
end in |
| 106 |
|
| 107 |
freeze_pos disj snapshot |
| 108 |
|
| 109 |
|
| 110 |
(* ------------------------------------------------------------------------- *) |
| 111 |
(* ENTRY POINT *) |
| 112 |
|
| 113 |
(* These functions all return a snapshot that has the extra disjunction rule |
| 114 |
* generated (if necessary) *) |
| 115 |
|
| 116 |
(* The at_top flag means that the code is not surrounded by starrable |
| 117 |
* components, ie. it should not be split into two rules. (it needs to be more |
| 118 |
* accurate; see rule_body.ml) |
| 119 |
*) |
| 120 |
|
| 121 |
(* Returns snapshot that has added generated statement disjunction rule *) |
| 122 |
let generate_statement ~stmtdotsfn ~strfn ~stmtfn ~stmt ~at_top = |
| 123 |
|
| 124 |
(* inserts one position if in generation mode. |
| 125 |
* We only want one position per disjunction case (since they all have the |
| 126 |
* same metaposition), so just add position to first possible case. |
| 127 |
*) |
| 128 |
let sdotsfn sd snp = |
| 129 |
let std_no_pos = List.fold_left (fun a b -> a >> stmtfn b) (fun x -> x) in |
| 130 |
let rec std' l snp = |
| 131 |
match l with |
| 132 |
| [] -> assert false (* no disj patches with only unpositionable cases *) |
| 133 |
| x::xs -> |
| 134 |
(match PG.statement_pos x snp with |
| 135 |
| Some (x, snp) -> |
| 136 |
(Snap.set_no_gen true |
| 137 |
>> std_no_pos (x::xs) |
| 138 |
>> Snap.set_no_gen false) snp |
| 139 |
| None -> std' xs (stmtfn x snp)) in |
| 140 |
let add_pos_function = if Snap.no_gen snp then std_no_pos else std' in |
| 141 |
add_pos_function (Ast0.unwrap sd) snp in |
| 142 |
|
| 143 |
match Ast0.unwrap stmt with |
| 144 |
| Ast0.Disj(lp, sdlist, pipes, rp) -> |
| 145 |
handle_disj ~lp ~rp ~pipes ~cases:sdlist ~casefn:sdotsfn |
| 146 |
~singlefn:stmtdotsfn ~strfn ~at_top |
| 147 |
| _ -> failwith "only disj allowed in here" |
| 148 |
|
| 149 |
|
| 150 |
(* Returns snapshot that has added generated expression disjunction rule *) |
| 151 |
let generate_expression ~strfn ~exprfn ~expr ~at_top s = |
| 152 |
|
| 153 |
(* inserts one position if in generation mode *) |
| 154 |
let expposfn e snp = |
| 155 |
if Snap.no_gen snp then |
| 156 |
exprfn e snp |
| 157 |
else |
| 158 |
match PG.expression_pos e snp with |
| 159 |
| Some (ee, snp) -> exprfn ee snp |
| 160 |
| None -> failwith "no unpos cases" in |
| 161 |
|
| 162 |
match Ast0.unwrap expr with |
| 163 |
| Ast0.DisjExpr(lp, elist, pipes, rp) -> |
| 164 |
handle_disj |
| 165 |
~lp ~rp ~pipes ~cases:elist |
| 166 |
~casefn:expposfn ~singlefn:expposfn ~strfn ~at_top s |
| 167 |
| _ -> failwith "only disj allowed in here" |
| 168 |
|
| 169 |
|
| 170 |
(* Returns snapshot that has added generated ident disjunction rule *) |
| 171 |
let generate_ident ~strfn ~identfn ~ident ~at_top s = |
| 172 |
|
| 173 |
(* inserts one position if in generation mode *) |
| 174 |
let idposfn i snp = |
| 175 |
if Snap.no_gen snp then |
| 176 |
identfn i snp |
| 177 |
else |
| 178 |
let (i, snp) = PG.ident_pos i snp in |
| 179 |
identfn i snp in |
| 180 |
|
| 181 |
match Ast0.unwrap ident with |
| 182 |
| Ast0.DisjId(lp, ilist, pipes, rp) -> |
| 183 |
handle_disj |
| 184 |
~lp ~rp ~pipes ~cases:ilist |
| 185 |
~casefn:idposfn ~singlefn:idposfn ~strfn ~at_top s |
| 186 |
| _ -> failwith "only disj allowed in here" |
| 187 |
|
| 188 |
|
| 189 |
(* Returns snapshot that has added generated declaration disjunction rule *) |
| 190 |
let generate_declaration ~strfn ~declfn ~decl ~at_top s = |
| 191 |
|
| 192 |
(* inserts one position if in generation mode *) |
| 193 |
let decposfn d snp = |
| 194 |
if Snap.no_gen snp then |
| 195 |
declfn d snp |
| 196 |
else |
| 197 |
match PG.declaration_pos d snp with |
| 198 |
| Some (dd, snp) -> declfn dd snp |
| 199 |
| None -> failwith "no unpos cases" in |
| 200 |
|
| 201 |
match Ast0.unwrap decl with |
| 202 |
| Ast0.DisjDecl(lp, dlist, pipes, rp) -> |
| 203 |
handle_disj |
| 204 |
~lp ~rp ~pipes ~cases:dlist |
| 205 |
~casefn:decposfn ~singlefn:decposfn ~strfn ~at_top s |
| 206 |
| _ -> failwith "only disj allowed in here" |
| 207 |
|
| 208 |
(* Returns snapshot that has added generated declaration disjunction rule *) |
| 209 |
let generate_field ~strfn ~fieldfn ~field ~at_top s = |
| 210 |
|
| 211 |
(* inserts one position if in generation mode *) |
| 212 |
let decposfn d snp = |
| 213 |
if Snap.no_gen snp then |
| 214 |
fieldfn d snp |
| 215 |
else |
| 216 |
match PG.field_pos d snp with |
| 217 |
| Some (dd, snp) -> fieldfn dd snp |
| 218 |
| None -> failwith "no unpos cases" in |
| 219 |
|
| 220 |
match Ast0.unwrap field with |
| 221 |
| Ast0.DisjField(lp, dlist, pipes, rp) -> |
| 222 |
handle_disj |
| 223 |
~lp ~rp ~pipes ~cases:dlist |
| 224 |
~casefn:decposfn ~singlefn:decposfn ~strfn ~at_top s |
| 225 |
| _ -> failwith "only disj allowed in here" |