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 evaluate_targets env exprs =
25 List.iter (evaluate_target env) exprs
27 and evaluate_target env = function
28 | Ast.EGoalDefn _ | Ast.ETacticDefn _ -> assert false
31 | Ast.ECallGoal (loc, name, args) ->
32 let goal = Ast.getgoal env loc name in
33 run_goal env loc name args goal
36 | Ast.ETacticConstructor (loc, name, args) ->
37 (* All parameters of tactics must be simple constant expressions
38 * (strings, in future booleans, numbers, etc).
40 let args = List.map (Ast.to_constant env) args in
41 run_tactic env loc name args
43 (* If this is a goal then it's the same as calling goal(). If not
44 * then look up the variable and substitute it.
46 | Ast.EVar (loc, name) ->
47 let expr = Ast.getvar env loc name in
49 | EGoalDefn (loc, ([], _, _, _)) ->
50 evaluate_target env (Ast.ECallGoal (loc, name, []))
52 failwithf "%a: cannot call %s() since this goal has parameters"
53 Ast.string_loc loc name
55 evaluate_target env expr
58 (* Lists are inlined when found as a target. *)
59 | Ast.EList (loc, exprs) ->
60 evaluate_targets env exprs
62 (* A string (with or without substitutions) implies *file(filename). *)
63 | Ast.ESubsts (loc, str) ->
64 let str = Ast.substitute env loc str in
65 run_tactic env loc "*file" [Ast.CString str]
67 | Ast.EConstant (loc, c) ->
68 run_tactic env loc "*file" [c]
70 (* Run a goal by name. *)
71 and run_goal env loc name args (params, patterns, deps, code) =
72 Cmdline.debug "%a: running goal %s %a"
73 Ast.string_loc loc name Ast.string_expr (Ast.EList (Ast.noloc, args));
75 (* This is the point where we evaluate the goal arguments. We must
76 * do this before creating the new environment, because variables
77 * appearing in goal arguments don't refer to goal parameters.
79 let args = List.map (evaluate_goal_arg env) args in
81 (* Create a new environment which maps the parameter names to
86 try List.combine params args
87 with Invalid_argument _ ->
88 failwithf "%a: calling goal ‘%s’ with wrong number of arguments"
89 Ast.string_loc loc name in
90 List.fold_left (fun env (k, v) -> Ast.Env.add k v env) env params in
92 (* Check all dependencies have been updated. *)
93 evaluate_targets env deps;
95 (* Check if any target (ie. pattern) needs to be rebuilt.
96 * As with make, a goal with no targets is always run.
99 patterns = [] || List.exists (needs_rebuild env loc deps) patterns in
102 (* Run the code (if any). *)
106 (* Add some standard variables to the environment. *)
107 let expr_of_substs s = Ast.ESubsts (Ast.noloc, s) in
108 let expr_of_pattern = function
109 | Ast.PTactic (loc, tactic, targs) ->
110 Ast.ETacticConstructor (loc, tactic, List.map expr_of_substs targs)
112 let pexprs = List.map expr_of_pattern patterns in
113 let env = Ast.Env.add "@" (Ast.EList (Ast.noloc, pexprs)) env in
114 let env = Ast.Env.add "<" (Ast.EList (Ast.noloc, deps)) env in
118 | d :: _ -> Ast.Env.add "^" d env in
119 let code = Ast.to_shell_script env loc code in
120 let code = "set -e\nset -x\n\n" ^ code in
121 let r = Sys.command code in
123 eprintf "*** goal ‘%s’ failed with exit code %d\n" name r;
128 (* Check all targets were updated (else it's an error). *)
129 let pattern_still_needs_rebuild =
130 try Some (List.find (needs_rebuild env loc deps) patterns)
131 with Not_found -> None in
132 match pattern_still_needs_rebuild with
135 failwithf "%a: goal ‘%s’ ran successfully but it did not rebuild %a"
138 Ast.string_pattern pattern
141 (* Return whether the target (pattern) needs to be rebuilt. *)
142 and needs_rebuild env loc deps pattern =
143 Cmdline.debug "%a: testing if %a needs rebuild"
144 Ast.string_loc loc Ast.string_pattern pattern;
147 | Ast.PTactic (loc, tactic, targs) ->
148 (* Look up the tactic. *)
149 let params, code = Ast.gettactic env loc tactic in
151 (* Resolve the targs down to constants. Since needs_rebuild
152 * should be called with env containing the goal params, this
153 * should substitute any parameters in the tactic arguments.
155 let targs = List.map (Ast.substitute env loc) targs in
157 List.map (fun targ ->
158 Ast.EConstant (Ast.noloc, Ast.CString targ)) targs in
160 (* Create a new environment binding parameter names
165 try List.combine params targs
166 with Invalid_argument _ ->
167 failwithf "%a: calling tactic ‘%s’ with wrong number of arguments"
168 Ast.string_loc loc tactic in
169 List.fold_left (fun env (k, v) -> Ast.Env.add k v env) env params in
171 (* Add some standard variables to the environment. *)
172 let env = Ast.Env.add "<" (Ast.EList (Ast.noloc, deps)) env in
176 | d :: _ -> Ast.Env.add "^" d env in
177 let code = Ast.to_shell_script env loc code in
178 let code = "set -e\nset -x\n\n" ^ code in
179 let r = Sys.command code in
180 if r = 99 (* means "needs rebuild" *) then true
181 else if r = 0 (* means "doesn't need rebuild" *) then false
183 eprintf "*** tactic ‘%s’ failed with exit code %d\n" tactic r;
187 (* Evaluate a goal argument. This substitutes any variables found,
188 * and recursively calls functions.
190 and evaluate_goal_arg env = function
191 | Ast.EVar (loc, name) ->
192 let expr = Ast.getvar env loc name in
193 evaluate_goal_arg env expr
195 | Ast.ESubsts (loc, str) ->
196 let str = Ast.substitute env loc str in
197 Ast.EConstant (loc, Ast.CString str)
199 | Ast.EList (loc, exprs) ->
200 Ast.EList (loc, List.map (evaluate_goal_arg env) exprs)
202 | Ast.ETacticConstructor (loc, name, exprs) ->
203 Ast.ETacticConstructor (loc, name, List.map (evaluate_goal_arg env) exprs)
205 | Ast.ECallGoal (loc, name, _) ->
206 (* Goals don't return anything so they cannot be used in
207 * goal args. Use a function instead.
209 failwithf "%a: cannot use goal ‘%s’ in goal argument"
210 Ast.string_loc loc name
214 | Ast.ETacticDefn _ as e -> e
216 (* Find the goal which matches the given tactic and run it.
217 * cargs is a list of parameters (all constants).
219 and run_tactic env loc tactic cargs =
220 Cmdline.debug "%a: running tactic %s" Ast.string_loc loc tactic;
222 (* Find all goals in the environment. Returns a list of (name, goal). *)
224 let env = Ast.Env.bindings env in
227 | name, Ast.EGoalDefn (loc, goal) -> Some (name, goal)
230 (* Find all patterns. Returns a list of (pattern, name, goal). *)
231 let patterns : (Ast.pattern * Ast.id * Ast.goal) list =
233 (List.map (fun (name, ((_, patterns, _, _) as goal)) ->
234 List.map (fun pattern -> (pattern, name, goal)) patterns) goals) in
236 (* Find any patterns (ie. tactics) which match the one we
237 * are searching for. This returns a binding for the goal args,
238 * so we end up with a list of (pattern, name, goal, args).
240 let patterns : (Ast.pattern * Ast.id * Ast.goal * Ast.expr list) list =
242 fun (pattern, name, ((params, _, _, _) as goal)) ->
243 match matching_pattern env loc tactic cargs pattern params with
245 | Some args -> Some (pattern, name, goal, args)
250 (* There's no matching goal, but we don't need one if
251 * the tactic doesn't need to be rebuilt.
253 let targs = List.map (function Ast.CString s -> [Ast.SString s]) cargs in
254 let p = Ast.PTactic (loc, tactic, targs) in
255 if needs_rebuild env loc [] p then (
256 let t = Ast.ETacticConstructor (loc, tactic,
257 List.map (fun c -> Ast.EConstant (loc, c))
259 failwithf "%a: don't know how to build %a"
260 Ast.string_loc loc Ast.string_expr t
264 (* One or more goals match. We run them all (although if
265 * one of them succeeds in rebuilding, it will cut short the rest).
268 fun (_, name, goal, args) ->
269 run_goal env loc name args goal
272 (* Test if pattern matches *tactic(cargs). If it does
273 * then we return Some args where args is the arguments that must
274 * be passed to the matching goal. The params parameter is
275 * the names of the parameters of that goal.
277 and matching_pattern env loc tactic cargs pattern params =
279 | Ast.PTactic (loc, ttactic, targs)
280 when tactic <> ttactic ||
281 List.length cargs <> List.length targs ->
282 None (* Can't possibly match if tactic name or #args is different. *)
283 | Ast.PTactic (loc, ttactic, targs) ->
284 (* Do the args match with a possible params binding? *)
285 try Some (matching_params env loc params targs cargs)
286 with Not_found -> None
288 (* Return a possible binding. For example the goal is:
289 * goal compile (name) = "%name.o": "%name.c" {}
290 * which means that params = ["name"] and targs = ["%name.o"].
292 * If we are called with cargs = ["file1.o"], we would
295 * On non-matching this raises Not_found.
297 and matching_params env loc params targs cargs =
298 (* This is going to record the resulting binding. *)
299 let res = ref Ast.Env.empty in
300 List.iter2 (matching_param env loc params res) targs cargs;
302 (* Rearrange the result into goal parameter order. Also this
303 * checks that every parameter got a binding.
307 (* Allow the Not_found exception to escape if no binding for this param. *)
308 fun param -> Ast.Env.find param res
311 (* If targ = "%name.o" and carg = "file.o" then this would set
312 * name => "file" in !res. If they don't match, raises Not_found.
314 and matching_param env loc params res targ carg =
316 | Ast.CString carg ->
317 (* Substitute any non parameters in targ from the environment. *)
321 | Ast.SString _ as s -> s
323 if not (List.mem name params) then (
325 let expr = Ast.getvar env loc name in
326 match Ast.to_constant env expr with
327 | Ast.CString s -> Ast.SString s
328 with Failure _ -> raise Not_found
334 (* Do the actual pattern matching. Any remaining SVar elements
335 * must refer to goal parameters.
337 let carg = ref carg in
338 let rec loop = function
340 (* End of targ, we must have matched all of carg. *)
341 if !carg <> "" then raise Not_found
342 | Ast.SString s :: rest ->
343 (* Does this match the first part of !carg? *)
344 let clen = String.length !carg in
345 let slen = String.length s in
346 if slen > clen || s <> String.sub !carg 0 slen then
348 (* Yes, so continue after the matching prefix. *)
349 carg := String.sub !carg slen (clen-slen);
351 | Ast.SVar name :: Ast.SString s :: rest ->
352 (* This is a goal parameter. Find s later in !carg. *)
353 let i = string_find !carg s in
354 if i = -1 then raise Not_found;
355 (* Set the binding in !res. *)
356 let r = Ast.EConstant (Ast.noloc,
357 Ast.CString (String.sub !carg 0 i)) in
358 res := Ast.Env.add name r !res;
359 (* Continue after the match. *)
360 let skip = i + String.length s in
361 carg := String.sub !carg skip (String.length !carg - skip);
363 | Ast.SVar name :: [] ->
364 (* Matches the whole remainder of the string. *)
365 let r = Ast.EConstant (Ast.noloc, Ast.CString !carg) in
366 res := Ast.Env.add name r !res
367 | Ast.SVar x :: Ast.SVar y :: _ ->
368 (* TODO! We cannot match a target like "%x%y". *)