From 35121643b159284d2ebfc1890d9de4dfed47d8be Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Mon, 31 Mar 2014 14:09:17 +0400 Subject: [PATCH] Fix stubs for JetModifierList, store modifier tokens in stub --- .../lang/psi/JetDeclarationModifierList.java | 32 ++++++++ .../jet/lang/psi/JetModifierList.java | 16 ++-- .../JetPrimaryConstructorModifierList.java | 32 ++++++++ .../psi/stubs/PsiJetModifierListStub.java | 26 ++++++ .../elements/JetModifierListElementType.java | 33 +++++++- .../stubs/elements/JetStubElementTypes.java | 9 ++- .../impl/PsiJetModifierListStubImpl.java | 79 +++++++++++++++++++ .../org/jetbrains/jet/lexer/JetTokens.java | 5 ++ 8 files changed, 218 insertions(+), 14 deletions(-) create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/psi/JetDeclarationModifierList.java create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPrimaryConstructorModifierList.java create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/PsiJetModifierListStub.java create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/impl/PsiJetModifierListStubImpl.java diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetDeclarationModifierList.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetDeclarationModifierList.java new file mode 100644 index 00000000000..fa63ed09992 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetDeclarationModifierList.java @@ -0,0 +1,32 @@ +/* + * Copyright 2010-2014 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; + +import com.intellij.lang.ASTNode; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.psi.stubs.PsiJetModifierListStub; +import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes; + +public class JetDeclarationModifierList extends JetModifierList { + public JetDeclarationModifierList(@NotNull ASTNode node) { + super(node); + } + + public JetDeclarationModifierList(@NotNull PsiJetModifierListStub stub) { + super(stub, JetStubElementTypes.MODIFIER_LIST); + } +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetModifierList.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetModifierList.java index 2ef8b38f2d3..8080b8f36f1 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetModifierList.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetModifierList.java @@ -19,12 +19,11 @@ package org.jetbrains.jet.lang.psi; import com.google.common.collect.Lists; import com.intellij.lang.ASTNode; import com.intellij.psi.PsiElement; +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.PsiJetPlaceHolderStub; -import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes; -import org.jetbrains.jet.lexer.JetKeywordToken; +import org.jetbrains.jet.lang.psi.stubs.PsiJetModifierListStub; import org.jetbrains.jet.lexer.JetModifierKeywordToken; import org.jetbrains.jet.lexer.JetToken; @@ -32,13 +31,14 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -public class JetModifierList extends JetElementImplStub { - public JetModifierList(@NotNull ASTNode node) { - super(node); +public abstract class JetModifierList extends JetElementImplStub { + + public JetModifierList(@NotNull PsiJetModifierListStub stub, @NotNull IStubElementType nodeType) { + super(stub, nodeType); } - public JetModifierList(@NotNull PsiJetPlaceHolderStub stub) { - super(stub, JetStubElementTypes.MODIFIER_LIST); + public JetModifierList(@NotNull ASTNode node) { + super(node); } @Override diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPrimaryConstructorModifierList.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPrimaryConstructorModifierList.java new file mode 100644 index 00000000000..704f273af8d --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPrimaryConstructorModifierList.java @@ -0,0 +1,32 @@ +/* + * Copyright 2010-2014 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; + +import com.intellij.lang.ASTNode; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.psi.stubs.PsiJetModifierListStub; +import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes; + +public class JetPrimaryConstructorModifierList extends JetModifierList { + public JetPrimaryConstructorModifierList(@NotNull ASTNode node) { + super(node); + } + + public JetPrimaryConstructorModifierList(@NotNull PsiJetModifierListStub stub) { + super(stub, JetStubElementTypes.PRIMARY_CONSTRUCTOR_MODIFIER_LIST); + } +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/PsiJetModifierListStub.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/PsiJetModifierListStub.java new file mode 100644 index 00000000000..43e8d27bcf0 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/PsiJetModifierListStub.java @@ -0,0 +1,26 @@ +/* + * Copyright 2010-2014 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.StubElement; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.psi.JetModifierList; +import org.jetbrains.jet.lexer.JetModifierKeywordToken; + +public interface PsiJetModifierListStub extends StubElement { + boolean hasModifier(@NotNull JetModifierKeywordToken modifierToken); +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetModifierListElementType.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetModifierListElementType.java index 5c89b91696f..62e5906ee47 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetModifierListElementType.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/elements/JetModifierListElementType.java @@ -16,12 +16,39 @@ package org.jetbrains.jet.lang.psi.stubs.elements; +import com.intellij.psi.stubs.StubElement; +import com.intellij.psi.stubs.StubInputStream; +import com.intellij.psi.stubs.StubOutputStream; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.lang.psi.JetModifierList; +import org.jetbrains.jet.lang.psi.stubs.PsiJetModifierListStub; +import org.jetbrains.jet.lang.psi.stubs.impl.PsiJetModifierListStubImpl; -public class JetModifierListElementType extends JetPlaceHolderStubElementType { - public JetModifierListElementType(@NotNull @NonNls String debugName) { - super(debugName, JetModifierList.class); +import java.io.IOException; + +import static org.jetbrains.jet.lang.psi.stubs.impl.PsiJetModifierListStubImpl.computeMaskFromPsi; + +public class JetModifierListElementType extends JetStubElementType { + public JetModifierListElementType(@NotNull @NonNls String debugName, @NotNull Class psiClass) { + super(debugName, psiClass, PsiJetModifierListStub.class); + } + + @Override + public PsiJetModifierListStub createStub(@NotNull T psi, StubElement parentStub) { + return new PsiJetModifierListStubImpl(parentStub, computeMaskFromPsi(psi), this); + } + + @Override + public void serialize(@NotNull PsiJetModifierListStub stub, @NotNull StubOutputStream dataStream) throws IOException { + int mask = ((PsiJetModifierListStubImpl) stub).getMask(); + dataStream.writeVarInt(mask); + } + + @NotNull + @Override + public PsiJetModifierListStub deserialize(@NotNull StubInputStream dataStream, StubElement parentStub) throws IOException { + int mask = dataStream.readVarInt(); + return new PsiJetModifierListStubImpl(parentStub, mask, this); } } 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 fa6a5a2d762..cebd30e3425 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 @@ -27,7 +27,7 @@ public interface JetStubElementTypes { JetClassElementType ENUM_ENTRY = new JetClassElementType("ENUM_ENTRY"); JetObjectElementType OBJECT_DECLARATION = new JetObjectElementType("OBJECT_DECLARATION"); JetPlaceHolderStubElementType CLASS_OBJECT = - new JetPlaceHolderStubElementType("CLASS_OBJECT", JetClassObject.class); + new JetPlaceHolderStubElementType("CLASS_OBJECT", JetClassObject.class); JetParameterElementType VALUE_PARAMETER = new JetParameterElementType("VALUE_PARAMETER"); JetPlaceHolderStubElementType VALUE_PARAMETER_LIST = @@ -49,8 +49,11 @@ public interface JetStubElementTypes { JetImportDirectiveElementType IMPORT_DIRECTIVE = new JetImportDirectiveElementType("IMPORT_DIRECTIVE"); - JetModifierListElementType MODIFIER_LIST = new JetModifierListElementType("MODIFIER_LIST"); - JetModifierListElementType PRIMARY_CONSTRUCTOR_MODIFIER_LIST = new JetModifierListElementType("PRIMARY_CONSTRUCTOR_MODIFIER_LIST"); + JetModifierListElementType MODIFIER_LIST = + new JetModifierListElementType("MODIFIER_LIST", JetDeclarationModifierList.class); + + JetModifierListElementType PRIMARY_CONSTRUCTOR_MODIFIER_LIST = + new JetModifierListElementType("PRIMARY_CONSTRUCTOR_MODIFIER_LIST", JetPrimaryConstructorModifierList.class); JetPlaceHolderStubElementType TYPE_CONSTRAINT_LIST = new JetPlaceHolderStubElementType("TYPE_CONSTRAINT_LIST", JetTypeConstraintList.class); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/impl/PsiJetModifierListStubImpl.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/impl/PsiJetModifierListStubImpl.java new file mode 100644 index 00000000000..4187144f43a --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/stubs/impl/PsiJetModifierListStubImpl.java @@ -0,0 +1,79 @@ +/* + * Copyright 2010-2014 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.ArrayUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.psi.JetModifierList; +import org.jetbrains.jet.lang.psi.stubs.PsiJetModifierListStub; +import org.jetbrains.jet.lang.psi.stubs.elements.JetModifierListElementType; +import org.jetbrains.jet.lexer.JetModifierKeywordToken; + +import static org.jetbrains.jet.lexer.JetTokens.MODIFIER_KEYWORDS_ARRAY; + +public class PsiJetModifierListStubImpl extends StubBase implements PsiJetModifierListStub { + + static { + assert MODIFIER_KEYWORDS_ARRAY.length <= 32 : "Current implementation depends on the ability to represent modifier list as bit mask"; + } + + public static int computeMaskFromPsi(@NotNull JetModifierList modifierList) { + int mask = 0; + JetModifierKeywordToken[] orderedKeywords = MODIFIER_KEYWORDS_ARRAY; + for (int i = 0; i < orderedKeywords.length; i++) { + JetModifierKeywordToken modifierKeywordToken = orderedKeywords[i]; + if (modifierList.hasModifier(modifierKeywordToken)) { + mask |= 1 << i; + } + } + return mask; + } + + private final int mask; + + public PsiJetModifierListStubImpl(StubElement parent, int mask, @NotNull JetModifierListElementType elementType) { + super(parent, elementType); + this.mask = mask; + } + + @Override + public boolean hasModifier(@NotNull JetModifierKeywordToken modifierToken) { + int index = ArrayUtil.indexOf(MODIFIER_KEYWORDS_ARRAY, modifierToken); + assert index >= 0 : "All JetModifierKeywordTokens should present in MODIFIER_KEYWORDS_ARRAY"; + return (mask & (1 << index)) != 0; + } + + public int getMask() { + return mask; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(super.toString()); + sb.append("["); + for (JetModifierKeywordToken modifierKeyword : MODIFIER_KEYWORDS_ARRAY) { + if (hasModifier(modifierKeyword)) { + sb.append(modifierKeyword.getValue()).append(" "); + } + } + sb.append("]"); + return sb.toString(); + } +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lexer/JetTokens.java b/compiler/frontend/src/org/jetbrains/jet/lexer/JetTokens.java index ec6a5e0d61d..e5b5a034690 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lexer/JetTokens.java +++ b/compiler/frontend/src/org/jetbrains/jet/lexer/JetTokens.java @@ -169,6 +169,11 @@ public interface JetTokens { CATCH_KEYWORD, FINALLY_KEYWORD, OUT_KEYWORD, FINAL_KEYWORD, VARARG_KEYWORD, REIFIED_KEYWORD ); + /* + This array is used in stub serialization: + 1. Do not change order. + 2. If you add an entry or change order, increase stub version. + */ JetModifierKeywordToken[] MODIFIER_KEYWORDS_ARRAY = new JetModifierKeywordToken[] { ABSTRACT_KEYWORD, ENUM_KEYWORD, OPEN_KEYWORD, INNER_KEYWORD, ANNOTATION_KEYWORD, OVERRIDE_KEYWORD, PRIVATE_KEYWORD,