Skip to content

Commit

Permalink
* Redundant calls to
Browse files Browse the repository at this point in the history
org.eclipse.jdt.internal.compiler.codegen.CodeStream.addVisibleLocalVariable

* Fixes #1889
  • Loading branch information
srikanth-sankaran committed Mar 5, 2024
1 parent 9b2ac9f commit cee692e
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1825,11 +1825,6 @@ public StringBuilder printExpressionNoParenthesis(int indent, StringBuilder outp
return this.right.printExpression(0, output);
}

@Override
public void addPatternVariables(BlockScope scope, CodeStream codeStream) {
this.left.addPatternVariables(scope, codeStream); // Srikanth
this.right.addPatternVariables(scope, codeStream);
}
@Override
public boolean containsPatternVariable() {
return this.left.containsPatternVariable() || this.right.containsPatternVariable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,13 +462,6 @@ public StringBuilder printExpressionNoParenthesis(int indent, StringBuilder outp
return this.valueIfFalse.printExpression(0, output);
}

@Override
public void addPatternVariables(BlockScope scope, CodeStream codeStream) {
this.condition.addPatternVariables(scope, codeStream);
this.valueIfTrue.addPatternVariables(scope, codeStream);
this.valueIfFalse.addPatternVariables(scope, codeStream);
}

@Override
public TypeBinding resolveType(BlockScope scope) {
// JLS3 15.25
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -782,9 +782,7 @@ public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean
throw new ShouldNotImplement(Messages.ast_missingCode);
}
}
public void addPatternVariables(BlockScope scope, CodeStream codeStream) {
// Nothing by default
}

