/* * Copyright 2009 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.base.Preconditions; import com.google.common.collect.HashMultimap; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Multimap; import com.google.javascript.jscomp.DefinitionsRemover.AssignmentDefinition; import com.google.javascript.jscomp.DefinitionsRemover.Definition; import com.google.javascript.jscomp.DefinitionsRemover.NamedFunctionDefinition; import com.google.javascript.jscomp.graph.GraphNode; import com.google.javascript.jscomp.graph.LinkedDirectedGraph; import com.google.javascript.rhino.Node; import com.google.javascript.rhino.jstype.JSType; import com.google.javascript.rhino.jstype.JSTypeNative; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; /** * A graph represents all the referencing of global names in the program. In * other words, it is a call and variable-name graph. * *
The NameReferenceGraph G for a program P is a directed graph G = (V, E) * where: * *
V ({@link Name}) represents all global names in P and E = (v, v'), v and * v' in V ({@link Reference} represents a reference use or definition from the * name v to v' in P. * *
There are two core results we are trying to compute. The first being able * to precisely identify the function body at any given call site with * {@link #getReferencesAt(Node)}. * *
The second result come directly from the previous one. The directed edge
* provides us with dependency information. If A->B, B might be needed (in this
* module) if A is needed (in this module). The converse of the this result is
* more useful. B is not needed if A is not needed.
*
*/
class NameReferenceGraph extends
LinkedDirectedGraph
*
*
* @return true if it's safe to change the signature of the name.
*/
public boolean canChangeSignature() {
// Ignore anything that is extern as they should not be changed.
// Also skip over any non-function names. Finally if a function has been
// alias, we don't know all of its callers and should not optimize.
//
// Also, if the function is called using .call or .apply, we don't try to
// optimize those call because the name graph does not give us enough
// information on the parameters.
// TODO(user) We'll be able to remove the check for call or apply once
// the name graph handles those call. The issue for now is that those
// calls aren't edges in the graph, so we don't have enough information to
// know if it's safe to change the method's signature.
return !(isExtern() ||
!isCallable() ||
isAliased() ||
isExported() ||
exposedToCallOrApply() ||
nameUsesArgumentsProperty());
}
/**
* Returns true if the the arguments property is used in any of the function
* definition.
* Ex. function foo(a,b,c) {return arguments.size;};
* @return True is arguments is present in one of the definitions.
*/
private boolean nameUsesArgumentsProperty() {
for (Definition definition : getDeclarations()) {
if (NodeUtil.isVarArgsFunction(definition.getRValue())) {
return true;
}
}
return false;
}
}
/**
* A reference site for a function or a variable reference. It can be a
* reference use or an assignment to that name.
*/
static class Reference {
// The node that references the name.
public final Node site;
// Parent pointer.
public final Node parent;
private JSModule module = null;
// A reference is unknown because we don't know the object's type.
// If A.x->B.y in the name graph and the edge is unknown. It implies
// A.x() reference to someObject.y and B.y MAY be the site.
private boolean isUnknown = false;
public Reference(Node site, Node parent) {
this.site = site;
this.parent = parent;
}
public boolean isUnknown() {
return isUnknown;
}
public void setUnknown(boolean isUnknown) {
this.isUnknown = isUnknown;
}
public JSModule getModule() {
return module;
}
public void setModule(JSModule module) {
this.module = module;
}
boolean isCall() {
return site.isCall();
}
/**
* Get accessor for retrieving the actual node corresponding to the
* reference.
*
* @return node representing the access/reference/call site
*/
public Node getSite() {
return site;
}
}
}