reduce-vars.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /***********************************************************************
  2. A JavaScript tokenizer / parser / beautifier / compressor.
  3. https://github.com/mishoo/UglifyJS2
  4. -------------------------------- (C) ---------------------------------
  5. Author: Mihai Bazon
  6. <mihai.bazon@gmail.com>
  7. http://mihai.bazon.net/blog
  8. Distributed under the BSD license:
  9. Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. * Redistributions of source code must retain the above
  14. copyright notice, this list of conditions and the following
  15. disclaimer.
  16. * Redistributions in binary form must reproduce the above
  17. copyright notice, this list of conditions and the following
  18. disclaimer in the documentation and/or other materials
  19. provided with the distribution.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
  21. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  29. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. SUCH DAMAGE.
  32. ***********************************************************************/
  33. import {
  34. AST_Accessor,
  35. AST_Array,
  36. AST_Assign,
  37. AST_Await,
  38. AST_Binary,
  39. AST_Block,
  40. AST_Call,
  41. AST_Case,
  42. AST_Chain,
  43. AST_Class,
  44. AST_ClassStaticBlock,
  45. AST_ClassExpression,
  46. AST_Conditional,
  47. AST_Default,
  48. AST_Defun,
  49. AST_Destructuring,
  50. AST_Do,
  51. AST_Exit,
  52. AST_Expansion,
  53. AST_For,
  54. AST_ForIn,
  55. AST_If,
  56. AST_LabeledStatement,
  57. AST_Lambda,
  58. AST_New,
  59. AST_Node,
  60. AST_Number,
  61. AST_ObjectKeyVal,
  62. AST_PropAccess,
  63. AST_Scope,
  64. AST_Sequence,
  65. AST_SimpleStatement,
  66. AST_Symbol,
  67. AST_SymbolCatch,
  68. AST_SymbolConst,
  69. AST_SymbolDeclaration,
  70. AST_SymbolDefun,
  71. AST_SymbolFunarg,
  72. AST_SymbolLambda,
  73. AST_SymbolRef,
  74. AST_This,
  75. AST_Toplevel,
  76. AST_Try,
  77. AST_Unary,
  78. AST_UnaryPrefix,
  79. AST_Undefined,
  80. AST_VarDef,
  81. AST_While,
  82. AST_Yield,
  83. walk,
  84. walk_body,
  85. TreeWalker,
  86. } from "../ast.js";
  87. import { HOP, make_node, noop } from "../utils/index.js";
  88. import { lazy_op, is_modified, is_lhs } from "./inference.js";
  89. import { INLINED, clear_flag } from "./compressor-flags.js";
  90. import { read_property, has_break_or_continue, is_recursive_ref } from "./common.js";
  91. /**
  92. * Define the method AST_Node#reduce_vars, which goes through the AST in
  93. * execution order to perform basic flow analysis
  94. */
  95. function def_reduce_vars(node, func) {
  96. node.DEFMETHOD("reduce_vars", func);
  97. }
  98. def_reduce_vars(AST_Node, noop);
  99. /** Clear definition properties */
  100. function reset_def(compressor, def) {
  101. def.assignments = 0;
  102. def.chained = false;
  103. def.direct_access = false;
  104. def.escaped = 0;
  105. def.recursive_refs = 0;
  106. def.references = [];
  107. def.single_use = undefined;
  108. if (
  109. def.scope.pinned()
  110. || (def.orig[0] instanceof AST_SymbolFunarg && def.scope.uses_arguments)
  111. ) {
  112. def.fixed = false;
  113. } else if (def.orig[0] instanceof AST_SymbolConst || !compressor.exposed(def)) {
  114. def.fixed = def.init;
  115. } else {
  116. def.fixed = false;
  117. }
  118. }
  119. function reset_variables(tw, compressor, node) {
  120. node.variables.forEach(function(def) {
  121. reset_def(compressor, def);
  122. if (def.fixed === null) {
  123. tw.defs_to_safe_ids.set(def.id, tw.safe_ids);
  124. mark(tw, def, true);
  125. } else if (def.fixed) {
  126. tw.loop_ids.set(def.id, tw.in_loop);
  127. mark(tw, def, true);
  128. }
  129. });
  130. }
  131. function reset_block_variables(compressor, node) {
  132. if (node.block_scope) node.block_scope.variables.forEach((def) => {
  133. reset_def(compressor, def);
  134. });
  135. }
  136. function push(tw) {
  137. tw.safe_ids = Object.create(tw.safe_ids);
  138. }
  139. function pop(tw) {
  140. tw.safe_ids = Object.getPrototypeOf(tw.safe_ids);
  141. }
  142. function mark(tw, def, safe) {
  143. tw.safe_ids[def.id] = safe;
  144. }
  145. function safe_to_read(tw, def) {
  146. if (def.single_use == "m") return false;
  147. if (tw.safe_ids[def.id]) {
  148. if (def.fixed == null) {
  149. var orig = def.orig[0];
  150. if (orig instanceof AST_SymbolFunarg || orig.name == "arguments") return false;
  151. def.fixed = make_node(AST_Undefined, orig);
  152. }
  153. return true;
  154. }
  155. return def.fixed instanceof AST_Defun;
  156. }
  157. function safe_to_assign(tw, def, scope, value) {
  158. if (def.fixed === undefined) return true;
  159. let def_safe_ids;
  160. if (def.fixed === null
  161. && (def_safe_ids = tw.defs_to_safe_ids.get(def.id))
  162. ) {
  163. def_safe_ids[def.id] = false;
  164. tw.defs_to_safe_ids.delete(def.id);
  165. return true;
  166. }
  167. if (!HOP(tw.safe_ids, def.id)) return false;
  168. if (!safe_to_read(tw, def)) return false;
  169. if (def.fixed === false) return false;
  170. if (def.fixed != null && (!value || def.references.length > def.assignments)) return false;
  171. if (def.fixed instanceof AST_Defun) {
  172. return value instanceof AST_Node && def.fixed.parent_scope === scope;
  173. }
  174. return def.orig.every((sym) => {
  175. return !(sym instanceof AST_SymbolConst
  176. || sym instanceof AST_SymbolDefun
  177. || sym instanceof AST_SymbolLambda);
  178. });
  179. }
  180. function ref_once(tw, compressor, def) {
  181. return compressor.option("unused")
  182. && !def.scope.pinned()
  183. && def.references.length - def.recursive_refs == 1
  184. && tw.loop_ids.get(def.id) === tw.in_loop;
  185. }
  186. function is_immutable(value) {
  187. if (!value) return false;
  188. return value.is_constant()
  189. || value instanceof AST_Lambda
  190. || value instanceof AST_This;
  191. }
  192. // A definition "escapes" when its value can leave the point of use.
  193. // Example: `a = b || c`
  194. // In this example, "b" and "c" are escaping, because they're going into "a"
  195. //
  196. // def.escaped is != 0 when it escapes.
  197. //
  198. // When greater than 1, it means that N chained properties will be read off
  199. // of that def before an escape occurs. This is useful for evaluating
  200. // property accesses, where you need to know when to stop.
  201. function mark_escaped(tw, d, scope, node, value, level = 0, depth = 1) {
  202. var parent = tw.parent(level);
  203. if (value) {
  204. if (value.is_constant()) return;
  205. if (value instanceof AST_ClassExpression) return;
  206. }
  207. if (
  208. parent instanceof AST_Assign && (parent.operator === "=" || parent.logical) && node === parent.right
  209. || parent instanceof AST_Call && (node !== parent.expression || parent instanceof AST_New)
  210. || parent instanceof AST_Exit && node === parent.value && node.scope !== d.scope
  211. || parent instanceof AST_VarDef && node === parent.value
  212. || parent instanceof AST_Yield && node === parent.value && node.scope !== d.scope
  213. ) {
  214. if (depth > 1 && !(value && value.is_constant_expression(scope))) depth = 1;
  215. if (!d.escaped || d.escaped > depth) d.escaped = depth;
  216. return;
  217. } else if (
  218. parent instanceof AST_Array
  219. || parent instanceof AST_Await
  220. || parent instanceof AST_Binary && lazy_op.has(parent.operator)
  221. || parent instanceof AST_Conditional && node !== parent.condition
  222. || parent instanceof AST_Expansion
  223. || parent instanceof AST_Sequence && node === parent.tail_node()
  224. ) {
  225. mark_escaped(tw, d, scope, parent, parent, level + 1, depth);
  226. } else if (parent instanceof AST_ObjectKeyVal && node === parent.value) {
  227. var obj = tw.parent(level + 1);
  228. mark_escaped(tw, d, scope, obj, obj, level + 2, depth);
  229. } else if (parent instanceof AST_PropAccess && node === parent.expression) {
  230. value = read_property(value, parent.property);
  231. mark_escaped(tw, d, scope, parent, value, level + 1, depth + 1);
  232. if (value) return;
  233. }
  234. if (level > 0) return;
  235. if (parent instanceof AST_Sequence && node !== parent.tail_node()) return;
  236. if (parent instanceof AST_SimpleStatement) return;
  237. d.direct_access = true;
  238. }
  239. const suppress = node => walk(node, node => {
  240. if (!(node instanceof AST_Symbol)) return;
  241. var d = node.definition();
  242. if (!d) return;
  243. if (node instanceof AST_SymbolRef) d.references.push(node);
  244. d.fixed = false;
  245. });
  246. def_reduce_vars(AST_Accessor, function(tw, descend, compressor) {
  247. push(tw);
  248. reset_variables(tw, compressor, this);
  249. descend();
  250. pop(tw);
  251. return true;
  252. });
  253. def_reduce_vars(AST_Assign, function(tw, descend, compressor) {
  254. var node = this;
  255. if (node.left instanceof AST_Destructuring) {
  256. suppress(node.left);
  257. return;
  258. }
  259. const finish_walk = () => {
  260. if (node.logical) {
  261. node.left.walk(tw);
  262. push(tw);
  263. node.right.walk(tw);
  264. pop(tw);
  265. return true;
  266. }
  267. };
  268. var sym = node.left;
  269. if (!(sym instanceof AST_SymbolRef)) return finish_walk();
  270. var def = sym.definition();
  271. var safe = safe_to_assign(tw, def, sym.scope, node.right);
  272. def.assignments++;
  273. if (!safe) return finish_walk();
  274. var fixed = def.fixed;
  275. if (!fixed && node.operator != "=" && !node.logical) return finish_walk();
  276. var eq = node.operator == "=";
  277. var value = eq ? node.right : node;
  278. if (is_modified(compressor, tw, node, value, 0)) return finish_walk();
  279. def.references.push(sym);
  280. if (!node.logical) {
  281. if (!eq) def.chained = true;
  282. def.fixed = eq ? function() {
  283. return node.right;
  284. } : function() {
  285. return make_node(AST_Binary, node, {
  286. operator: node.operator.slice(0, -1),
  287. left: fixed instanceof AST_Node ? fixed : fixed(),
  288. right: node.right
  289. });
  290. };
  291. }
  292. if (node.logical) {
  293. mark(tw, def, false);
  294. push(tw);
  295. node.right.walk(tw);
  296. pop(tw);
  297. return true;
  298. }
  299. mark(tw, def, false);
  300. node.right.walk(tw);
  301. mark(tw, def, true);
  302. mark_escaped(tw, def, sym.scope, node, value, 0, 1);
  303. return true;
  304. });
  305. def_reduce_vars(AST_Binary, function(tw) {
  306. if (!lazy_op.has(this.operator)) return;
  307. this.left.walk(tw);
  308. push(tw);
  309. this.right.walk(tw);
  310. pop(tw);
  311. return true;
  312. });
  313. def_reduce_vars(AST_Block, function(tw, descend, compressor) {
  314. reset_block_variables(compressor, this);
  315. });
  316. def_reduce_vars(AST_Case, function(tw) {
  317. push(tw);
  318. this.expression.walk(tw);
  319. pop(tw);
  320. push(tw);
  321. walk_body(this, tw);
  322. pop(tw);
  323. return true;
  324. });
  325. def_reduce_vars(AST_Class, function(tw, descend) {
  326. clear_flag(this, INLINED);
  327. push(tw);
  328. descend();
  329. pop(tw);
  330. return true;
  331. });
  332. def_reduce_vars(AST_ClassStaticBlock, function(tw, descend, compressor) {
  333. reset_block_variables(compressor, this);
  334. });
  335. def_reduce_vars(AST_Conditional, function(tw) {
  336. this.condition.walk(tw);
  337. push(tw);
  338. this.consequent.walk(tw);
  339. pop(tw);
  340. push(tw);
  341. this.alternative.walk(tw);
  342. pop(tw);
  343. return true;
  344. });
  345. def_reduce_vars(AST_Chain, function(tw, descend) {
  346. // Chains' conditions apply left-to-right, cumulatively.
  347. // If we walk normally we don't go in that order because we would pop before pushing again
  348. // Solution: AST_PropAccess and AST_Call push when they are optional, and never pop.
  349. // Then we pop everything when they are done being walked.
  350. const safe_ids = tw.safe_ids;
  351. descend();
  352. // Unroll back to start
  353. tw.safe_ids = safe_ids;
  354. return true;
  355. });
  356. def_reduce_vars(AST_Call, function (tw) {
  357. this.expression.walk(tw);
  358. if (this.optional) {
  359. // Never pop -- it's popped at AST_Chain above
  360. push(tw);
  361. }
  362. for (const arg of this.args) arg.walk(tw);
  363. return true;
  364. });
  365. def_reduce_vars(AST_PropAccess, function (tw) {
  366. if (!this.optional) return;
  367. this.expression.walk(tw);
  368. // Never pop -- it's popped at AST_Chain above
  369. push(tw);
  370. if (this.property instanceof AST_Node) this.property.walk(tw);
  371. return true;
  372. });
  373. def_reduce_vars(AST_Default, function(tw, descend) {
  374. push(tw);
  375. descend();
  376. pop(tw);
  377. return true;
  378. });
  379. function mark_lambda(tw, descend, compressor) {
  380. clear_flag(this, INLINED);
  381. push(tw);
  382. reset_variables(tw, compressor, this);
  383. var iife;
  384. if (!this.name
  385. && !this.uses_arguments
  386. && !this.pinned()
  387. && (iife = tw.parent()) instanceof AST_Call
  388. && iife.expression === this
  389. && !iife.args.some(arg => arg instanceof AST_Expansion)
  390. && this.argnames.every(arg_name => arg_name instanceof AST_Symbol)
  391. ) {
  392. // Virtually turn IIFE parameters into variable definitions:
  393. // (function(a,b) {...})(c,d) => (function() {var a=c,b=d; ...})()
  394. // So existing transformation rules can work on them.
  395. this.argnames.forEach((arg, i) => {
  396. if (!arg.definition) return;
  397. var d = arg.definition();
  398. // Avoid setting fixed when there's more than one origin for a variable value
  399. if (d.orig.length > 1) return;
  400. if (d.fixed === undefined && (!this.uses_arguments || tw.has_directive("use strict"))) {
  401. d.fixed = function() {
  402. return iife.args[i] || make_node(AST_Undefined, iife);
  403. };
  404. tw.loop_ids.set(d.id, tw.in_loop);
  405. mark(tw, d, true);
  406. } else {
  407. d.fixed = false;
  408. }
  409. });
  410. }
  411. descend();
  412. pop(tw);
  413. handle_defined_after_hoist(this);
  414. return true;
  415. }
  416. /**
  417. * It's possible for a hoisted function to use something that's not defined yet. Example:
  418. *
  419. * hoisted();
  420. * var defined_after = true;
  421. * function hoisted() {
  422. * // use defined_after
  423. * }
  424. *
  425. * This function is called on the parent to handle this issue.
  426. */
  427. function handle_defined_after_hoist(parent) {
  428. const defuns = [];
  429. walk(parent, node => {
  430. if (node === parent) return;
  431. if (node instanceof AST_Defun) defuns.push(node);
  432. if (
  433. node instanceof AST_Scope
  434. || node instanceof AST_SimpleStatement
  435. ) return true;
  436. });
  437. const symbols_of_interest = new Set();
  438. const defuns_of_interest = new Set();
  439. const potential_conflicts = [];
  440. for (const defun of defuns) {
  441. const fname_def = defun.name.definition();
  442. const found_self_ref_in_other_defuns = defuns.some(
  443. d => d !== defun && d.enclosed.indexOf(fname_def) !== -1
  444. );
  445. for (const def of defun.enclosed) {
  446. if (
  447. def.fixed === false
  448. || def === fname_def
  449. || def.scope.get_defun_scope() !== parent
  450. ) {
  451. continue;
  452. }
  453. // defun is hoisted, so always safe
  454. if (
  455. def.assignments === 0
  456. && def.orig.length === 1
  457. && def.orig[0] instanceof AST_SymbolDefun
  458. ) {
  459. continue;
  460. }
  461. if (found_self_ref_in_other_defuns) {
  462. def.fixed = false;
  463. continue;
  464. }
  465. // for the slower checks below this loop
  466. potential_conflicts.push({ defun, def, fname_def });
  467. symbols_of_interest.add(def.id);
  468. symbols_of_interest.add(fname_def.id);
  469. defuns_of_interest.add(defun);
  470. }
  471. }
  472. // linearize all symbols, and locate defs that are read after the defun
  473. if (potential_conflicts.length) {
  474. // All "symbols of interest", that is, defuns or defs, that we found.
  475. // These are placed in order so we can check which is after which.
  476. const found_symbols = [];
  477. // Indices of `found_symbols` which are writes
  478. const found_symbol_writes = new Set();
  479. // Defun ranges are recorded because we don't care if a function uses the def internally
  480. const defun_ranges = new Map();
  481. let tw;
  482. parent.walk((tw = new TreeWalker((node, descend) => {
  483. if (node instanceof AST_Defun && defuns_of_interest.has(node)) {
  484. const start = found_symbols.length;
  485. descend();
  486. const end = found_symbols.length;
  487. defun_ranges.set(node, { start, end });
  488. return true;
  489. }
  490. // if we found a defun on the list, mark IN_DEFUN=id and descend
  491. if (node instanceof AST_Symbol && node.thedef) {
  492. const id = node.definition().id;
  493. if (symbols_of_interest.has(id)) {
  494. if (node instanceof AST_SymbolDeclaration || is_lhs(node, tw)) {
  495. found_symbol_writes.add(found_symbols.length);
  496. }
  497. found_symbols.push(id);
  498. }
  499. }
  500. })));
  501. for (const { def, defun, fname_def } of potential_conflicts) {
  502. const defun_range = defun_ranges.get(defun);
  503. // find the index in `found_symbols`, with some special rules:
  504. const find = (sym_id, starting_at = 0, must_be_write = false) => {
  505. let index = starting_at;
  506. for (;;) {
  507. index = found_symbols.indexOf(sym_id, index);
  508. if (index === -1) {
  509. break;
  510. } else if (index >= defun_range.start && index < defun_range.end) {
  511. index = defun_range.end;
  512. continue;
  513. } else if (must_be_write && !found_symbol_writes.has(index)) {
  514. index++;
  515. continue;
  516. } else {
  517. break;
  518. }
  519. }
  520. return index;
  521. };
  522. const read_defun_at = find(fname_def.id);
  523. const wrote_def_at = find(def.id, read_defun_at + 1, true);
  524. const wrote_def_after_reading_defun = read_defun_at != -1 && wrote_def_at != -1 && wrote_def_at > read_defun_at;
  525. if (wrote_def_after_reading_defun) {
  526. def.fixed = false;
  527. }
  528. }
  529. }
  530. }
  531. def_reduce_vars(AST_Lambda, mark_lambda);
  532. def_reduce_vars(AST_Do, function(tw, descend, compressor) {
  533. reset_block_variables(compressor, this);
  534. const saved_loop = tw.in_loop;
  535. tw.in_loop = this;
  536. push(tw);
  537. this.body.walk(tw);
  538. if (has_break_or_continue(this)) {
  539. pop(tw);
  540. push(tw);
  541. }
  542. this.condition.walk(tw);
  543. pop(tw);
  544. tw.in_loop = saved_loop;
  545. return true;
  546. });
  547. def_reduce_vars(AST_For, function(tw, descend, compressor) {
  548. reset_block_variables(compressor, this);
  549. if (this.init) this.init.walk(tw);
  550. const saved_loop = tw.in_loop;
  551. tw.in_loop = this;
  552. push(tw);
  553. if (this.condition) this.condition.walk(tw);
  554. this.body.walk(tw);
  555. if (this.step) {
  556. if (has_break_or_continue(this)) {
  557. pop(tw);
  558. push(tw);
  559. }
  560. this.step.walk(tw);
  561. }
  562. pop(tw);
  563. tw.in_loop = saved_loop;
  564. return true;
  565. });
  566. def_reduce_vars(AST_ForIn, function(tw, descend, compressor) {
  567. reset_block_variables(compressor, this);
  568. suppress(this.init);
  569. this.object.walk(tw);
  570. const saved_loop = tw.in_loop;
  571. tw.in_loop = this;
  572. push(tw);
  573. this.body.walk(tw);
  574. pop(tw);
  575. tw.in_loop = saved_loop;
  576. return true;
  577. });
  578. def_reduce_vars(AST_If, function(tw) {
  579. this.condition.walk(tw);
  580. push(tw);
  581. this.body.walk(tw);
  582. pop(tw);
  583. if (this.alternative) {
  584. push(tw);
  585. this.alternative.walk(tw);
  586. pop(tw);
  587. }
  588. return true;
  589. });
  590. def_reduce_vars(AST_LabeledStatement, function(tw) {
  591. push(tw);
  592. this.body.walk(tw);
  593. pop(tw);
  594. return true;
  595. });
  596. def_reduce_vars(AST_SymbolCatch, function() {
  597. this.definition().fixed = false;
  598. });
  599. def_reduce_vars(AST_SymbolRef, function(tw, descend, compressor) {
  600. var d = this.definition();
  601. d.references.push(this);
  602. if (d.references.length == 1
  603. && !d.fixed
  604. && d.orig[0] instanceof AST_SymbolDefun) {
  605. tw.loop_ids.set(d.id, tw.in_loop);
  606. }
  607. var fixed_value;
  608. if (d.fixed === undefined || !safe_to_read(tw, d)) {
  609. d.fixed = false;
  610. } else if (d.fixed) {
  611. fixed_value = this.fixed_value();
  612. if (
  613. fixed_value instanceof AST_Lambda
  614. && is_recursive_ref(tw, d)
  615. ) {
  616. d.recursive_refs++;
  617. } else if (fixed_value
  618. && !compressor.exposed(d)
  619. && ref_once(tw, compressor, d)
  620. ) {
  621. d.single_use =
  622. fixed_value instanceof AST_Lambda && !fixed_value.pinned()
  623. || fixed_value instanceof AST_Class
  624. || d.scope === this.scope && fixed_value.is_constant_expression();
  625. } else {
  626. d.single_use = false;
  627. }
  628. if (is_modified(compressor, tw, this, fixed_value, 0, is_immutable(fixed_value))) {
  629. if (d.single_use) {
  630. d.single_use = "m";
  631. } else {
  632. d.fixed = false;
  633. }
  634. }
  635. }
  636. mark_escaped(tw, d, this.scope, this, fixed_value, 0, 1);
  637. });
  638. def_reduce_vars(AST_Toplevel, function(tw, descend, compressor) {
  639. this.globals.forEach(function(def) {
  640. reset_def(compressor, def);
  641. });
  642. reset_variables(tw, compressor, this);
  643. descend();
  644. handle_defined_after_hoist(this);
  645. return true;
  646. });
  647. def_reduce_vars(AST_Try, function(tw, descend, compressor) {
  648. reset_block_variables(compressor, this);
  649. push(tw);
  650. this.body.walk(tw);
  651. pop(tw);
  652. if (this.bcatch) {
  653. push(tw);
  654. this.bcatch.walk(tw);
  655. pop(tw);
  656. }
  657. if (this.bfinally) this.bfinally.walk(tw);
  658. return true;
  659. });
  660. def_reduce_vars(AST_Unary, function(tw) {
  661. var node = this;
  662. if (node.operator !== "++" && node.operator !== "--") return;
  663. var exp = node.expression;
  664. if (!(exp instanceof AST_SymbolRef)) return;
  665. var def = exp.definition();
  666. var safe = safe_to_assign(tw, def, exp.scope, true);
  667. def.assignments++;
  668. if (!safe) return;
  669. var fixed = def.fixed;
  670. if (!fixed) return;
  671. def.references.push(exp);
  672. def.chained = true;
  673. def.fixed = function() {
  674. return make_node(AST_Binary, node, {
  675. operator: node.operator.slice(0, -1),
  676. left: make_node(AST_UnaryPrefix, node, {
  677. operator: "+",
  678. expression: fixed instanceof AST_Node ? fixed : fixed()
  679. }),
  680. right: make_node(AST_Number, node, {
  681. value: 1
  682. })
  683. });
  684. };
  685. mark(tw, def, true);
  686. return true;
  687. });
  688. def_reduce_vars(AST_VarDef, function(tw, descend) {
  689. var node = this;
  690. if (node.name instanceof AST_Destructuring) {
  691. suppress(node.name);
  692. return;
  693. }
  694. var d = node.name.definition();
  695. if (node.value) {
  696. if (safe_to_assign(tw, d, node.name.scope, node.value)) {
  697. d.fixed = function() {
  698. return node.value;
  699. };
  700. tw.loop_ids.set(d.id, tw.in_loop);
  701. mark(tw, d, false);
  702. descend();
  703. mark(tw, d, true);
  704. return true;
  705. } else {
  706. d.fixed = false;
  707. }
  708. }
  709. });
  710. def_reduce_vars(AST_While, function(tw, descend, compressor) {
  711. reset_block_variables(compressor, this);
  712. const saved_loop = tw.in_loop;
  713. tw.in_loop = this;
  714. push(tw);
  715. descend();
  716. pop(tw);
  717. tw.in_loop = saved_loop;
  718. return true;
  719. });