Skip to content

Commit

Permalink
Initial push
Browse files Browse the repository at this point in the history
  • Loading branch information
mcimadamore committed Apr 26, 2024
1 parent 89a551a commit 1309ab7
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,7 @@ public Body loopBody() {

@Override
public Block.Builder lower(Block.Builder b, OpTransformer opT) {
JavaType elementType = (JavaType) init.entryBlock().parameters().get(0).type();
ClassType elementType = (ClassType) init.entryBlock().parameters().get(0).type();
boolean isArray = expression.bodyType().returnType() instanceof ArrayType;

Block.Builder preHeader = b.block(expression.bodyType().returnType());
Expand Down Expand Up @@ -2398,15 +2398,15 @@ final class Record<T> implements Pattern {
"$" + Pattern.Record.class.getSimpleName()));

static JavaType bindingType(TypeElement t) {
return type(PATTERN_BINDING_TYPE, (JavaType) t);
return type(PATTERN_BINDING_TYPE, (JavaType.Argument) t);
}

static JavaType recordType(TypeElement t) {
return type(PATTERN_RECORD_TYPE, (JavaType) t);
return type(PATTERN_RECORD_TYPE, (JavaType.Argument) t);
}

static TypeElement targetType(TypeElement t) {
return ((ClassType) t).typeArguments().get(0);
return (TypeElement)((ClassType) t).typeArguments().get(0);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
/**
* An array type.
*/
public final class ArrayType implements JavaType {
public final class ArrayType implements JavaType, JavaType.Argument {
static final String NAME = "[";

final JavaType componentType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,22 @@

package java.lang.reflect.code.type;

import java.lang.reflect.code.TypeElement;
import java.util.List;
import java.util.Map;

/**
* A class type.
*/
public final class ClassType implements JavaType {
public final class ClassType implements JavaType, JavaType.Argument {
// Fully qualified name
private final String type;

private final List<JavaType> typeArguments;
private final List<Argument> typeArguments;

ClassType(String type) {
this(type, List.of());
}

ClassType(String type, List<JavaType> typeArguments) {
ClassType(String type, List<JavaType.Argument> typeArguments) {
switch (type) {
case "boolean", "char", "byte", "short", "int", "long",
"float", "double", "void" -> throw new IllegalArgumentException();
Expand All @@ -54,13 +52,20 @@ public final class ClassType implements JavaType {
@Override
public TypeDefinition toTypeDefinition() {
List<TypeDefinition> args = typeArguments.stream()
.map(TypeElement::toTypeDefinition)
.map(this::typeArgumentToDefinition)
.toList();

TypeDefinition td = new TypeDefinition(type, args);
return td;
}

private TypeDefinition typeArgumentToDefinition(Argument targ) {
return switch (targ) {
case JavaType jt -> jt.toTypeDefinition();
case WildcardTypeArgument wta -> wta.toTypeDefinition();
};
}

@Override
public String toString() {
return toTypeDefinition().toString();
Expand Down Expand Up @@ -114,7 +119,7 @@ public boolean hasTypeArguments() {
return !typeArguments.isEmpty();
}

public List<JavaType> typeArguments() {
public List<JavaType.Argument> typeArguments() {
return typeArguments;
}

Expand Down Expand Up @@ -147,4 +152,5 @@ static String toBytecodeDescriptor(String type) {
static String toClassDescriptor(String type) {
return type.replace('.', '/');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.lang.constant.ClassDesc;
import java.lang.reflect.code.TypeElement;
import java.lang.reflect.code.type.WildcardType.BoundKind;
import java.lang.reflect.code.type.WildcardTypeArgument.BoundKind;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -107,20 +107,26 @@ public TypeElement constructType(TypeDefinition tree) {
identifier = tree.identifier();
}

List<JavaType> typeArguments = new ArrayList<>(tree.arguments().size());
List<JavaType.Argument> typeArguments = new ArrayList<>(tree.arguments().size());
for (TypeDefinition child : tree.arguments()) {
TypeElement t = JAVA_TYPE_FACTORY.constructType(child);
if (!(t instanceof JavaType a)) {
throw new IllegalArgumentException("Bad type: " + tree);
if (child.identifier().equals("+") || child.identifier().equals("-")) {
// wildcard type argument
TypeElement bound = JAVA_TYPE_FACTORY.constructType(child.arguments().get(0));
if (!(bound instanceof JavaType javaBound)) {
throw new IllegalArgumentException("Bad type: " + tree);
}
WildcardTypeArgument.BoundKind kind = identifier.equals("+") ?
BoundKind.EXTENDS : BoundKind.SUPER;
typeArguments.add(JavaType.wildcard(kind, javaBound));
} else {
TypeElement t = JAVA_TYPE_FACTORY.constructType(child);
if (!(t instanceof JavaType.Argument a)) {
throw new IllegalArgumentException("Bad type: " + tree);
}
typeArguments.add(a);
}
typeArguments.add(a);
}
if (identifier.equals("+") || identifier.equals("-")) {
// wildcard type
BoundKind kind = identifier.equals("+") ?
BoundKind.EXTENDS : BoundKind.SUPER;
return JavaType.wildcard(kind, typeArguments.get(0));
} else if (identifier.contains("::")) {
if (identifier.contains("::")) {
// type-var
if (typeArguments.size() != 1) {
throw new IllegalArgumentException("Bad type-variable bounds: " + tree);
Expand All @@ -130,12 +136,12 @@ public TypeElement constructType(TypeDefinition tree) {
// class type-var
return JavaType.typeVarRef(parts[1],
(JavaType)constructType(parseTypeDef(parts[0])),
typeArguments.get(0));
(JavaType)typeArguments.get(0));
} else {
// method type-var
return JavaType.typeVarRef(parts[2],
parseMethodRef(String.format("%s::%s", parts[0], parts[1])),
typeArguments.get(0));
(JavaType)typeArguments.get(0));
}
}
JavaType t = switch (identifier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.lang.constant.ClassDesc;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.code.TypeElement;
import java.lang.reflect.code.type.WildcardType.BoundKind;
import java.lang.reflect.code.type.WildcardTypeArgument.BoundKind;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -38,7 +38,7 @@
*/
// @@@ Extend from this interface to model Java types with more fidelity
public sealed interface JavaType extends TypeElement permits ClassType, ArrayType,
PrimitiveType, WildcardType, TypeVarRef {
PrimitiveType, WildcardTypeArgument, TypeVarRef {

// @@@ Share with general void type?
JavaType VOID = new PrimitiveType("void");
Expand Down Expand Up @@ -166,6 +166,7 @@ static JavaType type(Class<?> c) {
}
}

// @@@: these factories should work in terms of j.l.r.Type for the type arguments?
static JavaType type(Class<?> c, Class<?>... typeArguments) {
return type(c, List.of(typeArguments));
}
Expand All @@ -177,7 +178,7 @@ static JavaType type(Class<?> c, List<Class<?>> typeArguments) {
return array(type(c.getComponentType(), typeArguments));
} else {
return new ClassType(c.getName(),
typeArguments.stream().map(JavaType::type).toList());
typeArguments.stream().map(t -> (Argument)JavaType.type(t)).toList());
}
}

Expand Down Expand Up @@ -213,11 +214,11 @@ private static JavaType ofNominalDescriptorStringInternal(String descriptor, int
}
}

static JavaType type(JavaType t, JavaType... typeArguments) {
static JavaType type(JavaType t, Argument... typeArguments) {
return type(t, List.of(typeArguments));
}

static JavaType type(JavaType t, List<JavaType> typeArguments) {
static JavaType type(JavaType t, List<Argument> typeArguments) {
if (t.isPrimitive()) {
throw new IllegalArgumentException("Cannot parameterize a primitive type");
} else if (t.isArray()) {
Expand Down Expand Up @@ -266,17 +267,17 @@ static ArrayType array(JavaType elementType, int dims) {
*
* @return an unbounded wildcard type.
*/
static WildcardType wildcard() {
return new WildcardType(BoundKind.EXTENDS, JavaType.J_L_OBJECT);
static WildcardTypeArgument wildcard() {
return new WildcardTypeArgument(BoundKind.EXTENDS, JavaType.J_L_OBJECT);
}

/**
* Constructs a bounded wildcard type of the given kind.
*
* @return a bounded wildcard type.
*/
static WildcardType wildcard(BoundKind kind, JavaType bound) {
return new WildcardType(kind, bound);
static WildcardTypeArgument wildcard(BoundKind kind, JavaType bound) {
return new WildcardTypeArgument(kind, bound);
}

/**
Expand Down Expand Up @@ -305,4 +306,10 @@ static TypeVarRef typeVarRef(String name, MethodRef owner, JavaType bound) {
static JavaType ofString(String s) {
/*__throw new UnsupportedOperationException();__*/ return (JavaType) CoreTypeFactory.JAVA_TYPE_FACTORY.constructType(java.lang.reflect.code.parser.impl.DescParser.parseTypeDefinition(s));
}

/**
* A marker interface for type-arguments in a parameterized type.
*/
sealed interface Argument permits ClassType, ArrayType, TypeVarRef, WildcardTypeArgument {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
/**
* A type-variable reference.
*/
public final class TypeVarRef implements JavaType {
public final class TypeVarRef implements JavaType, JavaType.Argument {

final String name;
final Object owner;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
/**
* A wildcard type.
*/
public final class WildcardType implements JavaType {
public final class WildcardTypeArgument implements JavaType.Argument {

final BoundKind kind;
final JavaType boundType;

WildcardType(BoundKind kind, JavaType boundType) {
WildcardTypeArgument(BoundKind kind, JavaType boundType) {
this.kind = kind;
this.boundType = boundType;
}
Expand All @@ -55,8 +55,7 @@ public BoundKind boundKind() {
return kind;
}

@Override
public TypeDefinition toTypeDefinition() {
/* package */ TypeDefinition toTypeDefinition() {
String prefix = kind == BoundKind.EXTENDS ? "+" : "-";
return new TypeDefinition(prefix, List.of(boundType.toTypeDefinition()));
}
Expand All @@ -69,7 +68,7 @@ public String toString() {
@Override
public boolean equals(Object o) {
if (this == o) return true;
return o instanceof WildcardType that &&
return o instanceof WildcardTypeArgument that &&
kind.equals(that.kind) &&
boundType.equals(that.boundType);
}
Expand All @@ -79,36 +78,6 @@ public int hashCode() {
return Objects.hash(boundType, kind);
}

@Override
public JavaType erasure() {
throw new UnsupportedOperationException("Wildcard type");
}

@Override
public JavaType toBasicType() {
throw new UnsupportedOperationException("Wildcard type");
}

@Override
public String toNominalDescriptorString() {
throw new UnsupportedOperationException("Wildcard type");
}

@Override
public boolean isClass() {
return false;
}

@Override
public boolean isArray() {
return false;
}

@Override
public boolean isPrimitive() {
return false;
}

public enum BoundKind {
EXTENDS,
SUPER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ Value buildAttributeValue(Object value) {


Value buildMap(JavaType keyType, JavaType valueType, List<Value> keysAndValues) {
JavaType mapType = type(J_U_MAP, keyType, valueType);
JavaType mapType = type(J_U_MAP, (JavaType.Argument)keyType, (JavaType.Argument)valueType);
if (keysAndValues.isEmpty()) {
return builder.op(invoke(MAP_OF));
} else {
Expand All @@ -345,8 +345,8 @@ Value buildMap(JavaType keyType, JavaType valueType, List<Value> keysAndValues)
}


Value buildList(JavaType elementType, List<Value> elements) {
JavaType listType = type(J_U_LIST, elementType);
Value buildList(JavaType.Argument elementType, List<Value> elements) {
JavaType listType = type(J_U_LIST, (JavaType.Argument)elementType);
if (elements.size() < 11) {
MethodRef listOf = MethodRef.method(J_U_LIST, "of",
J_U_LIST, Collections.nCopies(elements.size(), J_L_OBJECT));
Expand Down
Loading

0 comments on commit 1309ab7

Please sign in to comment.