001    /*
002     * Copyright 2010-2015 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.kotlin.psi;
018    
019    import com.intellij.lang.ASTNode;
020    import com.intellij.navigation.ItemPresentation;
021    import com.intellij.navigation.ItemPresentationProviders;
022    import com.intellij.psi.PsiElement;
023    import com.intellij.psi.tree.TokenSet;
024    import org.jetbrains.annotations.NotNull;
025    import org.jetbrains.annotations.Nullable;
026    import org.jetbrains.kotlin.lexer.JetTokens;
027    import org.jetbrains.kotlin.psi.stubs.KotlinParameterStub;
028    import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes;
029    import org.jetbrains.kotlin.psi.typeRefHelpers.TypeRefHelpersPackage;
030    
031    import java.util.Collections;
032    import java.util.List;
033    
034    public class JetParameter extends JetNamedDeclarationStub<KotlinParameterStub> implements JetCallableDeclaration {
035    
036        public JetParameter(@NotNull ASTNode node) {
037            super(node);
038        }
039    
040        public JetParameter(@NotNull KotlinParameterStub stub) {
041            super(stub, JetStubElementTypes.VALUE_PARAMETER);
042        }
043    
044        @Override
045        public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
046            return visitor.visitParameter(this, data);
047        }
048    
049        @Override
050        @Nullable
051        public JetTypeReference getTypeReference() {
052            return getStubOrPsiChild(JetStubElementTypes.TYPE_REFERENCE);
053        }
054    
055        @Override
056        @Nullable
057        public JetTypeReference setTypeReference(@Nullable JetTypeReference typeRef) {
058            return TypeRefHelpersPackage.setTypeReference(this, getNameIdentifier(), typeRef);
059        }
060    
061        @Nullable
062        @Override
063        public PsiElement getColon() {
064            return findChildByType(JetTokens.COLON);
065        }
066    
067        public boolean hasDefaultValue() {
068            KotlinParameterStub stub = getStub();
069            if (stub != null) {
070                return stub.hasDefaultValue();
071            }
072            return getDefaultValue() != null;
073        }
074    
075        @Nullable
076        public JetExpression getDefaultValue() {
077            KotlinParameterStub stub = getStub();
078            if (stub != null && !stub.hasDefaultValue()) {
079                return null;
080            }
081            boolean passedEQ = false;
082            ASTNode child = getNode().getFirstChildNode();
083            while (child != null) {
084                if (child.getElementType() == JetTokens.EQ) passedEQ = true;
085                if (passedEQ && child.getPsi() instanceof JetExpression) {
086                    return (JetExpression) child.getPsi();
087                }
088                child = child.getTreeNext();
089            }
090    
091            return null;
092        }
093    
094        public boolean isMutable() {
095            KotlinParameterStub stub = getStub();
096            if (stub != null) {
097                return stub.isMutable();
098            }
099    
100            return findChildByType(JetTokens.VAR_KEYWORD) != null;
101        }
102    
103        public boolean isVarArg() {
104            JetModifierList modifierList = getModifierList();
105            return modifierList != null && modifierList.hasModifier(JetTokens.VARARG_KEYWORD);
106        }
107    
108        public boolean hasValOrVar() {
109            KotlinParameterStub stub = getStub();
110            if (stub != null) {
111                return stub.hasValOrVar();
112            }
113            return getValOrVarKeyword() != null;
114        }
115    
116        @Nullable
117        public PsiElement getValOrVarKeyword() {
118            KotlinParameterStub stub = getStub();
119            if (stub != null && !stub.hasValOrVar()) {
120                return null;
121            }
122            return findChildByType(VAL_VAR_TOKEN_SET);
123        }
124    
125        private static final TokenSet VAL_VAR_TOKEN_SET = TokenSet.create(JetTokens.VAL_KEYWORD, JetTokens.VAR_KEYWORD);
126    
127        @Override
128        public ItemPresentation getPresentation() {
129            return ItemPresentationProviders.getItemPresentation(this);
130        }
131    
132        public boolean isLoopParameter() {
133            return getParent() instanceof JetForExpression;
134        }
135    
136        @Nullable
137        @Override
138        public JetParameterList getValueParameterList() {
139            return null;
140        }
141    
142        @NotNull
143        @Override
144        public List<JetParameter> getValueParameters() {
145            return Collections.emptyList();
146        }
147    
148        @Nullable
149        @Override
150        public JetTypeReference getReceiverTypeReference() {
151            return null;
152        }
153    
154        @Nullable
155        @Override
156        public JetTypeParameterList getTypeParameterList() {
157            return null;
158        }
159    
160        @Nullable
161        @Override
162        public JetTypeConstraintList getTypeConstraintList() {
163            return null;
164        }
165    
166        @NotNull
167        @Override
168        public List<JetTypeConstraint> getTypeConstraints() {
169            return Collections.emptyList();
170        }
171    
172        @NotNull
173        @Override
174        public List<JetTypeParameter> getTypeParameters() {
175            return Collections.emptyList();
176        }
177    
178        @Nullable
179        public JetFunction getOwnerFunction() {
180            PsiElement parent = getParent();
181            if (parent == null) return null;
182            PsiElement grandparent = parent.getParent();
183            if (!(grandparent instanceof JetFunction)) return null;
184            return (JetFunction) grandparent;
185        }
186    }