Introduce CodegenStatementVisitor
Which will be used for generating statements (as opposed to generating expressions always, as it is now) Add StatementGenTest, which will check if we actually generate statements and not expressions of type Unit, popping them off the stack later
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2010-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.codegen;
|
||||
|
||||
import org.jetbrains.jet.lang.psi.JetElement;
|
||||
import org.jetbrains.jet.lang.psi.JetIfExpression;
|
||||
import org.jetbrains.jet.lang.psi.JetReturnExpression;
|
||||
import org.jetbrains.jet.lang.psi.JetVisitor;
|
||||
|
||||
public class CodegenStatementVisitor extends JetVisitor<StackValue, StackValue> {
|
||||
private final ExpressionCodegen codegen;
|
||||
|
||||
public CodegenStatementVisitor(ExpressionCodegen codegen) {
|
||||
this.codegen = codegen;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StackValue visitJetElement(JetElement element, StackValue receiver) {
|
||||
return element.accept(codegen, receiver);
|
||||
}
|
||||
}
|
||||
@@ -94,6 +94,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
|
||||
|
||||
private final BindingContext bindingContext;
|
||||
final CodegenContext context;
|
||||
private final CodegenStatementVisitor statementVisitor;
|
||||
|
||||
private final Stack<BlockStackElement> blockStackElements = new Stack<BlockStackElement>();
|
||||
private final Collection<String> localVariableNames = new HashSet<String>();
|
||||
@@ -181,6 +182,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
|
||||
};
|
||||
this.bindingContext = state.getBindingContext();
|
||||
this.context = context;
|
||||
this.statementVisitor = new CodegenStatementVisitor(this);
|
||||
}
|
||||
|
||||
public GenerationState getState() {
|
||||
@@ -217,6 +219,10 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
|
||||
}
|
||||
|
||||
public StackValue genQualified(StackValue receiver, JetElement selector) {
|
||||
return genQualified(receiver, selector, this);
|
||||
}
|
||||
|
||||
private StackValue genQualified(StackValue receiver, JetElement selector, JetVisitor<StackValue, StackValue> visitor) {
|
||||
if (tempVariables.containsKey(selector)) {
|
||||
throw new IllegalStateException("Inconsistent state: expression saved to a temporary variable is a selector");
|
||||
}
|
||||
@@ -224,7 +230,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
|
||||
markLineNumber(selector);
|
||||
}
|
||||
try {
|
||||
return selector.accept(this, receiver);
|
||||
return selector.accept(visitor, receiver);
|
||||
}
|
||||
catch (ProcessCanceledException e) {
|
||||
throw e;
|
||||
@@ -262,6 +268,10 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
|
||||
gen(expr, expressionType(expr));
|
||||
}
|
||||
|
||||
private StackValue genStatement(JetElement statement) {
|
||||
return genQualified(StackValue.none(), statement, statementVisitor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public StackValue visitClass(JetClass klass, StackValue data) {
|
||||
return visitClassOrObject(klass);
|
||||
@@ -1186,11 +1196,15 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
|
||||
generateLocalFunctionDeclaration((JetNamedFunction) statement, leaveTasks);
|
||||
}
|
||||
|
||||
boolean isStatement = iterator.hasNext() || !isInsideNonUnitFunction();
|
||||
|
||||
StackValue result = isStatement ? genStatement(statement) : gen(statement);
|
||||
|
||||
if (!iterator.hasNext()) {
|
||||
answer = gen(statement);
|
||||
answer = result;
|
||||
}
|
||||
else {
|
||||
gen(statement, Type.VOID_TYPE);
|
||||
result.put(Type.VOID_TYPE, v);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1203,6 +1217,12 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
|
||||
return answer;
|
||||
}
|
||||
|
||||
private boolean isInsideNonUnitFunction() {
|
||||
DeclarationDescriptor descriptor = context.getContextDescriptor();
|
||||
JetType unit = KotlinBuiltIns.getInstance().getUnitType();
|
||||
return descriptor instanceof CallableDescriptor && !unit.equals(((CallableDescriptor) descriptor).getReturnType());
|
||||
}
|
||||
|
||||
private void generateLocalVariableDeclaration(
|
||||
@NotNull JetVariableDeclaration variableDeclaration,
|
||||
final @NotNull Label blockEnd,
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2010-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.codegen;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.ConfigurationKind;
|
||||
|
||||
public class StatementGenTest extends CodegenTestCase {
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY);
|
||||
}
|
||||
|
||||
private void doTest() {
|
||||
loadFile("statements/" + getTestName(true) + ".kt");
|
||||
String text = generateToText();
|
||||
// 'getstatic' means we refer to Unit.VALUE, which we shouldn't since these tests contain only statements
|
||||
assertNoGetStatic(text);
|
||||
}
|
||||
|
||||
private void assertNoGetStatic(@NotNull String text) {
|
||||
assertFalse(text, text.toLowerCase().contains("getstatic"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user