Skip to content

Commit

Permalink
Cleanups related to CheckCasts.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=162538489
  • Loading branch information
dimvar authored and blickly committed Jul 20, 2017
1 parent 9218de8 commit 89e476c
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 46 deletions.
10 changes: 10 additions & 0 deletions src/com/google/javascript/jscomp/GlobalTypeInfo.java
Expand Up @@ -256,6 +256,16 @@ public JSType getType(String typeName) {
}
}

@Override
public String createGetterPropName(String originalPropName) {
return this.commonTypes.createGetterPropName(originalPropName);
}

@Override
public String createSetterPropName(String originalPropName) {
return this.commonTypes.createSetterPropName(originalPropName);
}

@Override
public TypeI createUnionType(List<? extends TypeI> members) {
checkArgument(!members.isEmpty(), "Cannot create union type with no members");
Expand Down
12 changes: 6 additions & 6 deletions src/com/google/javascript/jscomp/GlobalTypeInfoCollector.java
Expand Up @@ -528,7 +528,7 @@ private void checkAndFreezeNominalType(RawNominalType rawType) {
if (superClass != null) {
checkState(superClass.isFrozen());
// TODO(blickly): Can we optimize this to skip unnecessary iterations?
for (String pname : superClass.getAllPropsOfClass()) {
for (String pname : superClass.getPropertyNames()) {
if (superClass.isAbstractClass()
&& superClass.hasAbstractMethod(pname)
&& !rawType.isAbstractClass()
Expand All @@ -546,7 +546,7 @@ private void checkAndFreezeNominalType(RawNominalType rawType) {
// Collect inherited types for extended/implemented interfaces
for (NominalType superInterf : rawType.getInterfaces()) {
checkState(superInterf.isFrozen());
for (String pname : superInterf.getAllPropsOfInterface()) {
for (String pname : superInterf.getPropertyNames()) {
nonInheritedPropNames.remove(pname);
checkSuperProperty(rawType, superInterf, pname,
propMethodTypesToProcess, propTypesToProcess);
Expand Down Expand Up @@ -1818,9 +1818,9 @@ private void visitNamespacePropertyDeclaration(Node declNode, Node recv, String
declNode);
checkArgument(currentScope.isNamespace(recv));
if (declNode.isGetterDef()) {
pname = JSType.createGetterPropName(pname);
pname = getCommonTypes().createGetterPropName(pname);
} else if (declNode.isSetterDef()) {
pname = JSType.createSetterPropName(pname);
pname = getCommonTypes().createSetterPropName(pname);
}
// Named types have already been crawled in CollectNamedTypes
if (declNode.isStringKey() && currentScope.isNamespace(declNode.getFirstChild())) {
Expand Down Expand Up @@ -2518,9 +2518,9 @@ private void mayAddPropToPrototype(
methodScope = visitFunctionLate(initializer, rawType);
methodType = methodScope.getDeclaredFunctionType();
if (defSite.isGetterDef()) {
pname = JSType.createGetterPropName(pname);
pname = getCommonTypes().createGetterPropName(pname);
} else if (defSite.isSetterDef()) {
pname = JSType.createSetterPropName(pname);
pname = getCommonTypes().createSetterPropName(pname);
}
} else if (jsdoc != null && jsdoc.containsFunctionDeclaration()
// This can happen with stray jsdocs in an object literal
Expand Down
8 changes: 4 additions & 4 deletions src/com/google/javascript/jscomp/NewTypeInference.java
Expand Up @@ -2787,10 +2787,10 @@ private EnvTypePair analyzeObjLitFwd(
String specialPropName;
JSType propType;
if (prop.isGetterDef()) {
specialPropName = JSType.createGetterPropName(pname);
specialPropName = commonTypes.createGetterPropName(pname);
propType = funType.getReturnType();
} else {
specialPropName = JSType.createSetterPropName(pname);
specialPropName = commonTypes.createSetterPropName(pname);
propType = pair.type;
}
result = result.withProperty(new QualifiedName(specialPropName), propType);
Expand Down Expand Up @@ -3345,7 +3345,7 @@ private EnvTypePair analyzePropAccessFwd(Node receiver, String pname,
propAccessNode.getParent(), CONST_PROPERTY_DELETED, pname));
}
// Then, analyze the property access.
QualifiedName getterPname = new QualifiedName(JSType.createGetterPropName(pname));
QualifiedName getterPname = new QualifiedName(commonTypes.createGetterPropName(pname));
if (recvType.hasProp(getterPname)) {
return new EnvTypePair(pair.env, recvType.getProp(getterPname));
}
Expand Down Expand Up @@ -4402,7 +4402,7 @@ && mayWarnAboutConstProp(propAccessNode, recvType, pname)) {
mayWarnAboutDictPropAccess(obj, recvType);
}
QualifiedName setterPname =
new QualifiedName(JSType.createSetterPropName(pname.getLeftmostName()));
new QualifiedName(commonTypes.createSetterPropName(pname.getLeftmostName()));
if (recvType.hasProp(setterPname)) {
FunctionType funType = recvType.getProp(setterPname).getFunType();
checkNotNull(funType);
Expand Down
8 changes: 0 additions & 8 deletions src/com/google/javascript/jscomp/newtypes/JSType.java
Expand Up @@ -1359,14 +1359,6 @@ public final JSType withFunction(FunctionType ft, NominalType fnNominal) {
getObjTypeIfSingletonObj().withFunction(ft, fnNominal));
}

public static String createGetterPropName(String originalPropName) {
return "%getter_fun" + originalPropName;
}

public static String createSetterPropName(String originalPropName) {
return "%setter_fun" + originalPropName;
}

public final boolean isSingletonObj() {
return getMask() == NON_SCALAR_MASK && getObjs().size() == 1;
}
Expand Down
8 changes: 8 additions & 0 deletions src/com/google/javascript/jscomp/newtypes/JSTypes.java
Expand Up @@ -601,4 +601,12 @@ public boolean isNumStrScalarOrObj(JSType t) {
boolean isBottomPropertyMap(PersistentMap<String, Property> map) {
return map == BOTTOM_PROPERTY_MAP;
}

public String createGetterPropName(String originalPropName) {
return "%getter_fun" + originalPropName;
}

public String createSetterPropName(String originalPropName) {
return "%setter_fun" + originalPropName;
}
}
17 changes: 2 additions & 15 deletions src/com/google/javascript/jscomp/newtypes/NominalType.java
Expand Up @@ -336,21 +336,8 @@ boolean hasAncestorInterface(RawNominalType ancestor) {
return this.rawType.hasAncestorInterface(ancestor);
}

ImmutableSet<String> getPropertyNames() {
if (isClass()) {
return getAllPropsOfClass();
} else {
return getAllPropsOfInterface();
}
}

// TODO(dimvar): consolidate the next two methods into one
public ImmutableSet<String> getAllPropsOfInterface() {
return this.rawType.getAllPropsOfInterface();
}

public ImmutableSet<String> getAllPropsOfClass() {
return this.rawType.getAllPropsOfClass();
public ImmutableSet<String> getPropertyNames() {
return this.rawType.getPropertyNames();
}

public Set<String> getAllOwnClassProps() {
Expand Down
12 changes: 6 additions & 6 deletions src/com/google/javascript/jscomp/newtypes/ObjectType.java
Expand Up @@ -710,7 +710,7 @@ private boolean isSubtypeOfHelper(boolean keepLoosenessOfThis,
if (checkOnlyLocalProps) {
otherPropNames = other.props.keySet();
} else {
otherPropNames = otherNt.getAllPropsOfInterface();
otherPropNames = otherNt.getPropertyNames();
if (otherPropNames == null) {
// Can't check structural interfaces for subtyping during GlobalTypeInfo
return false;
Expand Down Expand Up @@ -1367,10 +1367,10 @@ private Node getPropertyDefSiteHelper(String propertyName, boolean ownProp) {
Property p = ownProp ? getLeftmostOwnProp(qname) : getLeftmostProp(qname);
// Try getters and setters specially.
if (p == null) {
p = getLeftmostProp(new QualifiedName(JSType.createGetterPropName(propertyName)));
p = getLeftmostProp(new QualifiedName(this.commonTypes.createGetterPropName(propertyName)));
}
if (p == null) {
p = getLeftmostProp(new QualifiedName(JSType.createSetterPropName(propertyName)));
p = getLeftmostProp(new QualifiedName(this.commonTypes.createSetterPropName(propertyName)));
}
return p == null ? null : p.getDefSite();
}
Expand Down Expand Up @@ -1400,10 +1400,10 @@ boolean hasOwnProperty(QualifiedName qname) {
String pname = qname.getLeftmostName();
// Try getters and setters specially.
if (p == null) {
p = getLeftmostOwnProp(new QualifiedName(JSType.createGetterPropName(pname)));
p = getLeftmostOwnProp(new QualifiedName(this.commonTypes.createGetterPropName(pname)));
}
if (p == null) {
p = getLeftmostProp(new QualifiedName(JSType.createSetterPropName(pname)));
p = getLeftmostProp(new QualifiedName(this.commonTypes.createSetterPropName(pname)));
}
return p != null;
}
Expand Down Expand Up @@ -1506,7 +1506,7 @@ boolean unifyWithSubtype(ObjectType other, List<String> typeParameters,
return false;
}
Set<String> thisProps = !thisNt.isBuiltinObject() && thisNt.isStructuralInterface()
? thisNt.getAllPropsOfInterface() : this.props.keySet();
? thisNt.getPropertyNames() : this.props.keySet();
if (thisProps == null) {// Can happen during GTI when types aren't frozen yet.
return true;
}
Expand Down
25 changes: 20 additions & 5 deletions src/com/google/javascript/jscomp/newtypes/RawNominalType.java
Expand Up @@ -493,36 +493,51 @@ public Set<String> getAllOwnClassProps() {
return classProps.keySet();
}

ImmutableSet<String> getAllPropsOfInterface() {
/**
* Returns a set of properties defined or inferred on this type or any of its supertypes.
*/
ImmutableSet<String> getPropertyNames() {
return isClass() ? getAllPropsOfClass() : getAllPropsOfInterface();
}

/**
* Return all property names of this interface, including inherited properties.
*/
private ImmutableSet<String> getAllPropsOfInterface() {
if (!this.isFrozen) {
// During GlobalTypeInfo, we sometimes try to check subtyping between
// structural interfaces, but it's not possible because we may have not
// seen all their properties yet.
return null;
}
if (isClass()) {
checkState(this.name.equals("Object"));
checkState(this.name.equals("Object"), this.name);
return getAllPropsOfClass();
}
if (this.allProps == null) {
ImmutableSet.Builder<String> builder = ImmutableSet.builder();
if (interfaces != null) {
for (NominalType interf : interfaces) {
builder.addAll(interf.getAllPropsOfInterface());
builder.addAll(interf.getPropertyNames());
}
}
this.allProps = builder.addAll(protoProps.keySet()).build();
}
return this.allProps;
}

ImmutableSet<String> getAllPropsOfClass() {
/**
* Return all property names of this class, including inherited properties.
* We don't look at ancestor interfaces because interface properties also appear as
* prototype properties of classes.
*/
private ImmutableSet<String> getAllPropsOfClass() {
checkState(isClass());
checkState(this.isFrozen);
if (this.allProps == null) {
ImmutableSet.Builder<String> builder = ImmutableSet.builder();
if (this.superclass != null) {
builder.addAll(this.superclass.getAllPropsOfClass());
builder.addAll(this.superclass.getPropertyNames());
}
this.allProps = builder.addAll(classProps.keySet()).addAll(protoProps.keySet()).build();
}
Expand Down
3 changes: 1 addition & 2 deletions src/com/google/javascript/rhino/ObjectTypeI.java
Expand Up @@ -164,8 +164,7 @@ public interface ObjectTypeI extends TypeI {
TypeI getEnumeratedTypeOfEnumObject();

/**
* Returns a set of properties defined or inferred on this type or any of
* its supertypes.
* Returns a set of properties defined or inferred on this type or any of its supertypes.
*/
Set<String> getPropertyNames();
}
16 changes: 16 additions & 0 deletions src/com/google/javascript/rhino/TypeIRegistry.java
Expand Up @@ -69,6 +69,22 @@ public interface TypeIRegistry extends Serializable {

String getReadableTypeName(Node n);

/**
* For NTI, this method returns an obfuscated name that represents the getter.
* For OTI, the property name is unchanged.
*/
String createGetterPropName(String originalPropName);

/**
* For NTI, this method returns an obfuscated name that represents the setter.
* For OTI, the property name is unchanged.
*
* This method is a bit of a hack. In the future, NTI can handle getters/setters more
* gracefully by having a field in the Property class.
* See also: https://github.com/google/closure-compiler/issues/2545.
*/
String createSetterPropName(String originalPropName);

<T extends TypeI> T getType(String typeName);

TypeI createUnionType(List<? extends TypeI> variants);
Expand Down
10 changes: 10 additions & 0 deletions src/com/google/javascript/rhino/jstype/JSTypeRegistry.java
Expand Up @@ -1011,6 +1011,16 @@ public String getReadableTypeNameNoDeref(Node n) {
return getReadableJSTypeName(n, false);
}

@Override
public String createGetterPropName(String originalPropName) {
return originalPropName;
}

@Override
public String createSetterPropName(String originalPropName) {
return originalPropName;
}

/**
* Given a node, get a human-readable name for the type of that node so
* that will be easy for the programmer to find the original declaration.
Expand Down

0 comments on commit 89e476c

Please sign in to comment.