Skip to content

Commit

Permalink
Simplify NominalTypeBuilder to no longer need any Runnables.
Browse files Browse the repository at this point in the history
This is possibly being a little bit looser in NTI, using instance types in place of prototype objects when the latter are unavailable.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=171857502
  • Loading branch information
shicks authored and Tyler Breisacher committed Oct 11, 2017
1 parent f7a9ddb commit 97cf26f
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 290 deletions.
6 changes: 3 additions & 3 deletions src/com/google/javascript/jscomp/ChromeCodingConvention.java
Expand Up @@ -54,9 +54,9 @@ public String getSingletonGetterClassName(Node callNode) {


@Override @Override
public void applySingletonGetter(NominalTypeBuilder classType, FunctionTypeI getterType) { public void applySingletonGetter(NominalTypeBuilder classType, FunctionTypeI getterType) {
Node defSite = ((FunctionTypeI) classType.constructor().toTypeI()).getSource(); Node defSite = classType.constructor().getSource();
classType.constructor().declareProperty("getInstance", getterType, defSite); classType.declareConstructorProperty("getInstance", getterType, defSite);
classType.constructor().declareProperty("instance_", classType.instance().toTypeI(), defSite); classType.declareConstructorProperty("instance_", classType.instance(), defSite);
} }


@Override @Override
Expand Down
19 changes: 7 additions & 12 deletions src/com/google/javascript/jscomp/ClosureCodingConvention.java
Expand Up @@ -76,22 +76,17 @@ public void applySubclassRelationship(
final NominalTypeBuilder parent, final NominalTypeBuilder child, SubclassType type) { final NominalTypeBuilder parent, final NominalTypeBuilder child, SubclassType type) {
super.applySubclassRelationship(parent, child, type); super.applySubclassRelationship(parent, child, type);
if (type == SubclassType.INHERITS) { if (type == SubclassType.INHERITS) {
final FunctionTypeI childCtor = (FunctionTypeI) child.constructor().toTypeI(); final FunctionTypeI childCtor = child.constructor();
child.beforeFreeze(new Runnable() { child.declareConstructorProperty(
@Override "superClass_", parent.prototypeOrInstance(), childCtor.getSource());
public void run() {
child.constructor().declareProperty(
"superClass_", parent.prototype().toTypeI(), childCtor.getSource());
}
}, parent);
// Notice that constructor functions do not need to be covariant on the superclass. // Notice that constructor functions do not need to be covariant on the superclass.
// So if G extends F, new G() and new F() can accept completely different argument // So if G extends F, new G() and new F() can accept completely different argument
// types, but G.prototype.constructor needs to be covariant on F.prototype.constructor. // types, but G.prototype.constructor needs to be covariant on F.prototype.constructor.
// To get around this, we just turn off type-checking on arguments and return types // To get around this, we just turn off type-checking on arguments and return types
// of G.prototype.constructor. // of G.prototype.constructor.
FunctionTypeI qmarkCtor = FunctionTypeI qmarkCtor =
childCtor.toBuilder().withUnknownReturnType().withNoParameters().build(); childCtor.toBuilder().withUnknownReturnType().withNoParameters().build();
child.prototype().declareProperty("constructor", qmarkCtor, childCtor.getSource()); child.declarePrototypeProperty("constructor", qmarkCtor, childCtor.getSource());
} }
} }


Expand Down Expand Up @@ -320,9 +315,9 @@ public String getSingletonGetterClassName(Node callNode) {


@Override @Override
public void applySingletonGetter(NominalTypeBuilder classType, FunctionTypeI getterType) { public void applySingletonGetter(NominalTypeBuilder classType, FunctionTypeI getterType) {
Node defSite = ((FunctionTypeI) classType.constructor().toTypeI()).getSource(); Node defSite = classType.constructor().getSource();
classType.constructor().declareProperty("getInstance", getterType, defSite); classType.declareConstructorProperty("getInstance", getterType, defSite);
classType.constructor().declareProperty("instance_", classType.instance().toTypeI(), defSite); classType.declareConstructorProperty("instance_", classType.instance(), defSite);
} }


@Override @Override
Expand Down
43 changes: 18 additions & 25 deletions src/com/google/javascript/jscomp/GlobalTypeInfoCollector.java
Expand Up @@ -296,8 +296,6 @@ public class GlobalTypeInfoCollector implements CompilerPass {
// Keyed on RawNominalTypes and property names // Keyed on RawNominalTypes and property names
private HashBasedTable<RawNominalType, String, PropertyDef> propertyDefs = private HashBasedTable<RawNominalType, String, PropertyDef> propertyDefs =
HashBasedTable.create(); HashBasedTable.create();
private final NominalTypeBuilderNti.LateProperties lateProps =
new NominalTypeBuilderNti.LateProperties();
private final Set<RawNominalType> inProgressFreezes = new LinkedHashSet<>(); private final Set<RawNominalType> inProgressFreezes = new LinkedHashSet<>();
private final GlobalTypeInfo globalTypeInfo; private final GlobalTypeInfo globalTypeInfo;
private final OrderedExterns orderedExterns; private final OrderedExterns orderedExterns;
Expand Down Expand Up @@ -541,11 +539,6 @@ private void checkAndFreezeNominalType(RawNominalType rawType) {
} }
} }


