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.navigation.ItemPresentation;
021    import com.intellij.navigation.ItemPresentationProviders;
022    import org.jetbrains.annotations.NotNull;
023    import org.jetbrains.annotations.Nullable;
024    import org.jetbrains.jet.lang.psi.stubs.PsiJetParameterStub;
025    import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes;
026    import org.jetbrains.jet.lexer.JetTokens;
027    
028    public class JetParameter extends JetNamedDeclarationStub<PsiJetParameterStub> {
029    
030        public JetParameter(@NotNull ASTNode node) {
031            super(node);
032        }
033    
034        public JetParameter(@NotNull PsiJetParameterStub stub) {
035            super(stub, JetStubElementTypes.VALUE_PARAMETER);
036        }
037    
038        @Override
039        public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
040            return visitor.visitParameter(this, data);
041        }
042    
043        @Nullable
044        public JetTypeReference getTypeReference() {
045            return getStubOrPsiChild(JetStubElementTypes.TYPE_REFERENCE);
046        }
047    
048        public boolean hasDefaultValue() {
049            PsiJetParameterStub stub = getStub();
050            if (stub != null) {
051                return stub.hasDefaultValue();
052            }
053            return getDefaultValue() != null;
054        }
055    
056        @Nullable
057        public JetExpression getDefaultValue() {
058            PsiJetParameterStub stub = getStub();
059            if (stub != null && !stub.hasDefaultValue()) {
060                return null;
061            }
062            boolean passedEQ = false;
063            ASTNode child = getNode().getFirstChildNode();
064            while (child != null) {
065                if (child.getElementType() == JetTokens.EQ) passedEQ = true;
066                if (passedEQ && child.getPsi() instanceof JetExpression) {
067                    return (JetExpression) child.getPsi();
068                }
069                child = child.getTreeNext();
070            }
071    
072            return null;
073        }
074    
075        public boolean isMutable() {
076            PsiJetParameterStub stub = getStub();
077            if (stub != null) {
078                return stub.isMutable();
079            }
080    
081            return findChildByType(JetTokens.VAR_KEYWORD) != null;
082        }
083    
084        public boolean isVarArg() {
085            JetModifierList modifierList = getModifierList();
086            return modifierList != null && modifierList.hasModifier(JetTokens.VARARG_KEYWORD);
087        }
088    
089        public boolean hasValOrVarNode() {
090            PsiJetParameterStub stub = getStub();
091            if (stub != null) {
092                return stub.hasValOrValNode();
093            }
094            return getValOrVarNode() != null;
095        }
096    
097        @Nullable
098        public ASTNode getValOrVarNode() {
099            PsiJetParameterStub stub = getStub();
100            if (stub != null && !stub.hasValOrValNode()) {
101                return null;
102            }
103            ASTNode val = getNode().findChildByType(JetTokens.VAL_KEYWORD);
104            if (val != null) return val;
105    
106            return getNode().findChildByType(JetTokens.VAR_KEYWORD);
107        }
108    
109        @Override
110        public ItemPresentation getPresentation() {
111            return ItemPresentationProviders.getItemPresentation(this);
112        }
113    
114        public boolean isLoopParameter() {
115            return getParent() instanceof JetForExpression;
116        }
117    }