Skip to content

Commit

Permalink
Added sufficient support for wildcards so that everything compiles an…
Browse files Browse the repository at this point in the history
…d existing unit tests run
  • Loading branch information
stanhebben committed Feb 28, 2025
1 parent 7778b45 commit 3df6cf0
Show file tree
Hide file tree
Showing 16 changed files with 166 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import java.util.List;

/**
* Represents a wildcard type with an upper bound. (e.g. ? super Number)
*/
public class WildcardInTypeID implements TypeID {
public final TypeID upperBound;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

import java.util.List;

/**
* Represents a wildcard type with an upper bound. (eg. ? extends Number)
* <p>
* This is used in the context of type arguments, where the type argument is not known, but it is known that it is a
* subtype of the given bound.
*/
public class WildcardOutTypeID implements TypeID {
public final TypeID lowerBound;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,16 @@ public Void visitOptional(OptionalTypeID type) {
//NO-OP
return null;
}

@Override
public Void visitWildcardIn(WildcardInTypeID type) {
//NO-OP
return null;
}

@Override
public Void visitWildcardOut(WildcardOutTypeID type) {
//NO-OP
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,16 @@ public Void visitRange(JavaWriter writer, RangeTypeID range) {
public Void visitOptional(JavaWriter writer, OptionalTypeID type) {
return type.baseType.accept(writer, this);
}

@Override
public Void visitWildcardIn(JavaWriter writer, WildcardInTypeID type) throws RuntimeException {
writer.constant(Object.class);
return null;
}

@Override
public Void visitWildcardOut(JavaWriter writer, WildcardOutTypeID type) throws RuntimeException {
type.lowerBound.accept(writer, this);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,16 @@ public Void visitOptional(OptionalTypeID type) {
//NO-OP
return null;
}

@Override
public Void visitWildcardIn(WildcardInTypeID type) {
//NO-OP
return null;
}

@Override
public Void visitWildcardOut(WildcardOutTypeID type) {
//NO-OP
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,14 @@ public String visitOptional(OptionalTypeID type) {
result.append(type.baseType.accept(this));
return result.toString();
}

@Override
public String visitWildcardIn(WildcardInTypeID type) {
return "-" + type.upperBound.accept(this);
}

@Override
public String visitWildcardOut(WildcardOutTypeID type) {
return "+" + type.lowerBound.accept(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,14 @@ public Boolean visitRange(RangeTypeID range) {
public Boolean visitOptional(OptionalTypeID type) {
return type.baseType.accept(this);
}

@Override
public Boolean visitWildcardIn(WildcardInTypeID type) {
return true;
}

@Override
public Boolean visitWildcardOut(WildcardOutTypeID type) {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ public String visitOptional(OptionalTypeID modified) {
return modified.withoutOptional().accept(forOptional);
}

@Override
public String visitWildcardIn(WildcardInTypeID type) {
return "Ljava/lang/Object;";
}

@Override
public String visitWildcardOut(WildcardOutTypeID type) {
return type.accept(this);
}

@Override
public String visitGenericMap(GenericMapTypeID map) {
return "Ljava/util/Map;";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@ public String visitOptional(OptionalTypeID type) {
return type.baseType.accept(this);
}

@Override
public String visitWildcardIn(WildcardInTypeID type) {
return "Ljava/lang/Object;";
}

@Override
public String visitWildcardOut(WildcardOutTypeID type) {
return type.lowerBound.accept(this);
}

public String getMethodSignatureExpansion(FunctionHeader header, TypeID expandedClass) {
final StringBuilder stringBuilder = new StringBuilder();
final ArrayList<TypeParameter> typeParameters = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,15 @@ public JavaTypeInfo visitRange(TypeID context, RangeTypeID range) {
public JavaTypeInfo visitOptional(TypeID context, OptionalTypeID type) {
return type.baseType == BasicTypeID.USIZE ? PRIMITIVE : OBJECT;
}

@Override
public JavaTypeInfo visitWildcardIn(TypeID context, WildcardInTypeID type) throws RuntimeException {
return OBJECT;
}

@Override
public JavaTypeInfo visitWildcardOut(TypeID context, WildcardOutTypeID type) throws RuntimeException {
return OBJECT;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ public String visitOptional(OptionalTypeID modified) {
return modified.baseType.accept(this);
}

@Override
public String visitWildcardIn(WildcardInTypeID type) {
return "java/lang/Object";
}

@Override
public String visitWildcardOut(WildcardOutTypeID type) {
return "java/lang/Object";
}

@Override
public String visitGenericMap(GenericMapTypeID map) {
return "java/util/Map";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,14 @@ public String visitOptional(OptionalTypeID type) {
result.append(type.baseType.accept(this));
return result.toString();
}

@Override
public String visitWildcardIn(WildcardInTypeID type) {
return "In" + type.upperBound.accept(this);
}

@Override
public String visitWildcardOut(WildcardOutTypeID type) {
return "Out" + type.lowerBound.accept(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,16 @@ public Void visitOptional(OptionalTypeID type) {
type.baseType.accept(this);
return null;
}

@Override
public Void visitWildcardIn(WildcardInTypeID type) {
type.upperBound.accept(this);
return null;
}

@Override
public Void visitWildcardOut(WildcardOutTypeID type) {
type.lowerBound.accept(this);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static List<IParsedType> parseTypeArguments(ZSTokenParser tokens) throws ParseEx

List<IParsedType> genericParameters = new ArrayList<>();
do {
IParsedType type = tryParse(tokens);
IParsedType type = tryParse(tokens, true);
if (type == null) {
tokens.reset();
return Collections.emptyList();
Expand All @@ -61,6 +61,10 @@ static List<IParsedType> parseTypeArguments(ZSTokenParser tokens) throws ParseEx
}

static IParsedType tryParse(ZSTokenParser tokens) throws ParseException {
return tryParse(tokens, false);
}

static IParsedType tryParse(ZSTokenParser tokens, boolean allowWildcards) throws ParseException {
CodePosition position = tokens.getPosition();

IParsedType result;
Expand Down Expand Up @@ -139,12 +143,22 @@ static IParsedType tryParse(ZSTokenParser tokens) throws ParseException {
}
case K_IN: {
tokens.next();

// TODO: should this be moved to the validator?
if (!allowWildcards)
throw new ParseException(tokens.getPosition(), "wildcards are not allowed here");

IParsedType upperBound = tryParse(tokens);
result = new ParsedWildcardInType(upperBound);
break;
}
case K_OUT: {
tokens.next();

// TODO: should this be moved to the validator?
if (!allowWildcards)
throw new ParseException(tokens.getPosition(), "wildcards are not allowed here");

IParsedType lowerBound = tryParse(tokens);
result = new ParsedWildcardOutType(lowerBound);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ public Void visitOptional(OptionalTypeID type) {
return null;
}

@Override
public Void visitWildcardIn(WildcardInTypeID type) {
validator.logError(position, CompileErrors.invalidSuperclass(type));
return null;
}

@Override
public Void visitWildcardOut(WildcardOutTypeID type) {
validator.logError(position, CompileErrors.invalidSuperclass(type));
return null;
}

@Override
public Void visitGenericMap(GenericMapTypeID map) {
validator.logError(position, CompileErrors.invalidSuperclass(map));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ public Void visitOptional(TypeContext context, OptionalTypeID type) {
return null;
}

@Override
public Void visitWildcardOut(TypeContext context, WildcardOutTypeID type) throws RuntimeException {
validate(context, type.lowerBound);
return null;
}

@Override
public Void visitWildcardIn(TypeContext context, WildcardInTypeID type) throws RuntimeException {
validate(context, type.upperBound);
return null;
}

@Override
public Void visitGenericMap(TypeContext context, GenericMapTypeID map) {
validate(context, map.value);
Expand Down

0 comments on commit 3df6cf0

Please sign in to comment.