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
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@
import javax.annotation.Nullable;

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

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

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

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

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

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

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

/**
* A global name reference. Contains references to the relevant parse tree
* node and its ancestors that may be affected.
* A global name reference. Contains references to the relevant parse tree node and its ancestors
* 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
// declarations than implied here, see Name#canCollapse
Expand Down Expand Up @@ -1563,7 +1561,7 @@ public StaticSourceFile getSourceFile() {
}

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

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

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

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

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

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

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

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

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

/** 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 JSType type;
final LinkedFlowSlot parent;
Expand All @@ -459,7 +456,7 @@ public boolean isTypeInferred() {
}

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

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

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

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

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

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

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

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

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

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

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

private boolean isTypeInferred(StaticSlot sym) {
if (sym instanceof StaticTypedSlot) {
return ((StaticTypedSlot<JSType>) sym).isTypeInferred();
return ((StaticTypedSlot) sym).isTypeInferred();
}
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/TypeInference.java
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ private FlowScope traverseName(Node n, FlowScope scope) {
updateScopeForTypeChange(scope, n, type, resultType);
type = resultType;
} else {
StaticTypedSlot<JSType> var = scope.getSlot(varName);
StaticTypedSlot var = scope.getSlot(varName);
if (var != null) {
// There are two situations where we don't want to use type information
// 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.
String qualifiedName = n.getQualifiedName();
StaticTypedSlot<JSType> var = qualifiedName != null ? scope.getSlot(qualifiedName) : null;
StaticTypedSlot var = qualifiedName != null ? scope.getSlot(qualifiedName) : null;
if (var != null) {
JSType varType = var.getType();
if (varType != null) {
Expand Down
8 changes: 4 additions & 4 deletions src/com/google/javascript/jscomp/TypeTransformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class TypeTransformation {

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

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

@SuppressWarnings("unchecked")
TypeTransformation(AbstractCompiler compiler, StaticTypedScope<JSType> typeEnv) {
TypeTransformation(AbstractCompiler compiler, StaticTypedScope typeEnv) {
this.compiler = compiler;
this.registry = compiler.getTypeRegistry();
this.typeEnv = typeEnv;
Expand All @@ -138,7 +138,7 @@ private JSType getType(String typeName) {
if (type != null) {
return type;
}
StaticTypedSlot<JSType> slot = typeEnv.getSlot(typeName);
StaticTypedSlot slot = typeEnv.getSlot(typeName);
type = slot != null ? slot.getType() : null;
if (type != null) {
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) {
String name = getCallArgument(ttlAst, 0).getString();
StaticTypedSlot<JSType> slot = typeEnv.getSlot(name);
StaticTypedSlot slot = typeEnv.getSlot(name);
JSType type = slot != null ? slot.getType() : null;
if (type == null) {
reportWarning(ttlAst, VAR_UNDEFINED, name);
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/TypeValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ void expectAllInterfaceProperties(NodeTraversal t, Node n,
*/
private void expectInterfaceProperty(NodeTraversal t, Node n,
ObjectType instance, ObjectType implementedInterface, String prop) {
StaticTypedSlot<JSType> propSlot = instance.getSlot(prop);
StaticTypedSlot propSlot = instance.getSlot(prop);
if (propSlot == null) {
// Not implemented
String sourceName = n.getSourceFileName();
Expand Down
29 changes: 13 additions & 16 deletions src/com/google/javascript/jscomp/TypedScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,21 @@
import com.google.javascript.rhino.jstype.StaticTypedSlot;

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

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

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

public JSDocInfo getJsdocOfTypeDeclaration(String typeName) {
StaticTypedSlot<JSType> slot = getSlot(typeName);
StaticTypedSlot slot = getSlot(typeName);
return slot == null ? null : slot.getJSDocInfo();
}
}
14 changes: 6 additions & 8 deletions src/com/google/javascript/jscomp/TypedVar.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@
/**
* {@link AbstractVar} subclass for use with {@link TypedScope}.
*
* <p>Note that this class inherits its {@link #equals} and {@link #hashCode}
* implementations from {@link ScopedName}, which does not include any type
* information. This is necessary because {@code Var}-keyed maps are used
* 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 untyped (or differently typed) var is added for the
* same symbol.
* <p>Note that this class inherits its {@link #equals} and {@link #hashCode} implementations from
* {@link ScopedName}, which does not include any type information. This is necessary because {@code
* Var}-keyed maps are used 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
* untyped (or differently typed) var is added for the same symbol.
*/
public class TypedVar extends AbstractVar<TypedScope, TypedVar>
implements StaticTypedSlot<JSType>, StaticTypedRef<JSType> {
implements StaticTypedSlot, StaticTypedRef {

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

0 comments on commit 446d16f

Please sign in to comment.