for (RawNominalType prerequisite : lateProps.prerequisites(rawType)) {
checkAndFreezeNominalType(prerequisite);
}
lateProps.defineProperties(rawType);

Multimap<String, DeclaredFunctionType> propMethodTypesToProcess = LinkedHashMultimap.create(); Multimap<String, DeclaredFunctionType> propMethodTypesToProcess = LinkedHashMultimap.create();
Multimap<String, JSType> propTypesToProcess = LinkedHashMultimap.create(); Multimap<String, JSType> propTypesToProcess = LinkedHashMultimap.create();
// Collect inherited types for extended classes // Collect inherited types for extended classes
Expand Down Expand Up @@ -1722,8 +1715,8 @@ private void applySubclassRelationship(SubclassRelationship rel) {
if (superClass != null && superClass.getConstructorFunction() != null if (superClass != null && superClass.getConstructorFunction() != null
&& subClass != null && subClass.getConstructorFunction() != null) { && subClass != null && subClass.getConstructorFunction() != null) {
convention.applySubclassRelationship( convention.applySubclassRelationship(
new NominalTypeBuilderNti(lateProps, superClass), new NominalTypeBuilderNti(superClass.getAsNominalType()),
new NominalTypeBuilderNti(lateProps, subClass), new NominalTypeBuilderNti(subClass.getAsNominalType()),
rel.type); rel.type);
} }
} }
Expand All @@ -1735,7 +1728,7 @@ private void applySingletonGetter(String className) {
JSType getInstanceType = JSType getInstanceType =
new FunctionTypeBuilder(getCommonTypes()).addRetType(instanceType).buildType(); new FunctionTypeBuilder(getCommonTypes()).addRetType(instanceType).buildType();
convention.applySingletonGetter( convention.applySingletonGetter(
new NominalTypeBuilderNti(lateProps, rawType), getInstanceType); new NominalTypeBuilderNti(rawType.getAsNominalType()), getInstanceType);
} }
} }


Expand Down Expand Up @@ -1767,12 +1760,12 @@ private void applyDelegateRelationship(DelegateRelationship rel) {
.addNominalType(delegateProxy.getInstanceAsJSType()) .addNominalType(delegateProxy.getInstanceAsJSType())
.buildFunction()); .buildFunction());
convention.applyDelegateRelationship( convention.applyDelegateRelationship(
new NominalTypeBuilderNti(lateProps, delegateSuper), new NominalTypeBuilderNti(delegateSuper.getAsNominalType()),
new NominalTypeBuilderNti(lateProps, delegateBase), new NominalTypeBuilderNti(delegateBase.getAsNominalType()),
new NominalTypeBuilderNti(lateProps, delegator), new NominalTypeBuilderNti(delegator.getAsNominalType()),
delegateProxy.getInstanceAsJSType(), delegateProxy.getInstanceAsJSType(),
findDelegate); findDelegate);
delegateProxies.add(new NominalTypeBuilderNti(lateProps, delegateProxy)); delegateProxies.add(new NominalTypeBuilderNti(delegateProxy.getAsNominalType()));
} }
} }


