Skip to content

Commit 683d3a4

Browse files
committed
Added sufficient support for wildcards so that everything compiles and existing unit tests run
1 parent c19a338 commit 683d3a4

File tree

16 files changed

+166
-1
lines changed

16 files changed

+166
-1
lines changed

CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/WildcardInTypeID.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
import java.util.List;
99

10+
/**
11+
* Represents a wildcard type with an upper bound. (e.g. ? super Number)
12+
*/
1013
public class WildcardInTypeID implements TypeID {
1114
public final TypeID upperBound;
1215

CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/WildcardOutTypeID.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88
import java.util.List;
99

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

JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaBoxingTypeVisitor.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,16 @@ public Void visitOptional(OptionalTypeID type) {
140140
//NO-OP
141141
return null;
142142
}
143+
144+
@Override
145+
public Void visitWildcardIn(WildcardInTypeID type) {
146+
//NO-OP
147+
return null;
148+
}
149+
150+
@Override
151+
public Void visitWildcardOut(WildcardOutTypeID type) {
152+
//NO-OP
153+
return null;
154+
}
143155
}

JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaTypeExpressionVisitor.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,16 @@ public Void visitRange(JavaWriter writer, RangeTypeID range) {
119119
public Void visitOptional(JavaWriter writer, OptionalTypeID type) {
120120
return type.baseType.accept(writer, this);
121121
}
122+
123+
@Override
124+
public Void visitWildcardIn(JavaWriter writer, WildcardInTypeID type) throws RuntimeException {
125+
writer.constant(Object.class);
126+
return null;
127+
}
128+
129+
@Override
130+
public Void visitWildcardOut(JavaWriter writer, WildcardOutTypeID type) throws RuntimeException {
131+
type.lowerBound.accept(writer, this);
132+
return null;
133+
}
122134
}

JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaUnboxingTypeVisitor.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,16 @@ public Void visitOptional(OptionalTypeID type) {
135135
//NO-OP
136136
return null;
137137
}
138+
139+
@Override
140+
public Void visitWildcardIn(WildcardInTypeID type) {
141+
//NO-OP
142+
return null;
143+
}
144+
145+
@Override
146+
public Void visitWildcardOut(WildcardOutTypeID type) {
147+
//NO-OP
148+
return null;
149+
}
138150
}

JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaSyntheticTypeSignatureConverter.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,14 @@ public String visitOptional(OptionalTypeID type) {
145145
result.append(type.baseType.accept(this));
146146
return result.toString();
147147
}
148+
149+
@Override
150+
public String visitWildcardIn(WildcardInTypeID type) {
151+
return "-" + type.upperBound.accept(this);
152+
}
153+
154+
@Override
155+
public String visitWildcardOut(WildcardOutTypeID type) {
156+
return "+" + type.lowerBound.accept(this);
157+
}
148158
}

JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaTypeCheckIfGenericVisitor.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,14 @@ public Boolean visitRange(RangeTypeID range) {
5656
public Boolean visitOptional(OptionalTypeID type) {
5757
return type.baseType.accept(this);
5858
}
59+
60+
@Override
61+
public Boolean visitWildcardIn(WildcardInTypeID type) {
62+
return true;
63+
}
64+
65+
@Override
66+
public Boolean visitWildcardOut(WildcardOutTypeID type) {
67+
return true;
68+
}
5969
}

JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaTypeDescriptorVisitor.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,16 @@ public String visitOptional(OptionalTypeID modified) {
152152
return modified.withoutOptional().accept(forOptional);
153153
}
154154

155+
@Override
156+
public String visitWildcardIn(WildcardInTypeID type) {
157+
return "Ljava/lang/Object;";
158+
}
159+
160+
@Override
161+
public String visitWildcardOut(WildcardOutTypeID type) {
162+
return type.accept(this);
163+
}
164+
155165
@Override
156166
public String visitGenericMap(GenericMapTypeID map) {
157167
return "Ljava/util/Map;";

JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaTypeGenericVisitor.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@ public String visitOptional(OptionalTypeID type) {
195195
return type.baseType.accept(this);
196196
}
197197

198+
@Override
199+
public String visitWildcardIn(WildcardInTypeID type) {
200+
return "Ljava/lang/Object;";
201+
}
202+
203+
@Override
204+
public String visitWildcardOut(WildcardOutTypeID type) {
205+
return type.lowerBound.accept(this);
206+
}
207+
198208
public String getMethodSignatureExpansion(FunctionHeader header, TypeID expandedClass) {
199209
final StringBuilder stringBuilder = new StringBuilder();
200210
final ArrayList<TypeParameter> typeParameters = new ArrayList<>();

JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaTypeInfo.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,15 @@ public JavaTypeInfo visitRange(TypeID context, RangeTypeID range) {
7979
public JavaTypeInfo visitOptional(TypeID context, OptionalTypeID type) {
8080
return type.baseType == BasicTypeID.USIZE ? PRIMITIVE : OBJECT;
8181
}
82+
83+
@Override
84+
public JavaTypeInfo visitWildcardIn(TypeID context, WildcardInTypeID type) throws RuntimeException {
85+
return OBJECT;
86+
}
87+
88+
@Override
89+
public JavaTypeInfo visitWildcardOut(TypeID context, WildcardOutTypeID type) throws RuntimeException {
90+
return OBJECT;
91+
}
8292
}
8393
}

0 commit comments

Comments
 (0)