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