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 org.jetbrains.annotations.NotNull; 024 import org.jetbrains.annotations.Nullable; 025 import org.jetbrains.kotlin.lexer.JetTokens; 026 import org.jetbrains.kotlin.psi.stubs.KotlinParameterStub; 027 import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes; 028 import org.jetbrains.kotlin.psi.typeRefHelpers.TypeRefHelpersPackage; 029 030 import java.util.Collections; 031 import java.util.List; 032 033 public class JetParameter extends JetNamedDeclarationStub<KotlinParameterStub> implements JetCallableDeclaration { 034 035 public JetParameter(@NotNull ASTNode node) { 036 super(node); 037 } 038 039 public JetParameter(@NotNull KotlinParameterStub stub) { 040 super(stub, JetStubElementTypes.VALUE_PARAMETER); 041 } 042 043 @Override 044 public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) { 045 return visitor.visitParameter(this, data); 046 } 047 048 @Override 049 @Nullable 050 public JetTypeReference getTypeReference() { 051 return getStubOrPsiChild(JetStubElementTypes.TYPE_REFERENCE); 052 } 053 054 @Override 055 @Nullable 056 public JetTypeReference setTypeReference(@Nullable JetTypeReference typeRef) { 057 return TypeRefHelpersPackage.setTypeReference(this, getNameIdentifier(), typeRef); 058 } 059 060 @Nullable 061 @Override 062 public PsiElement getColon() { 063 return findChildByType(JetTokens.COLON); 064 } 065 066 public boolean hasDefaultValue() { 067 KotlinParameterStub stub = getStub(); 068 if (stub != null) { 069 return stub.hasDefaultValue(); 070 } 071 return getDefaultValue() != null; 072 } 073 074 @Nullable 075 public JetExpression getDefaultValue() { 076 KotlinParameterStub stub = getStub(); 077 if (stub != null && !stub.hasDefaultValue()) { 078 return null; 079 } 080 boolean passedEQ = false; 081 ASTNode child = getNode().getFirstChildNode(); 082 while (child != null) { 083 if (child.getElementType() == JetTokens.EQ) passedEQ = true; 084 if (passedEQ && child.getPsi() instanceof JetExpression) { 085 return (JetExpression) child.getPsi(); 086 } 087 child = child.getTreeNext(); 088 } 089 090 return null; 091 } 092 093 public boolean isMutable() { 094 KotlinParameterStub stub = getStub(); 095 if (stub != null) { 096 return stub.isMutable(); 097 } 098 099 return findChildByType(JetTokens.VAR_KEYWORD) != null; 100 } 101 102 public boolean isVarArg() { 103 JetModifierList modifierList = getModifierList(); 104 return modifierList != null && modifierList.hasModifier(JetTokens.VARARG_KEYWORD); 105 } 106 107 public boolean hasValOrVarNode() { 108 KotlinParameterStub stub = getStub(); 109 if (stub != null) { 110 return stub.hasValOrVarNode(); 111 } 112 return getValOrVarNode() != null; 113 } 114 115 @Nullable 116 public ASTNode getValOrVarNode() { 117 KotlinParameterStub stub = getStub(); 118 if (stub != null && !stub.hasValOrVarNode()) { 119 return null; 120 } 121 ASTNode val = getNode().findChildByType(JetTokens.VAL_KEYWORD); 122 if (val != null) return val; 123 124 return getNode().findChildByType(JetTokens.VAR_KEYWORD); 125 } 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 }