Skip to content

Commit

Permalink
[NTI] Migrate delegate coding conventions to use NominalTypeBuilder.
Browse files Browse the repository at this point in the history
This paves the way for calling these conventions from GlobalTypeInfoCollector.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=170775519
  • Loading branch information
shicks authored and dimvar committed Oct 3, 2017
1 parent 4036fdf commit 9cf9f52
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 36 deletions.
20 changes: 10 additions & 10 deletions src/com/google/javascript/jscomp/CodingConvention.java
Expand Up @@ -22,12 +22,11 @@
import com.google.javascript.rhino.FunctionTypeI; import com.google.javascript.rhino.FunctionTypeI;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.NominalTypeBuilder; import com.google.javascript.rhino.NominalTypeBuilder;
import com.google.javascript.rhino.ObjectTypeI;
import com.google.javascript.rhino.StaticSourceFile; import com.google.javascript.rhino.StaticSourceFile;
import com.google.javascript.rhino.jstype.FunctionType; import com.google.javascript.rhino.TypeIRegistry;
import com.google.javascript.rhino.jstype.JSTypeNative; import com.google.javascript.rhino.jstype.JSTypeNative;
import com.google.javascript.rhino.jstype.JSTypeRegistry; import com.google.javascript.rhino.jstype.JSTypeRegistry;
import com.google.javascript.rhino.jstype.ObjectType;
import com.google.javascript.rhino.jstype.StaticTypedScope;
import java.io.Serializable; import java.io.Serializable;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
Expand Down Expand Up @@ -260,9 +259,11 @@ public void applySingletonGetter(
* also adds properties to the delegator and delegate base. * also adds properties to the delegator and delegate base.
*/ */
public void applyDelegateRelationship( public void applyDelegateRelationship(
ObjectType delegateSuperclass, ObjectType delegateBase, NominalTypeBuilder delegateSuperclass,
ObjectType delegator, FunctionType delegateProxy, NominalTypeBuilder delegateBase,
FunctionType findDelegate); NominalTypeBuilder delegator,
ObjectTypeI delegateProxy,
FunctionTypeI findDelegate);


/** /**
* @return the name of the delegate superclass. * @return the name of the delegate superclass.
Expand All @@ -279,12 +280,11 @@ public void checkForCallingConventionDefinitions(
* Defines the delegate proxy prototype properties. Their types depend on * Defines the delegate proxy prototype properties. Their types depend on
* properties of the delegate base methods. * properties of the delegate base methods.
* *
* @param delegateProxyPrototypes List of delegate proxy prototypes. * @param delegateProxies List of delegate proxy types.
*/ */
public void defineDelegateProxyPrototypeProperties( public void defineDelegateProxyPrototypeProperties(
JSTypeRegistry registry, TypeIRegistry registry,
StaticTypedScope<com.google.javascript.rhino.jstype.JSType> scope, List<NominalTypeBuilder> delegateProxies,
List<ObjectType> delegateProxyPrototypes,
Map<String, String> delegateCallingConventions); Map<String, String> delegateCallingConventions);


/** /**
Expand Down
36 changes: 18 additions & 18 deletions src/com/google/javascript/jscomp/CodingConventions.java
Expand Up @@ -23,11 +23,10 @@
import com.google.javascript.rhino.FunctionTypeI; import com.google.javascript.rhino.FunctionTypeI;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.NominalTypeBuilder; import com.google.javascript.rhino.NominalTypeBuilder;
import com.google.javascript.rhino.ObjectTypeI;
import com.google.javascript.rhino.StaticSourceFile; import com.google.javascript.rhino.StaticSourceFile;
import com.google.javascript.rhino.TypeIRegistry;
import com.google.javascript.rhino.jstype.FunctionType; import com.google.javascript.rhino.jstype.FunctionType;
import com.google.javascript.rhino.jstype.JSTypeRegistry;
import com.google.javascript.rhino.jstype.ObjectType;
import com.google.javascript.rhino.jstype.StaticTypedScope;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
Expand Down Expand Up @@ -232,12 +231,13 @@ public DelegateRelationship getDelegateRelationship(Node callNode) {


@Override @Override
public void applyDelegateRelationship( public void applyDelegateRelationship(
ObjectType delegateSuperclass, ObjectType delegateBase, NominalTypeBuilder delegateSuperclass,
ObjectType delegator, FunctionType delegateProxy, NominalTypeBuilder delegateBase,
FunctionType findDelegate) { NominalTypeBuilder delegator,
ObjectTypeI delegateProxy,
FunctionTypeI findDelegate) {
nextConvention.applyDelegateRelationship( nextConvention.applyDelegateRelationship(
delegateSuperclass, delegateBase, delegator, delegateSuperclass, delegateBase, delegator, delegateProxy, findDelegate);
delegateProxy, findDelegate);
} }


@Override @Override
Expand All @@ -254,12 +254,11 @@ public void checkForCallingConventionDefinitions(


@Override @Override
public void defineDelegateProxyPrototypeProperties( public void defineDelegateProxyPrototypeProperties(
JSTypeRegistry registry, TypeIRegistry registry,
StaticTypedScope<com.google.javascript.rhino.jstype.JSType> scope, List<NominalTypeBuilder> delegateProxies,
List<ObjectType> delegateProxyPrototypes,
Map<String, String> delegateCallingConventions) { Map<String, String> delegateCallingConventions) {
nextConvention.defineDelegateProxyPrototypeProperties( nextConvention.defineDelegateProxyPrototypeProperties(
registry, scope, delegateProxyPrototypes, delegateCallingConventions); registry, delegateProxies, delegateCallingConventions);
} }


@Override @Override
Expand Down Expand Up @@ -490,9 +489,11 @@ public DelegateRelationship getDelegateRelationship(Node callNode) {


@Override @Override
public void applyDelegateRelationship( public void applyDelegateRelationship(
ObjectType delegateSuperclass, ObjectType delegateBase, NominalTypeBuilder delegateSuperclass,
ObjectType delegator, FunctionType delegateProxy, NominalTypeBuilder delegateBase,
FunctionType findDelegate) { NominalTypeBuilder delegator,
ObjectTypeI delegateProxy,
FunctionTypeI findDelegate) {
// do nothing. // do nothing.
} }


Expand All @@ -509,9 +510,8 @@ public void checkForCallingConventionDefinitions(Node n,


@Override @Override
public void defineDelegateProxyPrototypeProperties( public void defineDelegateProxyPrototypeProperties(
JSTypeRegistry registry, TypeIRegistry registry,
StaticTypedScope<com.google.javascript.rhino.jstype.JSType> scope, List<NominalTypeBuilder> delegateProxies,
List<ObjectType> delegateProxyPrototypes,
Map<String, String> delegateCallingConventions) { Map<String, String> delegateCallingConventions) {
// do nothing. // do nothing.
} }
Expand Down
28 changes: 20 additions & 8 deletions src/com/google/javascript/jscomp/TypedScopeCreator.java
Expand Up @@ -64,6 +64,7 @@
import com.google.javascript.rhino.InputId; import com.google.javascript.rhino.InputId;
import com.google.javascript.rhino.JSDocInfo; import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.NominalTypeBuilder;
import com.google.javascript.rhino.Token; import com.google.javascript.rhino.Token;
import com.google.javascript.rhino.jstype.EnumType; import com.google.javascript.rhino.jstype.EnumType;
import com.google.javascript.rhino.jstype.FunctionParamBuilder; import com.google.javascript.rhino.jstype.FunctionParamBuilder;
Expand Down Expand Up @@ -155,7 +156,7 @@ final class TypedScopeCreator implements ScopeCreator {
private final TypeValidator validator; private final TypeValidator validator;
private final CodingConvention codingConvention; private final CodingConvention codingConvention;
private final JSTypeRegistry typeRegistry; private final JSTypeRegistry typeRegistry;
private final List<ObjectType> delegateProxyPrototypes = new ArrayList<>(); private final List<FunctionType> delegateProxyCtors = new ArrayList<>();
private final Map<String, String> delegateCallingConventions = new HashMap<>(); private final Map<String, String> delegateCallingConventions = new HashMap<>();
private final boolean runsAfterNTI; private final boolean runsAfterNTI;


Expand Down Expand Up @@ -255,9 +256,15 @@ public TypedScope createScope(Node root, Scope parent) {
scopeBuilder.resolveStubDeclarations(); scopeBuilder.resolveStubDeclarations();


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


newScope.setTypeResolver(scopeBuilder); newScope.setTypeResolver(scopeBuilder);
Expand Down Expand Up @@ -1588,10 +1595,15 @@ private void applyDelegateRelationship(
false /* isAbstract */); false /* isAbstract */);
delegateProxy.setPrototypeBasedOn(delegateBaseObject); delegateProxy.setPrototypeBasedOn(delegateBaseObject);


