Skip to content

Commit

Permalink
Use TypeName in the generator, too
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadelessFox committed Sep 24, 2024
1 parent 02a96d2 commit 8244db7
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ private AttributeInfo(
) {
this.name = name;
this.category = category;
this.typeName = TypeName.of(typeName);
this.typeName = TypeName.parse(typeName);
this.type = type;
this.getter = getter;
this.setter = setter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,29 @@
public sealed interface TypeName {
@NotNull
static TypeName of(@NotNull String name) {
return new Simple(name);
}

@NotNull
static TypeName of(@NotNull String name, TypeName argument) {
return new Parameterized(name, argument);
}

@NotNull
static TypeName parse(@NotNull String name) {
int start = name.indexOf('<');
if (start < 0) {
return new Simple(name);
return of(name);
}
int end = name.lastIndexOf('>');
if (start == 0 || end < start + 1) {
throw new IllegalArgumentException("Invalid template name: '" + name + "'");
throw new IllegalArgumentException("Invalid parameterized name: '" + name + "'");
}
String rawType = name.substring(0, start);
String argumentType = name.substring(start + 1, end);
return new Parameterized(rawType, of(argumentType));
return of(rawType, parse(argumentType));
}

@NotNull
String name();

@NotNull
String fullName();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.shade.decima.rtti.generator;

import com.shade.decima.rtti.TypeName;
import com.shade.decima.rtti.generator.data.ClassTypeInfo;
import com.shade.decima.rtti.generator.data.TypeInfo;
import com.shade.util.NotNull;
Expand Down Expand Up @@ -102,7 +103,8 @@ public SourceVersion getSupportedSourceVersion() {
private void reportMissingCallbacks(@NotNull TypeContext context, @NotNull GenerateBindings annotation) {
Set<String> typesMissingCallback = context.types().stream()
.filter(info -> info instanceof ClassTypeInfo cls && cls.messages().contains("MsgReadBinary"))
.map(TypeInfo::fullName)
.map(TypeInfo::typeName)
.map(TypeName::fullName)
.collect(Collectors.toSet());

for (GenerateBindings.Callback callback : annotation.callbacks()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.shade.decima.rtti.TypeName;
import com.shade.decima.rtti.generator.data.*;
import com.shade.util.NotNull;

Expand All @@ -13,18 +14,18 @@
import java.util.*;

public class TypeContext {
private final Map<String, TypeInfo> types = new TreeMap<>(String::compareToIgnoreCase);
private final Map<TypeName, TypeInfo> types = new TreeMap<>(Comparator.comparing(TypeName::fullName, String::compareToIgnoreCase));

public void load(@NotNull Path path) throws IOException {
try (BufferedReader reader = Files.newBufferedReader(path)) {
JsonObject root = JsonParser.parseReader(reader).getAsJsonObject();

Resolver resolver = new Resolver() {
private final Map<String, FutureRef> pending = new HashMap<>();
private final Map<TypeName, FutureRef> pending = new HashMap<>();

@NotNull
@Override
public TypeInfoRef get(@NotNull String name) {
public TypeInfoRef get(@NotNull TypeName name) {
if (pending.containsKey(name)) {
return pending.get(name);
}
Expand All @@ -36,30 +37,24 @@ public TypeInfoRef get(@NotNull String name) {

pending.put(name, new FutureRef(name));

if (name.indexOf('<') >= 0) {
int templateStart = name.indexOf('<');
int templateEnd = name.lastIndexOf('>');
if (templateEnd < templateStart + 1) {
throw new IllegalArgumentException("Invalid template name: '" + name + "'");
}

String rawType = name.substring(0, templateStart);
String argumentType = name.substring(templateStart + 1, templateEnd);

return resolve(get(rawType, argumentType));
if (name instanceof TypeName.Parameterized parameterized) {
return resolve(getParameterizedType(parameterized.name(), parameterized.argument()));
} else {
JsonObject info = root.getAsJsonObject(name);
if (info == null) {
throw new IllegalArgumentException("Unknown type: " + name);
}
return resolve(getSimpleType(name.fullName()));
}
}

return resolve(loadSingleType(name, info, this));
@NotNull
private TypeInfo getSimpleType(@NotNull String name) {
JsonObject info = root.getAsJsonObject(name);
if (info == null) {
throw new IllegalArgumentException("Unknown type: " + name);
}
return loadSingleType(name, info, this);
}

@NotNull
@Override
public TypeInfo get(@NotNull String name, @NotNull String argument) {
private TypeInfo getParameterizedType(@NotNull String name, @NotNull TypeName argument) {
return switch (name) {
case "Array", "TinyArray" -> new ContainerTypeInfo(name, get(argument));
case "Ref", "cptr" -> new PointerTypeInfo(name, get(argument));
Expand All @@ -69,12 +64,12 @@ public TypeInfo get(@NotNull String name, @NotNull String argument) {

@NotNull
private TypeInfoRef resolve(@NotNull TypeInfo info) {
FutureRef ref = pending.remove(info.fullName());
FutureRef ref = pending.remove(info.typeName());
if (ref == null) {
throw new IllegalStateException("Type was not present in the queue: " + info.fullName());
throw new IllegalStateException("Type was not present in the queue: " + info.typeName());
}
if (types.put(info.fullName(), info) != null) {
throw new IllegalStateException("Type was already resolved: " + info.fullName());
if (types.put(info.typeName(), info) != null) {
throw new IllegalStateException("Type was already resolved: " + info.typeName());
}
ref.resolved = info;
return ref;
Expand All @@ -85,16 +80,11 @@ private TypeInfoRef resolve(@NotNull TypeInfo info) {
if (name.startsWith("$")) {
continue;
}
resolver.get(name);
resolver.get(TypeName.of(name));
}
}
}

@NotNull
public TypeInfo get(@NotNull String name) {
return Objects.requireNonNull(types.get(name), () -> "Unknown type: " + name);
}

@NotNull
public Collection<TypeInfo> types() {
return Collections.unmodifiableCollection(types.values());
Expand All @@ -115,7 +105,7 @@ private static TypeInfo loadSingleType(@NotNull String name, @NotNull JsonObject
@NotNull
private static TypeInfo loadAtomType(@NotNull String name, @NotNull JsonObject object, @NotNull Resolver resolver) {
String base = object.get("base_type").getAsString();
return new AtomTypeInfo(name, base.equals(name) ? null : resolver.get(base).value());
return new AtomTypeInfo(name, base.equals(name) ? null : resolver.get(TypeName.of(base)).value());
}

@NotNull
Expand Down Expand Up @@ -150,8 +140,9 @@ private static TypeInfo loadCompoundType(@NotNull String name, @NotNull JsonObje
if (object.has("bases")) {
for (JsonElement element : object.getAsJsonArray("bases")) {
JsonObject base = element.getAsJsonObject();
TypeName baseName = TypeName.of(base.get("name").getAsString());
bases.add(new ClassBaseInfo(
(ClassTypeInfo) resolver.get(base.get("name").getAsString()).value(),
(ClassTypeInfo) resolver.get(baseName).value(),
base.get("offset").getAsInt()
));
}
Expand All @@ -169,7 +160,7 @@ private static TypeInfo loadCompoundType(@NotNull String name, @NotNull JsonObje
attrs.add(new ClassAttrInfo(
attr.get("name").getAsString(),
category,
resolver.get(attr.get("type").getAsString()),
resolver.get(TypeName.parse(attr.get("type").getAsString())),
attr.has("min") ? attr.get("min").getAsString() : null,
attr.has("max") ? attr.get("max").getAsString() : null,
attrs.size(),
Expand Down Expand Up @@ -199,17 +190,14 @@ private static TypeInfo loadCompoundType(@NotNull String name, @NotNull JsonObje

private interface Resolver {
@NotNull
TypeInfoRef get(@NotNull String name);

@NotNull
TypeInfo get(@NotNull String name, @NotNull String argument);
TypeInfoRef get(@NotNull TypeName name);
}

private record ResolvedRef(@NotNull TypeInfo value) implements TypeInfoRef {
@NotNull
@Override
public String typeName() {
return value.fullName();
public TypeName typeName() {
return value.typeName();
}

@Override
Expand All @@ -219,16 +207,16 @@ public String toString() {
}

private static class FutureRef implements TypeInfoRef {
private final String name;
private final TypeName name;
private TypeInfo resolved;

public FutureRef(@NotNull String name) {
public FutureRef(@NotNull TypeName name) {
this.name = name;
}

@NotNull
@Override
public String typeName() {
public TypeName typeName() {
return name;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public TypeSpec generate(@NotNull TypeInfo type) {
} else if (type instanceof ContainerTypeInfo || type instanceof PointerTypeInfo) {
return null;
} else {
throw new IllegalArgumentException("Unknown type: " + type.fullName());
throw new IllegalArgumentException("Unknown type: " + type.typeName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ static String getJavaConstantName(@NotNull EnumTypeInfo info, @NotNull EnumValue
return "_" + info.values().indexOf(value);
}

String type = info.typeName().fullName();
String name = value.name();

if (name.toLowerCase(Locale.ROOT).startsWith(info.fullName().toLowerCase(Locale.ROOT))) {
name = name.substring(info.fullName().length() + 1);
if (name.toLowerCase(Locale.ROOT).startsWith(type.toLowerCase(Locale.ROOT))) {
name = name.substring(type.length() + 1);
}

name = name.replaceAll("[()]", "").replaceAll("/", "_");
Expand Down Expand Up @@ -121,12 +122,12 @@ static TypeName getTypeName(@NotNull TypeInfo type) {
return ParameterizedTypeName.get(ClassName.get(List.class), argument);
}
}
throw new IllegalArgumentException("Unknown type: " + type.fullName());
throw new IllegalArgumentException("Unknown type: " + type.typeName());
}

@NotNull
private static String getJavaTypeName(@NotNull TypeInfo info) {
String name = info.fullName();
String name = info.typeName().fullName();
if (info instanceof EnumTypeInfo && name.matches("^[Ee][A-Z].*$")) {
return name.substring(1);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.shade.decima.rtti.generator.data;

import com.shade.decima.rtti.TypeName;
import com.shade.util.NotNull;
import com.shade.util.Nullable;

Expand All @@ -9,7 +10,7 @@ public record AtomTypeInfo(
) implements TypeInfo {
@NotNull
@Override
public String fullName() {
return name;
public TypeName typeName() {
return TypeName.of(name);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.shade.decima.rtti.generator.data;

import com.shade.decima.rtti.TypeName;
import com.shade.util.NotNull;

import java.util.List;
Expand Down Expand Up @@ -27,8 +28,8 @@ public boolean isAssignableTo(@NotNull String name) {

@NotNull
@Override
public String fullName() {
return name;
public TypeName typeName() {
return TypeName.of(name);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.shade.decima.rtti.generator.data;

import com.shade.decima.rtti.TypeName;
import com.shade.util.NotNull;

public record ContainerTypeInfo(
Expand All @@ -8,7 +9,7 @@ public record ContainerTypeInfo(
) implements TypeInfo {
@NotNull
@Override
public String fullName() {
return "%s<%s>".formatted(name, type.typeName());
public TypeName typeName() {
return TypeName.of(name, type.typeName());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.shade.decima.rtti.generator.data;

import com.shade.decima.rtti.TypeName;
import com.shade.util.NotNull;

import java.util.List;
Expand All @@ -12,7 +13,7 @@ public record EnumTypeInfo(
) implements TypeInfo {
@NotNull
@Override
public String fullName() {
return name;
public TypeName typeName() {
return TypeName.of(name);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.shade.decima.rtti.generator.data;

import com.shade.decima.rtti.TypeName;
import com.shade.util.NotNull;

public record PointerTypeInfo(
Expand All @@ -8,7 +9,7 @@ public record PointerTypeInfo(
) implements TypeInfo {
@NotNull
@Override
public String fullName() {
return "%s<%s>".formatted(name, type.typeName());
public TypeName typeName() {
return TypeName.of(name, type.typeName());
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.shade.decima.rtti.generator.data;

import com.shade.decima.rtti.TypeName;
import com.shade.util.NotNull;

public sealed interface TypeInfo
permits AtomTypeInfo, ClassTypeInfo, EnumTypeInfo, ContainerTypeInfo, PointerTypeInfo {

@NotNull
String fullName();
TypeName typeName();
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.shade.decima.rtti.generator.data;

import com.shade.decima.rtti.TypeName;
import com.shade.util.NotNull;

public interface TypeInfoRef {
@NotNull
String typeName();
TypeName typeName();

@NotNull
TypeInfo value();
Expand Down

0 comments on commit 8244db7

Please sign in to comment.