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 com.intellij.psi.util.PsiTreeUtil;
025 import org.jetbrains.annotations.NotNull;
026 import org.jetbrains.annotations.Nullable;
027 import org.jetbrains.kotlin.lexer.KtTokens;
028 import org.jetbrains.kotlin.psi.stubs.KotlinParameterStub;
029 import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes;
030 import org.jetbrains.kotlin.psi.typeRefHelpers.TypeRefHelpersKt;
031
032 import java.util.Collections;
033 import java.util.List;
034
035 public class KtParameter extends KtNamedDeclarationStub<KotlinParameterStub> implements KtCallableDeclaration, KtValVarKeywordOwner {
036
037 public KtParameter(@NotNull ASTNode node) {
038 super(node);
039 }
040
041 public KtParameter(@NotNull KotlinParameterStub stub) {
042 super(stub, KtStubElementTypes.VALUE_PARAMETER);
043 }
044
045 @Override
046 public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) {
047 return visitor.visitParameter(this, data);
048 }
049
050 @Override
051 @Nullable
052 public KtTypeReference getTypeReference() {
053 return getStubOrPsiChild(KtStubElementTypes.TYPE_REFERENCE);
054 }
055
056 @Override
057 @Nullable
058 public KtTypeReference setTypeReference(@Nullable KtTypeReference typeRef) {
059 return TypeRefHelpersKt.setTypeReference(this, getNameIdentifier(), typeRef);
060 }
061
062 @Nullable
063 @Override
064 public PsiElement getColon() {
065 return findChildByType(KtTokens.COLON);
066 }
067
068 @Nullable
069 public PsiElement getEqualsToken() {
070 return findChildByType(KtTokens.EQ);
071 }
072
073 public boolean hasDefaultValue() {
074 KotlinParameterStub stub = getStub();
075 if (stub != null) {
076 return stub.hasDefaultValue();
077 }
078 return getDefaultValue() != null;
079 }
080
081 @Nullable
082 public KtExpression getDefaultValue() {
083 KotlinParameterStub stub = getStub();
084 if (stub != null && !stub.hasDefaultValue()) return null;
085
086 PsiElement equalsToken = getEqualsToken();
087 return equalsToken != null ? PsiTreeUtil.getNextSiblingOfType(equalsToken, KtExpression.class) : null;
088 }
089
090 public boolean isMutable() {
091 KotlinParameterStub stub = getStub();
092 if (stub != null) {
093 return stub.isMutable();
094 }
095
096 return findChildByType(KtTokens.VAR_KEYWORD) != null;
097 }
098
099 public boolean isVarArg() {
100 KtModifierList modifierList = getModifierList();
101 return modifierList != null && modifierList.hasModifier(KtTokens.VARARG_KEYWORD);
102 }
103
104 public boolean hasValOrVar() {
105 KotlinParameterStub stub = getStub();
106 if (stub != null) {
107 return stub.hasValOrVar();
108 }
109 return getValOrVarKeyword() != null;
110 }
111
112 @Nullable
113 public PsiElement getValOrVarKeyword() {
114 KotlinParameterStub stub = getStub();
115 if (stub != null && !stub.hasValOrVar()) {
116 return null;
117 }
118 return findChildByType(VAL_VAR_TOKEN_SET);
119 }
120
121 private static final TokenSet VAL_VAR_TOKEN_SET = TokenSet.create(KtTokens.VAL_KEYWORD, KtTokens.VAR_KEYWORD);
122
123 @Override
124 public ItemPresentation getPresentation() {
125 return ItemPresentationProviders.getItemPresentation(this);
126 }
127
128 public boolean isLoopParameter() {
129 return getParent() instanceof KtForExpression;
130 }
131
132 @Nullable
133 @Override
134 public KtParameterList getValueParameterList() {
135 return null;
136 }
137
138 @NotNull
139 @Override
140 public List<KtParameter> getValueParameters() {
141 return Collections.emptyList();
142 }
143
144 @Nullable
145 @Override
146 public KtTypeReference getReceiverTypeReference() {
147 return null;
148 }
149
150 @Nullable
151 @Override
152 public KtTypeParameterList getTypeParameterList() {
153 return null;
154 }
155
156 @Nullable
157 @Override
158 public KtTypeConstraintList getTypeConstraintList() {
159 return null;
160 }
161
162 @NotNull
163 @Override
164 public List<KtTypeConstraint> getTypeConstraints() {
165 return Collections.emptyList();
166 }
167
168 @NotNull
169 @Override
170 public List<KtTypeParameter> getTypeParameters() {
171 return Collections.emptyList();
172 }
173
174 @Nullable
175 public KtFunction getOwnerFunction() {
176 PsiElement parent = getParent();
177 if (parent == null) return null;
178 PsiElement grandparent = parent.getParent();
179 if (!(grandparent instanceof KtFunction)) return null;
180 return (KtFunction) grandparent;
181 }
182 }