Skip to content

Commit

Permalink
Jet api refactoring: remove JetBodyDeclarationWithBody interface from…
Browse files Browse the repository at this point in the history
… JetFunctionLiteralExpression
  • Loading branch information
Mikhael Bogdanov committed Mar 20, 2013
1 parent 93b860e commit 8e5f0ab
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 58 deletions.
Expand Up @@ -33,9 +33,7 @@
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;
Expand Down Expand Up @@ -63,7 +61,7 @@ public ClosureCodegen(GenerationState state, MutableClosure closure) {
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;

Expand Down Expand Up @@ -97,7 +95,7 @@ public ClosureCodegen gen(JetExpression fun, CodegenContext context, ExpressionC


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);

Expand Down
Expand Up @@ -1246,18 +1246,18 @@ public StackValue visitFunctionLiteralExpression(JetFunctionLiteralExpression ex
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();
Expand Down
Expand Up @@ -81,7 +81,7 @@ public void gen(JetNamedFunction f) {


public void generateMethod(
@NotNull PsiElement declaration,
@NotNull JetDeclaration declaration,
@NotNull JvmMethodSignature jvmSignature,
boolean needJetAnnotations,
@Nullable String propertyTypeSignature,
Expand Down Expand Up @@ -112,7 +112,7 @@ public void generateMethod(
}

private void generateMethodHeaderAndBody(
@NotNull PsiElement declaration,
@NotNull JetDeclaration declaration,
@NotNull JvmMethodSignature jvmSignature,
boolean needJetAnnotations,
@Nullable String propertyTypeSignature,
Expand Down Expand Up @@ -152,7 +152,7 @@ private void generateMethodHeaderAndBody(
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 {
Expand All @@ -167,7 +167,7 @@ else if (declaration instanceof JetFunctionLiteralExpression || isLocalFun(bindi
@NotNull
private MethodBounds generateMethodBody(
@NotNull MethodVisitor mv,
@NotNull PsiElement funOrProperty,
@NotNull JetDeclaration funOrProperty,
@NotNull FunctionDescriptor functionDescriptor,
@NotNull MethodContext context,
@NotNull Method asmMethod,
Expand Down Expand Up @@ -206,7 +206,7 @@ private MethodBounds generateMethodBody(

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());

Expand All @@ -229,9 +229,9 @@ private MethodBounds generateMethodBody(
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);
}


Expand Down
Expand Up @@ -224,14 +224,15 @@ public void visitObjectLiteralExpression(JetObjectLiteralExpression expression)

@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);
Expand Down
Expand Up @@ -602,10 +602,7 @@ public void visitReturnExpression(JetReturnExpression expression) {
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);
Expand Down
Expand Up @@ -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();
Expand Down
Expand Up @@ -18,11 +18,13 @@

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);
}
Expand All @@ -43,30 +45,24 @@ public JetFunctionLiteral getFunctionLiteral() {
}

@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;
}

}
Expand Up @@ -147,8 +147,8 @@ private static SimpleFunctionDescriptorImpl createFunctionDescriptor(
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;
}

Expand Down Expand Up @@ -275,7 +275,7 @@ private static JetType computeUnsafeReturnType(
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)) {
Expand Down
Expand Up @@ -227,10 +227,11 @@ public JetTypeInfo visitDoWhileExpression(JetDoWhileExpression expression, Expre
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 {
Expand Down Expand Up @@ -479,10 +480,8 @@ public JetTypeInfo visitReturnExpression(JetReturnExpression expression, Express
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));
}
Expand All @@ -494,11 +493,11 @@ public JetTypeInfo visitReturnExpression(JetReturnExpression expression, Express
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) {
Expand Down
Expand Up @@ -180,9 +180,9 @@ public void checkFunctionReturnType(@NotNull JetScope functionInnerScope, @NotNu
? 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);
}
Expand Down
Expand Up @@ -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();
}
}
Expand All @@ -73,6 +74,15 @@ public void exitLabeledElement(@NotNull JetExpression expression) {
}
}

@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);
Expand Down Expand Up @@ -163,8 +173,9 @@ else if (declarationDescriptor instanceof PropertyDescriptor) {
}
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) {
Expand Down
Expand Up @@ -134,13 +134,13 @@ public void run() {
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);
Expand Down
Expand Up @@ -29,6 +29,7 @@
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;
Expand Down Expand Up @@ -154,6 +155,6 @@ private void mayBeAddThisParameterForExtensionFunction(@NotNull List<JsParameter
}

private boolean isExtensionFunction() {
return JsDescriptorUtils.isExtension(descriptor) && !(functionDeclaration instanceof JetFunctionLiteralExpression);
return JsDescriptorUtils.isExtension(descriptor) && !(functionDeclaration instanceof JetFunctionLiteral);
}
}

0 comments on commit 8e5f0ab

Please sign in to comment.