Jet api refactoring: remove JetBodyDeclarationWithBody interface from JetFunctionLiteralExpression

This commit is contained in:
Mikhael Bogdanov
2013-03-20 11:32:55 +04:00
parent 93b860e4ad
commit 8e5f0abb84
14 changed files with 60 additions and 58 deletions
@@ -33,9 +33,7 @@ import org.jetbrains.jet.codegen.state.GenerationStateAware;
import org.jetbrains.jet.codegen.state.JetTypeMapper;
import org.jetbrains.jet.codegen.state.JetTypeMapperMode;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.JetDeclarationWithBody;
import org.jetbrains.jet.lang.psi.JetElement;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
@@ -63,7 +61,7 @@ public class ClosureCodegen extends GenerationStateAware {
this.closure = closure;
}
public ClosureCodegen gen(JetExpression fun, CodegenContext context, ExpressionCodegen expressionCodegen) {
public ClosureCodegen gen(JetDeclarationWithBody fun, CodegenContext context, ExpressionCodegen expressionCodegen) {
SimpleFunctionDescriptor descriptor = bindingContext.get(BindingContext.FUNCTION, fun);
assert descriptor != null;
@@ -97,7 +95,7 @@ public class ClosureCodegen extends GenerationStateAware {
generateBridge(name.getInternalName(), funDescriptor, fun, cv);
generateBody(funDescriptor, cv, (JetDeclarationWithBody) fun, context, expressionCodegen);
generateBody(funDescriptor, cv, fun, context, expressionCodegen);
constructor = generateConstructor(funClass, fun, cv, closure);
@@ -1246,18 +1246,18 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
return gen(expression.getFunctionLiteral().getBodyExpression());
}
else {
return genClosure(expression);
return genClosure(expression.getFunctionLiteral());
}
}
private StackValue genClosure(JetExpression expression) {
FunctionDescriptor descriptor = bindingContext.get(BindingContext.FUNCTION, expression);
private StackValue genClosure(JetDeclarationWithBody declaration) {
FunctionDescriptor descriptor = bindingContext.get(BindingContext.FUNCTION, declaration);
ClassDescriptor classDescriptor =
bindingContext.get(CLASS_FOR_FUNCTION, descriptor);
//noinspection SuspiciousMethodCalls
CalculatedClosure closure = bindingContext.get(CLOSURE, classDescriptor);
ClosureCodegen closureCodegen = new ClosureCodegen(state, (MutableClosure) closure).gen(expression, context, this);
ClosureCodegen closureCodegen = new ClosureCodegen(state, (MutableClosure) closure).gen(declaration, context, this);
JvmClassName className = closureCodegen.name;
Type asmType = className.getAsmType();
@@ -81,7 +81,7 @@ public class FunctionCodegen extends GenerationStateAware {
public void generateMethod(
@NotNull PsiElement declaration,
@NotNull JetDeclaration declaration,
@NotNull JvmMethodSignature jvmSignature,
boolean needJetAnnotations,
@Nullable String propertyTypeSignature,
@@ -112,7 +112,7 @@ public class FunctionCodegen extends GenerationStateAware {
}
private void generateMethodHeaderAndBody(
@NotNull PsiElement declaration,
@NotNull JetDeclaration declaration,
@NotNull JvmMethodSignature jvmSignature,
boolean needJetAnnotations,
@Nullable String propertyTypeSignature,
@@ -152,7 +152,7 @@ public class FunctionCodegen extends GenerationStateAware {
if (expectedThisObject != null) {
thisType = typeMapper.mapType(expectedThisObject.getType());
}
else if (declaration instanceof JetFunctionLiteralExpression || isLocalFun(bindingContext, functionDescriptor)) {
else if (declaration instanceof JetFunctionLiteral || isLocalFun(bindingContext, functionDescriptor)) {
thisType = typeMapper.mapType(context.getThisDescriptor());
}
else {
@@ -167,7 +167,7 @@ public class FunctionCodegen extends GenerationStateAware {
@NotNull
private MethodBounds generateMethodBody(
@NotNull MethodVisitor mv,
@NotNull PsiElement funOrProperty,
@NotNull JetDeclaration funOrProperty,
@NotNull FunctionDescriptor functionDescriptor,
@NotNull MethodContext context,
@NotNull Method asmMethod,
@@ -206,7 +206,7 @@ public class FunctionCodegen extends GenerationStateAware {
boolean hasBodyExpression = hasBodyExpression(funOrProperty);
if (hasBodyExpression) {
JetDeclarationWithBody fun = (JetDeclarationWithBody)funOrProperty;
JetDeclarationWithBody fun = (JetDeclarationWithBody) funOrProperty;
ExpressionCodegen codegen = new ExpressionCodegen(mv, frameMap, asmMethod.getReturnType(), context, state);
codegen.returnExpression(fun.getBodyExpression());
@@ -229,9 +229,9 @@ public class FunctionCodegen extends GenerationStateAware {
return new MethodBounds(methodBegin, methodEnd);
}
private static boolean hasBodyExpression(PsiElement funOrProperty) {
private static boolean hasBodyExpression(JetDeclaration funOrProperty) {
return (funOrProperty instanceof JetDeclarationWithBody
&& ((JetDeclarationWithBody)funOrProperty).getBodyExpression() != null);
&& ((JetDeclarationWithBody) funOrProperty).getBodyExpression() != null);
}
@@ -224,14 +224,15 @@ class CodegenAnnotatingVisitor extends JetVisitorVoid {
@Override
public void visitFunctionLiteralExpression(JetFunctionLiteralExpression expression) {
JetFunctionLiteral functionLiteral = expression.getFunctionLiteral();
FunctionDescriptor functionDescriptor =
(FunctionDescriptor) bindingContext.get(DECLARATION_TO_DESCRIPTOR, expression);
(FunctionDescriptor) bindingContext.get(DECLARATION_TO_DESCRIPTOR, functionLiteral);
// working around a problem with shallow analysis
if (functionDescriptor == null) return;
String name = inventAnonymousClassName(expression);
ClassDescriptor classDescriptor = recordClassForFunction(functionDescriptor);
recordClosure(bindingTrace, expression, classDescriptor, peekFromStack(classStack), JvmClassName.byInternalName(name), true);
recordClosure(bindingTrace, functionLiteral, classDescriptor, peekFromStack(classStack), JvmClassName.byInternalName(name), true);
classStack.push(classDescriptor);
nameStack.push(name);
@@ -602,10 +602,7 @@ public class JetControlFlowProcessor {
subroutine = builder.getReturnSubroutine();
// TODO : a context check
}
//todo cache JetFunctionLiteral instead
if (subroutine instanceof JetFunctionLiteralExpression) {
subroutine = ((JetFunctionLiteralExpression) subroutine).getFunctionLiteral();
}
if (subroutine instanceof JetFunction || subroutine instanceof JetPropertyAccessor) {
if (returnedExpression == null) {
builder.returnNoValue(expression, subroutine);
@@ -16,13 +16,12 @@
package org.jetbrains.jet.lang.psi;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public interface JetDeclarationWithBody extends PsiElement {
public interface JetDeclarationWithBody extends JetDeclaration {
@Nullable
JetExpression getBodyExpression();
@@ -18,11 +18,13 @@ package org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.JetNodeTypes;
import org.jetbrains.jet.lexer.JetToken;
import java.util.List;
public class JetFunctionLiteralExpression extends JetExpressionImpl implements JetDeclarationWithBody {
public class JetFunctionLiteralExpression extends JetExpressionImpl {
public JetFunctionLiteralExpression(@NotNull ASTNode node) {
super(node);
}
@@ -43,30 +45,24 @@ public class JetFunctionLiteralExpression extends JetExpressionImpl implements J
}
@NotNull
@Override
public List<JetParameter> getValueParameters() {
return getFunctionLiteral().getValueParameters();
}
@Override
public JetBlockExpression getBodyExpression() {
return getFunctionLiteral().getBodyExpression();
}
@Override
public boolean hasBlockBody() {
return getFunctionLiteral().hasBlockBody();
}
@Override
public boolean hasDeclaredReturnType() {
return getFunctionLiteral().getReturnTypeRef() != null;
}
@NotNull
@Override
public JetElement asElement() {
return this;
}
}
@@ -147,8 +147,8 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
Visibilities.LOCAL,
/*isInline = */ false
);
context.trace.record(BindingContext.FUNCTION, expression, functionDescriptor);
BindingContextUtils.recordFunctionDeclarationToDescriptor(context.trace, expression, functionDescriptor);
context.trace.record(BindingContext.FUNCTION, functionLiteral, functionDescriptor);
BindingContextUtils.recordFunctionDeclarationToDescriptor(context.trace, functionLiteral, functionDescriptor);
return functionDescriptor;
}
@@ -275,7 +275,7 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
JetTypeReference returnTypeRef = functionLiteral.getReturnTypeRef();
if (returnTypeRef != null) {
JetType returnType = context.expressionTypingServices.getTypeResolver().resolveType(context.scope, returnTypeRef, context.trace, true);
context.expressionTypingServices.checkFunctionReturnType(expression, context.replaceScope(functionInnerScope).
context.expressionTypingServices.checkFunctionReturnType(expression.getFunctionLiteral(), context.replaceScope(functionInnerScope).
replaceExpectedType(returnType).replaceBindingTrace(temporaryTrace), temporaryTrace);
if (expectedReturnType != null) {
if (!JetTypeChecker.INSTANCE.isSubtypeOf(expectedReturnType, returnType)) {
@@ -227,10 +227,11 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
JetScope conditionScope = context.scope;
if (body instanceof JetFunctionLiteralExpression) {
JetFunctionLiteralExpression function = (JetFunctionLiteralExpression) body;
if (!function.getFunctionLiteral().hasParameterSpecification()) {
JetFunctionLiteral functionLiteral = function.getFunctionLiteral();
if (!functionLiteral.hasParameterSpecification()) {
WritableScope writableScope = newWritableScopeImpl(context, "do..while body scope");
conditionScope = writableScope;
context.expressionTypingServices.getBlockReturnedTypeWithWritableScope(writableScope, function.getFunctionLiteral().getBodyExpression().getStatements(), CoercionStrategy.NO_COERCION, context, context.trace);
context.expressionTypingServices.getBlockReturnedTypeWithWritableScope(writableScope, functionLiteral.getBodyExpression().getStatements(), CoercionStrategy.NO_COERCION, context, context.trace);
context.trace.record(BindingContext.BLOCK, function);
}
else {
@@ -479,10 +480,8 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
JetExpression returnedExpression = expression.getReturnedExpression();
JetType expectedType = TypeUtils.NO_EXPECTED_TYPE;
JetExpression parentDeclaration = PsiTreeUtil.getParentOfType(expression, JetDeclaration.class);
if (parentDeclaration instanceof JetFunctionLiteral) {
parentDeclaration = (JetFunctionLiteralExpression) parentDeclaration.getParent();
}
JetDeclaration parentDeclaration = PsiTreeUtil.getParentOfType(expression, JetDeclaration.class);
if (parentDeclaration instanceof JetParameter) {
context.trace.report(RETURN_NOT_ALLOWED.on(expression));
}
@@ -494,11 +493,11 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
if (containingFunctionDescriptor != null) {
PsiElement containingFunction = BindingContextUtils.callableDescriptorToDeclaration(context.trace.getBindingContext(), containingFunctionDescriptor);
assert containingFunction != null;
if (containingFunction instanceof JetFunctionLiteralExpression) {
if (containingFunction instanceof JetFunctionLiteral) {
do {
containingFunctionDescriptor = DescriptorUtils.getParentOfType(containingFunctionDescriptor, FunctionDescriptor.class);
containingFunction = containingFunctionDescriptor != null ? BindingContextUtils.callableDescriptorToDeclaration(context.trace.getBindingContext(), containingFunctionDescriptor) : null;
} while (containingFunction instanceof JetFunctionLiteralExpression);
} while (containingFunction instanceof JetFunctionLiteral);
context.trace.report(RETURN_NOT_ALLOWED.on(expression));
}
if (containingFunctionDescriptor != null) {
@@ -180,9 +180,9 @@ public class ExpressionTypingServices {
? context.replaceExpectedType(NO_EXPECTED_TYPE)
: context;
if (function instanceof JetFunctionLiteralExpression) {
JetFunctionLiteralExpression functionLiteralExpression = (JetFunctionLiteralExpression) function;
JetBlockExpression blockExpression = functionLiteralExpression.getBodyExpression();
if (function instanceof JetFunctionLiteral) {
JetFunctionLiteral functionLiteral = (JetFunctionLiteral) function;
JetBlockExpression blockExpression = functionLiteral.getBodyExpression();
assert blockExpression != null;
getBlockReturnedType(newContext.scope, blockExpression, CoercionStrategy.COERCION_TO_UNIT, context, trace);
}
@@ -44,26 +44,27 @@ public class LabelResolver {
public LabelResolver() {}
public void enterLabeledElement(@NotNull LabelName labelName, @NotNull JetExpression labeledExpression) {
JetExpression deparenthesized = JetPsiUtil.deparenthesizeWithNoTypeResolution(labeledExpression);
if (deparenthesized != null) {
JetExpression cacheExpression = getCachingExpression(labeledExpression);
if (cacheExpression != null) {
Stack<JetElement> stack = labeledElements.get(labelName);
if (stack == null) {
stack = new Stack<JetElement>();
labeledElements.put(labelName, stack);
}
stack.push(deparenthesized);
stack.push(cacheExpression);
}
}
public void exitLabeledElement(@NotNull JetExpression expression) {
JetExpression deparenthesized = JetPsiUtil.deparenthesizeWithNoTypeResolution(expression);
JetExpression cacheExpression = getCachingExpression(expression);
// TODO : really suboptimal
for (Iterator<Map.Entry<LabelName,Stack<JetElement>>> mapIter = labeledElements.entrySet().iterator(); mapIter.hasNext(); ) {
Map.Entry<LabelName, Stack<JetElement>> entry = mapIter.next();
Stack<JetElement> stack = entry.getValue();
for (Iterator<JetElement> stackIter = stack.iterator(); stackIter.hasNext(); ) {
JetElement recorded = stackIter.next();
if (recorded == deparenthesized) {
if (recorded == cacheExpression) {
stackIter.remove();
}
}
@@ -73,6 +74,15 @@ public class LabelResolver {
}
}
@NotNull
private JetExpression getCachingExpression(@NotNull JetExpression labeledExpression) {
JetExpression expression = JetPsiUtil.deparenthesizeWithNoTypeResolution(labeledExpression);
if (expression instanceof JetFunctionLiteralExpression) {
expression = ((JetFunctionLiteralExpression) expression).getFunctionLiteral();
}
return expression;
}
@Nullable
private JetElement resolveControlLabel(@NotNull LabelName labelName, @NotNull JetSimpleNameExpression labelExpression, boolean reportUnresolved, ExpressionTypingContext context) {
Collection<DeclarationDescriptor> declarationsByLabel = context.scope.getDeclarationsByLabel(labelName);
@@ -163,8 +173,9 @@ public class LabelResolver {
}
else if (size == 0) {
JetElement element = resolveNamedLabel(labelName, targetLabel, false, context);
if (element instanceof JetFunctionLiteralExpression) {
DeclarationDescriptor declarationDescriptor = context.trace.getBindingContext().get(BindingContext.DECLARATION_TO_DESCRIPTOR, element);
if (element instanceof JetFunctionLiteral) {
DeclarationDescriptor declarationDescriptor =
context.trace.getBindingContext().get(BindingContext.DECLARATION_TO_DESCRIPTOR, element);
if (declarationDescriptor instanceof FunctionDescriptor) {
ReceiverParameterDescriptor thisReceiver = ((FunctionDescriptor) declarationDescriptor).getReceiverParameter();
if (thisReceiver != null) {
@@ -134,13 +134,13 @@ public class JetPositionManager implements PositionManager {
JetFile namespace = (JetFile) sourcePosition.getFile();
JetTypeMapper typeMapper = prepareTypeMapper(namespace);
PsiElement element = PsiTreeUtil.getParentOfType(sourcePosition.getElementAt(), JetClassOrObject.class, JetFunctionLiteralExpression.class, JetNamedFunction.class);
PsiElement element = PsiTreeUtil.getParentOfType(sourcePosition.getElementAt(), JetClassOrObject.class, JetFunctionLiteral.class, JetNamedFunction.class);
if (element instanceof JetClassOrObject) {
result.set(getJvmInternalNameForImpl(typeMapper, (JetClassOrObject) element));
}
else if (element instanceof JetFunctionLiteralExpression) {
else if (element instanceof JetFunctionLiteral) {
result.set(classNameForAnonymousClass(typeMapper.getBindingContext(),
(JetFunctionLiteralExpression) element).getInternalName());
((JetFunctionLiteral) element)).getInternalName());
}
else if (element instanceof JetNamedFunction) {
PsiElement parent = PsiTreeUtil.getParentOfType(element, JetClassOrObject.class, JetFunctionLiteralExpression.class, JetNamedFunction.class);
@@ -29,6 +29,7 @@ import org.jetbrains.jet.lang.descriptors.Modality;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.psi.JetDeclarationWithBody;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetFunctionLiteral;
import org.jetbrains.jet.lang.psi.JetFunctionLiteralExpression;
import org.jetbrains.k2js.translate.context.Namer;
import org.jetbrains.k2js.translate.context.TranslationContext;
@@ -154,6 +155,6 @@ public final class FunctionTranslator extends AbstractTranslator {
}
private boolean isExtensionFunction() {
return JsDescriptorUtils.isExtension(descriptor) && !(functionDeclaration instanceof JetFunctionLiteralExpression);
return JsDescriptorUtils.isExtension(descriptor) && !(functionDeclaration instanceof JetFunctionLiteral);
}
}
@@ -57,7 +57,7 @@ public class LiteralFunctionTranslator {
}
public JsExpression translate(@NotNull JetFunctionLiteralExpression declaration) {
FunctionDescriptor descriptor = getFunctionDescriptor(rootContext.bindingContext(), declaration);
FunctionDescriptor descriptor = getFunctionDescriptor(rootContext.bindingContext(), declaration.getFunctionLiteral());
JsFunction fun = createFunction();
TranslationContext funContext;
@@ -75,7 +75,7 @@ public class LiteralFunctionTranslator {
funContext = rootContext.contextWithScope(fun);
}
fun.getBody().getStatements().addAll(translateFunctionBody(descriptor, declaration, funContext).getStatements());
fun.getBody().getStatements().addAll(translateFunctionBody(descriptor, declaration.getFunctionLiteral(), funContext).getStatements());
InnerFunctionTranslator translator = null;
if (!asInner) {