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;
018    
019    import com.intellij.lang.ASTNode;
020    import com.intellij.psi.PsiElement;
021    import com.intellij.psi.tree.IElementType;
022    import org.jetbrains.annotations.NotNull;
023    import org.jetbrains.annotations.Nullable;
024    import org.jetbrains.jet.JetNodeTypes;
025    import org.jetbrains.jet.lexer.JetTokens;
026    
027    import java.util.Collections;
028    import java.util.List;
029    
030    abstract public class JetFunctionNotStubbed extends JetTypeParameterListOwnerNotStubbed
031            implements JetFunction {
032    
033        public JetFunctionNotStubbed(@NotNull ASTNode node) {
034            super(node);
035        }
036    
037        @Override
038        @Nullable
039        public JetParameterList getValueParameterList() {
040            return (JetParameterList) findChildByType(JetNodeTypes.VALUE_PARAMETER_LIST);
041        }
042    
043        @Override
044        @NotNull
045        public List<JetParameter> getValueParameters() {
046            JetParameterList list = getValueParameterList();
047            return list != null ? list.getParameters() : Collections.<JetParameter>emptyList();
048        }
049    
050        @Override
051        @Nullable
052        public JetExpression getBodyExpression() {
053            return findChildByClass(JetExpression.class);
054        }
055    
056        @Override
057        public boolean hasDeclaredReturnType() {
058            return getReturnTypeRef() != null;
059        }
060    
061        @Override
062        @Nullable
063        public JetTypeReference getReceiverTypeRef() {
064            PsiElement child = getFirstChild();
065            while (child != null) {
066                IElementType tt = child.getNode().getElementType();
067                if (tt == JetTokens.LPAR || tt == JetTokens.COLON) break;
068                if (child instanceof JetTypeReference) {
069                    return (JetTypeReference) child;
070                }
071                child = child.getNextSibling();
072            }
073    
074            return null;
075        }
076    
077        @Override
078        @Nullable
079        public JetTypeReference getReturnTypeRef() {
080            boolean colonPassed = false;
081            PsiElement child = getFirstChild();
082            while (child != null) {
083                IElementType tt = child.getNode().getElementType();
084                if (tt == JetTokens.COLON) {
085                    colonPassed = true;
086                }
087                if (colonPassed && child instanceof JetTypeReference) {
088                    return (JetTypeReference) child;
089                }
090                child = child.getNextSibling();
091            }
092    
093            return null;
094        }
095    
096        @NotNull
097        @Override
098        public JetElement asElement() {
099            return this;
100        }
101    
102        @Override
103        public boolean isLocal() {
104            PsiElement parent = getParent();
105            return !(parent instanceof JetFile || parent instanceof JetClassBody);
106        }
107    }