From a3649c47c04f26a1f8bfa8886e51f6dd19beea27 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Tue, 19 Jun 2012 21:30:41 +0400 Subject: [PATCH] JetTypeParameter stub --- .../src/org/jetbrains/jet/JetNodeTypes.java | 2 +- .../org/jetbrains/jet/lang/psi/JetClass.java | 5 + .../jet/lang/psi/JetTypeParameter.java | 22 ++++- .../lang/psi/JetTypeParameterListOwner.java | 7 -- .../JetTypeParameterListOwnerNotStubbed.java | 6 +- .../psi/JetTypeParameterListOwnerStub.java | 10 +- .../jet/lang/psi/stubs/PsiJetClassStub.java | 7 +- .../psi/stubs/PsiJetTypeParameterStub.java | 29 ++++++ .../stubs/elements/JetClassElementType.java | 21 +++-- .../stubs/elements/JetFileElementType.java | 2 +- .../stubs/elements/JetStubElementTypes.java | 2 + .../elements/JetTypeParameterElementType.java | 93 +++++++++++++++++++ .../psi/stubs/impl/PsiJetClassStubImpl.java | 30 +++--- .../impl/PsiJetTypeParameterStubImpl.java | 68 ++++++++++++++ .../jet/plugin/stubs/JetStubsTest.java | 8 ++ 15 files changed, 261 insertions(+), 51 deletions(-) create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/PsiJetTypeParameterStub.java create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetTypeParameterElementType.java create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/impl/PsiJetTypeParameterStubImpl.java diff --git a/compiler/frontend/src/org/jetbrains/jet/JetNodeTypes.java b/compiler/frontend/src/org/jetbrains/jet/JetNodeTypes.java index 37f74c04113..efdd67a211c 100644 --- a/compiler/frontend/src/org/jetbrains/jet/JetNodeTypes.java +++ b/compiler/frontend/src/org/jetbrains/jet/JetNodeTypes.java @@ -43,7 +43,7 @@ public interface JetNodeTypes { JetNodeType ANONYMOUS_INITIALIZER = new JetNodeType("ANONYMOUS_INITIALIZER", JetClassInitializer.class); JetNodeType TYPE_PARAMETER_LIST = new JetNodeType("TYPE_PARAMETER_LIST", JetTypeParameterList.class); - JetNodeType TYPE_PARAMETER = new JetNodeType("TYPE_PARAMETER", JetTypeParameter.class); + IElementType TYPE_PARAMETER = JetStubElementTypes.TYPE_PARAMETER; JetNodeType DELEGATION_SPECIFIER_LIST = new JetNodeType("DELEGATION_SPECIFIER_LIST", JetDelegationSpecifierList.class); JetNodeType DELEGATOR_BY = new JetNodeType("DELEGATOR_BY", JetDelegatorByExpressionSpecifier.class); JetNodeType DELEGATOR_SUPER_CALL = new JetNodeType("DELEGATOR_SUPER_CALL", JetDelegatorToSuperCall.class); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetClass.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetClass.java index dd30881604f..01d967a53fe 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetClass.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetClass.java @@ -145,6 +145,11 @@ public class JetClass extends JetTypeParameterListOwnerStub imp } public boolean isTrait() { + PsiJetClassStub stub = getStub(); + if (stub != null) { + return stub.isTrait(); + } + return findChildByType(JetTokens.TRAIT_KEYWORD) != null; } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeParameter.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeParameter.java index bbf44880fb5..9315f2703db 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeParameter.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeParameter.java @@ -17,20 +17,27 @@ package org.jetbrains.jet.lang.psi; import com.intellij.lang.ASTNode; +import com.intellij.psi.stubs.IStubElementType; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.JetNodeTypes; +import org.jetbrains.jet.lang.psi.stubs.PsiJetTypeParameterStub; +import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes; import org.jetbrains.jet.lang.types.Variance; import org.jetbrains.jet.lexer.JetTokens; /** * @author max */ -public class JetTypeParameter extends JetNamedDeclarationNotStubbed { +public class JetTypeParameter extends JetNamedDeclarationStub { public JetTypeParameter(@NotNull ASTNode node) { super(node); } + public JetTypeParameter(@NotNull PsiJetTypeParameterStub stub, @NotNull IStubElementType nodeType) { + super(stub, nodeType); + } + @Override public void accept(@NotNull JetVisitorVoid visitor) { visitor.visitTypeParameter(this); @@ -43,6 +50,13 @@ public class JetTypeParameter extends JetNamedDeclarationNotStubbed { @NotNull public Variance getVariance() { + PsiJetTypeParameterStub stub = getStub(); + if (stub != null) { + if (stub.isOutVariance()) return Variance.OUT_VARIANCE; + if (stub.isInVariance()) return Variance.IN_VARIANCE; + return Variance.INVARIANT; + } + JetModifierList modifierList = getModifierList(); if (modifierList == null) return Variance.INVARIANT; @@ -55,4 +69,10 @@ public class JetTypeParameter extends JetNamedDeclarationNotStubbed { public JetTypeReference getExtendsBound() { return (JetTypeReference) findChildByType(JetNodeTypes.TYPE_REFERENCE); } + + @NotNull + @Override + public IStubElementType getElementType() { + return JetStubElementTypes.TYPE_PARAMETER; + } } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeParameterListOwner.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeParameterListOwner.java index 01d20696cf8..ea8914f7012 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeParameterListOwner.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeParameterListOwner.java @@ -17,7 +17,6 @@ package org.jetbrains.jet.lang.psi; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.util.List; @@ -25,12 +24,6 @@ import java.util.List; * @author Nikolay Krasko */ public interface JetTypeParameterListOwner extends JetNamedDeclaration { - @Nullable - JetTypeParameterList getTypeParameterList(); - - @Nullable - JetTypeConstraintList getTypeConstraintList(); - @NotNull List getTypeConstraints(); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeParameterListOwnerNotStubbed.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeParameterListOwnerNotStubbed.java index 52164e0b8f1..f5ac555e4f7 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeParameterListOwnerNotStubbed.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeParameterListOwnerNotStubbed.java @@ -34,15 +34,13 @@ abstract class JetTypeParameterListOwnerNotStubbed extends JetNamedDeclarationNo super(node); } - @Override @Nullable - public JetTypeParameterList getTypeParameterList() { + JetTypeParameterList getTypeParameterList() { return (JetTypeParameterList) findChildByType(JetNodeTypes.TYPE_PARAMETER_LIST); } - @Override @Nullable - public JetTypeConstraintList getTypeConstraintList() { + JetTypeConstraintList getTypeConstraintList() { return (JetTypeConstraintList) findChildByType(JetNodeTypes.TYPE_CONSTRAINT_LIST); } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeParameterListOwnerStub.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeParameterListOwnerStub.java index 9ef112122e1..6150a3ed2f9 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeParameterListOwnerStub.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeParameterListOwnerStub.java @@ -18,7 +18,7 @@ package org.jetbrains.jet.lang.psi; import com.intellij.lang.ASTNode; import com.intellij.psi.stubs.IStubElementType; -import com.intellij.psi.stubs.StubElement; +import com.intellij.psi.stubs.NamedStub; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.JetNodeTypes; @@ -29,7 +29,7 @@ import java.util.List; /** * @author Nikolay Krasko */ -abstract class JetTypeParameterListOwnerStub extends JetNamedDeclarationStub implements JetTypeParameterListOwner { +abstract class JetTypeParameterListOwnerStub extends JetNamedDeclarationStub implements JetTypeParameterListOwner { public JetTypeParameterListOwnerStub(@NotNull T stub, @NotNull IStubElementType nodeType) { super(stub, nodeType); } @@ -38,15 +38,13 @@ abstract class JetTypeParameterListOwnerStub extends JetN super(node); } - @Override @Nullable - public JetTypeParameterList getTypeParameterList() { + JetTypeParameterList getTypeParameterList() { return (JetTypeParameterList) findChildByType(JetNodeTypes.TYPE_PARAMETER_LIST); } - @Override @Nullable - public JetTypeConstraintList getTypeConstraintList() { + JetTypeConstraintList getTypeConstraintList() { return (JetTypeConstraintList) findChildByType(JetNodeTypes.TYPE_CONSTRAINT_LIST); } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/PsiJetClassStub.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/PsiJetClassStub.java index 3c74702a4e6..94cc619a8a8 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/PsiJetClassStub.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/PsiJetClassStub.java @@ -28,15 +28,12 @@ import java.util.List; * @author Nikolay Krasko */ public interface PsiJetClassStub extends NamedStub { - - @NonNls @Nullable String getQualifiedName(); + boolean isTrait(); + @NotNull List getSuperNames(); - - boolean isDeprecated(); - boolean hasDeprecatedAnnotation(); } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/PsiJetTypeParameterStub.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/PsiJetTypeParameterStub.java new file mode 100644 index 00000000000..aebe251a98b --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/PsiJetTypeParameterStub.java @@ -0,0 +1,29 @@ +/* + * Copyright 2010-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.lang.psi.stubs; + +import com.intellij.psi.stubs.NamedStub; +import org.jetbrains.jet.lang.psi.JetTypeParameter; + +/** + * @author Nikolay Krasko + */ +public interface PsiJetTypeParameterStub extends NamedStub { + String getExtendBoundTypeText(); + boolean isInVariance(); + boolean isOutVariance(); +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetClassElementType.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetClassElementType.java index af946560ae0..b72b96b51db 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetClassElementType.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetClassElementType.java @@ -63,14 +63,16 @@ public class JetClassElementType extends JetStubElementType superNames = stub.getSuperNames(); + dataStream.writeBoolean(stub.isTrait()); + + List superNames = stub.getSuperNames(); dataStream.writeVarInt(superNames.size()); for (String name : superNames) { dataStream.writeName(name); @@ -79,18 +81,17 @@ public class JetClassElementType extends JetStubElementType { - public static final int STUB_VERSION = 5; + public static final int STUB_VERSION = 6; public JetFileElementType() { super("jet.FILE", JetLanguage.INSTANCE); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetStubElementTypes.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetStubElementTypes.java index 0fbcfcb38ed..0cdf963e265 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetStubElementTypes.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetStubElementTypes.java @@ -24,4 +24,6 @@ public interface JetStubElementTypes { JetClassElementType CLASS = new JetClassElementType("CLASS"); JetFunctionElementType FUNCTION = new JetFunctionElementType("FUN"); + + JetTypeParameterElementType TYPE_PARAMETER = new JetTypeParameterElementType("TYPE_PARAMETER"); } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetTypeParameterElementType.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetTypeParameterElementType.java new file mode 100644 index 00000000000..cc3006b717f --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetTypeParameterElementType.java @@ -0,0 +1,93 @@ +/* + * Copyright 2010-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.lang.psi.stubs.elements; + +import com.intellij.lang.ASTNode; +import com.intellij.lang.LighterAST; +import com.intellij.lang.LighterASTNode; +import com.intellij.psi.stubs.IndexSink; +import com.intellij.psi.stubs.StubElement; +import com.intellij.psi.stubs.StubInputStream; +import com.intellij.psi.stubs.StubOutputStream; +import com.intellij.util.io.StringRef; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.psi.JetTypeParameter; +import org.jetbrains.jet.lang.psi.JetTypeReference; +import org.jetbrains.jet.lang.psi.stubs.PsiJetTypeParameterStub; +import org.jetbrains.jet.lang.psi.stubs.impl.PsiJetTypeParameterStubImpl; +import org.jetbrains.jet.lang.types.Variance; + +import java.io.IOException; + +/** + * @author Nikolay Krasko + */ +public class JetTypeParameterElementType extends JetStubElementType { + public JetTypeParameterElementType(@NotNull @NonNls String debugName) { + super(debugName); + } + + @Override + public JetTypeParameter createPsiFromAst(@NotNull ASTNode node) { + return new JetTypeParameter(node); + } + + @Override + public PsiJetTypeParameterStub createStub(LighterAST tree, LighterASTNode node, StubElement parentStub) { + return null; + } + + @Override + public JetTypeParameter createPsi(@NotNull PsiJetTypeParameterStub stub) { + return new JetTypeParameter(stub, JetStubElementTypes.TYPE_PARAMETER); + } + + @Override + public PsiJetTypeParameterStub createStub(@NotNull JetTypeParameter psi, StubElement parentStub) { + JetTypeReference extendsBound = psi.getExtendsBound(); + return new PsiJetTypeParameterStubImpl(JetStubElementTypes.TYPE_PARAMETER, parentStub, + psi.getName(), + extendsBound != null ? extendsBound.getText() : null, + psi.getVariance() == Variance.IN_VARIANCE, + psi.getVariance() == Variance.OUT_VARIANCE); + } + + @Override + public void serialize(PsiJetTypeParameterStub stub, StubOutputStream dataStream) throws IOException { + dataStream.writeName(stub.getName()); + dataStream.writeName(stub.getExtendBoundTypeText()); + dataStream.writeBoolean(stub.isInVariance()); + dataStream.writeBoolean(stub.isOutVariance()); + } + + @Override + public PsiJetTypeParameterStub deserialize(StubInputStream dataStream, StubElement parentStub) throws IOException { + StringRef name = dataStream.readName(); + StringRef extendBoundTypeText = dataStream.readName(); + boolean isInVariance = dataStream.readBoolean(); + boolean isOutVariance = dataStream.readBoolean(); + + return new PsiJetTypeParameterStubImpl(JetStubElementTypes.TYPE_PARAMETER, parentStub, + name, extendBoundTypeText, isInVariance, isOutVariance); + } + + @Override + public void indexStub(PsiJetTypeParameterStub stub, IndexSink sink) { + // No index + } +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/impl/PsiJetClassStubImpl.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/impl/PsiJetClassStubImpl.java index 34de79d8d1b..8a6db9da76d 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/impl/PsiJetClassStubImpl.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/impl/PsiJetClassStubImpl.java @@ -36,15 +36,16 @@ public class PsiJetClassStubImpl extends StubBase implements PsiJetCla private final StringRef qualifiedName; private final StringRef name; private final StringRef[] superNames; + private final boolean isTrait; public PsiJetClassStubImpl( JetClassElementType type, - final StubElement parent, + StubElement parent, @Nullable final String qualifiedName, - final String name, - final List superNames) { - - this(type, parent, StringRef.fromString(qualifiedName), StringRef.fromString(name), wrapStrings(superNames)); + String name, + List superNames, + boolean isTrait) { + this(type, parent, StringRef.fromString(qualifiedName), StringRef.fromString(name), wrapStrings(superNames), isTrait); } private static StringRef[] wrapStrings(List names) { @@ -57,15 +58,17 @@ public class PsiJetClassStubImpl extends StubBase implements PsiJetCla public PsiJetClassStubImpl( JetClassElementType type, - final StubElement parent, - final StringRef qualifiedName, - final StringRef name, - final StringRef[] superNames) { + StubElement parent, + StringRef qualifiedName, + StringRef name, + StringRef[] superNames, + boolean isTrait) { super(parent, type); this.qualifiedName = qualifiedName; this.name = name; this.superNames = superNames; + this.isTrait = isTrait; } @Override @@ -74,13 +77,8 @@ public class PsiJetClassStubImpl extends StubBase implements PsiJetCla } @Override - public boolean isDeprecated() { - return false; - } - - @Override - public boolean hasDeprecatedAnnotation() { - return false; + public boolean isTrait() { + return isTrait; } @Override diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/impl/PsiJetTypeParameterStubImpl.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/impl/PsiJetTypeParameterStubImpl.java new file mode 100644 index 00000000000..48884d2dfc8 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/impl/PsiJetTypeParameterStubImpl.java @@ -0,0 +1,68 @@ +/* + * Copyright 2010-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.lang.psi.stubs.impl; + +import com.intellij.psi.stubs.StubBase; +import com.intellij.psi.stubs.StubElement; +import com.intellij.util.io.StringRef; +import org.jetbrains.jet.lang.psi.JetTypeParameter; +import org.jetbrains.jet.lang.psi.stubs.PsiJetTypeParameterStub; +import org.jetbrains.jet.lang.psi.stubs.elements.JetTypeParameterElementType; + +/** + * @author Nikolay Krasko + */ +public class PsiJetTypeParameterStubImpl extends StubBase implements PsiJetTypeParameterStub { + private final StringRef name; + private final StringRef extendBoundTypeText; + private final boolean isInVariance; + private final boolean isOutVariance; + + public PsiJetTypeParameterStubImpl(JetTypeParameterElementType type, final StubElement parent, + StringRef name, StringRef extendBoundTypeText, boolean isInVariance, boolean isOutVariance) { + super(parent, type); + this.name = name; + this.extendBoundTypeText = extendBoundTypeText; + this.isInVariance = isInVariance; + this.isOutVariance = isOutVariance; + } + + public PsiJetTypeParameterStubImpl(JetTypeParameterElementType type, final StubElement parent, + String name, String extendBoundTypeText, boolean isInVariance, boolean isOutVariance) { + this(type, parent, StringRef.fromString(name), StringRef.fromString(extendBoundTypeText), isInVariance, isOutVariance); + } + + @Override + public String getExtendBoundTypeText() { + return StringRef.toString(extendBoundTypeText); + } + + @Override + public boolean isInVariance() { + return isInVariance; + } + + @Override + public boolean isOutVariance() { + return isOutVariance; + } + + @Override + public String getName() { + return StringRef.toString(name); + } +} diff --git a/idea/tests/org/jetbrains/jet/plugin/stubs/JetStubsTest.java b/idea/tests/org/jetbrains/jet/plugin/stubs/JetStubsTest.java index 973e87e16bd..64c331f67cb 100644 --- a/idea/tests/org/jetbrains/jet/plugin/stubs/JetStubsTest.java +++ b/idea/tests/org/jetbrains/jet/plugin/stubs/JetStubsTest.java @@ -47,4 +47,12 @@ public class JetStubsTest extends LightCodeInsightFixtureTestCase { final List names = stub.getSuperNames(); assertSameElements(names, "ArrayList", "alist"); } + + public void testClassIsTrait() { + PsiFile psiFile = myFixture.configureByText("foo.kt", "trait Test { }"); + final List declarations = ((JetFile) psiFile).getDeclarations(); + final JetClass jetClass = (JetClass) declarations.get(0); + final PsiJetClassStub stub = JetStubElementTypes.CLASS.createStub(jetClass, null); + assertEquals(true, stub.isTrait()); + } }