Debian Codegraph @

tools/spgen/source/position_generator.ml

raw
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 Ast = Ast_cocci
9 module Snap = Snapshot
10
11 (* ------------------------------------------------------------------------- *)
12
13 (* Given an Ast0 component, returns the same component with a generated
14 * metaposition added.
15 *
16 * This is all pretty messy and requires, for each component, individual
17 * assessment of where to put the position.
18 * The general heuristic is to add it either:
19 * - recursively to a smaller subcomponent (e.g. first expression in e1 + e2)
20 * - at an id (e.g. a function name) or
21 * - at an mcode (e.g. an operator).
22 *)
23
24 (* ------------------------------------------------------------------------- *)
25 (* POSITION HELPERS *)
26
27 (* always make a new pos even if the mcode already has an associated position,
28 * an existing pos might have undesirable constraints or inheritance.
29 *)
30 let make_pos (_, arity, info, mcodekind, _, adj) snp
31 : Ast0.anything * Snap.t =
32 let (name, snp) = Snap.add_position snp in
33 let meta_mcode = (("",name),arity,info,Ast0.PLUS Ast.ONE,ref [],adj) in
34 let list_constraints = Ast.CstrTrue in
35 let meta_collect = Ast.PER in
36 let new_pos = Ast0.MetaPos(meta_mcode, list_constraints, meta_collect) in
37 (Ast0.MetaPosTag(new_pos), snp)
38
39
40 (* ------------------------------------------------------------------------- *)
41 (* ALWAYS POSITION GENERATORS: always possible to generate pos *)
42
43 (* adds generated metaposition to mcode unless it is optional.
44 * yes, this contradicts the above statement. But it will never result in an
45 * error because it is not possible to write an SmPL script with only optional
46 * components.
47 *)
48 let mcode_pos ((x, a, info, mc, pos, q) as mco) snp
49 : 'a Ast0.mcode * Snap.t =
50 if a = Ast0.OPT then
51 (mco, snp)
52 else
53 let (newpos, snp) = make_pos mco snp in
54 ((x, a, info, mc, ref (newpos :: !pos), q), snp)
55
56 (* helper for adding mcode position, reconstructs component *)
57 let mcode
58 ~mc (* 'a Ast0.mcode *)
59 ~constructor (* mc:'a Ast0.mcode -> 'b *)
60 snp (* Snap.t *)
61 : 'b Ast0.wrap * Snap.t =
62 let (mc, snp) = mcode_pos mc snp in
63 (Ast0.wrap (constructor ~mc), snp)
64
65 let assignOp_pos a snp
66 : Ast0.base_assignOp Ast0.wrap * Snap.t =
67 match Ast0.unwrap a with
68 | Ast0.SimpleAssign mc ->
69 let constructor ~mc = Ast0.SimpleAssign mc in
70 mcode ~mc ~constructor snp
71 | Ast0.OpAssign mc ->
72 let constructor ~mc = Ast0.OpAssign mc in
73 mcode ~mc ~constructor snp
74 | Ast0.MetaAssign (mc, v, w) ->
75 let constructor ~mc = Ast0.MetaAssign(mc,v,w) in
76 mcode ~mc ~constructor snp
77
78 let binaryOp_pos a snp
79 : Ast0.base_binaryOp Ast0.wrap * Snap.t =
80 match Ast0.unwrap a with
81 | Ast0.Arith mc ->
82 let constructor ~mc = Ast0.Arith mc in
83 mcode ~mc ~constructor snp
84 | Ast0.Logical mc ->
85 let constructor ~mc = Ast0.Logical mc in
86 mcode ~mc ~constructor snp
87 | Ast0.MetaBinary (mc,v,w) ->
88 let constructor ~mc = Ast0.MetaBinary(mc,v,w) in
89 mcode ~mc ~constructor snp
90
91 let rec ident_pos i snp
92 : Ast0.base_ident Ast0.wrap * Snap.t =
93 match Ast0.unwrap i with
94 | Ast0.Id mc ->
95 let constructor ~mc = Ast0.Id mc in
96 mcode ~mc ~constructor snp
97 | Ast0.MetaId(mc, i, s, p) ->
98 let constructor ~mc = Ast0.MetaId(mc,i,s,p) in
99 mcode ~mc ~constructor snp
100 | Ast0.MetaFunc(mc, i, p) ->
101 let constructor ~mc = Ast0.MetaFunc(mc,i,p) in
102 mcode ~mc ~constructor snp
103 | Ast0.MetaLocalFunc(mc, i, p) ->
104 let constructor ~mc = Ast0.MetaLocalFunc(mc,i,p) in
105 mcode ~mc ~constructor snp
106 | Ast0.DisjId _ | Ast0.ConjId _ ->
107 (i, snp)
108 | Ast0.OptIdent (id) ->
109 let (id, snp) = ident_pos id snp in
110 (Ast0.wrap (Ast0.OptIdent (id)), snp)
111 | Ast0.AsIdent(id1, id2) ->
112 failwith "pos_gen: <id1 as id2> should only be in metavars"
113
114
115 (* ------------------------------------------------------------------------- *)
116 (* HELPERS FOR SOMETIMES POSITION GENERATORS *)
117
118 let all_same = function [] -> true | x :: xs -> List.for_all (( = ) x) xs
119
120 (* wraps Ast0 component and wraps it in Some *)
121 let wrap (a : 'a) (snp : Snap.t)
122 : ('a Ast0.wrap * Snap.t) option =
123 Some (Ast0.wrap a, snp)
124
125 (* adds position to the mcode, reconstructs component, and wraps it in Some *)
126 let mcode_wrap
127 ~mc (* 'a Ast0.mcode *)
128 ~constructor (* mc:'a Ast0.mcode -> b *)
129 snp (* Snap.t *)
130 : ('b Ast0.wrap * Snap.t) option =
131 Some (mcode ~mc ~constructor snp)
132
133 (* adds position to the id, reconstructs component, and wraps it in Some *)
134 let id_wrap
135 ~id (* Ast0.base_ident Ast0.wrap *)
136 ~constructor (* id:Ast0.base_ident Ast0.wrap -> 'a *)
137 snp (* Snap.t *)
138 : ('a Ast0.wrap * Snap.t) option =
139 let (id, snp) = ident_pos id snp in
140 wrap (constructor ~id) snp
141
142 (* generic helper function.
143 * Arguments:
144 * - item, an Ast0 component: 'a
145 * - item_posfn, position generator: 'a -> Snap.t -> ('b * Snap.t) option
146 * - constructor: item:'b -> 'c
147 * - alt, alt function: unit -> ('c Ast0.wrap * Snap.t) option
148 * - snp, snapshot: Snap.t
149 *
150 * Tries to generate position with item_posfn and reconstruct outer structure.
151 * If no position could be generated, call alt() as a backup.
152 *)
153 let item_wrap ~item ~item_posfn ~constructor ?(alt = fun _ -> None) snp
154 : ('c Ast0.wrap * Snap.t) option =
155 match item_posfn item snp with
156 | Some (item, snp) -> wrap (constructor ~item) snp
157 | None -> alt()
158
159
160 (* ------------------------------------------------------------------------- *)
161 (* SOMETIMES POSITION GENERATORS - not always possible to generate pos *)
162
163 (* DISJUNCTION RELATED: type_pos and case_line_pos *)
164
165 (* These functions are deliberately left unfinished for now.
166 * Implementing requires changes to disj_generator, but the cases are fairly
167 * rare, so for now just throw an exception if encountered.
168 * (to implement, disjunctions should return None here, and be added as special
169 * cases in rule_body.ml and disj_generator.ml. See DisjExpr for example.)
170 *)
171
172 let rec type_pos t snp
173 : (Ast0.base_typeC Ast0.wrap * Snap.t) option =
174 match Ast0.unwrap t with
175 | Ast0.AsType _
176 | Ast0.BaseType _ ->
177 None
178 | Ast0.DisjType(lp,tlist,pipelist,rp) ->
179 let boollist = Snap.get_disj (Ast0.get_mcode_line lp) snp in
180 if all_same boollist then None
181 else failwith (
182 "pos_gen: Mixed match/patch type disjunctions not supported " ^
183 "in position generator."
184 )
185 | Ast0.ConjType(lp,tlist,pipelist,rp) -> None (* not sure *)
186 | Ast0.ConstVol(cv1,t,cv2) ->
187 let constructor ~item = Ast0.ConstVol(cv1,item,cv2) in
188 item_wrap ~item:t ~item_posfn:type_pos ~constructor snp
189 | Ast0.Signed(sign,t) ->
190 let constructor ~mc = Ast0.Signed(mc,t) in
191 mcode_wrap ~mc:sign ~constructor snp
192 | Ast0.Pointer(t,star) ->
193 let constructor ~mc = Ast0.Pointer(t,mc) in
194 mcode_wrap ~mc:star ~constructor snp
195 | Ast0.ParenType(lp,t,rp) ->
196 let constructor ~mc = Ast0.ParenType(lp,t,rp) in
197 mcode_wrap ~mc:rp ~constructor snp
198 | Ast0.FunctionType(t,lp,params,rp) ->
199 let constructor ~mc = Ast0.FunctionType(t,lp,params,rp) in
200 mcode_wrap ~mc:rp ~constructor snp
201 | Ast0.Array(t,lb,expr,rb) ->
202 let constructor ~mc = Ast0.Array(t,lb,expr,mc) in
203 mcode_wrap ~mc:rb ~constructor snp
204 | Ast0.Decimal(dec,lp,expr,comma,expr2,rp) ->
205 let constructor ~mc = Ast0.Decimal(dec,lp,expr,comma,expr2,mc) in
206 mcode_wrap ~mc:rp ~constructor snp
207 | Ast0.EnumName(enum, key, Some nm) ->
208 let constructor ~id = Ast0.EnumName(enum, key, Some id) in
209 id_wrap ~id:nm ~constructor snp
210 | Ast0.EnumName(enum, key, None) ->
211 let constructor ~mc = Ast0.EnumName(mc, key, None) in
212 mcode_wrap ~mc:enum ~constructor snp
213 | Ast0.EnumDef(t,base,lcb,exprdots,rcb) ->
214 let c ~item ~mc = Ast0.EnumDef(item,base,lcb,exprdots,mc) in
215 let alt() = mcode_wrap ~mc:rcb ~constructor:(c ~item:t) snp in
216 item_wrap ~item:t ~item_posfn:type_pos ~constructor:(c ~mc:rcb) ~alt snp
217 | Ast0.StructUnionName(sumc,Some nm) ->
218 let constructor ~id = Ast0.StructUnionName(sumc,Some id) in
219 id_wrap ~id:nm ~constructor snp
220 | Ast0.StructUnionName(sumc,None) ->
221 let constructor ~mc = Ast0.StructUnionName(mc,None) in
222 mcode_wrap ~mc:sumc ~constructor snp
223 | Ast0.StructUnionDef(t,lcb,decldots,rcb) ->
224 let c ~item ~mc = Ast0.StructUnionDef(item,lcb,decldots,mc) in
225 let alt() = mcode_wrap ~mc:rcb ~constructor:(c ~item:t) snp in
226 item_wrap ~item:t ~item_posfn:type_pos ~constructor:(c ~mc:rcb) ~alt snp
227 | Ast0.TypeOfExpr(sizeofmc, lp, exp, rp) ->
228 let constructor ~mc = Ast0.TypeOfExpr(mc, lp, exp, rp) in
229 mcode_wrap ~mc:rp ~constructor snp
230 | Ast0.TypeOfType(sizeofmc, lp, typec, rp) ->
231 let _ = type_pos typec snp in (* sanity check for disj *)
232 let constructor ~mc = Ast0.TypeOfType(mc, lp, typec, rp) in
233 mcode_wrap ~mc:sizeofmc ~constructor snp
234 | Ast0.TypeName(nm) ->
235 let constructor ~mc = Ast0.TypeName(mc) in
236 mcode_wrap ~mc:nm ~constructor snp
237 | Ast0.QualifiedType(Some typec,coloncolon,name) ->
238 let _ = type_pos typec snp in
239 let constructor ~id = Ast0.QualifiedType(Some typec, coloncolon, id) in
240 id_wrap ~id:name ~constructor snp
241 | Ast0.QualifiedType(None,coloncolon,name) ->
242 let constructor ~id = Ast0.QualifiedType(None, coloncolon, id) in
243 id_wrap ~id:name ~constructor snp
244 | Ast0.AutoType(auto) ->
245 let constructor ~mc = Ast0.AutoType(mc) in
246 mcode_wrap ~mc:auto ~constructor snp
247 | Ast0.MetaType(mnmc,cstr,pure) ->
248 let constructor ~mc = Ast0.MetaType(mc,cstr,pure) in
249 mcode_wrap ~mc:mnmc ~constructor snp
250 | Ast0.OptType(t) ->
251 let constructor ~item = Ast0.OptType(item) in
252 item_wrap ~item:t ~item_posfn:type_pos ~constructor snp
253 | Ast0.TemplateType(name, lab, dots, rab) ->
254 let constructor ~mc = Ast0.TemplateType(name, mc, dots, rab) in
255 mcode_wrap ~mc:lab ~constructor snp
256
257 (* NB: if implementing disj generation, make sure that the statement dots in
258 * the clist are generated in no_gen mode...
259 *)
260 let case_line_pos c snp
261 : (Ast0.base_case_line Ast0.wrap * Snap.t) option =
262 match Ast0.unwrap c with
263 | Ast0.DisjCase(lp, clist, pipelist, rp) ->
264 let boollist = Snap.get_disj (Ast0.get_mcode_line lp) snp in
265 if all_same boollist then None
266 else failwith (
267 "pos_gen: Mixed match/patch case disjunctions in switch cases " ^
268 "not supported in position generator."
269 )
270 | _ -> None
271
272 let case_line_dots_pos c snp
273 : (Ast0.base_case_line Ast0.wrap * Snap.t) option list =
274 List.map (fun x -> case_line_pos x snp) (Ast0.unwrap c)
275
276 let rec expression_pos exp snp
277 : (Ast0.base_expression Ast0.wrap * Snap.t) option =
278
279 (* try adding a position to the internal expression. If that failed, try
280 * the alt function (usually, we use mcodes or ids as fallbacks)
281 *)
282 let exp_wrap ~exp ~constructor ?(alt = fun _ -> None) snp =
283 let constructor ~item = constructor ~exp:item in
284 item_wrap ~item:exp ~item_posfn:expression_pos ~constructor ~alt snp in
285
286 (* try adding a position to internal expressions, first try exp1, then exp2.
287 * if both have failed then call alt function.
288 *)
289 let exp_wrap2 ~exp1 ~exp2 ~constructor ?(alt = fun _ -> None) snp =
290 let c1 ~exp = constructor ~exp1:exp ~exp2 in
291 let c2 ~exp = constructor ~exp1 ~exp2:exp in
292 let try_exp2() = exp_wrap ~exp:exp2 ~constructor:c2 ~alt snp in
293 exp_wrap ~exp:exp1 ~constructor:c1 ~alt:try_exp2 snp in
294
295 match Ast0.unwrap exp with
296 | Ast0.NestExpr _
297 | Ast0.Edots _
298 | Ast0.AsExpr _
299 | Ast0.AsSExpr _
300 | Ast0.EComma _
301 | Ast0.MetaExprList _
302 | Ast0.DisjExpr _
303 | Ast0.ConjExpr _ ->
304 None
305 | Ast0.Ident(id) ->
306 let constructor ~id = Ast0.Ident id in
307 id_wrap ~id ~constructor snp
308 | Ast0.Constant(mc) ->
309 let constructor ~mc = Ast0.Constant mc in
310 mcode_wrap ~mc ~constructor snp
311 | Ast0.StringConstant(q1, sd, q2, sz) ->
312 let constructor ~mc = Ast0.StringConstant (q1, sd, mc, sz) in
313 mcode_wrap ~mc:q2 ~constructor snp
314 | Ast0.FunCall(exp, lp, expdots, rp) ->
315 let c ~exp ~mc = Ast0.FunCall(exp, mc, expdots, rp) in
316 let alt() = mcode_wrap ~mc:lp ~constructor:(c ~exp) snp in
317 exp_wrap ~exp ~constructor:(c ~mc:lp) ~alt snp
318 | Ast0.Assignment(exp1, asop, exp2, st) ->
319 let c ~exp1 ~exp2 ~mc = Ast0.Assignment(exp1, mc, exp2, st) in
320 let alt() =
321 let (mc, snp) = assignOp_pos asop snp in
322 wrap (c ~exp1 ~exp2 ~mc) snp in
323 exp_wrap2 ~exp1 ~exp2 ~constructor:(c ~mc:asop) ~alt snp
324 | Ast0.Sequence(exp1, com, exp2) ->
325 let c ~exp1 ~exp2 ~mc = Ast0.Sequence(exp1, mc, exp2) in
326 let alt() = mcode_wrap ~mc:com ~constructor:(c ~exp1 ~exp2) snp in
327 exp_wrap2 ~exp1 ~exp2 ~constructor:(c ~mc:com) ~alt snp
328 | Ast0.CondExpr(exp1, why, expopt, colon, exp2) ->
329 let c ~exp1 ~exp2 ~mc = Ast0.CondExpr(exp1, mc, expopt, colon, exp2) in
330 let alt() = mcode_wrap ~mc:why ~constructor:(c ~exp1 ~exp2) snp in
331 exp_wrap2 ~exp1 ~exp2 ~constructor:(c ~mc:why) ~alt snp
332 | Ast0.Postfix(exp, fixmc) ->
333 let c ~exp ~mc = Ast0.Postfix (exp, mc) in
334 let alt() = mcode_wrap ~mc:fixmc ~constructor:(c ~exp) snp in
335 exp_wrap ~exp ~constructor:(c ~mc:fixmc) ~alt snp
336 | Ast0.Infix(exp, fixmc) ->
337 let c ~exp ~mc = Ast0.Infix (exp, mc) in
338 let alt() = mcode_wrap ~mc:fixmc ~constructor:(c ~exp) snp in
339 exp_wrap ~exp ~constructor:(c ~mc:fixmc) ~alt snp
340 | Ast0.Unary(exp, unmc) ->
341 let c ~exp ~mc = Ast0.Unary (exp, mc) in
342 let alt() = mcode_wrap ~mc:unmc ~constructor:(c ~exp) snp in
343 exp_wrap ~exp ~constructor:(c ~mc:unmc) ~alt snp
344 | Ast0.Binary(exp1, bin, exp2) ->
345 let c ~exp1 ~exp2 ~mc = Ast0.Binary(exp1, mc, exp2) in
346 let alt() =
347 let (mc, snp) = binaryOp_pos bin snp in
348 wrap (c ~exp1 ~exp2 ~mc) snp in
349 exp_wrap2 ~exp1 ~exp2 ~constructor:(c ~mc:bin) ~alt snp
350 | Ast0.Nested(exp1, bin, exp2) ->
351 let c ~exp1 ~exp2 ~mc = Ast0.Nested(exp1, mc, exp2) in
352 let alt() =
353 let (mc, snp) = binaryOp_pos bin snp in
354 wrap (c ~exp1 ~exp2 ~mc) snp in
355 exp_wrap2 ~exp1 ~exp2 ~constructor:(c ~mc:bin) ~alt snp
356 | Ast0.Paren(lp, exp, rp) ->
357 let c ~exp ~mc = Ast0.Paren (mc, exp, rp) in
358 let alt() = mcode_wrap ~mc:lp ~constructor:(c ~exp) snp in
359 exp_wrap ~exp ~constructor:(c ~mc:lp) ~alt snp
360 | Ast0.ArrayAccess(exp, lb, expdots, rb) ->
361 let c ~exp ~mc = Ast0.ArrayAccess(exp, mc, expdots, rb) in
362 let alt() = mcode_wrap ~mc:lb ~constructor:(c ~exp) snp in
363 exp_wrap ~exp ~constructor:(c ~mc:lb) ~alt snp
364 | Ast0.RecordAccess(exp, stop, id) ->
365 let c ~exp ~id = Ast0.RecordAccess(exp, stop, id) in
366 let alt() = id_wrap ~id ~constructor:(c ~exp) snp in
367 exp_wrap ~exp ~constructor:(c ~id) ~alt snp
368 | Ast0.RecordPtAccess(exp, arrow, id) ->
369 let c ~exp ~id = Ast0.RecordPtAccess(exp, arrow, id) in
370 let alt() = id_wrap ~id ~constructor:(c ~exp) snp in
371 exp_wrap ~exp ~constructor:(c ~id) ~alt snp
372 | Ast0.QualifiedAccess(Some typec, coloncolon, id) ->
373 let _ = type_pos typec snp in
374 let constructor ~id = Ast0.QualifiedAccess(Some typec, coloncolon, id) in
375 id_wrap ~id:id ~constructor snp
376 | Ast0.QualifiedAccess(None, coloncolon, id) ->
377 let constructor ~id = Ast0.QualifiedAccess(None, coloncolon, id) in
378 id_wrap ~id:id ~constructor snp
379 | Ast0.Cast(lp, typec, rp, exp) ->
380 let _ = type_pos typec snp in (* sanity check for disj *)
381 let c ~exp ~mc = Ast0.Cast(lp, typec, mc, exp) in
382 let alt() = mcode_wrap ~mc:rp ~constructor:(c ~exp) snp in
383 exp_wrap ~exp ~constructor:(c ~mc:rp) ~alt snp
384 | Ast0.SizeOfExpr(sizeofmc, exp) ->
385 let constructor ~mc = Ast0.SizeOfExpr(mc, exp) in
386 mcode_wrap ~mc:sizeofmc ~constructor snp
387 | Ast0.SizeOfType(sizeofmc, lp, typec, rp) ->
388 let _ = type_pos typec snp in (* sanity check for disj *)
389 let constructor ~mc = Ast0.SizeOfType(mc, lp, typec, rp) in
390 mcode_wrap ~mc:sizeofmc ~constructor snp
391 | Ast0.New(nw,pp_opt,lp_opt,ty,rp_opt,args_opt) ->
392 let constructor ~mc = Ast0.New(mc,pp_opt,lp_opt,ty,rp_opt,args_opt) in
393 mcode_wrap ~mc:nw ~constructor snp
394 | Ast0.Delete(deletemc, exp) ->
395 let constructor ~mc = Ast0.Delete(mc, exp) in
396 mcode_wrap ~mc:deletemc ~constructor snp
397 | Ast0.DeleteArr(deletemc,lb,rb,exp) ->
398 let constructor ~mc = Ast0.DeleteArr(mc,lb,rb,exp) in
399 mcode_wrap ~mc:deletemc ~constructor snp
400 | Ast0.TemplateInst(name, lab, dots, rab) ->
401 let constructor ~mc = Ast0.TemplateInst(name, mc, dots, rab) in
402 mcode_wrap ~mc:lab ~constructor snp
403 | Ast0.TupleExpr(init) ->
404 let constructor ~item = Ast0.TupleExpr(item) in
405 item_wrap ~item:init ~item_posfn:initialiser_pos ~constructor snp
406 | Ast0.TypeExp(typec) ->
407 let constructor ~item = Ast0.TypeExp(item) in
408 item_wrap ~item:typec ~item_posfn:type_pos ~constructor snp
409 | Ast0.Constructor(lp, typec, rp, init) ->
410 let _ = type_pos typec snp in (* sanity check for disj *)
411 let constructor ~mc = Ast0.Constructor (mc, typec, rp, init) in
412 mcode_wrap ~mc:lp ~constructor snp
413 | Ast0.MetaErr (mc, co, pu) -> (* is this ever within the rule body? *)
414 let constructor ~mc = Ast0.MetaErr (mc, co, pu) in
415 mcode_wrap ~mc ~constructor snp
416 | Ast0.MetaExpr(mc, co, ty, fo, pu, bitfield) ->
417 let constructor ~mc = Ast0.MetaExpr (mc, co, ty, fo, pu, bitfield) in
418 mcode_wrap ~mc ~constructor snp
419 | Ast0.OptExp exp ->
420 let constructor ~exp = Ast0.OptExp exp in
421 exp_wrap ~exp ~constructor snp
422
423 and initialiser_pos init snp
424 : (Ast0.base_initialiser Ast0.wrap * Snap.t) option =
425 match Ast0.unwrap init with
426 Ast0.MetaInit(mc,b,c) ->
427 let constructor ~mc = Ast0.MetaInit(mc,b,c) in
428 mcode_wrap ~mc ~constructor snp
429 | Ast0.MetaInitList(mc,b,c,d) ->
430 let constructor ~mc = Ast0.MetaInitList(mc,b,c,d) in
431 mcode_wrap ~mc ~constructor snp
432 | Ast0.InitExpr(exp) ->
433 let constructor ~item = Ast0.InitExpr(item) in
434 item_wrap ~item:exp ~item_posfn:expression_pos ~constructor snp
435 | Ast0.InitList(mc,initlist,rb,ordered) ->
436 let constructor ~mc = Ast0.InitList(mc,initlist,rb,ordered) in
437 mcode_wrap ~mc ~constructor snp
438 | Ast0.InitGccExt(designators,mc,ini) ->
439 let constructor ~mc = Ast0.InitGccExt(designators,mc,ini) in
440 mcode_wrap ~mc ~constructor snp
441 | Ast0.InitGccName(name,mc,ini) ->
442 let constructor ~mc = Ast0.InitGccName(name,mc,ini) in
443 mcode_wrap ~mc ~constructor snp
444 | Ast0.IComma(mc) ->
445 let constructor ~mc = Ast0.IComma(mc) in
446 mcode_wrap ~mc ~constructor snp
447 | Ast0.Idots(a,b) -> None
448 | Ast0.OptIni(ini) ->
449 let constructor ~item = Ast0.OptIni(item) in
450 item_wrap ~item:ini ~item_posfn:initialiser_pos ~constructor snp
451 | Ast0.AsInit(ini,asini) -> None
452
453 (* redefine exp_wrap outside scope of expression_pos due to internal exp_wrap
454 * being typed to only work for expression constructors and not 'a constructors
455 *)
456 let exp_wrap ~exp ~constructor ?(alt = fun _ -> None) snp
457 : ('a Ast0.wrap * Snap.t) option =
458 let constructor ~item = constructor ~exp:item in
459 item_wrap ~item:exp ~item_posfn:expression_pos ~constructor ~alt snp
460
461 let rec declaration_pos decl snp
462 : (Ast0.base_declaration Ast0.wrap * Snap.t) option =
463 match Ast0.unwrap decl with
464 Ast0.DisjDecl _
465 | Ast0.ConjDecl _
466 | Ast0.MetaDecl _
467 | Ast0.AsDecl _ ->
468 None
469 | Ast0.Init(al, st, ty, id, endattr, eq, ini, sem) ->
470 let _ = type_pos ty snp in (* sanity check *)
471 let constructor ~id = Ast0.Init(al, st, ty, id, endattr, eq, ini, sem) in
472 id_wrap ~id ~constructor snp
473 | Ast0.UnInit(al, st, ty, id, endattr, sem) ->
474 let _ = type_pos ty snp in (* sanity check *)
475 let constructor ~id = Ast0.UnInit(al, st, ty, id, endattr, sem) in
476 id_wrap ~id ~constructor snp
477 | Ast0.TyDecl (t, sem) ->
478 let c ~item ~mc = Ast0.TyDecl(item, mc) in
479 let alt() = mcode_wrap ~mc:sem ~constructor:(c ~item:t) snp in
480 item_wrap ~item:t ~item_posfn:type_pos ~constructor:(c ~mc:sem) ~alt snp
481 | Ast0.Typedef (tm, tc, tc2, sem) ->
482 let constructor ~mc = Ast0.Typedef (mc, tc, tc2, sem) in
483 mcode_wrap ~mc:tm ~constructor snp
484 | Ast0.MacroDecl (st,pa,id,lp,ed,rp,attr,sem) ->
485 let constructor ~id = Ast0.MacroDecl (st, pa, id, lp, ed, rp, attr, sem) in
486 id_wrap ~id ~constructor snp
487 | Ast0.MacroDeclInit (st,pa,id,lp,ed,rp,attr,eq,init,sem) ->
488 let constructor ~id = Ast0.MacroDeclInit (st,pa,id,lp,ed,rp,attr,eq,init,sem) in
489 id_wrap ~id ~constructor snp
490 | Ast0.OptDecl(dec) ->
491 let constructor ~item = Ast0.OptDecl item in
492 item_wrap ~item:dec ~item_posfn:declaration_pos ~constructor snp
493 | Ast0.FunProto(fninfo,id,lp1,params,va,rp1,sem) ->
494 let constructor ~id = Ast0.FunProto(fninfo,id,lp1,params,va,rp1,sem) in
495 id_wrap ~id ~constructor snp
496
497 let rec field_pos decl snp
498 : (Ast0.base_field Ast0.wrap * Snap.t) option =
499 match Ast0.unwrap decl with
500 Ast0.DisjField _
501 | Ast0.ConjField _
502 | Ast0.Fdots _
503 | Ast0.MetaField _
504 | Ast0.MetaFieldList _
505 | Ast0.Field(_, None, _, _, _) -> None
506 | Ast0.Field(ty, Some id, bf, endattr, sem) ->
507 let _ = type_pos ty snp in (* sanity check *)
508 let constructor ~id = Ast0.Field(ty, Some id, bf, endattr, sem) in
509 id_wrap ~id ~constructor snp
510 | Ast0.MacroDeclField (id,lp,ed,rp,attr,sem) ->
511 let constructor ~id = Ast0.MacroDeclField (id, lp, ed, rp, attr, sem) in
512 id_wrap ~id ~constructor snp
513 | Ast0.CppField di ->
514 let constructor ~item = Ast0.CppField item in
515 item_wrap ~item:di ~item_posfn:directive_pos ~constructor snp
516 | Ast0.AccSpec (mc,dd) ->
517 let constructor ~mc = Ast0.AccSpec (mc,dd) in
518 mcode_wrap ~mc ~constructor snp
519 | Ast0.OptField(dec) ->
520 let constructor ~item = Ast0.OptField item in
521 item_wrap ~item:dec ~item_posfn:field_pos ~constructor snp
522
523 and directive_pos di snp
524 : (Ast0.base_directive Ast0.wrap * Snap.t) option =
525 match Ast0.unwrap di with
526 Ast0.Include (incmc,filemc) ->
527 let constructor ~mc = Ast0.Include(incmc, mc) in
528 mcode_wrap ~mc:filemc ~constructor snp
529 | Ast0.MetaInclude (incmc,filemc) ->
530 let constructor ~mc = Ast0.MetaInclude(mc, filemc) in
531 mcode_wrap ~mc:incmc ~constructor snp
532 | Ast0.Pragma (pragmc, id, praginfo) ->
533 let constructor ~id = Ast0.Pragma(pragmc, id, praginfo) in
534 id_wrap ~id ~constructor snp
535 | Ast0.UsingNamespace (usngmc,nmspcmc,namemc,semmc) ->
536 let constructor ~mc = Ast0.UsingNamespace(usngmc, mc, namemc, semmc) in
537 mcode_wrap ~mc:nmspcmc ~constructor snp
538 | Ast0.UsingTypename (usngmc,namemc,eqmc,tnmc,tymc,semmc) ->
539 let constructor ~mc = Ast0.UsingTypename(usngmc, namemc, mc, tnmc, tymc, semmc) in
540 mcode_wrap ~mc:eqmc ~constructor snp
541 | Ast0.UsingMember(usngmc,namemc,semmc) ->
542 let constructor ~mc = Ast0.UsingMember(usngmc, namemc, mc) in
543 mcode_wrap ~mc:semmc ~constructor snp
544
545 let forinfo_pos f snp
546 : (Ast0.base_forinfo Ast0.wrap * Snap.t) option =
547 match Ast0.unwrap f with
548 | Ast0.ForExp (Some exp,sem1,expo1,sem2,expo2) ->
549 let c ~exp ~mc = Ast0.ForExp(Some exp,mc,expo1,sem2,expo2) in
550 let alt() = mcode_wrap ~mc:sem1 ~constructor:(c ~exp) snp in
551 exp_wrap ~exp ~constructor:(c ~mc:sem1) ~alt snp
552 | Ast0.ForExp (None,sem1,expo1,sem2,expo2) ->
553 let constructor ~mc = Ast0.ForExp (None,mc,expo1,sem2,expo2) in
554 mcode_wrap ~mc:sem1 ~constructor snp
555 | Ast0.ForDecl (bef,decl,expo1,sem,expo2) ->
556 let constructor ~item = Ast0.ForDecl(bef,item,expo1,sem,expo2) in
557 item_wrap ~item:decl ~item_posfn:declaration_pos ~constructor snp
558 | Ast0.ForRange (bef,decl,inio) ->
559 let constructor ~item = Ast0.ForRange(bef,item,inio) in
560 item_wrap ~item:decl ~item_posfn:declaration_pos ~constructor snp
561
562 let rec statement_pos s snp
563 : (Ast0.base_statement Ast0.wrap * Snap.t) option =
564 match Ast0.unwrap s with
565
566 (* these cannot have positions (disjunctions are handled separately) *)
567 | Ast0.Nest _
568 | Ast0.Dots _
569 | Ast0.Disj _
570 | Ast0.Conj _
571 | Ast0.MetaStmtList _ -> None
572 | Ast0.AsStmt _ -> None
573 | Ast0.MetaStmt _ -> None
574
575 (* uncertainty of whether these should be handled! *)
576 | Ast0.Exec _ -> None
577 | Ast0.TopExp _ -> None
578 | Ast0.TopId _ -> None
579 | Ast0.Ty _ -> None
580 | Ast0.TopInit _ -> None
581
582 | Ast0.CppTop di ->
583 let c ~item = Ast0.CppTop item in
584 item_wrap ~item:di ~item_posfn:directive_pos ~constructor:c snp
585
586 | Ast0.Undef (defmc, id) ->
587 let constructor ~id = Ast0.Undef(defmc, id) in
588 id_wrap ~id ~constructor snp
589 | Ast0.Define (defmc, id, defparam, stmtdots) ->
590 (* if the #define directive has parameters, we cannot put the position
591 * after the identifier as it will break up the token. Therefore we put
592 * it after the parenthesis.
593 *)
594 let c ~id ~defparam = Ast0.Define(defmc, id, defparam, stmtdots) in
595 (match Ast0.unwrap defparam with
596 | Ast0.NoParams ->
597 id_wrap ~id ~constructor:(c ~defparam) snp
598 | Ast0.DParams(lp, defp, rp) ->
599 let (newrp, snp) = mcode_pos rp snp in
600 let defparam = Ast0.wrap (Ast0.DParams(lp, defp, newrp)) in
601 wrap (c ~id ~defparam) snp
602 )
603 | Ast0.OptStm stm ->
604 let c ~item = Ast0.OptStm item in
605 item_wrap ~item:stm ~item_posfn:statement_pos ~constructor:c snp
606 | Ast0.ExprStatement(None, sem) ->
607 None
608 | Ast0.ExprStatement(Some exp, sem) ->
609 let c ~exp ~mc = Ast0.ExprStatement(Some exp, mc) in
610 let alt() = mcode_wrap ~mc:sem ~constructor:(c ~exp) snp in
611 exp_wrap ~exp ~constructor:(c ~mc:sem) ~alt snp
612 | Ast0.Exp exp ->
613 let constructor ~exp = Ast0.Exp exp in
614 exp_wrap ~exp ~constructor snp
615 | Ast0.Decl (bef, decl) ->
616 let c ~item = Ast0.Decl (bef, item) in
617 item_wrap ~item:decl ~item_posfn:declaration_pos ~constructor:c snp
618 | Ast0.Seq (lb, stmtdots, rb) ->
619 let constructor ~mc = Ast0.Seq(mc, stmtdots, rb) in
620 mcode_wrap ~mc:lb ~constructor snp
621 | Ast0.IfThen (ifm, l, exp, r, st, a) ->
622 let c ~exp ~mc = Ast0.IfThen(mc, l, exp, r, st, a) in
623 let alt() = mcode_wrap ~mc:ifm ~constructor:(c ~exp) snp in
624 exp_wrap ~exp ~constructor:(c ~mc:ifm) ~alt snp
625 | Ast0.IfThenElse (ifm, l, exp, r, s1, e, s2, a) ->
626 let c ~exp ~mc = Ast0.IfThenElse(mc, l, exp, r, s1, e, s2, a) in
627 let alt() = mcode_wrap ~mc:ifm ~constructor:(c ~exp) snp in
628 exp_wrap ~exp ~constructor:(c ~mc:ifm) ~alt snp
629 | Ast0.While (whmc, l, cond, r, s, a) ->
630 let constructor ~mc = Ast0.While (mc, l, cond, r, s, a) in
631 mcode_wrap ~mc:whmc ~constructor snp
632 | Ast0.Do (d, s, whmc, l, exp, r, sem) ->
633 let c ~exp ~mc = Ast0.Do(mc, s, whmc, l, exp, r, sem) in
634 let alt() = mcode_wrap ~mc:d ~constructor:(c ~exp) snp in
635 exp_wrap ~exp ~constructor:(c ~mc:d) ~alt snp
636 | Ast0.For (fo,lp, fi,rp,stmt,a) ->
637 let c ~item ~mc = Ast0.For (mc,lp,item,rp,stmt,a) in
638 let alt() = mcode_wrap ~mc:fo ~constructor:(c ~item:fi) snp in
639 item_wrap
640 ~item:fi ~item_posfn:forinfo_pos ~constructor:(c ~mc:fo) ~alt snp
641 | Ast0.Iterator (id,lp,expdots,rp,stmt,a) ->
642 let constructor ~id = Ast0.Iterator(id, lp, expdots, rp, stmt, a) in
643 id_wrap ~id ~constructor snp
644 | Ast0.Switch (sw, lp, exp, rp, lb, sd, cd, rb) ->
645 let _ = case_line_dots_pos cd snp in (* sanity check for disj *)
646 let c ~exp ~mc = Ast0.Switch(mc, lp, exp, rp, lb, sd, cd, rb) in
647 let alt() = mcode_wrap ~mc:sw ~constructor:(c ~exp) snp in
648 exp_wrap ~exp ~constructor:(c ~mc:sw) ~alt snp
649 | Ast0.Label (id,col) ->
650 let constructor ~id = Ast0.Label(id, col) in
651 id_wrap ~id ~constructor snp
652 | Ast0.Goto (goto,id,sem) ->
653 let constructor ~id = Ast0.Goto(goto, id, sem) in
654 id_wrap ~id ~constructor snp
655 | Ast0.Break (bmc,sem) ->
656 let constructor ~mc = Ast0.Break(mc, sem) in
657 mcode_wrap ~mc:bmc ~constructor snp
658 | Ast0.Continue (cmc,sem) ->
659 let constructor ~mc = Ast0.Continue(mc, sem) in
660 mcode_wrap ~mc:cmc ~constructor snp
661 | Ast0.ReturnExpr (retmc, exp, sem) ->
662 let c ~exp ~mc = Ast0.ReturnExpr(mc, exp, sem) in
663 let alt() = mcode_wrap ~mc:retmc ~constructor:(c ~exp) snp in
664 exp_wrap ~exp ~constructor:(c ~mc:retmc) ~alt snp
665 | Ast0.Return (retmc,sem) ->
666 let constructor ~mc = Ast0.Return(mc, sem) in
667 mcode_wrap ~mc:retmc ~constructor snp
668 | Ast0.FunDecl (b, f, id, lp, ps, op, rp, ea, lb, sd, rb, a) ->
669 let constructor ~id = Ast0.FunDecl(b,f,id,lp,ps,op,rp,ea,lb,sd,rb,a) in
670 id_wrap ~id ~constructor snp
671 | Ast0.TemplateDefinition(tmpkw,lab,params,rab,stmt) ->
672 let constructor ~mc = Ast0.TemplateDefinition(mc,lab,params,rab,stmt) in
673 mcode_wrap ~mc:tmpkw ~constructor snp