Skip to content

Commit

Permalink
[NTI] s/finalize/freeze/g
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=152734004
  • Loading branch information
shicks authored and brad4d committed Apr 11, 2017
1 parent 8315486 commit 3a775bf
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 64 deletions.
40 changes: 20 additions & 20 deletions src/com/google/javascript/jscomp/GlobalTypeInfo.java
Expand Up @@ -477,21 +477,21 @@ public void process(Node externs, Node root) {
win = rawType; win = rawType;
continue; continue;
} }
checkAndFinalizeNominalType(rawType); checkAndFreezeNominalType(rawType);
} }
JSType globalThisType; JSType globalThisType;
if (win != null) { if (win != null) {
// Copy properties from window to Window.prototype, because sometimes // Copy properties from window to Window.prototype, because sometimes
// people pass window around rather than using it directly. // people pass window around rather than using it directly.
// Copying the properties is correct only when there is a single object // Copying the properties is correct only when there is a single object
// of type Window in the program. But in very rare cases, people subclass Window. // of type Window in the program. But in very rare cases, people subclass Window.
// Then, win is finalized here and we don't copy the properties. // Then, win is frozen here and we don't copy the properties.
// Window has been subclassed iff it is already finalized here. // Window has been subclassed iff it is already frozen here.
Namespace winNs = this.globalScope.getNamespace(WINDOW_INSTANCE); Namespace winNs = this.globalScope.getNamespace(WINDOW_INSTANCE);
if (winNs != null && !win.isFinalized()) { if (winNs != null && !win.isFrozen()) {
winNs.copyWindowProperties(this.commonTypes, win); winNs.copyWindowProperties(this.commonTypes, win);
} }
checkAndFinalizeNominalType(win); checkAndFreezeNominalType(win);
// Type the global THIS as window // Type the global THIS as window
globalThisType = win.getInstanceAsJSType(); globalThisType = win.getInstanceAsJSType();
} else { } else {
Expand All @@ -505,7 +505,7 @@ public void process(Node externs, Node root) {
nominaltypesByNode = null; nominaltypesByNode = null;
propertyDefs = null; propertyDefs = null;
for (NTIScope s : scopes) { for (NTIScope s : scopes) {
s.finalizeScope(); s.freezeScope();
} }


// Traverse the externs and annotate them with types. // Traverse the externs and annotate them with types.
Expand Down Expand Up @@ -577,7 +577,7 @@ private JSType simpleInferDeclaration(Declaration decl) {
} }


private Collection<PropertyDef> getPropDefsFromInterface(NominalType nominalType, String pname) { private Collection<PropertyDef> getPropDefsFromInterface(NominalType nominalType, String pname) {
Preconditions.checkArgument(nominalType.isFinalized()); Preconditions.checkArgument(nominalType.isFrozen());
Preconditions.checkArgument(nominalType.isInterface() || nominalType.isBuiltinObject()); Preconditions.checkArgument(nominalType.isInterface() || nominalType.isBuiltinObject());
if (nominalType.getPropDeclaredType(pname) == null) { if (nominalType.getPropDeclaredType(pname) == null) {
return ImmutableSet.of(); return ImmutableSet.of();
Expand All @@ -595,7 +595,7 @@ private Collection<PropertyDef> getPropDefsFromInterface(NominalType nominalType


private PropertyDef getPropDefFromClass(NominalType nominalType, String pname) { private PropertyDef getPropDefFromClass(NominalType nominalType, String pname) {
while (nominalType.getPropDeclaredType(pname) != null) { while (nominalType.getPropDeclaredType(pname) != null) {
Preconditions.checkArgument(nominalType.isFinalized()); Preconditions.checkArgument(nominalType.isFrozen());
Preconditions.checkArgument(nominalType.isClass()); Preconditions.checkArgument(nominalType.isClass());


if (propertyDefs.get(nominalType.getId(), pname) != null) { if (propertyDefs.get(nominalType.getId(), pname) != null) {
Expand All @@ -607,18 +607,18 @@ private PropertyDef getPropDefFromClass(NominalType nominalType, String pname) {
return null; return null;
} }


private void checkAndFinalizeNominalType(RawNominalType rawType) { private void checkAndFreezeNominalType(RawNominalType rawType) {
if (rawType.isFinalized()) { if (rawType.isFrozen()) {
return; return;
} }
NominalType superClass = rawType.getSuperClass(); NominalType superClass = rawType.getSuperClass();
Set<String> nonInheritedPropNames = rawType.getAllOwnProps(); Set<String> nonInheritedPropNames = rawType.getAllOwnProps();
if (superClass != null && !superClass.isFinalized()) { if (superClass != null && !superClass.isFrozen()) {
checkAndFinalizeNominalType(superClass.getRawNominalType()); checkAndFreezeNominalType(superClass.getRawNominalType());
} }
for (NominalType superInterf : rawType.getInterfaces()) { for (NominalType superInterf : rawType.getInterfaces()) {
if (!superInterf.isFinalized()) { if (!superInterf.isFrozen()) {
checkAndFinalizeNominalType(superInterf.getRawNominalType()); checkAndFreezeNominalType(superInterf.getRawNominalType());
} }
} }


Expand All @@ -627,7 +627,7 @@ private void checkAndFinalizeNominalType(RawNominalType rawType) {
Multimap<String, JSType> propTypesToProcess = LinkedHashMultimap.create(); Multimap<String, JSType> propTypesToProcess = LinkedHashMultimap.create();
// Collect inherited types for extended classes // Collect inherited types for extended classes
if (superClass != null) { if (superClass != null) {
Preconditions.checkState(superClass.isFinalized()); Preconditions.checkState(superClass.isFrozen());
// TODO(blickly): Can we optimize this to skip unnecessary iterations? // TODO(blickly): Can we optimize this to skip unnecessary iterations?
for (String pname : superClass.getAllPropsOfClass()) { for (String pname : superClass.getAllPropsOfClass()) {
if (superClass.isAbstractClass() if (superClass.isAbstractClass()
Expand All @@ -646,7 +646,7 @@ private void checkAndFinalizeNominalType(RawNominalType rawType) {


// Collect inherited types for extended/implemented interfaces // Collect inherited types for extended/implemented interfaces
for (NominalType superInterf : rawType.getInterfaces()) { for (NominalType superInterf : rawType.getInterfaces()) {
Preconditions.checkState(superInterf.isFinalized()); Preconditions.checkState(superInterf.isFrozen());
for (String pname : superInterf.getAllPropsOfInterface()) { for (String pname : superInterf.getAllPropsOfInterface()) {
nonInheritedPropNames.remove(pname); nonInheritedPropNames.remove(pname);
checkSuperProperty(rawType, superInterf, pname, checkSuperProperty(rawType, superInterf, pname,
Expand Down Expand Up @@ -742,12 +742,12 @@ private void checkAndFinalizeNominalType(RawNominalType rawType) {
} }
} }


// Finalize nominal type once all properties are added. // Freeze nominal type once all properties are added.
rawType.finalize(); rawType.freeze();
if (rawType.isBuiltinObject()) { if (rawType.isBuiltinObject()) {
NominalType literalObj = this.commonTypes.getLiteralObjNominalType(); NominalType literalObj = this.commonTypes.getLiteralObjNominalType();
if (!literalObj.isFinalized()) { if (!literalObj.isFrozen()) {
literalObj.getRawNominalType().finalize(); literalObj.getRawNominalType().freeze();
} }
} }
} }
Expand Down
14 changes: 7 additions & 7 deletions src/com/google/javascript/jscomp/NTIScope.java
Expand Up @@ -52,8 +52,8 @@ final class NTIScope implements DeclaredTypeRegistry {
// Name on the function AST node; null for top scope & anonymous functions // Name on the function AST node; null for top scope & anonymous functions
private final String name; private final String name;
private final JSTypes commonTypes; private final JSTypes commonTypes;
// Becomes true after finalizeScope is run; so it's true during NTI. // Becomes true after freezeScope is run; so it's true during NTI.
private boolean isFinalized = false; private boolean isFrozen = false;


// A local w/out declared type is mapped to null, not to this.commonTypes.UNKNOWN. // A local w/out declared type is mapped to null, not to this.commonTypes.UNKNOWN.
private final Map<String, JSType> locals = new LinkedHashMap<>(); private final Map<String, JSType> locals = new LinkedHashMap<>();
Expand All @@ -66,7 +66,7 @@ final class NTIScope implements DeclaredTypeRegistry {
// and are defined in an outer scope. // and are defined in an outer scope.
private final Set<String> outerVars = new LinkedHashSet<>(); private final Set<String> outerVars = new LinkedHashSet<>();
// When a function is also used as a namespace, we add entries to both // When a function is also used as a namespace, we add entries to both
// localFunDefs and localNamespaces. After finalizeScope (when NTI runs), // localFunDefs and localNamespaces. After freezeScope (when NTI runs),
// the function has an entry in localFunDefs, and in locals or externs. // the function has an entry in localFunDefs, and in locals or externs.
private final Map<String, NTIScope> localFunDefs = new LinkedHashMap<>(); private final Map<String, NTIScope> localFunDefs = new LinkedHashMap<>();
private ImmutableSet<String> unknownTypeNames = ImmutableSet.of(); private ImmutableSet<String> unknownTypeNames = ImmutableSet.of();
Expand Down Expand Up @@ -187,7 +187,7 @@ boolean isLocalFunDef(String name) {


boolean isFunctionNamespace(String name) { boolean isFunctionNamespace(String name) {
Preconditions.checkArgument(!name.contains(".")); Preconditions.checkArgument(!name.contains("."));
Preconditions.checkState(isFinalized); Preconditions.checkState(isFrozen);
Declaration d = getDeclaration(name, false); Declaration d = getDeclaration(name, false);
if (d == null || d.getFunctionScope() == null || d.getTypeOfSimpleDecl() == null) { if (d == null || d.getFunctionScope() == null || d.getTypeOfSimpleDecl() == null) {
return false; return false;
Expand Down Expand Up @@ -573,7 +573,7 @@ private Declaration getLocalDeclaration(String name, boolean includeTypes) {
} else if (localFunDefs.containsKey(name)) { } else if (localFunDefs.containsKey(name)) {
// After finalization, the externs contain the correct type for // After finalization, the externs contain the correct type for
// external function namespaces, don't rely on localFunDefs // external function namespaces, don't rely on localFunDefs
if (isFinalized && externs.containsKey(name)) { if (isFrozen && externs.containsKey(name)) {
type = externs.get(name); type = externs.get(name);
} }
} else if (localTypedefs.containsKey(name) || localNamespaces.containsKey(name)) { } else if (localTypedefs.containsKey(name) || localNamespaces.containsKey(name)) {
Expand Down Expand Up @@ -656,7 +656,7 @@ void resolveEnums(JSTypeCreatorFromJSDoc typeParser) {
localEnums = null; localEnums = null;
} }


void finalizeScope() { void freezeScope() {
Preconditions.checkNotNull(this.declaredType, "No declared type for scope: %s", this.root); Preconditions.checkNotNull(this.declaredType, "No declared type for scope: %s", this.root);
unknownTypeNames = ImmutableSet.of(); unknownTypeNames = ImmutableSet.of();
// For now, we put types of namespaces directly into the locals. // For now, we put types of namespaces directly into the locals.
Expand Down Expand Up @@ -690,7 +690,7 @@ void finalizeScope() {
localNamespaces = ImmutableMap.of(); localNamespaces = ImmutableMap.of();
localTypedefs = ImmutableMap.of(); localTypedefs = ImmutableMap.of();
escapedVars = ImmutableSet.of(); escapedVars = ImmutableSet.of();
isFinalized = true; isFrozen = true;
} }


// A scope must know about the free variables used in outer scopes, // A scope must know about the free variables used in outer scopes,
Expand Down
6 changes: 3 additions & 3 deletions src/com/google/javascript/jscomp/newtypes/Namespace.java
Expand Up @@ -33,7 +33,7 @@
*/ */
public abstract class Namespace { public abstract class Namespace {
private Map<String, Namespace> namespaces = ImmutableMap.of(); private Map<String, Namespace> namespaces = ImmutableMap.of();
// Finalize the namespace after GTI and null-out the typedefs. // Freeze the namespace after GTI and null-out the typedefs.
private Map<String, Typedef> typedefs = ImmutableMap.of(); private Map<String, Typedef> typedefs = ImmutableMap.of();
// "Simple type" properties (i.e. represented as JSTypes rather than something // "Simple type" properties (i.e. represented as JSTypes rather than something
// more specific). // more specific).
Expand Down Expand Up @@ -257,9 +257,9 @@ public final void copyWindowProperties(JSTypes commonTypes, RawNominalType win)
if (ns instanceof RawNominalType) { if (ns instanceof RawNominalType) {
RawNominalType rawType = (RawNominalType) ns; RawNominalType rawType = (RawNominalType) ns;
// Hack: circular namespace here, skip adding Window. // Hack: circular namespace here, skip adding Window.
if (!rawType.isFinalized()) { if (!rawType.isFrozen()) {
Preconditions.checkState(rawType.getName().equals("Window"), Preconditions.checkState(rawType.getName().equals("Window"),
"Unexpected unfinalized type %s", rawType.getName()); "Unexpected unfrozen type %s", rawType.getName());
continue; continue;
} }
} }
Expand Down
20 changes: 10 additions & 10 deletions src/com/google/javascript/jscomp/newtypes/NominalType.java
Expand Up @@ -55,8 +55,8 @@ public final class NominalType {
// expect fully-instantiated types for properties defined on types, etc., but by accessing // expect fully-instantiated types for properties defined on types, etc., but by accessing
// the raw nominal type directly they will get the uninstantiated generic types instead. // the raw nominal type directly they will get the uninstantiated generic types instead.
public RawNominalType getRawNominalType() { public RawNominalType getRawNominalType() {
// If the raw nominal type is finalized, then we are not in GlobalTypeInfo any more. // If the raw nominal type is frozen, then we are not in GlobalTypeInfo any more.
Preconditions.checkState(!this.rawType.isFinalized()); Preconditions.checkState(!this.rawType.isFrozen());
return this.rawType; return this.rawType;
} }


Expand Down Expand Up @@ -284,8 +284,8 @@ boolean isStructuralInterface() {
return this.rawType.isStructuralInterface(); return this.rawType.isStructuralInterface();
} }


public boolean isFinalized() { public boolean isFrozen() {
return this.rawType.isFinalized(); return this.rawType.isFrozen();
} }


boolean hasAncestorClass(RawNominalType ancestor) { boolean hasAncestorClass(RawNominalType ancestor) {
Expand All @@ -309,22 +309,22 @@ public Set<String> getAllOwnClassProps() {
} }


public NominalType getInstantiatedSuperclass() { public NominalType getInstantiatedSuperclass() {
Preconditions.checkState(this.rawType.isFinalized()); Preconditions.checkState(this.rawType.isFrozen());
if (this.rawType.getSuperClass() == null) { if (this.rawType.getSuperClass() == null) {
return null; return null;
} }
return this.rawType.getSuperClass().instantiateGenerics(typeMap); return this.rawType.getSuperClass().instantiateGenerics(typeMap);
} }


public JSType getPrototypePropertyOfCtor() { public JSType getPrototypePropertyOfCtor() {
Preconditions.checkState(this.rawType.isFinalized()); Preconditions.checkState(this.rawType.isFrozen());
return this.rawType.getCtorPropDeclaredType("prototype"); return this.rawType.getCtorPropDeclaredType("prototype");
} }


// We require finalization for the interfaces here because the inheritance // We require finalization for the interfaces here because the inheritance
// chain of each type may not be correct until after the type is finalized. // chain of each type may not be correct until after the type is frozen.
public ImmutableSet<NominalType> getInstantiatedInterfaces() { public ImmutableSet<NominalType> getInstantiatedInterfaces() {
Preconditions.checkState(this.rawType.isFinalized()); Preconditions.checkState(this.rawType.isFrozen());
ImmutableSet.Builder<NominalType> result = ImmutableSet.builder(); ImmutableSet.Builder<NominalType> result = ImmutableSet.builder();
for (NominalType interf : this.rawType.getInterfaces()) { for (NominalType interf : this.rawType.getInterfaces()) {
result.add(interf.instantiateGenerics(typeMap)); result.add(interf.instantiateGenerics(typeMap));
Expand All @@ -333,7 +333,7 @@ public ImmutableSet<NominalType> getInstantiatedInterfaces() {
} }


// The main difference from getInstantiatedInterfaces is that this method // The main difference from getInstantiatedInterfaces is that this method
// can be used on non-finalized types. // can be used on non-frozen types.
private ImmutableSet<NominalType> getInstantiatedIObjectInterfaces() { private ImmutableSet<NominalType> getInstantiatedIObjectInterfaces() {
ImmutableSet.Builder<NominalType> result = ImmutableSet.builder(); ImmutableSet.Builder<NominalType> result = ImmutableSet.builder();
for (NominalType interf : this.rawType.getInterfaces()) { for (NominalType interf : this.rawType.getInterfaces()) {
Expand Down Expand Up @@ -437,7 +437,7 @@ boolean isNominalSubtypeOf(NominalType other) {
return true; return true;
} }
if (other.isInterface()) { if (other.isInterface()) {
// If thisRaw is not finalized, thisRaw.interfaces may be null. // If thisRaw is not frozen, thisRaw.interfaces may be null.
for (NominalType i : thisRaw.getInterfaces()) { for (NominalType i : thisRaw.getInterfaces()) {
if (i.instantiateGenerics(this.typeMap).isNominalSubtypeOf(other)) { if (i.instantiateGenerics(this.typeMap).isNominalSubtypeOf(other)) {
return true; return true;
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/newtypes/ObjectType.java
Expand Up @@ -1331,7 +1331,7 @@ boolean unifyWithSubtype(ObjectType other, List<String> typeParameters,
} }
Set<String> thisProps = !thisNt.isBuiltinObject() && thisNt.isStructuralInterface() Set<String> thisProps = !thisNt.isBuiltinObject() && thisNt.isStructuralInterface()
? thisNt.getAllPropsOfInterface() : this.props.keySet(); ? thisNt.getAllPropsOfInterface() : this.props.keySet();
if (thisProps == null) {// Can happen during GTI when types aren't finalized yet. if (thisProps == null) {// Can happen during GTI when types aren't frozen yet.
return true; return true;
} }
return unifyPropsWithSubtype(other, thisProps, typeParameters, typeMultimap, subSuperMap); return unifyPropsWithSubtype(other, thisProps, typeParameters, typeMultimap, subSuperMap);
Expand Down

0 comments on commit 3a775bf

Please sign in to comment.