Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions src/main/java/io/papermc/codebook/lvt/LvtNamer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
import dev.denwav.hypo.asm.AsmMethodData;
import dev.denwav.hypo.core.HypoContext;
import dev.denwav.hypo.hydrate.generic.HypoHydration;
import dev.denwav.hypo.hydrate.generic.MethodClosure;
import dev.denwav.hypo.hydrate.generic.LambdaClosure;
import dev.denwav.hypo.hydrate.generic.LocalClassClosure;
import dev.denwav.hypo.model.data.ClassData;
import dev.denwav.hypo.model.data.FieldData;
import dev.denwav.hypo.model.data.HypoKey;
Expand Down Expand Up @@ -100,14 +101,14 @@ private void fillNames0(final MethodData method) throws IOException {
// If it does, we need to keep track of the LVTs we inherit
@Nullable AsmMethodData outerMethod = null;
int @Nullable [] outerMethodParamLvtIndices = null;
final @Nullable List<MethodClosure<MethodData>> lambdaCalls = method.get(HypoHydration.LAMBDA_CALLS);
final @Nullable List<LambdaClosure> lambdaCalls = method.get(HypoHydration.LAMBDA_CALLS);
// Only track synthetic, non-synthetic means a method reference which does not behave as a closure (does not
// capture LVT)
if (lambdaCalls != null && method.isSynthetic()) {
for (final MethodClosure<MethodData> lambdaCall : lambdaCalls) {
for (final LambdaClosure lambdaCall : lambdaCalls) {
// lambdaCall.getClosure() -> The lambda method
// lambdaCall.getContainingMethod() -> The outer method
if (lambdaCall.getClosure().equals(method)) {
if (lambdaCall.getLambda().equals(method)) {
outerMethod = (AsmMethodData) lambdaCall.getContainingMethod();
if (outerMethod.equals(method)) {
// lambdas can be recursive
Expand All @@ -132,11 +133,11 @@ private void fillNames0(final MethodData method) throws IOException {
// A method cannot be both a lambda expression and a local class, so if we've already determined an outer
// method, there's nothing to do here.
Set<String> innerClassFieldNames = Set.of();
@Nullable MethodClosure<ClassData> localClassClosure = null;
@Nullable LocalClassClosure localClassClosure = null;
int @Nullable [] innerClassOuterMethodParamLvtIndices = null;
localCheck:
if (outerMethod == null) {
final @Nullable List<MethodClosure<ClassData>> localClasses = parentClass.get(HypoHydration.LOCAL_CLASSES);
final @Nullable List<LocalClassClosure> localClasses = parentClass.get(HypoHydration.LOCAL_CLASSES);
if (localClasses == null || localClasses.isEmpty()) {
break localCheck;
}
Expand Down Expand Up @@ -175,7 +176,8 @@ private void fillNames0(final MethodData method) throws IOException {
// The only way to handle this kin of clash it to go back up and fix the
// local variable, we can't fix it here.
fixOuterScopeName(
localClassClosure,
new ClosureInfo(
localClassClosure.getContainingMethod(), localClassClosure.getParamLvtIndices()),
outerLvt.name,
RootLvtSuggester.determineFinalName(outerLvt.name, scopedNames),
outerLvt.index);
Expand Down Expand Up @@ -368,8 +370,8 @@ private static void _collectAllFields(final ClassData container, final ClassData
}

private static void fixOuterScopeName(
final MethodClosure<?> parentDef, final String badName, final String newName, final int lvtIndex) {
final MethodData containing = parentDef.getContainingMethod();
final ClosureInfo closureInfo, final String badName, final String newName, final int lvtIndex) {
final MethodData containing = closureInfo.containing();
final MethodNode node = ((AsmMethodData) containing).getNode();

for (final LocalVariableNode lvt : node.localVariables) {
Expand All @@ -392,32 +394,36 @@ private static void fixOuterScopeName(
}
}

final @Nullable List<MethodClosure<MethodData>> lambdas = containing.get(HypoHydration.LAMBDA_CALLS);
final @Nullable List<MethodClosure<ClassData>> localClasses = containing.get(HypoHydration.LOCAL_CLASSES);
final ArrayList<MethodClosure<?>> innerClosures = new ArrayList<>(
final @Nullable List<LambdaClosure> lambdas = containing.get(HypoHydration.LAMBDA_CALLS);
final @Nullable List<LocalClassClosure> localClasses = containing.get(HypoHydration.LOCAL_CLASSES);
final ArrayList<ClosureInfo> innerClosureContainingMethods = new ArrayList<>(
(lambdas != null ? lambdas.size() : 0) + (localClasses != null ? localClasses.size() : 0));
if (lambdas != null) {
for (final MethodClosure<MethodData> lambda : lambdas) {
for (final LambdaClosure lambda : lambdas) {
if (!lambda.getContainingMethod().equals(containing)) {
innerClosures.add(lambda);
innerClosureContainingMethods.add(
new ClosureInfo(lambda.getContainingMethod(), lambda.getParamLvtIndices()));
}
}
}
if (localClasses != null) {
for (final MethodClosure<ClassData> localClass : localClasses) {
for (final LocalClassClosure localClass : localClasses) {
if (!localClass.getContainingMethod().equals(containing)) {
innerClosures.add(localClass);
innerClosureContainingMethods.add(
new ClosureInfo(localClass.getContainingMethod(), localClass.getParamLvtIndices()));
}
}
}

for (final MethodClosure<?> closure : innerClosures) {
if (closure.getParamLvtIndices().length > lvtIndex) {
fixOuterScopeName(closure, badName, newName, closure.getParamLvtIndices()[lvtIndex]);
for (final ClosureInfo closure : innerClosureContainingMethods) {
if (closure.paramLvtIndices().length > lvtIndex) {
fixOuterScopeName(closure, badName, newName, closure.paramLvtIndices()[lvtIndex]);
}
}
}

private record ClosureInfo(MethodData containing, int[] paramLvtIndices) {}

private static String packageName(final ClassData classData) {
final String name = classData.name();
final int lastIndex = name.lastIndexOf('/');
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/io/papermc/codebook/pages/FixJarPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

package io.papermc.codebook.pages;

import static java.util.Objects.requireNonNullElse;

import com.google.common.collect.Iterables;
import dev.denwav.hypo.asm.AsmClassData;
import dev.denwav.hypo.asm.AsmConstructorData;
Expand All @@ -42,6 +40,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.checkerframework.checker.nullness.qual.Nullable;
Expand Down Expand Up @@ -111,8 +110,22 @@ private static void addAnnotations(final AsmClassData classData) {
continue;
}

final @Nullable MethodData targetMethod = method.get(HypoHydration.SYNTHETIC_SOURCE);
final MethodData baseMethod = requireNonNullElse(targetMethod, method);
final MethodData baseMethod;
final @Nullable Set<MethodData> syntheticSources = method.get(HypoHydration.SYNTHETIC_SOURCES);
if (syntheticSources == null) {
baseMethod = method;
} else {
outer:
{
for (final MethodData targetMethod : syntheticSources) {
if (targetMethod.parentClass().equals(method.parentClass())) {
baseMethod = targetMethod;
break outer;
}
}
return;
}
}

if (baseMethod.superMethod() != null) {
final MethodNode node = ((AsmMethodData) method).getNode();
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/io/papermc/codebook/pages/InspectJarPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import dev.denwav.hypo.hydrate.HydrationManager;
import dev.denwav.hypo.mappings.ChangeChain;
import dev.denwav.hypo.mappings.MappingsCompletionManager;
import dev.denwav.hypo.mappings.contributors.CopyLambdaParametersDown;
import dev.denwav.hypo.mappings.contributors.CopyMappingsDown;
import dev.denwav.hypo.mappings.contributors.CopyRecordParameters;
import io.papermc.codebook.exceptions.UnexpectedException;
Expand Down Expand Up @@ -120,7 +121,10 @@ public void exec() {

// Fill in any missing mapping information
final MappingSet completedMappings = ChangeChain.create()
.addLink(CopyMappingsDown.createWithoutOverwrite(), CopyRecordParameters.create())
.addLink(
CopyMappingsDown.createWithoutOverwrite(),
CopyLambdaParametersDown.createWithoutOverwrite(),
CopyRecordParameters.create())
.applyChain(lorenzMappings, MappingsCompletionManager.create(ctx));

this.bind(ParamMappings.KEY).to(completedMappings);
Expand Down