Refactored methods for val/var in PSI

This commit is contained in:
Valentin Kipyatkov
2015-05-29 23:14:20 +03:00
parent 034b74d3e5
commit 8fd6a64be9
71 changed files with 241 additions and 229 deletions
@@ -125,7 +125,7 @@ public abstract class DataClassMethodGenerator {
private List<PropertyDescriptor> getDataProperties() {
List<PropertyDescriptor> result = Lists.newArrayList();
for (JetParameter parameter : getPrimaryConstructorParameters()) {
if (parameter.hasValOrVarNode()) {
if (parameter.hasValOrVar()) {
result.add(bindingContext.get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, parameter));
}
}
@@ -104,7 +104,7 @@ public abstract class ClassBodyCodegen extends MemberCodegen<JetClassOrObject> {
private void generatePrimaryConstructorProperties(PropertyCodegen propertyCodegen, JetClassOrObject origin) {
boolean isAnnotation = origin instanceof JetClass && ((JetClass) origin).isAnnotation();
for (JetParameter p : getPrimaryConstructorParameters()) {
if (p.hasValOrVarNode()) {
if (p.hasValOrVar()) {
PropertyDescriptor propertyDescriptor = bindingContext.get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, p);
if (propertyDescriptor != null) {
if (!isAnnotation) {
@@ -1109,7 +1109,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
int curParam = 0;
List<ValueParameterDescriptor> parameters = constructorDescriptor.getValueParameters();
for (JetParameter parameter : getPrimaryConstructorParameters()) {
if (parameter.hasValOrVarNode()) {
if (parameter.hasValOrVar()) {
VariableDescriptor descriptor = parameters.get(curParam);
Type type = typeMapper.mapType(descriptor);
iv.load(0, classAsmType);
@@ -673,7 +673,7 @@ public class JetFlowInformationProvider {
report(Errors.UNUSED_PARAMETER.on((JetParameter) element, variableDescriptor), ctxt);
}
else if (owner instanceof JetPrimaryConstructor) {
if (!((JetParameter) element).hasValOrVarNode() &&
if (!((JetParameter) element).hasValOrVar() &&
!((JetPrimaryConstructor) owner).getContainingClass().isAnnotation()) {
report(Errors.UNUSED_PARAMETER.on((JetParameter) element, variableDescriptor), ctxt);
}
@@ -317,7 +317,7 @@ public object PositioningStrategies {
public val VAL_OR_VAR_NODE: PositioningStrategy<JetProperty> = object : PositioningStrategy<JetProperty>() {
override fun mark(element: JetProperty): List<TextRange> {
return markNode(element.getValOrVarNode())
return markElement(element.getValOrVarKeyword())
}
}
@@ -245,7 +245,7 @@ private object DebugTextBuildingVisitor : JetVisitor<String, Unit>() {
override fun visitParameter(parameter: JetParameter, data: Unit?): String? {
return buildText {
if (parameter.hasValOrVarNode()) {
if (parameter.hasValOrVar()) {
if (parameter.isMutable()) append("var ") else append("val ")
}
val name = parameter.getNameAsName()
@@ -54,8 +54,8 @@ public class JetMultiDeclaration extends JetDeclarationImpl {
}
@Nullable
public ASTNode getValOrVarNode() {
return getNode().findChildByType(TokenSet.create(VAL_KEYWORD, VAR_KEYWORD));
public PsiElement getValOrVarKeyword() {
return findChildByType(TokenSet.create(VAL_KEYWORD, VAR_KEYWORD));
}
@Nullable
@@ -127,8 +127,10 @@ public class JetMultiDeclarationEntry extends JetNamedDeclarationNotStubbed impl
}
@Override
public ASTNode getValOrVarNode() {
return getParentNode().findChildByType(TokenSet.create(VAL_KEYWORD, VAR_KEYWORD));
public PsiElement getValOrVarKeyword() {
ASTNode node = getParentNode().findChildByType(TokenSet.create(VAL_KEYWORD, VAR_KEYWORD));
if (node == null) return null;
return node.getPsi();
}
@Nullable
@@ -21,6 +21,7 @@ import com.intellij.navigation.ItemPresentation;
import com.intellij.navigation.ItemPresentationProviders;
import com.intellij.psi.PsiElement;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.tree.TokenSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.lexer.JetTokens;
@@ -105,26 +106,25 @@ public class JetParameter extends JetNamedDeclarationStub<KotlinParameterStub> i
return modifierList != null && modifierList.hasModifier(JetTokens.VARARG_KEYWORD);
}
public boolean hasValOrVarNode() {
public boolean hasValOrVar() {
KotlinParameterStub stub = getStub();
if (stub != null) {
return stub.hasValOrVarNode();
return stub.hasValOrVar();
}
return getValOrVarNode() != null;
return getValOrVarKeyword() != null;
}
@Nullable
public ASTNode getValOrVarNode() {
public PsiElement getValOrVarKeyword() {
KotlinParameterStub stub = getStub();
if (stub != null && !stub.hasValOrVarNode()) {
if (stub != null && !stub.hasValOrVar()) {
return null;
}
ASTNode val = getNode().findChildByType(JetTokens.VAL_KEYWORD);
if (val != null) return val;
return getNode().findChildByType(JetTokens.VAR_KEYWORD);
return findChildByType(VAL_VAR_TOKEN_SET);
}
private static final TokenSet VAL_VAR_TOKEN_SET = TokenSet.create(JetTokens.VAL_KEYWORD, JetTokens.VAR_KEYWORD);
@Override
public ItemPresentation getPresentation() {
return ItemPresentationProviders.getItemPresentation(this);
@@ -268,12 +268,14 @@ public class JetProperty extends JetTypeParameterListOwnerStub<KotlinPropertyStu
@Override
@NotNull
public ASTNode getValOrVarNode() {
ASTNode node = getNode().findChildByType(TokenSet.create(VAL_KEYWORD, VAR_KEYWORD));
assert node != null : "Val or var should always exist for property";
return node;
public PsiElement getValOrVarKeyword() {
PsiElement element = findChildByType(VAL_VAR_TOKEN_SET);
assert element != null : "Val or var should always exist for property";
return element;
}
private static final TokenSet VAL_VAR_TOKEN_SET = TokenSet.create(JetTokens.VAL_KEYWORD, JetTokens.VAR_KEYWORD);
@Override
public ItemPresentation getPresentation() {
return ItemPresentationProviders.getItemPresentation(this);
@@ -31,7 +31,6 @@ import org.jetbrains.kotlin.lexer.JetKeywordToken
import org.jetbrains.kotlin.lexer.JetModifierKeywordToken
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.renderName
import org.jetbrains.kotlin.psi.JetPsiFactory.CallableBuilder.Target
import org.jetbrains.kotlin.resolve.ImportPath
import java.io.PrintWriter
@@ -46,18 +45,14 @@ public var JetFile.moduleInfo: ModuleInfo? by UserDataProperty(Key.create("MODUL
public class JetPsiFactory(private val project: Project) {
public fun createValNode(): ASTNode {
public fun createValKeyword(): PsiElement {
val property = createProperty("val x = 1")
return property.getValOrVarNode()
return property.getValOrVarKeyword()
}
public fun createVarNode(): ASTNode {
public fun createVarKeyword(): PsiElement {
val property = createProperty("var x = 1")
return property.getValOrVarNode()
}
public fun createValOrVarNode(text: String): ASTNode {
return createParameterList("($text int x)").getParameters().first().getValOrVarNode()!!
return property.getValOrVarKeyword()
}
public fun createSafeCallNode(): ASTNode {
@@ -448,7 +448,7 @@ public class JetPsiUtil {
@Nullable
public static JetClass getClassIfParameterIsProperty(@NotNull JetParameter jetParameter) {
if (jetParameter.hasValOrVarNode()) {
if (jetParameter.hasValOrVar()) {
PsiElement grandParent = jetParameter.getParent().getParent();
if (grandParent instanceof JetPrimaryConstructor) {
return ((JetPrimaryConstructor) grandParent).getContainingClassOrNull();
@@ -767,7 +767,7 @@ public class JetPsiUtil {
PsiElement parent = declaration.getParent();
// val/var parameter of primary constructor should not be considered as local
if (((JetParameter) declaration).getValOrVarNode() != null && parent != null && parent.getParent() instanceof JetPrimaryConstructor) {
if (((JetParameter) declaration).hasValOrVar() && parent != null && parent.getParent() instanceof JetPrimaryConstructor) {
return null;
}
@@ -16,12 +16,12 @@
package org.jetbrains.kotlin.psi;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.Nullable;
public interface JetVariableDeclaration extends JetCallableDeclaration, JetWithExpressionInitializer {
boolean isVar();
@Nullable
ASTNode getValOrVarNode();
PsiElement getValOrVarKeyword();
}
@@ -299,7 +299,7 @@ public inline fun <reified T : JetElement, R> flatMapDescendantsOfTypeVisitor(ac
public fun JetClassOrObject.effectiveDeclarations(): List<JetDeclaration> {
return when(this) {
is JetClass -> getDeclarations() + getPrimaryConstructorParameters().filter { p -> p.hasValOrVarNode() }
is JetClass -> getDeclarations() + getPrimaryConstructorParameters().filter { p -> p.hasValOrVar() }
else -> getDeclarations()
}
}
@@ -83,7 +83,7 @@ public trait KotlinEnumEntrySuperclassReferenceExpressionStub : StubElement<JetE
public trait KotlinParameterStub : KotlinStubWithFqName<JetParameter> {
public fun isMutable(): Boolean
public fun hasValOrVarNode(): Boolean
public fun hasValOrVar(): Boolean
public fun hasDefaultValue(): Boolean
}
@@ -39,14 +39,14 @@ public class JetParameterElementType extends JetStubElementType<KotlinParameterS
FqName fqName = psi.getFqName();
StringRef fqNameRef = StringRef.fromString(fqName != null ? fqName.asString() : null);
return new KotlinParameterStubImpl(parentStub, fqNameRef, StringRef.fromString(psi.getName()),
psi.isMutable(), psi.hasValOrVarNode(), psi.hasDefaultValue());
psi.isMutable(), psi.hasValOrVar(), psi.hasDefaultValue());
}
@Override
public void serialize(@NotNull KotlinParameterStub stub, @NotNull StubOutputStream dataStream) throws IOException {
dataStream.writeName(stub.getName());
dataStream.writeBoolean(stub.isMutable());
dataStream.writeBoolean(stub.hasValOrVarNode());
dataStream.writeBoolean(stub.hasValOrVar());
dataStream.writeBoolean(stub.hasDefaultValue());
FqName name = stub.getFqName();
dataStream.writeName(name != null ? name.asString() : null);
@@ -29,7 +29,7 @@ public class KotlinParameterStubImpl(
private val fqName: StringRef?,
private val name: StringRef?,
private val isMutable: Boolean,
private val hasValOrVarNode: Boolean,
private val hasValOrVar: Boolean,
private val hasDefaultValue: Boolean
) : KotlinStubBaseImpl<JetParameter>(parent, JetStubElementTypes.VALUE_PARAMETER), KotlinParameterStub {
override fun getName(): String? {
@@ -41,6 +41,6 @@ public class KotlinParameterStubImpl(
}
override fun isMutable() = isMutable
override fun hasValOrVarNode() = hasValOrVarNode
override fun hasValOrVar() = hasValOrVar
override fun hasDefaultValue() = hasDefaultValue
}
@@ -174,7 +174,7 @@ public interface BindingContext {
PsiElement declarationPsiElement = DescriptorToSourceUtils.descriptorToDeclaration(propertyDescriptor);
if (declarationPsiElement instanceof JetParameter) {
JetParameter jetParameter = (JetParameter) declarationPsiElement;
return jetParameter.hasValOrVarNode() ||
return jetParameter.hasValOrVar() ||
backingFieldRequired; // this part is unused because we do not allow access to constructor parameters in member bodies
}
if (propertyDescriptor.getModality() == Modality.ABSTRACT) return false;
@@ -332,7 +332,7 @@ public class DeclarationsChecker {
private void checkValOnAnnotationParameter(JetClass aClass) {
for (JetParameter parameter : aClass.getPrimaryConstructorParameters()) {
if (!parameter.hasValOrVarNode()) {
if (!parameter.hasValOrVar()) {
trace.report(MISSING_VAL_ON_ANNOTATION_PARAMETER.on(parameter));
}
}
@@ -19,7 +19,6 @@ package org.jetbrains.kotlin.resolve;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import kotlin.jvm.functions.Function0;
import org.jetbrains.annotations.NotNull;
@@ -1193,9 +1192,9 @@ public class DescriptorResolver {
@NotNull JetParameter parameter,
@NotNull DiagnosticFactory1<PsiElement, JetKeywordToken> diagnosticFactory
) {
ASTNode valOrVarNode = parameter.getValOrVarNode();
if (valOrVarNode != null) {
trace.report(diagnosticFactory.on(valOrVarNode.getPsi(), ((JetKeywordToken) valOrVarNode.getElementType())));
PsiElement valOrVar = parameter.getValOrVarKeyword();
if (valOrVar != null) {
trace.report(diagnosticFactory.on(valOrVar, ((JetKeywordToken) valOrVar.getNode().getElementType())));
}
}
@@ -348,7 +348,7 @@ class FunctionDescriptorResolver(
private fun checkConstructorParameterHasNoModifier(trace: BindingTrace, parameter: JetParameter) {
// If is not a property, then it must have no modifier
if (!parameter.hasValOrVarNode()) {
if (!parameter.hasValOrVar()) {
DescriptorResolver.checkParameterHasNoModifier(trace, parameter)
}
}
@@ -192,7 +192,7 @@ public class LazyTopDownAnalyzer {
private fun registerPrimaryConstructorParameters(klass: JetClass) {
for (jetParameter in klass.getPrimaryConstructorParameters()) {
if (jetParameter.hasValOrVarNode()) {
if (jetParameter.hasValOrVar()) {
c.getPrimaryConstructorParameterProperties().put(jetParameter, lazyDeclarationResolver!!.resolveToDescriptor(jetParameter) as PropertyDescriptor)
}
}
@@ -146,7 +146,7 @@ public class LazyDeclarationResolver {
JetClass jetClass = ((JetPrimaryConstructor) grandFather).getContainingClass();
// This is a primary constructor parameter
ClassDescriptor classDescriptor = getClassDescriptor(jetClass);
if (parameter.hasValOrVarNode()) {
if (parameter.hasValOrVar()) {
classDescriptor.getDefaultType().getMemberScope().getProperties(parameter.getNameAsSafeName());
return getBindingContext().get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, parameter);
}
@@ -33,7 +33,7 @@ public class PsiBasedClassMemberDeclarationProvider(
}
for (parameter in classInfo.getPrimaryConstructorParameters()) {
if (parameter.hasValOrVarNode()) {
if (parameter.hasValOrVar()) {
index.putToIndex(parameter)
}
}
@@ -316,7 +316,7 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
WritableScopeImpl scope = new WritableScopeImpl(JetScope.Empty.INSTANCE$, primaryConstructor, RedeclarationHandler.DO_NOTHING, "Scope with constructor parameters in " + getName());
for (int i = 0; i < originalClassInfo.getPrimaryConstructorParameters().size(); i++) {
JetParameter jetParameter = originalClassInfo.getPrimaryConstructorParameters().get(i);
if (!jetParameter.hasValOrVarNode()) {
if (!jetParameter.hasValOrVar()) {
scope.addVariableDescriptor(primaryConstructor.getValueParameters().get(i));
}
}
@@ -145,7 +145,7 @@ public open class LazyClassMemberScope(
for (parameter in constructor.getValueParameters()) {
if (parameter.getType().isError()) continue
if (!primaryConstructorParameters.get(parameter.getIndex()).hasValOrVarNode()) continue
if (!primaryConstructorParameters.get(parameter.getIndex()).hasValOrVar()) continue
val properties = getProperties(parameter.getName())
if (properties.isEmpty()) continue
@@ -220,7 +220,7 @@ public open class LazyClassMemberScope(
if (name != valueParameterDescriptor.getName()) continue
val parameter = primaryConstructorParameters.get(valueParameterDescriptor.getIndex())
if (parameter.hasValOrVarNode()) {
if (parameter.hasValOrVar()) {
val propertyDescriptor = c.descriptorResolver.resolvePrimaryConstructorParameterToAProperty(
thisDescriptor, valueParameterDescriptor, thisDescriptor.getScopeForClassHeaderResolution(), parameter, trace)
result.add(propertyDescriptor)
@@ -44,7 +44,7 @@ public fun getJvmSignatureDiagnostics(element: PsiElement, otherDiagnostics: Dia
if (element is JetPropertyAccessor) {
parent = parent?.getParent()
}
if (element is JetParameter && element.getValOrVarNode() != null) {
if (element is JetParameter && element.hasValOrVar()) {
// property declared in constructor
val parentClass = (parent?.getParent()?.getParent() as? JetClass)
if (parentClass != null) {