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.psi.PsiElement;
021    import com.intellij.psi.tree.IElementType;
022    import com.intellij.psi.tree.TokenSet;
023    import org.jetbrains.annotations.NotNull;
024    import org.jetbrains.annotations.Nullable;
025    import org.jetbrains.kotlin.psi.stubs.KotlinNameReferenceExpressionStub;
026    import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes;
027    import org.jetbrains.kotlin.name.Name;
028    import org.jetbrains.kotlin.lexer.JetTokens;
029    
030    import static org.jetbrains.kotlin.lexer.JetTokens.*;
031    
032    public class JetNameReferenceExpression extends JetExpressionImplStub<KotlinNameReferenceExpressionStub> implements JetSimpleNameExpression {
033    
034        private static final TokenSet NAME_REFERENCE_EXPRESSIONS = TokenSet.create(IDENTIFIER, FIELD_IDENTIFIER, THIS_KEYWORD, SUPER_KEYWORD);
035    
036        public JetNameReferenceExpression(@NotNull ASTNode node) {
037            super(node);
038        }
039    
040        public JetNameReferenceExpression(@NotNull KotlinNameReferenceExpressionStub stub) {
041            super(stub, JetStubElementTypes.REFERENCE_EXPRESSION);
042        }
043    
044        @Override
045        @NotNull
046        public String getReferencedName() {
047            KotlinNameReferenceExpressionStub stub = getStub();
048            if (stub != null) {
049                return stub.getReferencedName();
050            }
051            return JetSimpleNameExpressionImpl.Helper.getReferencedNameImpl(this);
052        }
053    
054        @Override
055        @NotNull
056        public Name getReferencedNameAsName() {
057            return JetSimpleNameExpressionImpl.Helper.getReferencedNameAsNameImpl(this);
058        }
059    
060        @Override
061        @NotNull
062        public PsiElement getReferencedNameElement() {
063            PsiElement element = findChildByType(NAME_REFERENCE_EXPRESSIONS);
064            if (element == null) {
065                return this;
066            }
067            return element;
068        }
069    
070        @Override
071        @Nullable
072        public PsiElement getIdentifier() {
073            return findChildByType(JetTokens.IDENTIFIER);
074        }
075    
076        @NotNull
077        @Override
078        public IElementType getReferencedNameElementType() {
079            return JetSimpleNameExpressionImpl.Helper.getReferencedNameElementTypeImpl(this);
080        }
081    
082        @Override
083        public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
084            return visitor.visitSimpleNameExpression(this, data);
085        }
086    }