Skip to content

Commit

Permalink
Added preliminary support for text blocks and simple instanceof pat…
Browse files Browse the repository at this point in the history
…tern matching.

Some transforms no longer run if the source compiler target didn't support them.
Minor code cleanup.
  • Loading branch information
mstrobel committed Jun 19, 2021
1 parent 0ae5095 commit 1d9fcd6
Show file tree
Hide file tree
Showing 27 changed files with 637 additions and 204 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.strobel.assembler.metadata;

import com.strobel.core.Closeables;
import com.strobel.core.Comparer;
import com.strobel.core.VerifyArgument;

public enum LanguageFeature {
ENUM_CLASSES(CompilerTarget.JDK1_5),
FOR_EACH_LOOPS(CompilerTarget.JDK1_5),
TRY_WITH_RESOURCES(CompilerTarget.JDK1_7),
DEFAULT_INTERFACE_METHODS(CompilerTarget.JDK1_8),
STATIC_INTERFACE_METHODS(CompilerTarget.JDK1_8),
LAMBDA_EXPRESSIONS(CompilerTarget.JDK1_8),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,22 @@ public boolean isSupported(final TypeDefinition versionSource, final @NotNull La

return feature.isAvailable(target, allowPreview);
}

public CompilerTarget target() {
return target(_currentType);
}

public CompilerTarget target(final TypeDefinition versionSource) {
CompilerTarget target = _settings.getForcedCompilerTarget();

if (target == null && versionSource != null) {
target = versionSource.getCompilerTarget();
}

if (target == null) {
target = CompilerTarget.DEFAULT;
}

return target;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.strobel.decompiler.languages.Languages;
import com.strobel.decompiler.languages.java.JavaFormattingOptions;

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public class DecompilerSettings {
private ITypeLoader _typeLoader;
private boolean _includeLineNumbersInBytecode = true;
Expand All @@ -46,7 +47,7 @@ public class DecompilerSettings {
private String _outputDirectory;
private boolean _showDebugLineNumbers;
private boolean _simplifyMemberReferences;
private int _minLinesForTextBlocks = 3;
private int _textBlockLineMinimum = 3;
private CompilerTarget _forcedCompilerTarget;
private boolean _arePreviewFeaturesEnabled;

Expand Down Expand Up @@ -225,12 +226,12 @@ public final void setForceFullyQualifiedReferences(final boolean forceFullyQuali
_forceFullyQualifiedReferences = forceFullyQualifiedReferences;
}

public final int getMinLinesForTextBlocks() {
return _minLinesForTextBlocks;
public final int getTextBlockLineMinimum() {
return _textBlockLineMinimum;
}

public final void setMinLinesForTextBlocks(final int minLinesForTextBlocks) {
_minLinesForTextBlocks = minLinesForTextBlocks;
public final void setTextBlockLineMinimum(final int textBlockLineMinimum) {
_textBlockLineMinimum = textBlockLineMinimum;
}

public final CompilerTarget getForcedCompilerTarget() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import static com.strobel.core.CollectionUtilities.*;
import static com.strobel.decompiler.ast.PatternMatching.*;

@SuppressWarnings("ConstantConditions")
@SuppressWarnings({ "ConstantConditions", "UnusedReturnValue" })
public final class AstOptimizer {
private final static Logger LOG = Logger.getLogger(AstOptimizer.class.getSimpleName());

Expand Down Expand Up @@ -3485,6 +3485,10 @@ protected InlineLambdasOptimization(final DecompilerContext context, final Block

@Override
public boolean run(final List<Node> body, final Expression head, final int position) {
if (!context.isSupported(LanguageFeature.LAMBDA_EXPRESSIONS)) {
return false;
}

final StrongBox<DynamicCallSite> c = new StrongBox<>();
final List<Expression> a = new ArrayList<>();

Expand Down Expand Up @@ -3552,7 +3556,7 @@ private Lambda tryInlineLambda(final Expression site, final DynamicCallSite call

final MethodBody methodBody = resolvedMethod.getBody();
final List<ParameterDefinition> parameters = resolvedMethod.getParameters();
final Variable[] parameterMap = new Variable[methodBody.getMaxLocals()];
// final Variable[] parameterMap = new Variable[methodBody.getMaxLocals()];

final List<Node> nodes = new ArrayList<>();
final Block body = new Block();
Expand All @@ -3570,7 +3574,7 @@ private Lambda tryInlineLambda(final Expression site, final DynamicCallSite call
variable.setType(context.getCurrentMethod().getDeclaringType());
variable.setOriginalParameter(context.getCurrentMethod().getBody().getThisParameter());

parameterMap[0] = variable;
// parameterMap[0] = variable;

lambdaParameters.add(variable);
}
Expand All @@ -3583,7 +3587,7 @@ private Lambda tryInlineLambda(final Expression site, final DynamicCallSite call
variable.setOriginalParameter(p);
variable.setLambdaParameter(true);

parameterMap[p.getSlot()] = variable;
// parameterMap[p.getSlot()] = variable;

lambdaParameters.add(variable);
}
Expand Down Expand Up @@ -3808,6 +3812,7 @@ private interface ExpressionOptimization {
private static abstract class AbstractBasicBlockOptimization implements BasicBlockOptimization {
protected final static BasicBlock EMPTY_BLOCK = new BasicBlock();

@SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
protected final Map<Label, MutableInteger> labelGlobalRefCount = new DefaultMap<>(MutableInteger.SUPPLIER);
protected final Map<Label, BasicBlock> labelToBasicBlock = new DefaultMap<>(Suppliers.forValue(EMPTY_BLOCK));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public interface IOutputFormatter {
void writeToken(String token);
void writeLiteral(String value);
void writeTextLiteral(String value);
void writeTextBlock(String value);

void space();

Expand All @@ -48,6 +49,6 @@ public interface IOutputFormatter {
* instructs 'this' formatter to forget what it used to know about the sequence of line
* number offsets in the source code
*/
public void resetLineNumberOffsets( OffsetToLineNumberConverter offset2LineNumber);
void resetLineNumberOffsets( OffsetToLineNumberConverter offset2LineNumber);

}
Loading

0 comments on commit 1d9fcd6

Please sign in to comment.