| /* |
| * Copyright 2000-2013 JetBrains s.r.o. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package com.jetbrains.python.psi.impl; |
| |
| import com.intellij.lang.ASTNode; |
| import com.intellij.psi.PsiElement; |
| import com.intellij.psi.PsiPolyVariantReference; |
| import com.intellij.psi.PsiReference; |
| import com.intellij.psi.util.QualifiedName; |
| import com.jetbrains.python.PyElementTypes; |
| import com.jetbrains.python.PyNames; |
| import com.jetbrains.python.PyTokenTypes; |
| import com.jetbrains.python.PythonDialectsTokenSetProvider; |
| import com.jetbrains.python.psi.*; |
| import com.jetbrains.python.psi.impl.references.PyOperatorReference; |
| import com.jetbrains.python.psi.resolve.PyResolveContext; |
| import com.jetbrains.python.psi.types.PyType; |
| import com.jetbrains.python.psi.types.TypeEvalContext; |
| import org.jetbrains.annotations.NotNull; |
| import org.jetbrains.annotations.Nullable; |
| |
| /** |
| * @author yole |
| */ |
| public class PyPrefixExpressionImpl extends PyElementImpl implements PyPrefixExpression { |
| public PyPrefixExpressionImpl(ASTNode astNode) { |
| super(astNode); |
| } |
| |
| @Override |
| public PyExpression getOperand() { |
| return (PyExpression)childToPsi(PythonDialectsTokenSetProvider.INSTANCE.getExpressionTokens(), 0); |
| } |
| |
| @Nullable |
| public PsiElement getPsiOperator() { |
| final ASTNode node = getNode(); |
| final ASTNode child = node.findChildByType(PyElementTypes.UNARY_OPS); |
| return child != null ? child.getPsi() : null; |
| } |
| |
| @NotNull |
| @Override |
| public PyElementType getOperator() { |
| final PsiElement op = getPsiOperator(); |
| assert op != null; |
| return (PyElementType)op.getNode().getElementType(); |
| } |
| |
| @Override |
| protected void acceptPyVisitor(PyElementVisitor pyVisitor) { |
| pyVisitor.visitPyPrefixExpression(this); |
| } |
| |
| @Override |
| public PsiReference getReference() { |
| return getReference(PyResolveContext.noImplicits()); |
| } |
| |
| @NotNull |
| @Override |
| public PsiPolyVariantReference getReference(PyResolveContext context) { |
| return new PyOperatorReference(this, context); |
| } |
| |
| @Override |
| public PyType getType(@NotNull TypeEvalContext context, @NotNull TypeEvalContext.Key key) { |
| if (getOperator() == PyTokenTypes.NOT_KEYWORD) { |
| return PyBuiltinCache.getInstance(this).getBoolType(); |
| } |
| final PsiReference ref = getReference(PyResolveContext.noImplicits().withTypeEvalContext(context)); |
| final PsiElement resolved = ref.resolve(); |
| if (resolved instanceof Callable) { |
| // TODO: Make PyPrefixExpression a PyCallSiteExpression, use getCallType() here and analyze it in PyTypeChecker.analyzeCallSite() |
| return ((Callable)resolved).getReturnType(context, key); |
| } |
| return null; |
| } |
| |
| @Override |
| public PyExpression getQualifier() { |
| return getOperand(); |
| } |
| |
| @Nullable |
| @Override |
| public QualifiedName asQualifiedName() { |
| return PyPsiUtils.asQualifiedName(this); |
| } |
| |
| @Override |
| public boolean isQualified() { |
| return getQualifier() != null; |
| } |
| |
| @Override |
| public String getReferencedName() { |
| PyElementType t = getOperator(); |
| if (t == PyTokenTypes.PLUS) { |
| return PyNames.POS; |
| } |
| else if (t == PyTokenTypes.MINUS) { |
| return PyNames.NEG; |
| } |
| return getOperator().getSpecialMethodName(); |
| } |
| |
| @Override |
| public ASTNode getNameElement() { |
| final PsiElement op = getPsiOperator(); |
| return op != null ? op.getNode() : null; |
| } |
| } |