001    /*
002     * Copyright 2010-2013 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.jet.lang.psi.stubs.elements;
018    
019    import com.intellij.lang.ASTNode;
020    import com.intellij.psi.PsiElement;
021    import com.intellij.psi.stubs.IndexSink;
022    import com.intellij.psi.stubs.StubElement;
023    import com.intellij.psi.stubs.StubInputStream;
024    import com.intellij.psi.stubs.StubOutputStream;
025    import com.intellij.util.io.StringRef;
026    import org.jetbrains.annotations.NonNls;
027    import org.jetbrains.annotations.NotNull;
028    import org.jetbrains.jet.lang.psi.JetDeclaration;
029    import org.jetbrains.jet.lang.psi.JetFile;
030    import org.jetbrains.jet.lang.psi.JetNamedFunction;
031    import org.jetbrains.jet.lang.psi.stubs.PsiJetFunctionStub;
032    import org.jetbrains.jet.lang.psi.stubs.impl.PsiJetFunctionStubImpl;
033    import org.jetbrains.jet.lang.resolve.lazy.ResolveSessionUtils;
034    import org.jetbrains.jet.lang.resolve.name.FqName;
035    
036    import java.io.IOException;
037    
038    public class JetFunctionElementType extends JetStubElementType<PsiJetFunctionStub, JetNamedFunction> {
039    
040        public JetFunctionElementType(@NotNull @NonNls String debugName) {
041            super(debugName, JetNamedFunction.class, PsiJetFunctionStub.class);
042        }
043    
044        @Override
045        public PsiJetFunctionStub createStub(@NotNull JetNamedFunction psi, @NotNull StubElement parentStub) {
046            boolean isTopLevel = psi.getParent() instanceof JetFile;
047            boolean isExtension = psi.getReceiverTypeRef() != null;
048            FqName fqName = ResolveSessionUtils.safeFqNameForLazyResolve(psi);
049            boolean hasBlockBody = psi.hasBlockBody();
050            boolean hasBody = psi.hasBody();
051            return new PsiJetFunctionStubImpl(parentStub, StringRef.fromString(psi.getName()), isTopLevel, fqName,
052                                              isExtension, hasBlockBody, hasBody, psi.hasTypeParameterListBeforeFunctionName());
053        }
054    
055        @Override
056        public void serialize(@NotNull PsiJetFunctionStub stub, @NotNull StubOutputStream dataStream) throws IOException {
057            dataStream.writeName(stub.getName());
058            dataStream.writeBoolean(stub.isTopLevel());
059    
060            FqName fqName = stub.getFqName();
061            dataStream.writeName(fqName != null ? fqName.asString() : null);
062    
063            dataStream.writeBoolean(stub.isExtension());
064            dataStream.writeBoolean(stub.hasBlockBody());
065            dataStream.writeBoolean(stub.hasBody());
066            dataStream.writeBoolean(stub.hasTypeParameterListBeforeFunctionName());
067        }
068    
069        @NotNull
070        @Override
071        public PsiJetFunctionStub deserialize(@NotNull StubInputStream dataStream, StubElement parentStub) throws IOException {
072            StringRef name = dataStream.readName();
073            boolean isTopLevel = dataStream.readBoolean();
074    
075            StringRef fqNameAsString = dataStream.readName();
076            FqName fqName = fqNameAsString != null ? new FqName(fqNameAsString.toString()) : null;
077    
078            boolean isExtension = dataStream.readBoolean();
079            boolean hasBlockBody = dataStream.readBoolean();
080            boolean hasBody = dataStream.readBoolean();
081            boolean hasTypeParameterListBeforeFunctionName = dataStream.readBoolean();
082    
083            return new PsiJetFunctionStubImpl(parentStub, name, isTopLevel, fqName, isExtension, hasBlockBody, hasBody,
084                                              hasTypeParameterListBeforeFunctionName);
085        }
086    
087        @Override
088        public void indexStub(@NotNull PsiJetFunctionStub stub, @NotNull IndexSink sink) {
089            StubIndexServiceFactory.getInstance().indexFunction(stub, sink);
090        }
091    }