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 void accept(@NotNull JetVisitorVoid visitor) {
057 visitor.visitParameter(this);
058 }
059
060 @Override
061 public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
062 return visitor.visitParameter(this, data);
063 }
064
065 @Nullable
066 public JetTypeReference getTypeReference() {
067 return (JetTypeReference) findChildByType(JetNodeTypes.TYPE_REFERENCE);
068 }
069
070 @Nullable
071 public JetExpression getDefaultValue() {
072 boolean passedEQ = false;
073 ASTNode child = getNode().getFirstChildNode();
074 while (child != null) {
075 if (child.getElementType() == JetTokens.EQ) passedEQ = true;
076 if (passedEQ && child.getPsi() instanceof JetExpression) {
077 return (JetExpression) child.getPsi();
078 }
079 child = child.getTreeNext();
080 }
081
082 return null;
083 }
084
085 public boolean isMutable() {
086 PsiJetParameterStub stub = getStub();
087 if (stub != null) {
088 return stub.isMutable();
089 }
090
091 return findChildByType(JetTokens.VAR_KEYWORD) != null;
092 }
093
094 public boolean isVarArg() {
095 PsiJetParameterStub stub = getStub();
096 if (stub != null) {
097 return stub.isVarArg();
098 }
099
100 JetModifierList modifierList = getModifierList();
101 return modifierList != null && modifierList.getModifierNode(JetTokens.VARARG_KEYWORD) != null;
102 }
103
104 @Nullable
105 public ASTNode getValOrVarNode() {
106 ASTNode val = getNode().findChildByType(JetTokens.VAL_KEYWORD);
107 if (val != null) return val;
108
109 return getNode().findChildByType(JetTokens.VAR_KEYWORD);
110 }
111
112 @Override
113 public ItemPresentation getPresentation() {
114 return ItemPresentationProviders.getItemPresentation(this);
115 }
116 }