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