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();
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,
new FunctionGenerationStrategy.FunctionDefault(state, functionDescriptor, function));
}
@@ -202,8 +202,7 @@ public class JetFlowInformationProvider {
assert subroutine instanceof JetDeclarationWithBody;
JetDeclarationWithBody function = (JetDeclarationWithBody) subroutine;
JetExpression bodyExpression = function.getBodyExpression();
if (bodyExpression == null) return;
if (!function.hasBody()) return;
List<JetElement> returnedExpressions = Lists.newArrayList();
collectReturnExpressions(returnedExpressions);
@@ -31,6 +31,8 @@ public interface JetDeclarationWithBody extends JetDeclaration {
boolean hasBlockBody();
boolean hasBody();
boolean hasDeclaredReturnType();
@NotNull
@@ -73,4 +73,9 @@ public class JetFunctionLiteral extends JetFunctionNotStubbed {
public FqName getFqName() {
return null;
}
@Override
public boolean hasBody() {
return getBodyExpression() != null;
}
}
@@ -101,6 +101,11 @@ public class JetNamedFunction extends JetTypeParameterListOwnerStub<PsiJetFuncti
return findChildByClass(JetExpression.class);
}
@Override
public boolean hasBody() {
return getBodyExpression() != null;
}
@Override
public boolean hasDeclaredReturnType() {
return getReturnTypeRef() != null;
@@ -76,6 +76,11 @@ public class JetPropertyAccessor extends JetDeclarationImpl
return getEqualsToken() == null;
}
@Override
public boolean hasBody() {
return getBodyExpression() != null;
}
@Nullable
public PsiElement getEqualsToken() {
return findChildByType(JetTokens.EQ);
@@ -625,8 +625,7 @@ public class BodyResolver {
if (!needCompleteAnalysis) return;
JetExpression bodyExpression = function.getBodyExpression();
if (bodyExpression != null) {
if (function.hasBody()) {
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) {
JetExpression bodyExpression = function.getBodyExpression();
if (bodyExpression == null) return;
if (!function.hasBody()) return;
JetFlowInformationProvider flowInformationProvider = new JetFlowInformationProvider(function, trace);
if (c.getTopDownAnalysisParameters().isDeclaredLocally()) {
flowInformationProvider.checkForLocalClassOrObjectMode();
@@ -356,7 +356,7 @@ public class DeclarationsChecker {
else {
assert member instanceof JetFunction;
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) {
trace.report(PUBLIC_MEMBER_SHOULD_SPECIFY_TYPE.on(member));
@@ -398,10 +398,10 @@ public class DeclarationsChecker {
if (delegate != null) {
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));
}
if (setter != null && setter.getBodyExpression() != null) {
if (setter != null && setter.hasBody()) {
trace.report(ABSTRACT_PROPERTY_WITH_SETTER.on(setter));
}
}
@@ -413,8 +413,8 @@ public class DeclarationsChecker {
) {
JetPropertyAccessor getter = property.getGetter();
JetPropertyAccessor setter = property.getSetter();
boolean hasAccessorImplementation = (getter != null && getter.getBodyExpression() != null) ||
(setter != null && setter.getBodyExpression() != null);
boolean hasAccessorImplementation = (getter != null && getter.hasBody()) ||
(setter != null && setter.hasBody());
if (propertyDescriptor.getModality() == Modality.ABSTRACT) {
if (property.getDelegateExpressionOrInitializer() == null && property.getTypeRef() == null) {
@@ -480,7 +480,7 @@ public class DeclarationsChecker {
if (hasAbstractModifier && inTrait) {
trace.report(ABSTRACT_MODIFIER_IN_TRAIT.on(function));
}
boolean hasBody = function.getBodyExpression() != null;
boolean hasBody = function.hasBody();
if (hasBody && hasAbstractModifier) {
trace.report(ABSTRACT_FUNCTION_WITH_BODY.on(function, functionDescriptor));
}
@@ -493,7 +493,7 @@ public class DeclarationsChecker {
return;
}
modifiersChecker.checkIllegalModalityModifiers(function);
if (function.getBodyExpression() == null && !hasAbstractModifier) {
if (!function.hasBody() && !hasAbstractModifier) {
trace.report(NON_MEMBER_FUNCTION_NO_BODY.on(function, functionDescriptor));
}
}
@@ -333,8 +333,7 @@ public class DescriptorResolver {
returnType = KotlinBuiltIns.getInstance().getUnitType();
}
else {
JetExpression bodyExpression = function.getBodyExpression();
if (bodyExpression != null) {
if (function.hasBody()) {
returnType =
DeferredType.createRecursionIntolerant(
storageManager,
@@ -352,8 +351,7 @@ public class DescriptorResolver {
returnType = ErrorUtils.createErrorType("No type, no body");
}
}
boolean hasBody = function.getBodyExpression() != null;
Modality modality = resolveModalityFromModifiers(function, getDefaultModality(containingDescriptor, hasBody));
Modality modality = resolveModalityFromModifiers(function, getDefaultModality(containingDescriptor, function.hasBody()));
Visibility visibility = resolveVisibilityFromModifiers(function, getDefaultVisibility(function, containingDescriptor));
functionDescriptor.initialize(
receiverType,
@@ -963,11 +961,11 @@ public class DescriptorResolver {
boolean hasBody = property.getDelegateExpressionOrInitializer() != null;
if (!hasBody) {
JetPropertyAccessor getter = property.getGetter();
if (getter != null && getter.getBodyExpression() != null) {
if (getter != null && getter.hasBody()) {
hasBody = true;
}
JetPropertyAccessor setter = property.getSetter();
if (!hasBody && setter != null && setter.getBodyExpression() != null) {
if (!hasBody && setter != null && setter.hasBody()) {
hasBody = true;
}
}
@@ -1148,7 +1146,7 @@ public class DescriptorResolver {
setterDescriptor = new PropertySetterDescriptorImpl(propertyDescriptor, annotations,
resolveModalityFromModifiers(setter, propertyDescriptor.getModality()),
resolveVisibilityFromModifiers(setter, propertyDescriptor.getVisibility()),
setter.getBodyExpression() != null, false,
setter.hasBody(), false,
CallableMemberDescriptor.Kind.DECLARATION, null);
if (parameter != null) {
@@ -1225,7 +1223,7 @@ public class DescriptorResolver {
getterDescriptor = new PropertyGetterDescriptorImpl(propertyDescriptor, annotations,
resolveModalityFromModifiers(getter, propertyDescriptor.getModality()),
resolveVisibilityFromModifiers(getter, propertyDescriptor.getVisibility()),
getter.getBodyExpression() != null, false,
getter.hasBody(), false,
CallableMemberDescriptor.Kind.DECLARATION, null);
getterDescriptor.initialize(returnType);
trace.record(BindingContext.PROPERTY_ACCESSOR, getter, getterDescriptor);
@@ -100,8 +100,7 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
@Override
public JetTypeInfo visitFunctionLiteralExpression(@NotNull JetFunctionLiteralExpression expression, ExpressionTypingContext context) {
JetBlockExpression bodyExpression = expression.getFunctionLiteral().getBodyExpression();
if (bodyExpression == null) return null;
if (!expression.getFunctionLiteral().hasBody()) return null;
JetType expectedType = context.expectedType;
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.Nullable;
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.JetFunction;
import org.jetbrains.jet.lang.psi.JetPsiFactory;
@@ -51,18 +50,16 @@ public class AddFunctionBodyFix extends JetIntentionAction<JetFunction> {
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return super.isAvailable(project, editor, file) &&
element.getBodyExpression() == null;
return super.isAvailable(project, editor, file) && !element.hasBody();
}
@Override
public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException {
JetFunction newElement = (JetFunction) element.copy();
JetExpression bodyExpression = newElement.getBodyExpression();
if (!(newElement.getLastChild() instanceof PsiWhiteSpace)) {
newElement.add(JetPsiFactory.createWhiteSpace(project));
}
if (bodyExpression == null) {
if (!newElement.hasBody()) {
newElement.add(JetPsiFactory.createEmptyBody(project));
}
element.replace(newElement);
@@ -52,8 +52,7 @@ public class RemoveFunctionBodyFix extends JetIntentionAction<JetFunction> {
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return super.isAvailable(project, editor, file) &&
element.getBodyExpression() != null;
return super.isAvailable(project, editor, file) && element.hasBody();
}
@Override
@@ -102,11 +102,11 @@ public final class PropertyTranslator extends AbstractTranslator {
}
private boolean hasCustomGetter() {
return declaration != null && declaration.getGetter() != null && getCustomGetterDeclaration().getBodyExpression() != null;
return declaration != null && declaration.getGetter() != null && getCustomGetterDeclaration().hasBody();
}
private boolean hasCustomSetter() {
return declaration != null && declaration.getSetter() != null && getCustomSetterDeclaration().getBodyExpression() != null;
return declaration != null && declaration.getSetter() != null && getCustomSetterDeclaration().hasBody();
}
@NotNull
@@ -110,8 +110,7 @@ public final class FunctionTranslator extends AbstractTranslator {
}
private void translateBody() {
JetExpression jetBodyExpression = functionDeclaration.getBodyExpression();
if (jetBodyExpression == null) {
if (!functionDeclaration.hasBody()) {
assert descriptor.getModality().equals(Modality.ABSTRACT);
return;
}