2 * Copyright (C) 2019 Richard W.M. Jones
3 * Copyright (C) 2019 Red Hat Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program 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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 let rec run_targets env exprs =
25 List.iter (run_target env) exprs
27 and run_target env = function
28 | Ast.EGoalDefn _ | Ast.EFuncDefn _ | Ast.ETacticDefn _ -> assert false
30 (* Call a goal or function. *)
31 | Ast.ECall (loc, name, args) ->
32 let expr = Ast.getvar env loc name in
34 | Ast.EGoalDefn (_, goal) ->
35 run_goal env loc name args goal []
36 | Ast.EFuncDefn (_, func) ->
37 let expr = Eval.call_function env loc name args func in
40 failwithf "%a: tried to call ‘%s’ which is not a goal or a function"
41 Ast.string_loc loc name
45 | Ast.ETacticCtor (loc, name, args) ->
46 (* All parameters of tactics must be simple constant expressions
47 * (strings, in future booleans, numbers, etc).
49 let args = List.map (Eval.to_constant env) args in
50 run_tactic env loc name args
52 (* If this is a goal then it's the same as calling goal(). If not
53 * then look up the variable and substitute it.
55 | Ast.EVar (loc, name) ->
56 let expr = Ast.getvar env loc name in
58 | Ast.EGoalDefn (loc, ([], _, _, _)) ->
59 run_target env (Ast.ECall (loc, name, []))
61 failwithf "%a: cannot call %s() since this goal has parameters"
62 Ast.string_loc loc name
67 (* Lists are inlined when found as a target. *)
68 | Ast.EList (loc, exprs) ->
71 (* A string (with or without substitutions) implies *file(filename). *)
72 | Ast.ESubsts (loc, str) ->
73 let str = Eval.substitute env loc str in
74 run_tactic env loc "*file" [Ast.CString str]
76 | Ast.EConstant (loc, c) ->
77 run_tactic env loc "*file" [c]
79 (* Run a goal by name. *)
80 and run_goal env loc name args (params, patterns, deps, code) extra_deps =
81 (* This is used to print the goal in debug and error messages only. *)
83 sprintf "%s (%s)" name
84 (String.concat ", " (List.map (Ast.string_expr ()) args)) in
85 Cmdline.debug "%a: running goal %s" Ast.string_loc loc debug_goal;
87 (* This is the point where we evaluate the goal arguments. We must
88 * do this before creating the new environment, because variables
89 * appearing in goal arguments don't refer to goal parameters.
91 let args = List.map (Eval.evaluate_goal_arg env) args in
93 (* Create a new environment which maps the parameter names to
98 try List.combine params args
99 with Invalid_argument _ ->
100 failwithf "%a: calling goal %s with wrong number of arguments, expecting %d args but got %d args"
101 Ast.string_loc loc debug_goal
102 (List.length params) (List.length args) in
103 List.fold_left (fun env (k, v) -> Ast.Env.add k v env) env params in
105 (* Check all dependencies have been updated. *)
106 run_targets env (deps @ extra_deps);
108 (* Check if any target (ie. pattern) needs to be rebuilt.
109 * As with make, a goal with no targets is always run.
113 List.exists (needs_rebuild env loc deps extra_deps) patterns in
116 (* Run the code (if any). *)
118 | None -> () (* No { CODE } section. *)
121 (* Add some standard variables to the environment. *)
122 let expr_of_substs s = Ast.ESubsts (Ast.noloc, s) in
123 let expr_of_pattern = function
124 | Ast.PTactic (loc, tactic, targs) ->
125 Ast.ETacticCtor (loc, tactic, List.map expr_of_substs targs)
127 let pexprs = List.map expr_of_pattern patterns in
128 let env = Ast.Env.add "@" (Ast.EList (Ast.noloc, pexprs)) env in
129 let env = Ast.Env.add "<" (Ast.EList (Ast.noloc, deps)) env in
131 (* NB: extra_deps are not added to %^ *)
134 | d :: _ -> Ast.Env.add "^" d env in
135 let r = Eval.run_code env loc code in
137 eprintf "*** goal ‘%s’ failed with exit code %d\n" name r;
141 (* Check all targets were updated after the code was
142 * run (else it's an error).
144 let pattern_still_needs_rebuild =
146 Some (List.find (needs_rebuild env loc deps extra_deps) patterns)
149 match pattern_still_needs_rebuild with
152 failwithf "%a: goal %s ran successfully but it did not rebuild %a"
153 Ast.string_loc loc debug_goal Ast.string_pattern pattern
157 (* Return whether the target (pattern) needs to be rebuilt. *)
158 and needs_rebuild env loc deps extra_deps pattern =
159 Cmdline.debug "%a: testing if %a needs rebuild"
160 Ast.string_loc loc Ast.string_pattern pattern;
163 | Ast.PTactic (loc, tactic, targs) ->
164 (* Look up the tactic. *)
165 let params, code = Ast.gettactic env loc tactic in
167 (* Resolve the targs down to constants. Since needs_rebuild
168 * should be called with env containing the goal params, this
169 * should substitute any parameters in the tactic arguments.
171 let targs = List.map (Eval.substitute env loc) targs in
173 List.map (fun targ ->
174 Ast.EConstant (Ast.noloc, Ast.CString targ)) targs in
176 (* Create a new environment binding parameter names
181 try List.combine params targs
182 with Invalid_argument _ ->
183 failwithf "%a: calling tactic ‘%s’ with wrong number of arguments"
184 Ast.string_loc loc tactic in
185 List.fold_left (fun env (k, v) -> Ast.Env.add k v env) env params in
187 (* Add some standard variables to the environment. *)
188 let env = Ast.Env.add "<" (Ast.EList (Ast.noloc, deps)) env in
190 (* NB: extra_deps are not added to %^ *)
193 | d :: _ -> Ast.Env.add "^" d env in
194 let r = Eval.run_code env loc code in
195 if r = 99 (* means "needs rebuild" *) then true
196 else if r = 0 (* means "doesn't need rebuild" *) then false
198 eprintf "*** tactic ‘%s’ failed with exit code %d\n" tactic r;
202 (* Find the goal which matches the given tactic and run it.
203 * cargs is a list of parameters (all constants).
205 and run_tactic env loc tactic cargs =
206 (* This is used to print the tactic in debug and error messages only. *)
209 (Ast.ETacticCtor (loc, tactic,
210 List.map (fun c -> Ast.EConstant (loc, c)) cargs)) in
211 Cmdline.debug "%a: running tactic %s" Ast.string_loc loc debug_tactic;
213 (* Find all goals in the environment. Returns a list of (name, goal). *)
215 let env = Ast.Env.bindings env in
218 | name, Ast.EGoalDefn (loc, goal) -> Some (name, goal)
221 (* Find all patterns. Returns a list of (pattern, name, goal). *)
222 let patterns : (Ast.pattern * Ast.id * Ast.goal) list =
224 (List.map (fun (name, ((_, patterns, _, _) as goal)) ->
225 List.map (fun pattern -> (pattern, name, goal)) patterns) goals) in
227 (* Find any patterns (ie. tactics) which match the one we
228 * are searching for. This returns a binding for the goal args,
229 * so we end up with a list of (pattern, name, goal, args).
231 let patterns : (Ast.pattern * Ast.id * Ast.goal * Ast.expr list) list =
233 fun (pattern, name, ((params, _, _, _) as goal)) ->
234 match matching_pattern env loc tactic cargs pattern params with
236 | Some args -> Some (pattern, name, goal, args)
241 (* There's no matching goal, but we don't need one if
242 * the tactic doesn't need to be rebuilt.
244 let targs = List.map (function Ast.CString s -> [Ast.SString s]) cargs in
245 let p = Ast.PTactic (loc, tactic, targs) in
246 if needs_rebuild env loc [] [] p then
247 failwithf "%a: don't know how to build %s"
248 Ast.string_loc loc debug_tactic
250 | [_, name, goal, args] ->
251 (* Single goal matches, run it. *)
252 run_goal env loc name args goal []
255 (* Two or more goals match. Only one must have a CODE section,
256 * and we combine the dependencies into a "supergoal".
258 let with_code, without_code =
260 fun (_, _, (_, _, _, code), _) -> code <> None
263 let (_, name, goal, args), extra_deps =
268 List.map (fun (_, _, (_, _, deps, _), _) -> deps) without_code
273 (* This is OK, it means we'll rebuild all dependencies
274 * but there is no code to run. Pick the first goal
275 * without code and the dependencies from the other goals.
277 let g = List.hd without_code in
280 List.map (fun (_, _, (_, _, deps, _), _) -> deps)
281 (List.tl without_code)
286 failwithf "%a: multiple goals found which match tactic %s, but more than one of these goals have {code} sections which is not allowed"
287 Ast.string_loc loc debug_tactic in
289 run_goal env loc name args goal extra_deps
291 (* Test if pattern matches *tactic(cargs). If it does
292 * then we return Some args where args is the arguments that must
293 * be passed to the matching goal. The params parameter is
294 * the names of the parameters of that goal.
296 and matching_pattern env loc tactic cargs pattern params =
298 | Ast.PTactic (loc, ttactic, targs)
299 when tactic <> ttactic ||
300 List.length cargs <> List.length targs ->
301 None (* Can't possibly match if tactic name or #args is different. *)
302 | Ast.PTactic (loc, ttactic, targs) ->
303 (* Do the args match with a possible params binding? *)
304 try Some (matching_params env loc params targs cargs)
305 with Not_found -> None
307 (* Return a possible binding. For example the goal is:
308 * goal compile (name) = "%name.o": "%name.c" {}
309 * which means that params = ["name"] and targs = ["%name.o"].
311 * If we are called with cargs = ["file1.o"], we would
314 * On non-matching this raises Not_found.
316 and matching_params env loc params targs cargs =
317 (* This is going to record the resulting binding. *)
318 let res = ref Ast.Env.empty in
319 List.iter2 (matching_param env loc params res) targs cargs;
321 (* Rearrange the result into goal parameter order. Also this
322 * checks that every parameter got a binding.
326 (* Allow the Not_found exception to escape if no binding for this param. *)
327 fun param -> Ast.Env.find param res
330 (* If targ = "%name.o" and carg = "file.o" then this would set
331 * name => "file" in !res. If they don't match, raises Not_found.
333 and matching_param env loc params res targ carg =
335 | Ast.CString carg ->
336 (* Substitute any non parameters in targ from the environment. *)
340 | Ast.SString _ as s -> s
342 if not (List.mem name params) then (
344 let expr = Ast.getvar env loc name in
345 match Eval.to_constant env expr with
346 | Ast.CString s -> Ast.SString s
347 with Failure _ -> raise Not_found
353 (* Do the actual pattern matching. Any remaining SVar elements
354 * must refer to goal parameters.
356 let carg = ref carg in
357 let rec loop = function
359 (* End of targ, we must have matched all of carg. *)
360 if !carg <> "" then raise Not_found
361 | Ast.SString s :: rest ->
362 (* Does this match the first part of !carg? *)
363 let clen = String.length !carg in
364 let slen = String.length s in
365 if slen > clen || s <> String.sub !carg 0 slen then
367 (* Yes, so continue after the matching prefix. *)
368 carg := String.sub !carg slen (clen-slen);
370 | Ast.SVar name :: Ast.SString s :: rest ->
371 (* This is a goal parameter. Find s later in !carg. *)
372 let i = string_find !carg s in
373 if i = -1 then raise Not_found;
374 (* Set the binding in !res. *)
375 let r = Ast.EConstant (Ast.noloc,
376 Ast.CString (String.sub !carg 0 i)) in
377 res := Ast.Env.add name r !res;
378 (* Continue after the match. *)
379 let skip = i + String.length s in
380 carg := String.sub !carg skip (String.length !carg - skip);
382 | Ast.SVar name :: [] ->
383 (* Matches the whole remainder of the string. *)
384 let r = Ast.EConstant (Ast.noloc, Ast.CString !carg) in
385 res := Ast.Env.add name r !res
386 | Ast.SVar x :: Ast.SVar y :: _ ->
387 (* TODO! We cannot match a target like "%x%y". *)