Debian Codegraph @

tools/spgen/source/meta_variable.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 Ast = Ast_cocci
8 module Ast0 = Ast0_cocci
9 module V0 = Visitor_ast0
10 module VT0 = Visitor_ast0_types
11 module S = Ast_tostring
12
13 (* ------------------------------------------------------------------------- *)
14
15 (* Takes a minus AST0 and extracts all metavariables used in the rule.
16 *
17 * In general, the metavariable layout is
18 * (type, (inherit_rule, metaname), constraints)
19 * e.g. parameter list[rule1.n] P is ("parameter list[rule1.n]", ("","P"), "")
20 * and position free.p1!=loop.ok is ("position", ("free", "p1"), "!=loop.ok")
21 *
22 * NOTE: inherit_rule is only for inherited rules, ie. akin to "rulename.mv".
23 * If the metavariable is in local scope, inherit_rule will be "".
24 *
25 * Named arguments in here:
26 * - rn is the rulename (string)
27 * - mc is an Ast0.mcode ('a mcode)
28 * - mn is an Ast.meta_name (type alias for (rule_name, metavar_name) tuple)
29 * - typ/before is the type (string), put in the type spot
30 * - constr/after is the constraint (string), put in the constraints spot
31 * - listlen is a list_len (Ast.list_len)
32 *)
33
34 (* ------------------------------------------------------------------------- *)
35 (* TYPES AND HELPERS *)
36
37 type meta_variable = string * (string * string) * string
38
39 let make_mv typ (rule, name) constr = (typ, (rule,name), constr)
40
41 (* If the externally specified rulename is the same as the internal rulename
42 * (ie. the one attached to the metavariable) OR the external rulename is
43 * nameless ("rule starting on line ..."), then there is no inheritance
44 * required; ie. return true.
45 *)
46 let no_inherit extern intern =
47 extern = intern || String.contains extern ' '
48
49 let name_str ~rn (r, mn) =
50 if no_inherit r rn
51 then mn
52 else r ^ "." ^ mn
53
54 let name_tup ~rn (r, mn) =
55 if no_inherit r rn
56 then ("", mn)
57 else (r, mn)
58
59 let str_tup str = ("", str)
60
61 let tostring_mv (t, rnm, c) =
62 let full_name = name_str ~rn:"" rnm in
63 String.concat "" [t; full_name; c]
64
65 (* the type used and returned by the visitor.
66 * we're using a set to eliminate duplicates, since a metavariable might
67 * appear several times in a rule, but we only want it declared once.
68 *)
69 module MVSet = Set.Make(
70 struct
71 type t = meta_variable
72
73 (* we use the normal string comparison, except if the string starts with
74 * "type", "typedef" or "identifier" in which case it comes before others.
75 * This is to ensure that types and identifiers get printed first since
76 * other metavariables might be dependent on them.
77 *)
78 let compare (t1,(_,n1),_) (t2,(_,n2),_) =
79
80 let starts_with s c = Str.string_match (Str.regexp ("^"^s)) c 0 in
81 let is_type = starts_with "type" in
82 let is_identifier = starts_with "identifier" in
83
84 match (is_type t1, is_type t2) with
85 | true, false -> -1
86 | false, true -> 1
87 | true, true -> String.compare n1 n2
88 | false, false ->
89 (match (is_identifier t1, is_identifier t2) with
90 | true, false -> -1
91 | false, true -> 1
92 | _ -> String.compare n1 n2
93 )
94 end
95 )
96
97 (* ------------------------------------------------------------------------- *)
98 (* STRING HELPERS *)
99
100 (* These functions take subcomponents of the AST0 and turn them into
101 * pretty strings for printing.
102 * ALL FUNCTIONS HERE RETURN STRINGS
103 *)
104
105 (* get string formatted version of type (used as front of meta expressions) *)
106 let type_c ~form =
107 (* TODO: figure out when default and prefix are used ... *)
108 let (default, prefix) =
109 match form with
110 | Ast.ANY -> ("expression ", "")
111 | Ast.ID -> ("idexpression ", "idexpression ")
112 | Ast.LocalID -> ("local idexpression ", "local idexpression ")
113 | Ast.GlobalID -> ("global idexpression ", "global idexpression ")
114 | Ast.CONST -> ("constant ", "constant ") in
115 let type2c a =
116 match Ast.string_of_fullType a with
117 | "unknown *" -> default ^ " *"
118 | a -> prefix ^ a in
119 function
120 | Some [a] -> type2c a
121 | Some a -> prefix ^ ("{" ^ (String.concat "," (List.map type2c a)) ^ "} ")
122 | None -> default
123
124 (* TODO: in SeedId, we sometimes (?) want to keep the rulename; but not if it
125 * has been declared before?
126 *)
127 let seed ~rn =
128 let se = function
129 | Ast.SeedString s -> "\"" ^ s ^ "\"" | Ast.SeedId (r,nm) -> nm in
130 function
131 | Ast.NoVal -> ""
132 | Ast.StringSeed s -> " = \"" ^ s ^ "\""
133 | Ast.ListSeed s -> " = " ^ (String.concat " ## " (List.map se s))
134 | Ast.ScriptSeed (_, lang, params, _pos, code) ->
135 Printf.sprintf " = script:%s (%s) {%s}"
136 lang
137 (String.concat "," (List.map (fun (nm,_) -> name_str ~rn nm) params))
138 code
139
140 let string_of_operator_constraint cstr =
141 match cstr with
142 Ast.CstrAssignOp op -> Ast.string_of_assignOp op
143 | Ast.CstrBinaryOp op -> Ast.string_of_binaryOp op
144
145 let rec constraints_to_buffer ~rn buffer cstr =
146 let string_of_expression e =
147 match Ast0.unwrap e with
148 Ast0.Ident id ->
149 begin
150 match Ast0.unwrap id with
151 Ast0.Id id -> Ast0.unwrap_mcode id
152 | _ -> failwith "string_of_expression"
153 end
154 | _ -> failwith "string_of_expression" in
155 let print_sub op first item =
156 match item with
157 Ast.CstrTrue | Ast.CstrFalse -> first
158 | _ ->
159 if not first then
160 Buffer.add_string buffer op;
161 Buffer.add_string buffer "(";
162 constraints_to_buffer ~rn buffer item;
163 Buffer.add_string buffer ")";
164 false in
165 let simple_item item =
166 match item with
167 Ast.CstrConstant (Ast.CstrInt (Ast.CstrIntEq i)) -> i
168 | Ast.CstrConstant (Ast.CstrString s) -> s
169 | Ast.CstrOperator item -> string_of_operator_constraint item
170 | Ast.CstrMeta_name mn -> name_str ~rn mn
171 | Ast.CstrExpr e -> string_of_expression e
172 | _ -> raise Exit in
173 match cstr with
174 Ast.CstrTrue | Ast.CstrSub [] -> ()
175 | Ast.CstrFalse ->
176 failwith "Ast_cocci.string_of_constraints: no syntax for Ast.CstrFalse"
177 | Ast.CstrExit ->
178 failwith "Ast_cocci.string_of_constraints: no syntax for Ast.CstrExit"
179 | Ast.CstrAnd list -> ignore (List.fold_left (print_sub " && ") true list)
180 | Ast.CstrOr list ->
181 begin
182 match try Some (List.map simple_item list) with Exit -> None with
183 Some [singleton] ->
184 Printf.bprintf buffer "= %s" singleton;
185 | Some (first :: others) ->
186 Printf.bprintf buffer "= {%s" first;
187 List.iter (fun s -> Printf.bprintf buffer ",%s" s) others;
188 Buffer.add_string buffer "}"
189 | _ -> ignore (List.fold_left (print_sub " || ") true list)
190 end
191 | Ast.CstrNot item ->
192 Buffer.add_string buffer "!(";
193 constraints_to_buffer ~rn buffer item;
194 Buffer.add_string buffer ")"
195 | Ast.CstrConstant (Ast.CstrInt (Ast.CstrIntEq i)) ->
196 Printf.bprintf buffer "= %s" i
197 | Ast.CstrConstant (Ast.CstrInt (Ast.CstrIntLeq i)) ->
198 Printf.bprintf buffer "<= %d" i
199 | Ast.CstrConstant (Ast.CstrInt (Ast.CstrIntGeq i)) ->
200 Printf.bprintf buffer ">= %d" i
201 | Ast.CstrConstant (Ast.CstrString s) -> Printf.bprintf buffer "= %s" s
202 | Ast.CstrOperator item ->
203 Printf.bprintf buffer "= %s" (string_of_operator_constraint item)
204 | Ast.CstrMeta_name mn -> Printf.bprintf buffer "= %s" (name_str ~rn mn)
205 | Ast.CstrRegexp (s, _) -> Printf.bprintf buffer "=~ \"%s\"" s
206 | Ast.CstrScript (_,(_, lang, params, _pos, code)) ->
207 Printf.bprintf buffer ": script:%s (%s) {%s}" lang
208 (String.concat "," (List.map (fun (nm,_) -> name_str ~rn nm) params))
209 code
210 | Ast.CstrExpr e -> Buffer.add_string buffer (string_of_expression e)
211 | Ast.CstrSub (first :: others) ->
212 Printf.bprintf buffer "<= {%s" (name_str ~rn first);
213 List.iter (fun mn -> Printf.bprintf buffer ",%s" (name_str ~rn mn))
214 others;
215 Buffer.add_string buffer "}"
216 | Ast.CstrType ty -> failwith "string_of_constraints: type"
217
218 let constraints ~rn cstr =
219 let buffer = Buffer.create 17 in
220 constraints_to_buffer ~rn buffer cstr;
221 let result = Buffer.contents buffer in
222 if result = "" then ""
223 else " " ^ result
224
225 let list_len ~rn = function
226 | Ast0.AnyListLen -> " "
227 | Ast0.MetaListLen ((mn,_,_,_,_,_),_) -> "[" ^ (name_str ~rn mn) ^ "] "
228 | Ast0.CstListLen i -> "[" ^ (string_of_int i) ^ "] "
229
230
231 (* ------------------------------------------------------------------------- *)
232 (* MSET HELPERS *)
233
234 (* These functions take subcomponents of the AST0 and turn them into MVSets of
235 * prettily formatted strings, ready to be bound in the combiner.
236 * ALL FUNCTIONS HERE RETURN META_VARIABLE MVSET.T
237 *)
238
239 (* Scours an optional list of type_c's for meta types and meta identifiers
240 * used in the types. If the type_c's have dependencies to other metavariables,
241 * we need to declare those metavariables as well of course.
242 *)
243 let types ~rn = function
244 | Some typecs ->
245 (* TODO: are the keep_bindings used for anything ? *)
246 let bin = function
247 Ast.Unitary -> ""
248 | Ast.Nonunitary -> ""
249 | Ast.Saved -> "" in
250 let get_meta_id acc ty =
251 match Ast_cocci.unwrap ty with
252 Ast_cocci.MetaId(mn, _, b, _) ->
253 let mn' = Ast_cocci.unwrap_mcode mn in
254 let metavar = make_mv "identifier" (name_tup ~rn mn') (bin b) in
255 MVSet.add metavar acc
256 | _ -> acc in
257 let rec get_meta_type acc ty =
258 match Ast_cocci.unwrap ty with
259 Ast.MetaType(mn, _, b, _) ->
260 let mn' = Ast_cocci.unwrap_mcode mn in
261 let metavar = make_mv "type " (name_tup ~rn mn') (bin b) in
262 MVSet.add metavar acc
263 | Ast.TypeName s ->
264 let metavar = ("typedef ", str_tup (Ast.unwrap_mcode s), "") in
265 MVSet.add metavar acc
266 | Ast.Decimal(_, _, nm1, _, nm2, _) ->
267 let get_meta_id_opt v o = Common.default v (get_meta_id v) o in
268 let acc =
269 get_meta_id_opt acc (Ast_cocci.ident_of_expression_opt nm1) in
270 let acc =
271 get_meta_id_opt acc
272 (Common.default None Ast_cocci.ident_of_expression_opt nm2) in
273 acc
274 | Ast.EnumName (_, _, Some n)
275 | Ast.StructUnionName(_, Some n) -> get_meta_id acc n
276 | Ast.SignedT (_, Some t) -> get_meta_type acc t
277 | Ast.Pointer (t, _)
278 | Ast.Array (t, _, _, _) -> get_meta_type_full acc t
279 | Ast.TypeOfExpr(_,_,exp,_) -> acc (* not sure *)
280 | Ast.TypeOfType(_,_,ty,_) -> get_meta_type_full acc ty
281 | _ -> acc
282 and get_meta_type_full acc ty =
283 get_meta_type acc (Common.just (Ast_cocci.typeC_of_fullType_opt ty)) in
284 List.fold_left get_meta_type_full MVSet.empty typecs
285 | None -> MVSet.empty
286
287 (* Function to call on mcodes. We are only interested in the mcodes because of
288 * the positions/metavars that might be attached to them.
289 * Note that attached metavariables might have metavariables attached to
290 * themselves as well!
291 *)
292 let mcode ~rn ~mc:(_,_,_,_,pos,_) =
293 let rec add_one_pos set =
294
295 (* adds the mn metavar + any metapositions attached to it to the set. *)
296 let handle_metavar ~typ ~mn ~positions ~set =
297 let mv = make_mv typ (name_tup ~rn mn) "" in
298 let added_mv_set = MVSet.add mv set in
299 MVSet.union added_mv_set (add_all_pos positions) in
300
301 (* extracting the node is equivalent to calling Ast0.unwrap *)
302 function
303 | Ast0.ExprTag {Ast0.node = Ast0.MetaExpr((mn,_,_,_,p,_),_,_,_,_,_); _} ->
304 handle_metavar ~typ:"expression " ~mn ~positions:!p ~set
305 | Ast0.StmtTag {Ast0.node = Ast0.MetaStmt((mn,_,_,_,p,_),_,_); _} ->
306 handle_metavar ~typ:"statement " ~mn ~positions:!p ~set
307 | Ast0.DeclTag {Ast0.node = Ast0.MetaDecl((mn,_,_,_,p,_),_,_); _} ->
308 handle_metavar ~typ:"declaration " ~mn ~positions:!p ~set
309 | Ast0.IdentTag {Ast0.node = Ast0.MetaId((mn,_,_,_,p,_),_,_,_); _} ->
310 handle_metavar ~typ:"identifier " ~mn ~positions:!p ~set
311 | Ast0.TypeCTag {Ast0.node = Ast0.MetaType((mn,_,_,_,p,_),_,_); _} ->
312 handle_metavar ~typ:"type " ~mn ~positions:!p ~set
313 | Ast0.MetaPosTag(Ast0.MetaPos((mn,_,_,_,_,_), mns, colt)) ->
314 let constr = constraints ~rn mns in
315 let collect = (match colt with Ast.PER -> "" | Ast.ALL -> " any") in
316 let pos = make_mv "position " (name_tup ~rn mn) (constr ^ collect) in
317 MVSet.add pos set
318 | Ast0.MetaPosTag(Ast0.MetaCom((mn,_,_,_,_,_),_)) ->
319 let com = make_mv "comments " (name_tup ~rn mn) "" in
320 MVSet.add com set
321 | _ -> failwith "should only have metavariables in here."
322
323 and add_all_pos lst = List.fold_left add_one_pos MVSet.empty lst in
324 add_all_pos !pos
325
326 (* turns mcode into MVSet of formatted strings.
327 * (mc : 'a mcode) is the mcode,
328 * (totup_fn : 'a -> string * string) formats the mcode value.
329 *)
330 let mc_format ~rn ~mc:((mn,_,_,_,_,_) as mc) ~totup_fn ~before ~after =
331 let pos = mcode ~rn ~mc in
332 let mv = make_mv before (totup_fn mn) after in
333 MVSet.add mv pos
334
335 let as_format a b afn bfn = failwith "\"as\" metavariables not supported"
336
337 (* turns meta_name mcode with list information into MVSet of formatted strings.
338 * (mc : Ast.meta_name mcode) becomes <before mc[ll_output]>.
339 *)
340 let list_format ~rn ~before ~mc:((mn,_,_,_,_,_) as mc) ~listlen =
341 let pos = mcode ~rn ~mc in
342 let mvname = name_tup ~rn mn in
343 let mv = make_mv (before ^ (list_len ~rn listlen)) mvname "" in
344 MVSet.add mv pos
345
346 (* for iterators and declarers *)
347 let ids ~rn ~typ ~id =
348 match Ast0.unwrap id with
349 | Ast0.Id mc ->
350 mc_format ~rn ~mc ~totup_fn:str_tup ~before:(typ ^ " name ") ~after:""
351 | Ast0.MetaId (mc, constr, s, _) -> (* ever seed here? *)
352 let constr' = constraints ~rn constr in
353 let totup_fn = name_tup ~rn in
354 mc_format ~rn ~mc ~totup_fn ~before:(typ ^ " ") ~after:constr'
355 | _ -> failwith (typ ^ " with non-(Id/MetaId). dunno what this means")
356
357
358 (* ------------------------------------------------------------------------- *)
359 (* THE COMBINER *)
360
361 (* MVSet Visitor_ast0_types.combiner_rec_functions
362 * Using the flat combiner from Visitor_ast0
363 *)
364
365 let metavar_combiner rn =
366 let option_default = MVSet.empty in
367 let bind x y = MVSet.union x y in
368
369 (* the mcodes might contain positions which should be declared as metavars *)
370 let mcode mc = mcode ~rn ~mc in
371 let meta_mcode a = failwith ("NOT ALLOWED") in (* should be handled before *)
372 let string_mcode = mcode in
373 let const_mcode = mcode in
374 let simpleAssign_mcode = mcode in
375 let opAssign_mcode = mcode in
376 let fix_mcode = mcode in
377 let unary_mcode = mcode in
378 let arithOp_mcode = mcode in
379 let logicalOp_mcode = mcode in
380 let cv_mcode = mcode in
381 let sign_mcode = mcode in
382 let struct_mcode = mcode in
383 let storage_mcode = mcode in
384 let inc_mcode = mcode in
385
386 (* apply the passed function, do nothing else *)
387 let donothing c fn v = fn v in
388 let dotsexprfn = donothing in
389 let dotsinitfn = donothing in
390 let dotsparamfn = donothing in
391 let dotstemplateparamfn = donothing in
392 let dotsstmtfn = donothing in
393 let dotsdeclfn = donothing in
394 let dotsfieldfn = donothing in
395 let dotsenumdeclfn = donothing in
396 let dotscasefn = donothing in
397 let dotsdefparfn = donothing in
398 let forinfofn = donothing in
399 let casefn = donothing in
400 let attributefn = donothing in
401 let topfn = donothing in
402 let enumdeclfn = donothing in
403
404 (* --- These are shortened formatting functions that return MVSets --- *)
405
406 (* Formats as <bef mn aft> where mn is extracted from meta_name mcode mc *)
407 let meta_mc_format ~mc ~typ ~constr =
408 mc_format ~rn ~mc ~totup_fn:(name_tup ~rn) ~before:typ ~after:constr in
409
410 (* Formats as <typename str> where str is extracted from string mcode mc *)
411 let str_mc_format ~mc ~typ =
412 mc_format ~rn ~mc ~totup_fn:str_tup ~before:typ ~after:"" in
413
414 (* Formats as <typename mn[listlen]>, mn extracted from meta_name mcode mc *)
415 let lst_format ~mc ~typ ~listlen =
416 list_format ~rn ~before:typ ~mc ~listlen in
417
418 (* --- Implementations of functions that handle possible metavariables --- *)
419
420 let identfn c fn v =
421 match Ast0.unwrap v with
422 | Ast0.MetaId(mc, constr, s, _) ->
423 let constr = constraints ~rn constr in
424 let seed = seed ~rn s in
425 if seed = "" then (* if it has a seed then it is fresh ... ? *)
426 meta_mc_format ~mc ~typ:"identifier " ~constr
427 else
428 meta_mc_format ~mc ~typ:"fresh identifier " ~constr:(constr ^ seed)
429 | Ast0.MetaFunc(mc, constr, _) ->
430 let constr = constraints ~rn constr in
431 meta_mc_format ~mc ~typ:"function " ~constr
432 | Ast0.MetaLocalFunc(mc, constr, _) ->
433 let constr = constraints ~rn constr in
434 meta_mc_format ~mc ~typ:"local function " ~constr
435 | _ -> fn v in
436
437 let stmtfn c fn v =
438 match Ast0.unwrap v with
439 | Ast0.MetaStmt (mc, constr, pure) ->
440 let constr = constraints ~rn constr in
441 meta_mc_format ~mc ~typ:"statement " ~constr
442 | Ast0.MetaStmtList (mc, listlen, constr, _) ->
443 lst_format ~mc ~typ:"statement list" ~listlen
444 | Ast0.AsStmt (s1, s2)->
445 let stmt = c.VT0.combiner_rec_statement in as_format s1 s2 stmt stmt
446 | Ast0.Iterator (id, _, expdots, _, stmt,_) ->
447 let expids = c.VT0.combiner_rec_expression_dots expdots in
448 let stmtid = MVSet.union expids (c.VT0.combiner_rec_statement stmt) in
449 let iteids = ids ~rn ~typ:"iterator" ~id in
450 MVSet.union iteids stmtid
451 | _ -> fn v in
452
453 let exprfn c fn v =
454 let exprfn = c.VT0.combiner_rec_expression in
455 match Ast0.unwrap v with
456 | Ast0.MetaErr (mc, constr, _) ->
457 let constr = constraints ~rn constr in
458 meta_mc_format ~mc ~typ:"error " ~constr
459 | Ast0.MetaExpr (mc, constr, typeclist, form, _, _bitfield) ->
460 let typeclist' =
461 Common.map_option (List.map (Ast0toast.typeC false)) typeclist in
462
463 (* types function finds metavariable types and identifiers that were
464 * used in this expression and therefore need to be declared. *)
465 let types = types ~rn typeclist' in
466
467 (* type_c function returns the types in pretty string format *)
468 let typ = type_c ~form typeclist' in
469 let constr = constraints ~rn constr in
470 MVSet.union (meta_mc_format ~mc ~typ ~constr) (types)
471 | Ast0.MetaExprList (mc, listlen, _, _) ->
472 lst_format ~mc ~typ:"expression list" ~listlen
473 | Ast0.AsExpr (e1, e2) -> as_format e1 e2 exprfn exprfn
474 | Ast0.AsSExpr (e1, s2) -> as_format e1 s2 exprfn stmtfn
475 | _ -> fn v in
476
477 let assignOpfn c fn v =
478 match Ast0.unwrap v with
479 | Ast0.MetaAssign (mc, constr, pure) ->
480 let constr = constraints ~rn constr in
481 meta_mc_format ~mc ~typ:"assignment operator " ~constr
482 | _ -> fn v in
483
484 let binaryOpfn c fn v =
485 match Ast0.unwrap v with
486 | Ast0.MetaBinary (mc, constr, pure) ->
487 let constr = constraints ~rn constr in
488 meta_mc_format ~mc ~typ:"binary operator " ~constr
489 | _ -> fn v in
490
491 let tyfn c fn v =
492 match Ast0.unwrap v with
493 | Ast0.MetaType (mc, _, pure) -> meta_mc_format ~mc ~typ:"type " ~constr:""
494 | Ast0.AsType (tc1, tc2) ->
495 let ty = c.VT0.combiner_rec_typeC in as_format tc1 tc2 ty ty
496
497 (* this clause generates unparsable scripts for who knows what reason ...
498 * TODO: need to find out if it should be included or not. For now, ignore.
499 *)
500 | Ast0.TypeName mc ->
501 let _ = str_mc_format ~mc ~typ:"typedef " in
502 fn v
503 | _ -> fn v in
504
505 let initfn c fn v =
506 match Ast0.unwrap v with
507 | Ast0.MetaInit(mc, idconstr, pure) ->
508 let constr = constraints ~rn idconstr in
509 meta_mc_format ~mc ~typ:"initializer " ~constr
510 | Ast0.MetaInitList(mc, listlen, _, pure) ->
511 lst_format ~mc ~typ:"initializer list " ~listlen
512 | Ast0.AsInit(i1,i2) ->
513 let ini = c.VT0.combiner_rec_initialiser in as_format i1 i2 ini ini
514 | _ -> fn v in
515
516 let paramfn c fn v =
517 match Ast0.unwrap v with
518 | Ast0.MetaParam(mc, idconstr, pure) ->
519 let constr = constraints ~rn idconstr in
520 meta_mc_format ~mc ~typ:"parameter " ~constr
521 | Ast0.MetaParamList(mc, listlen, _, pure) ->
522 lst_format ~mc ~typ:"parameter list" ~listlen
523 | Ast0.AsParam (ptd,ex) ->
524 let par = c.VT0.combiner_rec_parameter in
525 let expr = c.VT0.combiner_rec_expression in
526 as_format ptd ex par expr
527 | _ -> fn v in
528
529 let templateparamfn c fn v =
530 match Ast0.unwrap v with
531 (* FIXME: incomplete *)
532 Ast0.TypenameOrClassParam(tyorcl,id,Some (eq,ty)) ->
533 fn v
534 | Ast0.TypenameOrClassParam(tyorcl,id,None) ->
535 fn v
536 | Ast0.VarNameParam(ty,id,Some (eq,exp)) ->
537 fn v
538 | Ast0.VarNameParam(ty,id,None) ->
539 fn v
540 | Ast0.TPComma(comma) ->
541 fn v
542 | Ast0.TPDots(dots) ->
543 fn v
544 in
545
546 (*
547 Not used for now, visitor not parameterized by this...
548 let define_paramfn c fn v =
549 match Ast0.unwrap v with
550 | Ast0.MetaDParamList(mc, listlen, pure) ->
551 lst_format ~mc ~typ:"identifier list" ~listlen
552 | _ -> fn v in
553 *)
554
555 let declfn c fn v =
556 match Ast0.unwrap v with
557 Ast0.MetaDecl(mc, idconstr, pure) ->
558 let constr = constraints ~rn idconstr in
559 meta_mc_format ~mc ~typ:"declaration " ~constr
560 | Ast0.AsDecl(dc1, dc2) ->
561 let dec = c.VT0.combiner_rec_declaration in as_format dc1 dc2 dec dec
562 | Ast0.MacroDecl(_, _, id, _, expdots, _, _, _) ->
563 let expids = c.VT0.combiner_rec_expression_dots expdots in
564 MVSet.union (ids ~rn ~typ:"declarer" ~id) expids
565 | Ast0.MacroDeclInit(_, _, id, _, expdots, _, _, _, ini, _) ->
566 let expids = c.VT0.combiner_rec_expression_dots expdots in
567 let inid = MVSet.union expids (c.VT0.combiner_rec_initialiser ini) in
568 let declids = ids ~rn ~typ:"declarer" ~id in
569 MVSet.union declids inid
570 | _ -> fn v in
571
572 let fieldfn c fn v =
573 match Ast0.unwrap v with
574 Ast0.MetaField(mc, idconstr, pure) ->
575 let constr = constraints ~rn idconstr in
576 meta_mc_format ~mc ~typ:"field " ~constr
577 | Ast0.MetaFieldList (mc, listlen, _, pure) ->
578 lst_format ~mc ~typ:"field list" ~listlen
579 | _ -> fn v in
580
581 let string_fragmentfn c fn v =
582 match Ast0.unwrap v with
583 | Ast0.MetaFormatList(_, mc, _, listlen) ->
584 lst_format ~mc ~typ:"format list" ~listlen
585 | Ast0.FormatFragment(_, format) ->
586 (match Ast0.unwrap format with
587 | Ast0.MetaFormat(mc, idconstr) ->
588 let constr = constraints ~rn:rn idconstr in
589 meta_mc_format ~mc ~typ:"format " ~constr
590 | _ -> fn v
591 )
592 | _ -> fn v in
593
594 let attr_argfn c fn v =
595 match Ast0.unwrap v with
596 | Ast0.MetaAttr(mc, idconstr, pure) ->
597 let constr = constraints ~rn idconstr in
598 meta_mc_format ~mc ~typ:"parameter " ~constr
599 | _ -> fn v in
600
601 V0.combiner_default bind option_default
602 ~meta_mcode:meta_mcode ~string_mcode:string_mcode ~const_mcode:const_mcode
603 ~simpleAssign_mcode:simpleAssign_mcode ~opAssign_mcode:opAssign_mcode
604 ~fixOp_mcode:fix_mcode ~unaryOp_mcode:unary_mcode ~arithOp_mcode:arithOp_mcode
605 ~logicalOp_mcode:logicalOp_mcode ~cv_mcode:cv_mcode ~sign_mcode:sign_mcode
606 ~struct_mcode:struct_mcode ~storage_mcode:storage_mcode ~inc_mcode:inc_mcode
607 ~dotsexpr:dotsexprfn ~dotsinit:dotsinitfn ~dotsparam:dotsparamfn
608 ~dotstemplateparam:dotstemplateparamfn ~dotsstmt:dotsstmtfn ~dotsdecl:dotsdeclfn
609 ~dotsfield:dotsfieldfn ~dotsenumdecl:dotsenumdeclfn ~dotscase:dotscasefn
610 ~dotsdefpar:dotsdefparfn ~ident:identfn ~expr:exprfn ~assignOp:assignOpfn
611 ~binaryOp:binaryOpfn ~ty:tyfn ~init:initfn ~param:paramfn
612 ~template_param:templateparamfn ~decl:declfn ~field:fieldfn
613 ~enumdecl:enumdeclfn ~stmt:stmtfn ~forinfo:forinfofn ~case:casefn
614 ~string_fragment:string_fragmentfn ~attribute:attributefn ~attr_arg:attr_argfn
615 ~top:topfn ()
616
617
618 (* ------------------------------------------------------------------------- *)
619 (* ENTRY POINT *)
620
621 type t = meta_variable
622
623 let make ?(inherit_rule = "") ?(constraints = "") ~typ meta_name =
624 make_mv typ (inherit_rule, meta_name) constraints
625
626 let get_rule (_,(r,_),_) = r
627 let get_name (_,(_,nm),_) = nm
628
629 (* forces rule inheritance (except if rule is already inherited). *)
630 let inherit_rule ?(force = false) ~new_rule ((a,(b,c),d) as mv) =
631 if b = "" || force then (a,(new_rule,c),d) else mv
632
633 let print out mv = output_string out (tostring_mv mv)
634
635 (* prints the strings in the set on separate lines, ended with semicolons.
636 * if do_group, group all metavars of same type on the same line.
637 *)
638 let print_list out ~do_group mvs =
639 let group_by_type mvs =
640 let rec group acc = function
641 | [] -> acc
642 | ((t,_,_)::ls as b) ->
643 let (same, rest) = List.partition (fun (x,_,_) -> t = x) b in
644 group ((t, same) :: acc) rest in
645 let collapse_group (typ, lst) =
646 let mvs = List.map (fun (_,b,c) -> tostring_mv ("",b,c)) lst in
647 typ ^ (String.concat ", " mvs) in
648 List.rev_map collapse_group (group [] mvs) in
649
650 if do_group then begin
651 let grouped = group_by_type mvs in
652 List.iter (fun b -> output_string out (b ^ ";\n")) grouped
653 end else
654 List.iter (fun b -> output_string out ((tostring_mv b) ^ ";\n")) mvs
655
656 (* takes abstract syntax trees for a rule and extract all metavariables.
657 * That is, metavariables declared in the header, but unused in the body, are
658 * discarded. Returns list of meta_variable.t's.
659 *)
660 let extract ~minus_rule ~rule_name =
661 let mvcomb = metavar_combiner rule_name in
662 let minus = List.map mvcomb.VT0.combiner_rec_top_level minus_rule in
663 let comb = List.fold_left MVSet.union MVSet.empty minus in
664 MVSet.elements comb