class DisambiguateProperties extends java.lang.Object implements CompilerPass
Renamimg only occurs if there are two or more distinct properties with the same name.
This pass allows other passes, such as inlining and code removal to take advantage of type information implicitly.
Foo.a; Bar.a;
will become
Foo.a$Foo; Bar.a$Bar;NOTE(dimvar): For every property, this pass groups together the types that can't be disambiguated. If a type inherits from another type, their common properties can never be disambiguated, yet we have to compute this info once per property rather than just once in the pass. This is where the bulk of the time is spent. We have added many caches that help a lot, but it is probably worth it to revisit this pass and rewrite it in a way that does not compute the same thing over and over.
Modifier and Type | Class and Description |
---|---|
private class |
DisambiguateProperties.FindExternProperties
Finds all properties defined in the externs file and sets them as
ineligible for renaming from the type on which they are defined.
|
private class |
DisambiguateProperties.FindRenameableProperties
Traverses the tree, building a map from field names to Nodes for all
fields that can be renamed.
|
private class |
DisambiguateProperties.Property |
(package private) static class |
DisambiguateProperties.Warnings |
Modifier and Type | Field and Description |
---|---|
private ObjectType |
BOTTOM_OBJECT |
private AbstractCompiler |
compiler |
private java.util.Map<java.lang.String,java.util.IdentityHashMap<JSType,ObjectType>> |
gtwpCache |
private java.util.Map<FunctionType,java.lang.Iterable<ObjectType>> |
implementedInterfaces |
private java.util.Set<JSType> |
invalidatingTypes |
private com.google.common.collect.Multimap<java.lang.Object,JSError> |
invalidationMap
Map of a type to all the related errors that invalidated the type
for disambiguation.
|
private static java.util.logging.Logger |
logger |
private static int |
MAX_INVALIDATION_WARNINGS_PER_PROPERTY |
private static Pattern |
NONWORD_PATTERN |
private java.util.Map<java.lang.String,DisambiguateProperties.Property> |
properties |
private java.util.Map<java.lang.String,CheckLevel> |
propertiesToErrorFor
In practice any large code base will have thousands and thousands of
type invalidations, which makes reporting all of the errors useless.
|
private JSTypeRegistry |
registry |
Constructor and Description |
---|
DisambiguateProperties(AbstractCompiler compiler,
java.util.Map<java.lang.String,CheckLevel> propertiesToErrorFor) |
Modifier and Type | Method and Description |
---|---|
private void |
addInvalidatingType(JSType type) |
private java.util.Map<JSType,java.lang.String> |
buildPropNames(DisambiguateProperties.Property prop)
Chooses a name to use for renaming in each equivalence class and maps
the representative type of that class to that name.
|
private FunctionType |
getConstructor(JSType type) |
private JSType |
getInstanceFromPrototype(JSType type) |
protected DisambiguateProperties.Property |
getProperty(java.lang.String name)
Returns the property for the given name, creating it if necessary.
|
(package private) com.google.common.collect.Multimap<java.lang.String,java.util.Collection<JSType>> |
getRenamedTypesForTesting()
Returns a map from field name to types for which it will be renamed.
|
private JSType |
getType(Node node) |
private java.lang.Iterable<JSType> |
getTypeAlternatives(JSType type)
Returns the alternatives if this is a type that represents multiple
types, and null if not.
|
private com.google.common.collect.ImmutableSet<JSType> |
getTypesToSkipForType(JSType type)
Returns a set of types that should be skipped given the given type.
|
private java.util.Set<JSType> |
getTypesToSkipForTypeNonUnion(JSType type) |
(package private) ObjectType |
getTypeWithProperty(java.lang.String field,
JSType type)
Returns the type in the chain from the given type that contains the given
field or null if it is not found anywhere.
|
private ObjectType |
gtwpCacheGet(java.lang.String field,
JSType type) |
private void |
gtwpCachePut(java.lang.String field,
JSType type,
ObjectType top) |
private boolean |
isInvalidatingType(JSType type)
Returns true if a field reference on this type will invalidate all
references to that field as candidates for renaming.
|
private boolean |
isTypeToSkip(JSType type)
Determines whether the given type is one whose properties should not be
considered for renaming.
|
void |
process(Node externs,
Node root)
Process the JS with root node root.
|
private void |
recordInterfaces(FunctionType constructor,
JSType relatedType,
DisambiguateProperties.Property p)
Records that this property could be referenced from any interface that
this type, or any type in its superclass chain, implements.
|
private void |
recordInvalidatingType(JSType type,
JSError error)
Invalidates the given type, so that no properties on it will be renamed.
|
private void |
recordInvalidationError(JSType t,
JSError error) |
(package private) void |
renameProperties()
Renames all properties with references on more than one type.
|
private static final int MAX_INVALIDATION_WARNINGS_PER_PROPERTY
private static final java.util.logging.Logger logger
private static final Pattern NONWORD_PATTERN
private final AbstractCompiler compiler
private final java.util.Set<JSType> invalidatingTypes
private final JSTypeRegistry registry
private final ObjectType BOTTOM_OBJECT
private com.google.common.collect.Multimap<java.lang.Object,JSError> invalidationMap
private final java.util.Map<java.lang.String,CheckLevel> propertiesToErrorFor
private java.util.Map<FunctionType,java.lang.Iterable<ObjectType>> implementedInterfaces
private java.util.Map<java.lang.String,java.util.IdentityHashMap<JSType,ObjectType>> gtwpCache
private java.util.Map<java.lang.String,DisambiguateProperties.Property> properties
DisambiguateProperties(AbstractCompiler compiler, java.util.Map<java.lang.String,CheckLevel> propertiesToErrorFor)
private ObjectType gtwpCacheGet(java.lang.String field, JSType type)
private void gtwpCachePut(java.lang.String field, JSType type, ObjectType top)
public void process(Node externs, Node root)
CompilerPass
process
in interface CompilerPass
externs
- Top of external JS treeroot
- Top of JS treeprivate void recordInvalidatingType(JSType type, JSError error)
protected DisambiguateProperties.Property getProperty(java.lang.String name)
void renameProperties()
private java.util.Map<JSType,java.lang.String> buildPropNames(DisambiguateProperties.Property prop)
com.google.common.collect.Multimap<java.lang.String,java.util.Collection<JSType>> getRenamedTypesForTesting()
private void addInvalidatingType(JSType type)
private boolean isInvalidatingType(JSType type)
private com.google.common.collect.ImmutableSet<JSType> getTypesToSkipForType(JSType type)
private java.util.Set<JSType> getTypesToSkipForTypeNonUnion(JSType type)
private boolean isTypeToSkip(JSType type)
private java.lang.Iterable<JSType> getTypeAlternatives(JSType type)
ObjectType getTypeWithProperty(java.lang.String field, JSType type)
private void recordInterfaces(FunctionType constructor, JSType relatedType, DisambiguateProperties.Property p)
private FunctionType getConstructor(JSType type)