public LocalDeclaration getPatternVariable() {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,8 @@ public void generateCode(BlockScope currentScope, CodeStream codeStream) {
this.initializations[i].generateCode(this.scope, codeStream);
}
}
if (containsPatternVariable()) {
this.condition.addPatternVariables(currentScope, codeStream);
}
Constant cst = this.condition == null ? null : this.condition.optimizedBooleanConstant();
boolean conditionInjectsBindings = this.condition != null ? this.condition.bindingsWhenTrue().length > 0 : false;
boolean isConditionOptimizedFalse = cst != null && (cst != Constant.NotAConstant && cst.booleanValue() == false);
if (isConditionOptimizedFalse) {
this.condition.generateCode(this.scope, codeStream, false);
Expand All @@ -294,7 +292,10 @@ public void generateCode(BlockScope currentScope, CodeStream codeStream) {
actionLabel.tagBits |= BranchLabel.USED;
BranchLabel conditionLabel = new BranchLabel(codeStream);
this.breakLabel.initialize(codeStream);
if (this.continueLabel == null) {
if (this.continueLabel == null || conditionInjectsBindings) {
if (this.continueLabel != null) {
this.continueLabel.initialize(codeStream);
}
conditionLabel.place();
if ((this.condition != null) && (this.condition.constant == Constant.NotAConstant)) {
this.condition.generateOptimizedBoolean(this.scope, codeStream, null, this.breakLabel, true);
Expand All @@ -320,6 +321,9 @@ public void generateCode(BlockScope currentScope, CodeStream codeStream) {
codeStream.addDefinitelyAssignedVariables(
currentScope,
this.condIfTrueInitStateIndex);
codeStream.removeNotDefinitelyAssignedVariables(
currentScope,
this.condIfTrueInitStateIndex);
}
actionLabel.place();
this.action.generateCode(this.scope, codeStream);
Expand All @@ -344,12 +348,16 @@ public void generateCode(BlockScope currentScope, CodeStream codeStream) {
if (this.preCondInitStateIndex != -1) {
codeStream.removeNotDefinitelyAssignedVariables(currentScope, this.preCondInitStateIndex);
}
// generate the condition
conditionLabel.place();
if ((this.condition != null) && (this.condition.constant == Constant.NotAConstant)) {
this.condition.generateOptimizedBoolean(this.scope, codeStream, actionLabel, null, true);
// generate the condition or loop back to condition if it was flattened ahead of body
if (conditionInjectsBindings) {
codeStream.goto_(conditionLabel);
} else {
codeStream.goto_(actionLabel);
conditionLabel.place();
if ((this.condition != null) && (this.condition.constant == Constant.NotAConstant)) {
this.condition.generateOptimizedBoolean(this.scope, codeStream, actionLabel, null, true);
} else {
codeStream.goto_(actionLabel);
}
}

} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,13 @@ public boolean visit(RecordPattern recordPattern, BlockScope scope1) {
PatternsCollector patCollector = new PatternsCollector(guardedElseTarget);
this.condition.traverse(patCollector, currentScope);
}

@Override
public boolean isAlwaysTrue() {
Constant cst = this.condition.optimizedBooleanConstant();
return cst != Constant.NotAConstant && cst.booleanValue() == true;
}

@Override
public boolean coversType(TypeBinding type) {
return this.primaryPattern.coversType(type) && isAlwaysTrue();
Expand Down Expand Up @@ -167,18 +169,21 @@ public void traverse(ASTVisitor visitor, BlockScope scope) {
}
visitor.endVisit(this, scope);
}

@Override
public void suspendVariables(CodeStream codeStream, BlockScope scope) {
codeStream.removeNotDefinitelyAssignedVariables(scope, this.thenInitStateIndex1);
this.primaryPattern.suspendVariables(codeStream, scope);
}

@Override
public void resumeVariables(CodeStream codeStream, BlockScope scope) {
codeStream.addDefinitelyAssignedVariables(scope, this.thenInitStateIndex2);
this.primaryPattern.resumeVariables(codeStream, scope);
}

@Override
protected boolean isPatternTypeCompatible(TypeBinding other, BlockScope scope) {
return this.primaryPattern.isPatternTypeCompatible(other, scope);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean
if (this.elementVariable != null && this.elementVariable.binding != null) {
this.elementVariable.binding.modifiers &= ~ExtraCompilerModifiers.AccOutOfFlowScope;
}
addPatternVariables(currentScope, codeStream);

int pc = codeStream.position;

Expand Down Expand Up @@ -152,12 +151,10 @@ public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean
public void generateOptimizedBoolean(BlockScope currentScope, CodeStream codeStream, BranchLabel trueLabel, BranchLabel falseLabel, boolean valueRequired) {
// a label valued to nil means: by default we fall through the case...
// both nil means we leave the value on the stack

if (this.elementVariable == null && this.pattern == null) {
super.generateOptimizedBoolean(currentScope, codeStream, trueLabel, falseLabel, valueRequired);
return;
}
addPatternVariables(currentScope, codeStream);

int pc = codeStream.position;

Expand All @@ -177,12 +174,7 @@ public void generateOptimizedBoolean(BlockScope currentScope, CodeStream codeStr
this.expression.generateCode(currentScope, codeStream, true);
}

if (this.pattern instanceof RecordPattern) {
this.pattern.generateOptimizedBoolean(currentScope, codeStream, trueLabel, nextSibling);
} else {
codeStream.checkcast(this.type, this.type.resolvedType, codeStream.position);
codeStream.store(this.elementVariable.binding, false);
}
this.pattern.generateOptimizedBoolean(currentScope, codeStream, trueLabel, nextSibling);

if (valueRequired) {
codeStream.generateImplicitConversion(this.implicitConversion);
Expand Down Expand Up @@ -217,13 +209,6 @@ public StringBuilder printExpressionNoParenthesis(int indent, StringBuilder outp
this.expression.printExpression(indent, output).append(" instanceof "); //$NON-NLS-1$
return this.pattern == null ? this.type.print(0, output) : this.pattern.printExpression(0, output);
}

@Override
public void addPatternVariables(BlockScope currentScope, CodeStream codeStream) {
for (LocalVariableBinding local: bindingsWhenTrue()) {
codeStream.addVisibleLocalVariable(local);
}
}
@Override
public LocalVariableBinding[] bindingsWhenTrue() {
return this.pattern != null ? this.pattern.bindingsWhenTrue() : NO_VARIABLES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, Fl
this.bits |= ASTNode.IsLocalDeclarationReachable; // only set if actually reached
}
if (this.initialization == null) {
if (this.binding != null && this.binding.isPatternVariable())
this.bits |= FirstAssignmentToLocal;
return flowInfo;
}
this.initialization.checkNPEbyUnboxing(currentScope, flowContext, flowInfo);
Expand Down Expand Up @@ -162,22 +164,24 @@ public void generateCode(BlockScope currentScope, CodeStream codeStream) {

// something to initialize?
generateInit: {
if (this.initialization == null)
if (this.initialization == null && !this.binding.isPatternVariable())
break generateInit;
// forget initializing unused or final locals set to constant value (final ones are inlined)
if (this.binding.resolvedPosition < 0) {
if (this.initialization.constant != Constant.NotAConstant)
if (this.initialization != null) {
// forget initializing unused or final locals set to constant value (final ones are inlined)
if (this.binding.resolvedPosition < 0) {
if (this.initialization.constant != Constant.NotAConstant)
break generateInit;
// if binding unused generate then discard the value
this.initialization.generateCode(currentScope, codeStream, false);
break generateInit;
// if binding unused generate then discard the value
this.initialization.generateCode(currentScope, codeStream, false);
break generateInit;
}
this.initialization.generateCode(currentScope, codeStream, true);
// 26903, need extra cast to store null in array local var
if (this.binding.type.isArrayType()
&& ((this.initialization instanceof CastExpression) // arrayLoc = (type[])null
&& (((CastExpression)this.initialization).innermostCastedExpression().resolvedType == TypeBinding.NULL))){
codeStream.checkcast(this.binding.type);
}
this.initialization.generateCode(currentScope, codeStream, true);
// 26903, need extra cast to store null in array local var
if (this.binding.type.isArrayType()
&& ((this.initialization instanceof CastExpression) // arrayLoc = (type[])null
&& (((CastExpression)this.initialization).innermostCastedExpression().resolvedType == TypeBinding.NULL))){
codeStream.checkcast(this.binding.type);
}
}
codeStream.store(this.binding, false);
if ((this.bits & ASTNode.FirstAssignmentToLocal) != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean
}
if (this.rightInitStateIndex != -1) {
codeStream.addDefinitelyAssignedVariables(currentScope, this.rightInitStateIndex);
codeStream.removeNotDefinitelyAssignedVariables(currentScope, this.rightInitStateIndex);
}
if (rightIsConst) {
this.right.generateCode(currentScope, codeStream, false);
Expand Down Expand Up @@ -238,6 +239,7 @@ public void generateOptimizedBoolean(BlockScope currentScope, CodeStream codeStr
}
if (this.rightInitStateIndex != -1) {
codeStream.addDefinitelyAssignedVariables(currentScope, this.rightInitStateIndex);
codeStream.removeNotDefinitelyAssignedVariables(currentScope, this.rightInitStateIndex);
}
this.right.generateOptimizedBoolean(currentScope, codeStream, trueLabel, null, valueRequired && !rightIsConst);
if (valueRequired && rightIsTrue) {
Expand All @@ -256,8 +258,8 @@ public void generateOptimizedBoolean(BlockScope currentScope, CodeStream codeStr
break generateOperands; // no need to generate right operand
}
if (this.rightInitStateIndex != -1) {
codeStream
.addDefinitelyAssignedVariables(currentScope, this.rightInitStateIndex);
codeStream.addDefinitelyAssignedVariables(currentScope, this.rightInitStateIndex);
codeStream.removeNotDefinitelyAssignedVariables(currentScope, this.rightInitStateIndex);
}
this.right.generateOptimizedBoolean(currentScope, codeStream, null, falseLabel, valueRequired && !rightIsConst);
int pc = codeStream.position;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public Pattern getEnclosingPattern() {
public void setEnclosingPattern(RecordPattern enclosingPattern) {
this.enclosingPattern = enclosingPattern;
}

/**
* Implement the rules in the spec under 14.11.1.1 Exhaustive Switch Blocks
*
Expand All @@ -71,9 +72,11 @@ public void setEnclosingPattern(RecordPattern enclosingPattern) {
public boolean coversType(TypeBinding type) {
return false;
}

public boolean isAlwaysTrue() {
return true;
}

@Override
public void generateCode(BlockScope currentScope, CodeStream codeStream) {
setTargets(codeStream);
Expand All @@ -85,12 +88,15 @@ public void generateCode(BlockScope currentScope, CodeStream codeStream) {
if (this.thenTarget == null)
this.thenTarget = new BranchLabel(codeStream);
}

public void suspendVariables(CodeStream codeStream, BlockScope scope) {
// nothing by default
}

public void resumeVariables(CodeStream codeStream, BlockScope scope) {
// nothing by default
}

public abstract void generateOptimizedBoolean(BlockScope currentScope, CodeStream codeStream, BranchLabel trueLabel, BranchLabel falseLabel);

public TypeReference getType() {
Expand All @@ -105,4 +111,4 @@ public TypeReference getType() {
public StringBuilder print(int indent, StringBuilder output) {
return this.printExpression(indent, output);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,25 @@ public class RecordPattern extends TypePattern {
private TypeBinding expectedType; // for record pattern type inference

public RecordPattern(TypeReference type, int sourceStart, int sourceEnd) {
super(null); // no local declaration with record patterns
this.type = type;
this.sourceStart = sourceStart;
this.sourceEnd = sourceEnd;
}

@Override
public TypeReference getType() {
return this.type;
}

@Override
public boolean checkUnsafeCast(Scope scope, TypeBinding castType, TypeBinding expressionType, TypeBinding match, boolean isNarrowing) {
if (!castType.isReifiable())
return CastExpression.checkUnsafeCast(this, scope, castType, expressionType, match, isNarrowing);
else
return super.checkUnsafeCast(scope, castType, expressionType, match, isNarrowing);
}

@Override
public LocalVariableBinding[] bindingsWhenTrue() {
LocalVariableBinding [] variables = NO_VARIABLES;
Expand All @@ -66,6 +70,7 @@ public LocalVariableBinding[] bindingsWhenTrue() {
}
return variables;
}

@Override
public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
this.thenInitStateIndex1 = currentScope.methodScope().recordInitializationStates(flowInfo);
Expand All @@ -76,6 +81,7 @@ public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, Fl
this.thenInitStateIndex2 = currentScope.methodScope().recordInitializationStates(flowInfo);
return flowInfo;
}

@Override
public boolean coversType(TypeBinding t) {
if (TypeBinding.equalsEquals(t, this.resolvedType)) {
Expand Down Expand Up @@ -173,6 +179,7 @@ public TypeBinding resolveType(BlockScope scope) {
}
return this.resolvedType;
}

private ReferenceBinding inferRecordParameterization(BlockScope scope, ReferenceBinding proposedMatchingType) {
InferenceContext18 freshInferenceContext = new InferenceContext18(scope);
try {
Expand All @@ -181,10 +188,12 @@ private ReferenceBinding inferRecordParameterization(BlockScope scope, Reference
freshInferenceContext.cleanUp();
}
}

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

@Override
public boolean dominates(Pattern p) {
if (!this.resolvedType.isValidBinding())
Expand Down Expand Up @@ -282,10 +291,12 @@ original record instance as receiver - leaving the stack drained.
public void suspendVariables(CodeStream codeStream, BlockScope scope) {
codeStream.removeNotDefinitelyAssignedVariables(scope, this.thenInitStateIndex1);
}

@Override
public void resumeVariables(CodeStream codeStream, BlockScope scope) {
codeStream.addDefinitelyAssignedVariables(scope, this.thenInitStateIndex2);
}

@Override
public void traverse(ASTVisitor visitor, BlockScope scope) {
if (visitor.visit(this, scope)) {
Expand All @@ -311,4 +322,4 @@ public StringBuilder printExpression(int indent, StringBuilder output) {
output.append(')');
return output;
}
}
}

0 comments on commit cee692e

Please sign in to comment.