Expand Down Expand Up @@ -2163,18 +2156,18 @@ private void visitOtherPropertyDeclaration(Node getProp) {
return; return;
} }
JSType recvType = simpleInferExprType(recv); JSType recvType = simpleInferExprType(recv);
if (recvType == null) { // Might still be worth recording a property, e.g. on a function.
// Might still be worth recording a property, e.g. on a function. PropertyDef def = findPropertyDef(recv);
PropertyDef def = findPropertyDef(recv); if (def != null) {
if (def != null) { JSType type =
JSType type = getProp.getNext() != null
getProp.getNext() != null ? simpleInferExprType(getProp.getNext())
? simpleInferExprType(getProp.getNext()) : getCommonTypes().UNKNOWN;
: getCommonTypes().UNKNOWN; if (type != null) {
if (type != null) { def.addProperty(recv.getNext().getString(), type);
def.addProperty(recv.getNext().getString(), type);
}
} }
}
if (recvType == null) {
return; return;
} }
recvType = recvType.removeType(getCommonTypes().NULL_OR_UNDEFINED); recvType = recvType.removeType(getCommonTypes().NULL_OR_UNDEFINED);
Expand Down
46 changes: 19 additions & 27 deletions src/com/google/javascript/jscomp/TypedScopeCreator.java
Expand Up @@ -256,15 +256,13 @@ public TypedScope createScope(Node root, Scope parent) {
scopeBuilder.resolveStubDeclarations(); scopeBuilder.resolveStubDeclarations();


if (typedParent == null) { if (typedParent == null) {
try (NominalTypeBuilderOti.Factory factory = new NominalTypeBuilderOti.Factory()) { List<NominalTypeBuilder> delegateProxies = new ArrayList<>();
List<NominalTypeBuilder> delegateProxies = new ArrayList<>(); for (FunctionType delegateProxyCtor : delegateProxyCtors) {
for (FunctionType delegateProxyCtor : delegateProxyCtors) { delegateProxies.add(
delegateProxies.add( new NominalTypeBuilderOti(delegateProxyCtor, delegateProxyCtor.getInstanceType()));
factory.builder(delegateProxyCtor, delegateProxyCtor.getInstanceType()));
}
codingConvention.defineDelegateProxyPrototypeProperties(
typeRegistry, delegateProxies, delegateCallingConventions);
} }
codingConvention.defineDelegateProxyPrototypeProperties(
typeRegistry, delegateProxies, delegateCallingConventions);
} }


newScope.setTypeResolver(scopeBuilder); newScope.setTypeResolver(scopeBuilder);
Expand Down Expand Up @@ -1504,12 +1502,10 @@ private void checkForClassDefiningCalls(Node n) {
FunctionType superCtor = superClass.getConstructor(); FunctionType superCtor = superClass.getConstructor();
FunctionType subCtor = subClass.getConstructor(); FunctionType subCtor = subClass.getConstructor();
if (superCtor != null && subCtor != null) { if (superCtor != null && subCtor != null) {
try (NominalTypeBuilderOti.Factory factory = new NominalTypeBuilderOti.Factory()) { codingConvention.applySubclassRelationship(
codingConvention.applySubclassRelationship( new NominalTypeBuilderOti(superCtor, superClass),
factory.builder(superCtor, superClass), new NominalTypeBuilderOti(subCtor, subClass),
factory.builder(subCtor, subClass), relationship.type);
relationship.type);
}
} }
} }
} }
Expand All @@ -1524,10 +1520,8 @@ private void checkForClassDefiningCalls(Node n) {


if (functionType != null) { if (functionType != null) {
FunctionType getterType = typeRegistry.createFunctionType(objectType); FunctionType getterType = typeRegistry.createFunctionType(objectType);
try (NominalTypeBuilderOti.Factory factory = new NominalTypeBuilderOti.Factory()) { codingConvention.applySingletonGetter(
codingConvention.applySingletonGetter( new NominalTypeBuilderOti(functionType, objectType), getterType);
factory.builder(functionType, objectType), getterType);
}
} }
} }
} }
Expand Down Expand Up @@ -1595,15 +1589,13 @@ private void applyDelegateRelationship(
false /* isAbstract */); false /* isAbstract */);
delegateProxy.setPrototypeBasedOn(delegateBaseObject); delegateProxy.setPrototypeBasedOn(delegateBaseObject);


try (NominalTypeBuilderOti.Factory factory = new NominalTypeBuilderOti.Factory()) { codingConvention.applyDelegateRelationship(
codingConvention.applyDelegateRelationship( new NominalTypeBuilderOti(delegateSuperCtor, delegateSuperObject),
factory.builder(delegateSuperCtor, delegateSuperObject), new NominalTypeBuilderOti(delegateBaseCtor, delegateBaseObject),
factory.builder(delegateBaseCtor, delegateBaseObject), new NominalTypeBuilderOti(delegatorCtor, delegatorObject),
factory.builder(delegatorCtor, delegatorObject), (ObjectType) delegateProxy.getTypeOfThis(),
(ObjectType) delegateProxy.getTypeOfThis(), findDelegate);
findDelegate); delegateProxyCtors.add(delegateProxy);
delegateProxyCtors.add(delegateProxy);
}
} }
} }
} }
Expand Down
1 change: 0 additions & 1 deletion src/com/google/javascript/jscomp/newtypes/NominalType.java
Expand Up @@ -346,7 +346,6 @@ public Set<String> getAllNonInheritedInstanceProps() {
} }


public NominalType getInstantiatedSuperclass() { public NominalType getInstantiatedSuperclass() {
checkState(this.rawType.isFrozen());
if (this.rawType.getSuperClass() == null) { if (this.rawType.getSuperClass() == null) {
return null; return null;
} }
Expand Down

0 comments on commit 97cf26f

Please sign in to comment.