/* * Copyright 2008 The Closure Compiler Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.javascript.jscomp; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; import com.google.javascript.rhino.IR; import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Token; import java.util.List; import java.util.Map; import java.util.Set; /** *
Replaces references to aliasable keyword literals (true, false, * null) with variables and statements (throw) with functions declared in the * global scope. When combined with RenameVars, this pass helps to reduce the * number of bytes taken up by references to these keywords by replacing them * with references to variables and functions with shorter names.
* */ class AliasKeywords implements CompilerPass { /** Callback that finds the nodes that we will alias. */ private class FindAliasableNodes extends AbstractPostOrderCallback { @Override public void visit(NodeTraversal t, Node n, Node parent) { final int type = n.getType(); if (isAliasableType(type)) { visitAliasableNode(n, parent); } else if (type == Token.NAME) { visitNameNode(n); } } /** * Find the AliasSpecification associated with the node, and tell * that AliasSpecification about the new node. */ private void visitAliasableNode(Node n, Node parent) { AliasSpecification aliasableNodes = aliasTypes.get(n.getType()); aliasableNodes.visit(n, parent); } /** * Sanity check that our aliases are not already defined by * someone else. */ private void visitNameNode(Node n) { if (isAliasDefinition(n)) { throw new IllegalStateException( "Existing alias definition for " + Token.name(n.getType())); } } } /** * An AliasSpecification encapsulates all of the logic to find * aliasable nodes and alias those nodes, for a given alias name. Subclasses * fill in template methods, allowing for various kinds of aliasing. */ private abstract static class AliasSpecification { /** List of nodes to alias (e.g. all 'null' nodes). */ private final Mapfunction alias(p){throw p;}.
*/
protected abstract void insertAliasDeclaration(Node codeRoot);
/** Replaces the node n with its alias. */
protected abstract void aliasNode(Node n, Node parent);
}
/** Aliases throw statements with a function call. */
// TODO(user): Generalize this to work with typeof expressions.
private class ThrowAliasSpecification extends AliasSpecification {
ThrowAliasSpecification(String aliasName) {
super(aliasName, Token.THROW);
}
@Override
protected void aliasNode(Node throwNode, Node parent) {
Node name = NodeUtil.newName(
compiler.getCodingConvention(),
getAliasName(), throwNode, getAliasName());
Node aliasCall = IR.call( name, throwNode.removeFirstChild());
aliasCall.putBooleanProp(Node.FREE_CALL, true);
Node exprResult = IR.exprResult(aliasCall);
parent.replaceChild(throwNode, exprResult);
}
@Override
/**
* Adds alias function to codeRoot. See {@link #createAliasFunctionNode}).
*/
protected void insertAliasDeclaration(Node codeRoot) {
codeRoot.addChildToFront(createAliasFunctionNode(getAliasName()));
}
@Override
protected int minOccurrencesRequiredToAlias() {
return MIN_OCCURRENCES_REQUIRED_TO_ALIAS_THROW;
}
}
/**
* Calculates the minimum number of occurrences of throw needed to alias
* throw.
*/
static int estimateMinOccurrencesRequriedToAlias() {
// Assuming that the alias function name is two bytes in length, two bytes
// will be saved per occurrence of throw:
// throw e;, compared to
// TT(e);.
// However, the alias definition is some length, N, e.g.,
// function TT(t){throw t;}
// Hence there must be more than N/2 occurrences of throw to reduce
// the code size.
Node alias = createAliasFunctionNode("TT");
return InlineCostEstimator.getCost(alias) / 2 + 1;
}
/**
* Creates a function node that takes a single argument, the object to
* throw. The function throws the object.
*/
private static Node createAliasFunctionNode(String aliasName) {
final String PARAM_NAME = "jscomp_throw_param";
return IR.function(
IR.name(aliasName),
IR.paramList(IR.name(PARAM_NAME)),
IR.block(
IR.throwNode(IR.name(PARAM_NAME))));
}
/** Aliases literal keywords (e.g., null) with variable names. */
private class KeywordAliasSpecification extends AliasSpecification {
KeywordAliasSpecification(String aliasName, int tokenId) {
super(aliasName, tokenId);
}
@Override
protected int minOccurrencesRequiredToAlias() {
return MIN_OCCURRENCES_REQUIRED_TO_ALIAS_LITERAL;
}
@Override
protected void aliasNode(Node n, Node parent) {
Node aliasNode = NodeUtil.newName(
compiler.getCodingConvention(), getAliasName(), n, getAliasName());
parent.replaceChild(n, aliasNode);
}
@Override
/**
* Create the alias declaration (e.g. var $$ALIAS_NULL=null;).
*/
protected void insertAliasDeclaration(Node codeRoot) {
Node varNode = new Node(Token.VAR);
Node value = new Node(getTokenId());
Node name = NodeUtil.newName(
compiler.getCodingConvention(), getAliasName(),
varNode, getAliasName());
name.addChildToBack(value);
varNode.addChildToBack(name);
codeRoot.addChildrenToFront(varNode);
}
}
/** Aliases literal keywords (e.g., null) with variable names. */
private class VoidKeywordAliasSpecification extends AliasSpecification {
VoidKeywordAliasSpecification(String aliasName, int tokenId) {
super(aliasName, tokenId);
}
@Override
public void visit(Node n, Node parent) {
Node value = n.getFirstChild();
if (value.isNumber() && value.getDouble() == 0) {
super.visit(n, parent);
}
}
@Override
protected int minOccurrencesRequiredToAlias() {
return MIN_OCCURRENCES_REQUIRED_TO_ALIAS_LITERAL;
}
@Override
protected void aliasNode(Node n, Node parent) {
Node aliasNode = NodeUtil.newName(
compiler.getCodingConvention(), getAliasName(), n, getAliasName());
parent.replaceChild(n, aliasNode);
}
@Override
/**
* Create the alias declaration (e.g. var $$ALIAS_VOID=void 0;).
*/
protected void insertAliasDeclaration(Node codeRoot) {
Node varNode = new Node(Token.VAR);
Node value = IR.voidNode(IR.number(0));
Node name = NodeUtil.newName(
compiler.getCodingConvention(), getAliasName(),
varNode, getAliasName());
name.addChildToBack(value);
varNode.addChildToBack(name);
codeRoot.addChildrenToFront(varNode);
}
}
static final String ALIAS_NULL = "JSCompiler_alias_NULL";
static final String ALIAS_TRUE = "JSCompiler_alias_TRUE";
static final String ALIAS_FALSE = "JSCompiler_alias_FALSE";
static final String ALIAS_THROW = "JSCompiler_alias_THROW";
static final String ALIAS_VOID = "JSCompiler_alias_VOID";
/**
* Don't alias a keyword unless it's referenced at least
* MIN_OCCURRENCES_REQUIRED_TO_ALIAS_LITERAL times. Aliasing a keyword has a
* cost (e.g. 'var XX=true;' costs 12 bytes). We make up for this
* cost by replacing references to the keyword with variables that
* have shorter names. If there are only a few references to a
* keyword, the cost outweighs the benefit. It is not possible to
* determine the exact break-even point without compiling twice
* (once with aliasing, another without) and comparing the
* post-gzipped size, so we define a minimum number of references
* required in order to alias. We choose 6 because the alias cost is
* ~7-12 bytes (12 bytes for 'var XX=true;', 7 bytes for a
* subsequent declaration that does not require its own 'var ' or
* semicolon, e.g. var XX=true,XY=null;), but each reference saves
* 2-3 bytes (2 for true and null, 3 for false). Thus, the break
* even point is 3 at best, and 6 at worst. We could use a
* CostEstimator to be precise, but requiring a constant number of
* occurrences is much simpler, and the added precision of a
* CostEstimator would save us <10 bytes for some unlikely edge
* cases (e.g. where false is referenced exactly 5 times, but does
* not get aliased).
*/
static final int MIN_OCCURRENCES_REQUIRED_TO_ALIAS_LITERAL = 6;
/**
* Don't alias throw statements unless throw is used at least
* MIN_OCCURRENCES_REQUIRED_TO_ALIAS_THROW times.
*/
static final int MIN_OCCURRENCES_REQUIRED_TO_ALIAS_THROW =
estimateMinOccurrencesRequriedToAlias();
/** Reference to JS Compiler */
private final AbstractCompiler compiler;
/** List of alias specifications, stored in order which transformations
* should be applied. See {@link #createAliasSpecifications}.
*/
private final List