Skip to content

Commit

Permalink
For non-generic functions, move from null to empty list.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=91439977
  • Loading branch information
blickly committed Apr 17, 2015
1 parent 250b14f commit aa3a912
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public final class DeclaredFunctionType {
private final NominalType nominalType;
// Non-null iff this is a prototype method
private final NominalType receiverType;
// Non-null iff this function has an @template annotation
// Non-empty iff this function has an @template annotation
private final ImmutableList<String> typeParameters;

private DeclaredFunctionType(
Expand Down Expand Up @@ -94,6 +94,9 @@ static DeclaredFunctionType make(
if (optionalFormals == null) {
optionalFormals = new ArrayList<>();
}
if (typeParameters == null) {
typeParameters = ImmutableList.of();
}
return new DeclaredFunctionType(
requiredFormals, optionalFormals, restFormals, retType,
nominalType, receiverType, typeParameters);
Expand Down Expand Up @@ -145,15 +148,15 @@ public NominalType getReceiverType() {
}

public boolean isGeneric() {
return typeParameters != null;
return !typeParameters.isEmpty();
}

public ImmutableList<String> getTypeParameters() {
return typeParameters;
}

public boolean isTypeVariableInScope(String tvar) {
if (typeParameters != null && typeParameters.contains(tvar)) {
if (typeParameters.contains(tvar)) {
return true;
}
// We don't look at this.nominalType, b/c if this function is a generic
Expand Down
66 changes: 33 additions & 33 deletions src/com/google/javascript/jscomp/newtypes/FunctionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public final class FunctionType {
final NominalType nominalType;
// non-null iff this is a prototype method
private final NominalType receiverType;
// non-null iff this function has an @template annotation
// non-empty iff this function has an @template annotation
private final ImmutableList<String> typeParameters;
private static final boolean DEBUGGING = false;

Expand Down Expand Up @@ -80,7 +80,7 @@ private FunctionType(boolean isLoose) {
this.nominalType = null;
this.receiverType = null;
this.outerVarPreconditions = null;
this.typeParameters = null;
this.typeParameters = ImmutableList.of();
this.isLoose = isLoose;
}

Expand Down Expand Up @@ -136,6 +136,9 @@ static FunctionType normalized(
if (outerVars == null) {
outerVars = ImmutableMap.of();
}
if (typeParameters == null) {
typeParameters = ImmutableList.of();
}
if (restFormals != null) {
// Remove trailing optional params w/ type equal to restFormals
for (int i = optionalFormals.size() - 1; i >= 0; i--) {
Expand Down Expand Up @@ -304,7 +307,7 @@ public DeclaredFunctionType toDeclaredFunctionType() {
}
Preconditions.checkState(!isLoose());
// Don't do it for generic types.
if (typeParameters != null) {
if (isGeneric()) {
return null;
}
// Don't do it for anonymous constructors
Expand Down Expand Up @@ -587,7 +590,7 @@ boolean isLooseSubtypeOf(FunctionType f2) {
}

public boolean isGeneric() {
return typeParameters != null;
return !typeParameters.isEmpty();
}

public List<String> getTypeParameters() {
Expand All @@ -596,7 +599,7 @@ public List<String> getTypeParameters() {

boolean unifyWithSubtype(FunctionType other, List<String> typeParameters,
Multimap<String, JSType> typeMultimap) {
Preconditions.checkState(this.typeParameters == null);
Preconditions.checkState(this.typeParameters.isEmpty());
Preconditions.checkState(this.outerVarPreconditions.isEmpty());

if (this == LOOSE_TOP_FUNCTION || other == LOOSE_TOP_FUNCTION) {
Expand Down Expand Up @@ -680,8 +683,8 @@ static FunctionType unifyUnknowns(FunctionType f1, FunctionType f2) {
if (f1 == null || f2 == null) {
return null;
}
Preconditions.checkArgument(f1.typeParameters == null);
Preconditions.checkArgument(f2.typeParameters == null);
Preconditions.checkArgument(f1.typeParameters.isEmpty());
Preconditions.checkArgument(f2.typeParameters.isEmpty());
Preconditions.checkArgument(f1.outerVarPreconditions.isEmpty());
Preconditions.checkArgument(f2.outerVarPreconditions.isEmpty());
if (f1.equals(f2)) {
Expand Down Expand Up @@ -754,23 +757,21 @@ private FunctionType substituteNominalGenerics(Map<String, JSType> typeMap) {
return this;
}
Map<String, JSType> reducedMap = typeMap;
if (typeParameters != null) {
boolean foundShadowedTypeParam = false;
for (String typeParam : typeParameters) {
if (typeMap.containsKey(typeParam)) {
foundShadowedTypeParam = true;
break;
}
boolean foundShadowedTypeParam = false;
for (String typeParam : typeParameters) {
if (typeMap.containsKey(typeParam)) {
foundShadowedTypeParam = true;
break;
}
if (foundShadowedTypeParam) {
ImmutableMap.Builder<String, JSType> builder = ImmutableMap.builder();
for (Map.Entry<String, JSType> entry : typeMap.entrySet()) {
if (!typeParameters.contains(entry.getKey())) {
builder.put(entry);
}
}
if (foundShadowedTypeParam) {
ImmutableMap.Builder<String, JSType> builder = ImmutableMap.builder();
for (Map.Entry<String, JSType> entry : typeMap.entrySet()) {
if (!typeParameters.contains(entry.getKey())) {
builder.put(entry);
}
reducedMap = builder.build();
}
reducedMap = builder.build();
}
FunctionTypeBuilder builder = new FunctionTypeBuilder();
for (JSType reqFormal : requiredFormals) {
Expand Down Expand Up @@ -853,28 +854,27 @@ private FunctionType substituteParametricGenerics(
*/
FunctionType substituteGenerics(Map<String, JSType> concreteTypes) {
Preconditions.checkState(outerVarPreconditions.isEmpty());
Map<String, JSType> typeMap = concreteTypes;
if (typeParameters != null) {
ImmutableMap.Builder<String, JSType> builder = ImmutableMap.builder();
for (Map.Entry<String, JSType> concreteTypeEntry
: concreteTypes.entrySet()) {
if (!typeParameters.contains(concreteTypeEntry.getKey())) {
builder.put(concreteTypeEntry);
}
if (!isGeneric()) {
return substituteNominalGenerics(concreteTypes);
}
ImmutableMap.Builder<String, JSType> builder = ImmutableMap.builder();
for (Map.Entry<String, JSType> concreteTypeEntry
: concreteTypes.entrySet()) {
if (!typeParameters.contains(concreteTypeEntry.getKey())) {
builder.put(concreteTypeEntry);
}
typeMap = builder.build();
}
return substituteNominalGenerics(typeMap);
return substituteNominalGenerics(builder.build());
}

public FunctionType instantiateGenerics(Map<String, JSType> typeMap) {
Preconditions.checkNotNull(typeParameters);
Preconditions.checkState(isGeneric());
return substituteParametricGenerics(typeMap);
}

public FunctionType instantiateGenericsFromArgumentTypes(
List<JSType> argTypes) {
Preconditions.checkNotNull(typeParameters);
Preconditions.checkState(isGeneric());
if (argTypes.size() < getMinArity() || argTypes.size() > getMaxArity()) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ static class WrongParameterOrderException extends RuntimeException {
private NominalType nominalType;
// Only used to build DeclaredFunctionType for prototype methods
private NominalType receiverType;
// Non-null iff this function has an @template annotation
private ImmutableList<String> typeParameters;
// Non-empty iff this function has an @template annotation
private ImmutableList<String> typeParameters = ImmutableList.of();

static FunctionTypeBuilder qmarkFunctionBuilder() {
FunctionTypeBuilder builder = new FunctionTypeBuilder();
Expand Down Expand Up @@ -117,7 +117,8 @@ public FunctionTypeBuilder addNominalType(NominalType cl) {

public FunctionTypeBuilder addTypeParameters(
ImmutableList<String> typeParameters) {
Preconditions.checkState(this.typeParameters == null);
Preconditions.checkNotNull(typeParameters);
Preconditions.checkState(this.typeParameters.isEmpty());
this.typeParameters = typeParameters;
return this;
}
Expand Down

0 comments on commit aa3a912

Please sign in to comment.