class FunctionInjector
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
private static class |
FunctionInjector.CallSiteType
Supported call site types.
|
(package private) static class |
FunctionInjector.CanInlineResult |
(package private) static class |
FunctionInjector.InliningMode
The type of inlining to perform.
|
(package private) static class |
FunctionInjector.Reference
Holds a reference to the call node of a function call
|
Modifier and Type | Field and Description |
---|---|
private boolean |
allowDecomposition |
private boolean |
assumeMinimumCapture |
private boolean |
assumeStrictThis |
private static int |
COMMA_COST
The cost of a argument separator (a comma).
|
private AbstractCompiler |
compiler |
private java.util.Set<java.lang.String> |
knownConstants |
private static int |
NAME_COST_ESTIMATE
In order to estimate the cost of lining, we make the assumption that
Identifiers are reduced 2 characters.
|
private static int |
PAREN_COST
The cost of the parentheses needed to make a call.
|
private com.google.common.base.Supplier<java.lang.String> |
safeNameIdSupplier |
private com.google.common.base.Supplier<java.lang.String> |
throwawayNameSupplier |
Constructor and Description |
---|
FunctionInjector(AbstractCompiler compiler,
com.google.common.base.Supplier<java.lang.String> safeNameIdSupplier,
boolean allowDecomposition,
boolean assumeStrictThis,
boolean assumeMinimumCapture) |
Modifier and Type | Method and Description |
---|---|
private boolean |
callMeetsBlockInliningRequirements(FunctionInjector.Reference ref,
Node fnNode,
java.util.Set<java.lang.String> namesToAlias)
Determines whether a function can be inlined at a particular call site.
|
private FunctionInjector.CanInlineResult |
canInlineReferenceAsStatementBlock(FunctionInjector.Reference ref,
Node fnNode,
java.util.Set<java.lang.String> namesToAlias)
Determines whether a function can be inlined at a particular call site.
|
private FunctionInjector.CanInlineResult |
canInlineReferenceDirectly(FunctionInjector.Reference ref,
Node fnNode,
java.util.Set<java.lang.String> namesToAlias)
Determines whether a function can be inlined at a particular call site.
|
(package private) FunctionInjector.CanInlineResult |
canInlineReferenceToFunction(FunctionInjector.Reference ref,
Node fnNode,
java.util.Set<java.lang.String> needAliases,
boolean referencesThis,
boolean containsFunctions) |
private FunctionInjector.CallSiteType |
classifyCallSite(FunctionInjector.Reference ref)
Determine which, if any, of the supported types the call site is.
|
(package private) boolean |
doesFunctionMeetMinimumRequirements(java.lang.String fnName,
Node fnNode) |
private static boolean |
doesLowerCost(Node fnNode,
int callCost,
int directInlines,
int costDeltaDirect,
int blockInlines,
int costDeltaBlock,
boolean removable) |
private static int |
estimateCallCost(Node fnNode,
boolean referencesThis)
Gets an estimate of the cost in characters of making the function call:
the sum of the identifiers and the separators.
|
private ExpressionDecomposer |
getDecomposer(Scope scope) |
(package private) Node |
inline(FunctionInjector.Reference ref,
java.lang.String fnName,
Node fnNode)
Inline a function into the call site.
|
private static int |
inlineCostDelta(Node fnNode,
java.util.Set<java.lang.String> namesToAlias,
FunctionInjector.InliningMode mode) |
private Node |
inlineFunction(FunctionInjector.Reference ref,
Node fnNode,
java.lang.String fnName)
Inline a function which fulfills the requirements of
canInlineReferenceAsStatementBlock into the call site, replacing the
parent expression.
|
private Node |
inlineReturnValue(FunctionInjector.Reference ref,
Node fnNode)
Inline a function that fulfills the requirements of
canInlineReferenceDirectly into the call site, replacing only the CALL
node.
|
(package private) boolean |
inliningLowersCost(JSModule fnModule,
Node fnNode,
java.util.Collection<? extends FunctionInjector.Reference> refs,
java.util.Set<java.lang.String> namesToAlias,
boolean isRemovable,
boolean referencesThis)
Determine if inlining the function is likely to reduce the code size.
|
(package private) static boolean |
isDirectCallNodeReplacementPossible(Node fnNode)
Checks if the given function matches the criteria for an inlinable
function, and if so, adds it to our set of inlinable functions.
|
private boolean |
isSupportedCallType(Node callNode)
Only ".call" calls and direct calls to functions are supported.
|
(package private) void |
maybePrepareCall(FunctionInjector.Reference ref)
If required, rewrite the statement containing the call expression.
|
private static void |
removeConstantVarAnnotation(Scope scope,
java.lang.String name) |
void |
setKnownConstants(java.util.Set<java.lang.String> knownConstants)
Store the names of known constants to be used when classifying call-sites
in expressions.
|
private final AbstractCompiler compiler
private final boolean allowDecomposition
private java.util.Set<java.lang.String> knownConstants
private final boolean assumeStrictThis
private final boolean assumeMinimumCapture
private final com.google.common.base.Supplier<java.lang.String> safeNameIdSupplier
private final com.google.common.base.Supplier<java.lang.String> throwawayNameSupplier
private static final int NAME_COST_ESTIMATE
private static final int COMMA_COST
private static final int PAREN_COST
public FunctionInjector(AbstractCompiler compiler, com.google.common.base.Supplier<java.lang.String> safeNameIdSupplier, boolean allowDecomposition, boolean assumeStrictThis, boolean assumeMinimumCapture)
allowDecomposition
- Whether an effort should be made to break down
expressions into simpler expressions to allow functions to be injected
where they would otherwise be disallowed.boolean doesFunctionMeetMinimumRequirements(java.lang.String fnName, Node fnNode)
fnName
- The name of this function. This either the name of the
variable to which the function is assigned or the name from the FUNCTION
node.fnNode
- The FUNCTION node of the function to inspect.FunctionInjector.CanInlineResult canInlineReferenceToFunction(FunctionInjector.Reference ref, Node fnNode, java.util.Set<java.lang.String> needAliases, boolean referencesThis, boolean containsFunctions)
fnNode
- The function to evaluate for inlining.needAliases
- A set of function parameter names that can not be
used without aliasing. Returned by getUnsafeParameterNames().referencesThis
- Whether fnNode contains references to its this
object.containsFunctions
- Whether fnNode contains inner functions.private boolean isSupportedCallType(Node callNode)
callNode
- The call evaluate.Node inline(FunctionInjector.Reference ref, java.lang.String fnName, Node fnNode)
private Node inlineReturnValue(FunctionInjector.Reference ref, Node fnNode)
private FunctionInjector.CallSiteType classifyCallSite(FunctionInjector.Reference ref)
private ExpressionDecomposer getDecomposer(Scope scope)
void maybePrepareCall(FunctionInjector.Reference ref)
private Node inlineFunction(FunctionInjector.Reference ref, Node fnNode, java.lang.String fnName)
private static void removeConstantVarAnnotation(Scope scope, java.lang.String name)
static boolean isDirectCallNodeReplacementPossible(Node fnNode)
private FunctionInjector.CanInlineResult canInlineReferenceAsStatementBlock(FunctionInjector.Reference ref, Node fnNode, java.util.Set<java.lang.String> namesToAlias)
f(); a = foo(); var a = foo();
private boolean callMeetsBlockInliningRequirements(FunctionInjector.Reference ref, Node fnNode, java.util.Set<java.lang.String> namesToAlias)
private FunctionInjector.CanInlineResult canInlineReferenceDirectly(FunctionInjector.Reference ref, Node fnNode, java.util.Set<java.lang.String> namesToAlias)
function foo(a) { return a + a } x = foo(i++);
boolean inliningLowersCost(JSModule fnModule, Node fnNode, java.util.Collection<? extends FunctionInjector.Reference> refs, java.util.Set<java.lang.String> namesToAlias, boolean isRemovable, boolean referencesThis)
namesToAlias
- private static boolean doesLowerCost(Node fnNode, int callCost, int directInlines, int costDeltaDirect, int blockInlines, int costDeltaBlock, boolean removable)
private static int estimateCallCost(Node fnNode, boolean referencesThis)
referencesThis
- private static int inlineCostDelta(Node fnNode, java.util.Set<java.lang.String> namesToAlias, FunctionInjector.InliningMode mode)
public void setKnownConstants(java.util.Set<java.lang.String> knownConstants)