michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // This executes a very simple search for all functions that call a michael@0: // given set of functions. It's intended to be the simplest possible michael@0: // way of refactoring a common pattern of function calls. Of course, michael@0: // it's still up to a human to decide if the replacement is truely michael@0: // suitable, but this gets the low-hanging fruit. michael@0: michael@0: // Expects the variable 'patterns' to hold an object with replacement michael@0: // function names as keys and function lists as values. Any function michael@0: // in the tested source that calls all of the functions named in the michael@0: // list will be listed in the output as being likely candidates to michael@0: // instead call the replacement function. michael@0: michael@0: include("unstable/lazy_types.js"); michael@0: michael@0: var matches = {}; michael@0: michael@0: function identity(x) x; michael@0: michael@0: function process_cp_pre_genericize(fndecl) michael@0: { michael@0: var c = []; michael@0: function calls(t, stack) michael@0: { michael@0: try { michael@0: t.tree_check(CALL_EXPR); michael@0: var fn = callable_arg_function_decl(CALL_EXPR_FN(t)); michael@0: if (fn) michael@0: c.push(decl_name_string(fn)); michael@0: } michael@0: catch (e if e.TreeCheckError) { } michael@0: } michael@0: michael@0: walk_tree(DECL_SAVED_TREE(fndecl), calls); michael@0: michael@0: for (let [fnreplace, pattern] in patterns) michael@0: if (pattern.map(function(e){ return c.some(function(f) { return e == f; }); }).every(identity)) michael@0: if (fnreplace != (n = decl_name_string(fndecl))) michael@0: print(fnreplace +" could probably be used in "+ n); michael@0: }