Refactor: add hasBody() to JetDeclarationWithBody interface

Use it where appropriate
This commit is contained in:
Pavel V. Talanov
2014-03-26 17:14:57 +04:00
parent e7fd8c4118
commit 66aae37bc6
15 changed files with 41 additions and 35 deletions
@@ -98,7 +98,7 @@ public class FunctionCodegen extends ParentCodegenAwareImpl {
OwnerKind kind = owner.getContextKind(); OwnerKind kind = owner.getContextKind();
JvmMethodSignature method = typeMapper.mapSignature(functionDescriptor, kind); JvmMethodSignature method = typeMapper.mapSignature(functionDescriptor, kind);
if (kind != OwnerKind.TRAIT_IMPL || function.getBodyExpression() != null) { if (kind != OwnerKind.TRAIT_IMPL || function.hasBody()) {
generateMethod(function, method, functionDescriptor, generateMethod(function, method, functionDescriptor,
new FunctionGenerationStrategy.FunctionDefault(state, functionDescriptor, function)); new FunctionGenerationStrategy.FunctionDefault(state, functionDescriptor, function));
} }
@@ -202,8 +202,7 @@ public class JetFlowInformationProvider {
assert subroutine instanceof JetDeclarationWithBody; assert subroutine instanceof JetDeclarationWithBody;
JetDeclarationWithBody function = (JetDeclarationWithBody) subroutine; JetDeclarationWithBody function = (JetDeclarationWithBody) subroutine;
JetExpression bodyExpression = function.getBodyExpression(); if (!function.hasBody()) return;
if (bodyExpression == null) return;
List<JetElement> returnedExpressions = Lists.newArrayList(); List<JetElement> returnedExpressions = Lists.newArrayList();
collectReturnExpressions(returnedExpressions); collectReturnExpressions(returnedExpressions);
@@ -31,6 +31,8 @@ public interface JetDeclarationWithBody extends JetDeclaration {
boolean hasBlockBody(); boolean hasBlockBody();
boolean hasBody();
boolean hasDeclaredReturnType(); boolean hasDeclaredReturnType();
@NotNull @NotNull
@@ -73,4 +73,9 @@ public class JetFunctionLiteral extends JetFunctionNotStubbed {
public FqName getFqName() { public FqName getFqName() {
return null; return null;
} }
@Override
public boolean hasBody() {
return getBodyExpression() != null;
}
} }
@@ -101,6 +101,11 @@ public class JetNamedFunction extends JetTypeParameterListOwnerStub<PsiJetFuncti
return findChildByClass(JetExpression.class); return findChildByClass(JetExpression.class);
} }
@Override
public boolean hasBody() {
return getBodyExpression() != null;
}
@Override @Override
public boolean hasDeclaredReturnType() { public boolean hasDeclaredReturnType() {
return getReturnTypeRef() != null; return getReturnTypeRef() != null;
@@ -76,6 +76,11 @@ public class JetPropertyAccessor extends JetDeclarationImpl
return getEqualsToken() == null; return getEqualsToken() == null;
} }
@Override
public boolean hasBody() {
return getBodyExpression() != null;
}
@Nullable @Nullable
public PsiElement getEqualsToken() { public PsiElement getEqualsToken() {
return findChildByType(JetTokens.EQ); return findChildByType(JetTokens.EQ);
@@ -625,8 +625,7 @@ public class BodyResolver {
if (!needCompleteAnalysis) return; if (!needCompleteAnalysis) return;
JetExpression bodyExpression = function.getBodyExpression(); if (function.hasBody()) {
if (bodyExpression != null) {
expressionTypingServices.checkFunctionReturnType(functionInnerScope, function, functionDescriptor, c.getOuterDataFlowInfo(), null, trace); expressionTypingServices.checkFunctionReturnType(functionInnerScope, function, functionDescriptor, c.getOuterDataFlowInfo(), null, trace);
} }
@@ -87,8 +87,7 @@ public class ControlFlowAnalyzer {
} }
private void checkFunction(@NotNull BodiesResolveContext c, @NotNull JetDeclarationWithBody function, @Nullable JetType expectedReturnType) { private void checkFunction(@NotNull BodiesResolveContext c, @NotNull JetDeclarationWithBody function, @Nullable JetType expectedReturnType) {
JetExpression bodyExpression = function.getBodyExpression(); if (!function.hasBody()) return;
if (bodyExpression == null) return;
JetFlowInformationProvider flowInformationProvider = new JetFlowInformationProvider(function, trace); JetFlowInformationProvider flowInformationProvider = new JetFlowInformationProvider(function, trace);
if (c.getTopDownAnalysisParameters().isDeclaredLocally()) { if (c.getTopDownAnalysisParameters().isDeclaredLocally()) {
flowInformationProvider.checkForLocalClassOrObjectMode(); flowInformationProvider.checkForLocalClassOrObjectMode();
@@ -356,7 +356,7 @@ public class DeclarationsChecker {
else { else {
assert member instanceof JetFunction; assert member instanceof JetFunction;
JetFunction function = (JetFunction) member; JetFunction function = (JetFunction) member;
hasDeferredType = function.getReturnTypeRef() == null && function.getBodyExpression() != null && !function.hasBlockBody(); hasDeferredType = function.getReturnTypeRef() == null && function.hasBody() && !function.hasBlockBody();
} }
if ((memberDescriptor.getVisibility().isPublicAPI()) && memberDescriptor.getOverriddenDescriptors().size() == 0 && hasDeferredType) { if ((memberDescriptor.getVisibility().isPublicAPI()) && memberDescriptor.getOverriddenDescriptors().size() == 0 && hasDeferredType) {
trace.report(PUBLIC_MEMBER_SHOULD_SPECIFY_TYPE.on(member)); trace.report(PUBLIC_MEMBER_SHOULD_SPECIFY_TYPE.on(member));
@@ -398,10 +398,10 @@ public class DeclarationsChecker {
if (delegate != null) { if (delegate != null) {
trace.report(ABSTRACT_DELEGATED_PROPERTY.on(delegate)); trace.report(ABSTRACT_DELEGATED_PROPERTY.on(delegate));
} }
if (getter != null && getter.getBodyExpression() != null) { if (getter != null && getter.hasBody()) {
trace.report(ABSTRACT_PROPERTY_WITH_GETTER.on(getter)); trace.report(ABSTRACT_PROPERTY_WITH_GETTER.on(getter));
} }
if (setter != null && setter.getBodyExpression() != null) { if (setter != null && setter.hasBody()) {
trace.report(ABSTRACT_PROPERTY_WITH_SETTER.on(setter)); trace.report(ABSTRACT_PROPERTY_WITH_SETTER.on(setter));
} }
} }
@@ -413,8 +413,8 @@ public class DeclarationsChecker {
) { ) {
JetPropertyAccessor getter = property.getGetter(); JetPropertyAccessor getter = property.getGetter();
JetPropertyAccessor setter = property.getSetter(); JetPropertyAccessor setter = property.getSetter();
boolean hasAccessorImplementation = (getter != null && getter.getBodyExpression() != null) || boolean hasAccessorImplementation = (getter != null && getter.hasBody()) ||
(setter != null && setter.getBodyExpression() != null); (setter != null && setter.hasBody());
if (propertyDescriptor.getModality() == Modality.ABSTRACT) { if (propertyDescriptor.getModality() == Modality.ABSTRACT) {
if (property.getDelegateExpressionOrInitializer() == null && property.getTypeRef() == null) { if (property.getDelegateExpressionOrInitializer() == null && property.getTypeRef() == null) {
@@ -480,7 +480,7 @@ public class DeclarationsChecker {
if (hasAbstractModifier && inTrait) { if (hasAbstractModifier && inTrait) {
trace.report(ABSTRACT_MODIFIER_IN_TRAIT.on(function)); trace.report(ABSTRACT_MODIFIER_IN_TRAIT.on(function));
} }
boolean hasBody = function.getBodyExpression() != null; boolean hasBody = function.hasBody();
if (hasBody && hasAbstractModifier) { if (hasBody && hasAbstractModifier) {
trace.report(ABSTRACT_FUNCTION_WITH_BODY.on(function, functionDescriptor)); trace.report(ABSTRACT_FUNCTION_WITH_BODY.on(function, functionDescriptor));
} }
@@ -493,7 +493,7 @@ public class DeclarationsChecker {
return; return;
} }
modifiersChecker.checkIllegalModalityModifiers(function); modifiersChecker.checkIllegalModalityModifiers(function);
if (function.getBodyExpression() == null && !hasAbstractModifier) { if (!function.hasBody() && !hasAbstractModifier) {
trace.report(NON_MEMBER_FUNCTION_NO_BODY.on(function, functionDescriptor)); trace.report(NON_MEMBER_FUNCTION_NO_BODY.on(function, functionDescriptor));
} }
} }
@@ -333,8 +333,7 @@ public class DescriptorResolver {
returnType = KotlinBuiltIns.getInstance().getUnitType(); returnType = KotlinBuiltIns.getInstance().getUnitType();
} }
else { else {
JetExpression bodyExpression = function.getBodyExpression(); if (function.hasBody()) {
if (bodyExpression != null) {
returnType = returnType =
DeferredType.createRecursionIntolerant( DeferredType.createRecursionIntolerant(
storageManager, storageManager,
@@ -352,8 +351,7 @@ public class DescriptorResolver {
returnType = ErrorUtils.createErrorType("No type, no body"); returnType = ErrorUtils.createErrorType("No type, no body");
} }
} }
boolean hasBody = function.getBodyExpression() != null; Modality modality = resolveModalityFromModifiers(function, getDefaultModality(containingDescriptor, function.hasBody()));
Modality modality = resolveModalityFromModifiers(function, getDefaultModality(containingDescriptor, hasBody));
Visibility visibility = resolveVisibilityFromModifiers(function, getDefaultVisibility(function, containingDescriptor)); Visibility visibility = resolveVisibilityFromModifiers(function, getDefaultVisibility(function, containingDescriptor));
functionDescriptor.initialize( functionDescriptor.initialize(
receiverType, receiverType,
@@ -963,11 +961,11 @@ public class DescriptorResolver {
boolean hasBody = property.getDelegateExpressionOrInitializer() != null; boolean hasBody = property.getDelegateExpressionOrInitializer() != null;
if (!hasBody) { if (!hasBody) {
JetPropertyAccessor getter = property.getGetter(); JetPropertyAccessor getter = property.getGetter();
if (getter != null && getter.getBodyExpression() != null) { if (getter != null && getter.hasBody()) {
hasBody = true; hasBody = true;
} }
JetPropertyAccessor setter = property.getSetter(); JetPropertyAccessor setter = property.getSetter();
if (!hasBody && setter != null && setter.getBodyExpression() != null) { if (!hasBody && setter != null && setter.hasBody()) {
hasBody = true; hasBody = true;
} }
} }
@@ -1148,7 +1146,7 @@ public class DescriptorResolver {
setterDescriptor = new PropertySetterDescriptorImpl(propertyDescriptor, annotations, setterDescriptor = new PropertySetterDescriptorImpl(propertyDescriptor, annotations,
resolveModalityFromModifiers(setter, propertyDescriptor.getModality()), resolveModalityFromModifiers(setter, propertyDescriptor.getModality()),
resolveVisibilityFromModifiers(setter, propertyDescriptor.getVisibility()), resolveVisibilityFromModifiers(setter, propertyDescriptor.getVisibility()),
setter.getBodyExpression() != null, false, setter.hasBody(), false,
CallableMemberDescriptor.Kind.DECLARATION, null); CallableMemberDescriptor.Kind.DECLARATION, null);
if (parameter != null) { if (parameter != null) {
@@ -1225,7 +1223,7 @@ public class DescriptorResolver {
getterDescriptor = new PropertyGetterDescriptorImpl(propertyDescriptor, annotations, getterDescriptor = new PropertyGetterDescriptorImpl(propertyDescriptor, annotations,
resolveModalityFromModifiers(getter, propertyDescriptor.getModality()), resolveModalityFromModifiers(getter, propertyDescriptor.getModality()),
resolveVisibilityFromModifiers(getter, propertyDescriptor.getVisibility()), resolveVisibilityFromModifiers(getter, propertyDescriptor.getVisibility()),
getter.getBodyExpression() != null, false, getter.hasBody(), false,
CallableMemberDescriptor.Kind.DECLARATION, null); CallableMemberDescriptor.Kind.DECLARATION, null);
getterDescriptor.initialize(returnType); getterDescriptor.initialize(returnType);
trace.record(BindingContext.PROPERTY_ACCESSOR, getter, getterDescriptor); trace.record(BindingContext.PROPERTY_ACCESSOR, getter, getterDescriptor);
@@ -100,8 +100,7 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
@Override @Override
public JetTypeInfo visitFunctionLiteralExpression(@NotNull JetFunctionLiteralExpression expression, ExpressionTypingContext context) { public JetTypeInfo visitFunctionLiteralExpression(@NotNull JetFunctionLiteralExpression expression, ExpressionTypingContext context) {
JetBlockExpression bodyExpression = expression.getFunctionLiteral().getBodyExpression(); if (!expression.getFunctionLiteral().hasBody()) return null;
if (bodyExpression == null) return null;
JetType expectedType = context.expectedType; JetType expectedType = context.expectedType;
boolean functionTypeExpected = !noExpectedType(expectedType) && KotlinBuiltIns.getInstance().isFunctionOrExtensionFunctionType( boolean functionTypeExpected = !noExpectedType(expectedType) && KotlinBuiltIns.getInstance().isFunctionOrExtensionFunctionType(
@@ -26,7 +26,6 @@ import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.diagnostics.Diagnostic; import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetFunction; import org.jetbrains.jet.lang.psi.JetFunction;
import org.jetbrains.jet.lang.psi.JetPsiFactory; import org.jetbrains.jet.lang.psi.JetPsiFactory;
@@ -51,18 +50,16 @@ public class AddFunctionBodyFix extends JetIntentionAction<JetFunction> {
@Override @Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return super.isAvailable(project, editor, file) && return super.isAvailable(project, editor, file) && !element.hasBody();
element.getBodyExpression() == null;
} }
@Override @Override
public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException { public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException {
JetFunction newElement = (JetFunction) element.copy(); JetFunction newElement = (JetFunction) element.copy();
JetExpression bodyExpression = newElement.getBodyExpression();
if (!(newElement.getLastChild() instanceof PsiWhiteSpace)) { if (!(newElement.getLastChild() instanceof PsiWhiteSpace)) {
newElement.add(JetPsiFactory.createWhiteSpace(project)); newElement.add(JetPsiFactory.createWhiteSpace(project));
} }
if (bodyExpression == null) { if (!newElement.hasBody()) {
newElement.add(JetPsiFactory.createEmptyBody(project)); newElement.add(JetPsiFactory.createEmptyBody(project));
} }
element.replace(newElement); element.replace(newElement);
@@ -52,8 +52,7 @@ public class RemoveFunctionBodyFix extends JetIntentionAction<JetFunction> {
@Override @Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return super.isAvailable(project, editor, file) && return super.isAvailable(project, editor, file) && element.hasBody();
element.getBodyExpression() != null;
} }
@Override @Override
@@ -102,11 +102,11 @@ public final class PropertyTranslator extends AbstractTranslator {
} }
private boolean hasCustomGetter() { private boolean hasCustomGetter() {
return declaration != null && declaration.getGetter() != null && getCustomGetterDeclaration().getBodyExpression() != null; return declaration != null && declaration.getGetter() != null && getCustomGetterDeclaration().hasBody();
} }
private boolean hasCustomSetter() { private boolean hasCustomSetter() {
return declaration != null && declaration.getSetter() != null && getCustomSetterDeclaration().getBodyExpression() != null; return declaration != null && declaration.getSetter() != null && getCustomSetterDeclaration().hasBody();
} }
@NotNull @NotNull
@@ -110,8 +110,7 @@ public final class FunctionTranslator extends AbstractTranslator {
} }
private void translateBody() { private void translateBody() {
JetExpression jetBodyExpression = functionDeclaration.getBodyExpression(); if (!functionDeclaration.hasBody()) {
if (jetBodyExpression == null) {
assert descriptor.getModality().equals(Modality.ABSTRACT); assert descriptor.getModality().equals(Modality.ABSTRACT);
return; return;
} }