Skip to content

Commit

Permalink
Remove generics from OTI classes now that NTI is deleted.
Browse files Browse the repository at this point in the history
Specifically removes parameters from StaticTypedScope, StaticTypedSlot, and StaticTypedRef. JSType is now hard-coded everywhere.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=196789659
  • Loading branch information
shicks authored and blickly committed May 17, 2018
1 parent 00786f0 commit 446d16f
Show file tree
Hide file tree
Showing 27 changed files with 152 additions and 177 deletions.
30 changes: 14 additions & 16 deletions src/com/google/javascript/jscomp/GlobalNamespace.java
Expand Up @@ -42,14 +42,13 @@
import javax.annotation.Nullable; import javax.annotation.Nullable;


/** /**
* Builds a global namespace of all the objects and their properties in * Builds a global namespace of all the objects and their properties in the global scope. Also
* the global scope. Also builds an index of all the references to those names. * builds an index of all the references to those names.
* *
* @author nicksantos@google.com (Nick Santos) * @author nicksantos@google.com (Nick Santos)
*/ */
class GlobalNamespace class GlobalNamespace
implements StaticTypedScope<JSType>, implements StaticTypedScope, StaticSymbolTable<GlobalNamespace.Name, GlobalNamespace.Ref> {
StaticSymbolTable<GlobalNamespace.Name, GlobalNamespace.Ref> {


private final AbstractCompiler compiler; private final AbstractCompiler compiler;
private final Node root; private final Node root;
Expand Down Expand Up @@ -108,7 +107,7 @@ public Node getRootNode() {
} }


@Override @Override
public StaticTypedScope<JSType> getParentScope() { public StaticTypedScope getParentScope() {
return null; return null;
} }


Expand All @@ -135,7 +134,7 @@ public Iterable<Ref> getReferences(Name slot) {
} }


@Override @Override
public StaticTypedScope<JSType> getScope(Name slot) { public StaticTypedScope getScope(Name slot) {
return this; return this;
} }


