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.search.SearchScope;
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 hasValOrVarNode() {
109 KotlinParameterStub stub = getStub();
110 if (stub != null) {
111 return stub.hasValOrVarNode();
112 }
113 return getValOrVarNode() != null;
114 }
115
116 @Nullable
117 public ASTNode getValOrVarNode() {
118 KotlinParameterStub stub = getStub();
119 if (stub != null && !stub.hasValOrVarNode()) {
120 return null;
121 }
122 ASTNode val = getNode().findChildByType(JetTokens.VAL_KEYWORD);
123 if (val != null) return val;
124
125 return getNode().findChildByType(JetTokens.VAR_KEYWORD);
126 }
127
128 @Override
129 public ItemPresentation getPresentation() {
130 return ItemPresentationProviders.getItemPresentation(this);
131 }
132
133 public boolean isLoopParameter() {
134 return getParent() instanceof JetForExpression;
135 }
136
137 @Nullable
138 @Override
139 public JetParameterList getValueParameterList() {
140 return null;
141 }
142
143 @NotNull
144 @Override
145 public List<JetParameter> getValueParameters() {
146 return Collections.emptyList();
147 }
148
149 @Nullable
150 @Override
151 public JetTypeReference getReceiverTypeReference() {
152 return null;
153 }
154
155 @Nullable
156 @Override
157 public JetTypeParameterList getTypeParameterList() {
158 return null;
159 }
160
161 @Nullable
162 @Override
163 public JetTypeConstraintList getTypeConstraintList() {
164 return null;
165 }
166
167 @NotNull
168 @Override
169 public List<JetTypeConstraint> getTypeConstraints() {
170 return Collections.emptyList();
171 }
172
173 @NotNull
174 @Override
175 public List<JetTypeParameter> getTypeParameters() {
176 return Collections.emptyList();
177 }
178
179 @NotNull
180 @Override
181 public SearchScope getUseScope() {
182 PsiElement parent = getParent();
183 if (parent != null) {
184 PsiElement grandparent = parent.getParent();
185 if (grandparent instanceof JetNamedFunction) {
186 return grandparent.getUseScope();
187 }
188 }
189 return super.getUseScope();
190 }
191 }