codingConvention.applyDelegateRelationship( try (NominalTypeBuilderOti.Factory factory = new NominalTypeBuilderOti.Factory()) {
delegateSuperObject, delegateBaseObject, delegatorObject, codingConvention.applyDelegateRelationship(
delegateProxy, findDelegate); factory.builder(delegateSuperCtor, delegateSuperObject),
delegateProxyPrototypes.add(delegateProxy.getPrototype()); factory.builder(delegateBaseCtor, delegateBaseObject),
factory.builder(delegatorCtor, delegatorObject),
(ObjectType) delegateProxy.getTypeOfThis(),
findDelegate);
delegateProxyCtors.add(delegateProxy);
}
} }
} }
} }
Expand Down
Expand Up @@ -77,6 +77,12 @@ public void beforeFreeze(Runnable runnable, NominalTypeBuilder... prerequisites)
props.runnables.put(raw, runnable); props.runnables.put(raw, runnable);
} }


@Override
public NominalTypeBuilder superClass() {
NominalType nt = raw.getSuperClass();
return nt != null ? new NominalTypeBuilderNti(props, nt.getRawNominalType()) : null;
}

@Override @Override
public ObjectBuilder constructor() { public ObjectBuilder constructor() {
return new ObjectBuilderImpl(PropertySlot.CONSTRUCTOR); return new ObjectBuilderImpl(PropertySlot.CONSTRUCTOR);
Expand Down
3 changes: 3 additions & 0 deletions src/com/google/javascript/rhino/NominalTypeBuilder.java
Expand Up @@ -80,6 +80,9 @@ interface ObjectBuilder {
*/ */
void beforeFreeze(Runnable task, NominalTypeBuilder... prerequisites); void beforeFreeze(Runnable task, NominalTypeBuilder... prerequisites);


/** Returns a nominal type builder for the super class. */
NominalTypeBuilder superClass();

/** Returns the object builder for declaring properties on the constructor function. */ /** Returns the object builder for declaring properties on the constructor function. */
ObjectBuilder constructor(); ObjectBuilder constructor();


Expand Down
Expand Up @@ -109,6 +109,15 @@ public void beforeFreeze(Runnable task, NominalTypeBuilder... prerequisites) {
callbacks.add(task); callbacks.add(task);
} }


@Override
public NominalTypeBuilder superClass() {
FunctionType ctor = instance.object.getSuperClassConstructor();
if (ctor == null) {
return null;
}
return new NominalTypeBuilderOti(callbacks, ctor, ctor.getInstanceType());
}

@Override @Override
public ObjectBuilder constructor() { public ObjectBuilder constructor() {
return constructor; return constructor;
Expand Down

0 comments on commit 9cf9f52

Please sign in to comment.