Expand Down Expand Up @@ -1008,12 +1007,11 @@ Name getOrCreateName(String name, boolean shouldCreateProp) {
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------


/** /**
* A name defined in global scope (e.g. "a" or "a.b.c.d"). These form a tree. * A name defined in global scope (e.g. "a" or "a.b.c.d"). These form a tree. As the parse tree
* As the parse tree traversal proceeds, we'll discover that some names * traversal proceeds, we'll discover that some names correspond to JavaScript objects whose
* correspond to JavaScript objects whose properties we should consider * properties we should consider collapsing.
* collapsing.
*/ */
static class Name implements StaticTypedSlot<JSType> { static class Name implements StaticTypedSlot {
enum Type { enum Type {
CLASS, CLASS,
OBJECTLIT, OBJECTLIT,
Expand Down Expand Up @@ -1111,7 +1109,7 @@ public JSType getType() {
} }


@Override @Override
public StaticTypedScope<JSType> getScope() { public StaticTypedScope getScope() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }


Expand Down Expand Up @@ -1455,10 +1453,10 @@ boolean isModuleExport() {
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------


/** /**
* A global name reference. Contains references to the relevant parse tree * A global name reference. Contains references to the relevant parse tree node and its ancestors
* node and its ancestors that may be affected. * that may be affected.
*/ */
static class Ref implements StaticTypedRef<JSType> { static class Ref implements StaticTypedRef {


// Note: we are more aggressive about collapsing @enum and @constructor // Note: we are more aggressive about collapsing @enum and @constructor
// declarations than implied here, see Name#canCollapse // declarations than implied here, see Name#canCollapse
Expand Down Expand Up @@ -1563,7 +1561,7 @@ public StaticSourceFile getSourceFile() {
} }


@Override @Override
public StaticTypedSlot<JSType> getSymbol() { public StaticTypedSlot getSymbol() {
return name; return name;
} }


Expand Down
25 changes: 11 additions & 14 deletions src/com/google/javascript/jscomp/LinkedFlowScope.java
Expand Up @@ -164,19 +164,17 @@ public Node getRootNode() {
} }


@Override @Override
public StaticTypedScope<JSType> getParentScope() { public StaticTypedScope getParentScope() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }


/** /** Get the slot for the given symbol. */
* Get the slot for the given symbol.
*/
@Override @Override
public StaticTypedSlot<JSType> getSlot(String name) { public StaticTypedSlot getSlot(String name) {
return getSlot(getVarFromSyntacticScope(name)); return getSlot(getVarFromSyntacticScope(name));
} }


private StaticTypedSlot<JSType> getSlot(ScopedName var) { private StaticTypedSlot getSlot(ScopedName var) {
if (cache.dirtySymbols.contains(var)) { if (cache.dirtySymbols.contains(var)) {
for (LinkedFlowSlot slot = lastSlot; slot != null; slot = slot.parent) { for (LinkedFlowSlot slot = lastSlot; slot != null; slot = slot.parent) {
if (slot.var.equals(var)) { if (slot.var.equals(var)) {
Expand Down Expand Up @@ -210,7 +208,7 @@ private ScopedName getVarFromSyntacticScope(String name) {
} }


@Override @Override
public StaticTypedSlot<JSType> getOwnSlot(String name) { public StaticTypedSlot getOwnSlot(String name) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }


Expand All @@ -220,7 +218,7 @@ public FlowScope createChildFlowScope() {
} }


@Override @Override
public FlowScope createChildFlowScope(StaticTypedScope<JSType> scope) { public FlowScope createChildFlowScope(StaticTypedScope scope) {
frozen = true; frozen = true;


TypedScope typedScope = (TypedScope) scope; TypedScope typedScope = (TypedScope) scope;
Expand Down Expand Up @@ -381,10 +379,9 @@ public boolean equals(Object other) {
} }


/** /**
* Determines whether two slots are meaningfully different for the * Determines whether two slots are meaningfully different for the purposes of data flow analysis.
* purposes of data flow analysis.
*/ */
private static boolean diffSlots(StaticTypedSlot<JSType> slotA, StaticTypedSlot<JSType> slotB) { private static boolean diffSlots(StaticTypedSlot slotA, StaticTypedSlot slotB) {
boolean aIsNull = slotA == null || slotA.getType() == null; boolean aIsNull = slotA == null || slotA.getType() == null;
boolean bIsNull = slotB == null || slotB.getType() == null; boolean bIsNull = slotB == null || slotB.getType() == null;
if (aIsNull || bIsNull) { if (aIsNull || bIsNull) {
Expand Down Expand Up @@ -432,7 +429,7 @@ public int hashCode() {
} }


/** A static slot with a linked list built in. */ /** A static slot with a linked list built in. */
private static class LinkedFlowSlot implements StaticTypedSlot<JSType> { private static class LinkedFlowSlot implements StaticTypedSlot {
final ScopedName var; final ScopedName var;
final JSType type; final JSType type;
final LinkedFlowSlot parent; final LinkedFlowSlot parent;
Expand All @@ -459,7 +456,7 @@ public boolean isTypeInferred() {
} }


@Override @Override
public StaticTypedRef<JSType> getDeclaration() { public StaticTypedRef getDeclaration() {
return null; return null;
} }


Expand All @@ -469,7 +466,7 @@ public JSDocInfo getJSDocInfo() {
} }


@Override @Override
public StaticTypedScope<JSType> getScope() { public StaticTypedScope getScope() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
} }
Expand Down
12 changes: 5 additions & 7 deletions src/com/google/javascript/jscomp/PreprocessorSymbolTable.java
Expand Up @@ -32,15 +32,13 @@
import javax.annotation.Nullable; import javax.annotation.Nullable;


/** /**
* A symbol table for references that are removed by preprocessor passes * A symbol table for references that are removed by preprocessor passes (like {@code
* (like {@code ProcessClosurePrimitives}). * ProcessClosurePrimitives}).
* *
* @author nicksantos@google.com (Nick Santos) * @author nicksantos@google.com (Nick Santos)
*/ */
final class PreprocessorSymbolTable final class PreprocessorSymbolTable
implements StaticTypedScope<JSType>, implements StaticTypedScope, StaticSymbolTable<SimpleSlot, PreprocessorSymbolTable.Reference> {
StaticSymbolTable<SimpleSlot,
PreprocessorSymbolTable.Reference> {


/** /**
* All preprocessor symbols are globals. * All preprocessor symbols are globals.
Expand All @@ -67,7 +65,7 @@ public JSType getTypeOfThis() {
} }


@Override @Override
public StaticTypedScope<JSType> getParentScope() { public StaticTypedScope getParentScope() {
return null; return null;
} }


Expand All @@ -92,7 +90,7 @@ public Iterable<SimpleSlot> getAllSymbols() {
} }


@Override @Override
public StaticTypedScope<JSType> getScope(SimpleSlot slot) { public StaticTypedScope getScope(SimpleSlot slot) {
return this; return this;
} }


Expand Down
6 changes: 3 additions & 3 deletions src/com/google/javascript/jscomp/SymbolTable.java
Expand Up @@ -1880,21 +1880,21 @@ private int getLexicalScopeDepth(SymbolScope scope) {


private JSType getType(StaticSlot sym) { private JSType getType(StaticSlot sym) {
if (sym instanceof StaticTypedSlot) { if (sym instanceof StaticTypedSlot) {
return ((StaticTypedSlot<JSType>) sym).getType(); return ((StaticTypedSlot) sym).getType();
} }
return null; return null;
} }


private JSType getTypeOfThis(StaticScope s) { private JSType getTypeOfThis(StaticScope s) {
if (s instanceof StaticTypedScope) { if (s instanceof StaticTypedScope) {
return ((StaticTypedScope<JSType>) s).getTypeOfThis(); return ((StaticTypedScope) s).getTypeOfThis();
} }
return null; return null;
} }


private boolean isTypeInferred(StaticSlot sym) { private boolean isTypeInferred(StaticSlot sym) {
if (sym instanceof StaticTypedSlot) { if (sym instanceof StaticTypedSlot) {
return ((StaticTypedSlot<JSType>) sym).isTypeInferred(); return ((StaticTypedSlot) sym).isTypeInferred();
} }
return true; return true;
} }
Expand Down
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/TypeInference.java
Expand Up @@ -866,7 +866,7 @@ private FlowScope traverseName(Node n, FlowScope scope) {
updateScopeForTypeChange(scope, n, type, resultType); updateScopeForTypeChange(scope, n, type, resultType);
type = resultType; type = resultType;
} else { } else {
StaticTypedSlot<JSType> var = scope.getSlot(varName); StaticTypedSlot var = scope.getSlot(varName);
if (var != null) { if (var != null) {
// There are two situations where we don't want to use type information // There are two situations where we don't want to use type information
// from the scope, even if we have it. // from the scope, even if we have it.
Expand Down Expand Up @@ -1657,7 +1657,7 @@ private JSType getPropertyType(JSType objType, String propName,


// Scopes sometimes contain inferred type info about qualified names. // Scopes sometimes contain inferred type info about qualified names.
String qualifiedName = n.getQualifiedName(); String qualifiedName = n.getQualifiedName();
StaticTypedSlot<JSType> var = qualifiedName != null ? scope.getSlot(qualifiedName) : null; StaticTypedSlot var = qualifiedName != null ? scope.getSlot(qualifiedName) : null;
if (var != null) { if (var != null) {
JSType varType = var.getType(); JSType varType = var.getType();
if (varType != null) { if (varType != null) {
Expand Down
8 changes: 4 additions & 4 deletions src/com/google/javascript/jscomp/TypeTransformation.java
Expand Up @@ -94,7 +94,7 @@ class TypeTransformation {


private final AbstractCompiler compiler; private final AbstractCompiler compiler;
private final JSTypeRegistry registry; private final JSTypeRegistry registry;
private final StaticTypedScope<JSType> typeEnv; private final StaticTypedScope typeEnv;


/** /**
* A helper class for holding the information about the type variables * A helper class for holding the information about the type variables
Expand All @@ -111,7 +111,7 @@ private static class NameResolver {
} }


@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
TypeTransformation(AbstractCompiler compiler, StaticTypedScope<JSType> typeEnv) { TypeTransformation(AbstractCompiler compiler, StaticTypedScope typeEnv) {
this.compiler = compiler; this.compiler = compiler;
this.registry = compiler.getTypeRegistry(); this.registry = compiler.getTypeRegistry();
this.typeEnv = typeEnv; this.typeEnv = typeEnv;
Expand All @@ -138,7 +138,7 @@ private JSType getType(String typeName) {
if (type != null) { if (type != null) {
return type; return type;
} }
StaticTypedSlot<JSType> slot = typeEnv.getSlot(typeName); StaticTypedSlot slot = typeEnv.getSlot(typeName);
type = slot != null ? slot.getType() : null; type = slot != null ? slot.getType() : null;
if (type != null) { if (type != null) {
if (type.isConstructor() || type.isInterface()) { if (type.isConstructor() || type.isInterface()) {
Expand Down Expand Up @@ -761,7 +761,7 @@ private JSType evalMaprecord(Node ttlAst, NameResolver nameResolver) {


private JSType evalTypeOfVar(Node ttlAst) { private JSType evalTypeOfVar(Node ttlAst) {
String name = getCallArgument(ttlAst, 0).getString(); String name = getCallArgument(ttlAst, 0).getString();
StaticTypedSlot<JSType> slot = typeEnv.getSlot(name); StaticTypedSlot slot = typeEnv.getSlot(name);
JSType type = slot != null ? slot.getType() : null; JSType type = slot != null ? slot.getType() : null;
if (type == null) { if (type == null) {
reportWarning(ttlAst, VAR_UNDEFINED, name); reportWarning(ttlAst, VAR_UNDEFINED, name);
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/TypeValidator.java
Expand Up @@ -787,7 +787,7 @@ void expectAllInterfaceProperties(NodeTraversal t, Node n,
*/ */
private void expectInterfaceProperty(NodeTraversal t, Node n, private void expectInterfaceProperty(NodeTraversal t, Node n,
ObjectType instance, ObjectType implementedInterface, String prop) { ObjectType instance, ObjectType implementedInterface, String prop) {
StaticTypedSlot<JSType> propSlot = instance.getSlot(prop); StaticTypedSlot propSlot = instance.getSlot(prop);
if (propSlot == null) { if (propSlot == null) {
// Not implemented // Not implemented
String sourceName = n.getSourceFileName(); String sourceName = n.getSourceFileName();
Expand Down
29 changes: 13 additions & 16 deletions src/com/google/javascript/jscomp/TypedScope.java
Expand Up @@ -27,24 +27,21 @@
import com.google.javascript.rhino.jstype.StaticTypedSlot; import com.google.javascript.rhino.jstype.StaticTypedSlot;


/** /**
* TypedScope contains information about variables and their types. * TypedScope contains information about variables and their types. Scopes can be nested, a scope
* Scopes can be nested, a scope points back to its parent scope. * points back to its parent scope.
* <p> *
* TypedScope is also used as a lattice element for flow-sensitive type inference. * <p>TypedScope is also used as a lattice element for flow-sensitive type inference. As a lattice
* As a lattice element, a scope is viewed as a map from names to types. A name * element, a scope is viewed as a map from names to types. A name not in the map is considered to
* not in the map is considered to have the bottom type. The join of two maps m1 * have the bottom type. The join of two maps m1 and m2 is the map of the union of names with {@link
* and m2 is the map of the union of names with {@link JSType#getLeastSupertype} * JSType#getLeastSupertype} to meet the m1 type and m2 type.
* to meet the m1 type and m2 type.
* *
* @see NodeTraversal * @see NodeTraversal
* @see DataFlowAnalysis * @see DataFlowAnalysis
* * <p>Several methods in this class, such as {@code isBlockScope} throw an exception when
* Several methods in this class, such as {@code isBlockScope} throw an exception when called. * called. The reason for this is that we want to shadow methods from the parent class, to avoid
* The reason for this is that we want to shadow methods from the parent class, to avoid calling * calling them accidentally.
* them accidentally.
*/ */
public class TypedScope extends AbstractScope<TypedScope, TypedVar> public class TypedScope extends AbstractScope<TypedScope, TypedVar> implements StaticTypedScope {
implements StaticTypedScope<JSType> {


private final TypedScope parent; private final TypedScope parent;
private final int depth; private final int depth;
Expand Down Expand Up @@ -202,12 +199,12 @@ void setTypeResolver(TypeResolver resolver) {
} }


public JSType getNamespaceOrTypedefType(String typeName) { public JSType getNamespaceOrTypedefType(String typeName) {
StaticTypedSlot<JSType> slot = getSlot(typeName); StaticTypedSlot slot = getSlot(typeName);
return slot == null ? null : slot.getType(); return slot == null ? null : slot.getType();
} }


public JSDocInfo getJsdocOfTypeDeclaration(String typeName) { public JSDocInfo getJsdocOfTypeDeclaration(String typeName) {
StaticTypedSlot<JSType> slot = getSlot(typeName); StaticTypedSlot slot = getSlot(typeName);
return slot == null ? null : slot.getJSDocInfo(); return slot == null ? null : slot.getJSDocInfo();
} }
} }
14 changes: 6 additions & 8 deletions src/com/google/javascript/jscomp/TypedVar.java
Expand Up @@ -25,16 +25,14 @@
/** /**
* {@link AbstractVar} subclass for use with {@link TypedScope}. * {@link AbstractVar} subclass for use with {@link TypedScope}.
* *
* <p>Note that this class inherits its {@link #equals} and {@link #hashCode} * <p>Note that this class inherits its {@link #equals} and {@link #hashCode} implementations from
* implementations from {@link ScopedName}, which does not include any type * {@link ScopedName}, which does not include any type information. This is necessary because {@code
* information. This is necessary because {@code Var}-keyed maps are used * Var}-keyed maps are used across multiple top scopes, but it comes with the caveat that if {@code
* across multiple top scopes, but it comes with the caveat that if {@code * TypedVar} instances are stored in a set, the type information is at risk of disappearing if an
* TypedVar} instances are stored in a set, the type information is at risk * untyped (or differently typed) var is added for the same symbol.
* of disappearing if an untyped (or differently typed) var is added for the
* same symbol.
*/ */
public class TypedVar extends AbstractVar<TypedScope, TypedVar> public class TypedVar extends AbstractVar<TypedScope, TypedVar>
implements StaticTypedSlot<JSType>, StaticTypedRef<JSType> { implements StaticTypedSlot, StaticTypedRef {


private JSType type; private JSType type;
// The next two fields and the associated methods are only used by // The next two fields and the associated methods are only used by
Expand Down
Expand Up @@ -118,7 +118,7 @@ protected FlowScope nextPreciserScopeKnowingConditionOutcome(Node condition,
protected JSType getTypeIfRefinable(Node node, FlowScope scope) { protected JSType getTypeIfRefinable(Node node, FlowScope scope) {
switch (node.getToken()) { switch (node.getToken()) {
case NAME: case NAME:
StaticTypedSlot<JSType> nameVar = scope.getSlot(node.getString()); StaticTypedSlot nameVar = scope.getSlot(node.getString());
if (nameVar != null) { if (nameVar != null) {
JSType nameVarType = nameVar.getType(); JSType nameVarType = nameVar.getType();
if (nameVarType == null) { if (nameVarType == null) {
Expand All @@ -133,7 +133,7 @@ protected JSType getTypeIfRefinable(Node node, FlowScope scope) {
if (qualifiedName == null) { if (qualifiedName == null) {
return null; return null;
} }
StaticTypedSlot<JSType> propVar = scope.getSlot(qualifiedName); StaticTypedSlot propVar = scope.getSlot(qualifiedName);
JSType propVarType = null; JSType propVarType = null;
if (propVar != null) { if (propVar != null) {
propVarType = propVar.getType(); propVarType = propVar.getType();
Expand Down

0 comments on commit 446d16f

Please sign in to comment.