summaryrefslogtreecommitdiff
path: root/plugins/kotlin/uast/uast-kotlin-base/src/org/jetbrains/uast/kotlin/expressions/KotlinUBinaryExpression.kt
blob: 8dca70bd539e1466f06e407592971eec3b927212 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

package org.jetbrains.uast.kotlin

import com.intellij.psi.PsiMethod
import org.jetbrains.annotations.ApiStatus
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtBinaryExpression
import org.jetbrains.uast.UBinaryExpression
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UIdentifier
import org.jetbrains.uast.UastBinaryOperator

@ApiStatus.Internal
class KotlinUBinaryExpression(
    override val sourcePsi: KtBinaryExpression,
    givenParent: UElement?
) : KotlinAbstractUExpression(givenParent), UBinaryExpression, KotlinUElementWithType, KotlinEvaluatableUElement {

    companion object {
        val BITWISE_OPERATORS = mapOf(
            "or" to UastBinaryOperator.BITWISE_OR,
            "and" to UastBinaryOperator.BITWISE_AND,
            "xor" to UastBinaryOperator.BITWISE_XOR,
            "shl" to UastBinaryOperator.SHIFT_LEFT,
            "shr" to UastBinaryOperator.SHIFT_RIGHT,
            "ushr" to UastBinaryOperator.UNSIGNED_SHIFT_RIGHT
        )
    }

    override val leftOperand by lz {
        baseResolveProviderService.baseKotlinConverter.convertOrEmpty(sourcePsi.left, this)
    }

    override val rightOperand by lz {
        baseResolveProviderService.baseKotlinConverter.convertOrEmpty(sourcePsi.right, this)
    }

    override val operatorIdentifier: UIdentifier by lz {
        KotlinUIdentifier(sourcePsi.operationReference.getReferencedNameElement(), this)
    }

    override fun resolveOperator(): PsiMethod? =
        baseResolveProviderService.resolveCall(sourcePsi)

    override val operator: UastBinaryOperator
        get() = when (sourcePsi.operationToken) {
            KtTokens.EQ -> UastBinaryOperator.ASSIGN
            KtTokens.PLUS -> UastBinaryOperator.PLUS
            KtTokens.MINUS -> UastBinaryOperator.MINUS
            KtTokens.MUL -> UastBinaryOperator.MULTIPLY
            KtTokens.DIV -> UastBinaryOperator.DIV
            KtTokens.PERC -> UastBinaryOperator.MOD
            KtTokens.OROR -> UastBinaryOperator.LOGICAL_OR
            KtTokens.ANDAND -> UastBinaryOperator.LOGICAL_AND
            KtTokens.EQEQ -> UastBinaryOperator.EQUALS
            KtTokens.EXCLEQ -> UastBinaryOperator.NOT_EQUALS
            KtTokens.EQEQEQ -> UastBinaryOperator.IDENTITY_EQUALS
            KtTokens.EXCLEQEQEQ -> UastBinaryOperator.IDENTITY_NOT_EQUALS
            KtTokens.GT -> UastBinaryOperator.GREATER
            KtTokens.GTEQ -> UastBinaryOperator.GREATER_OR_EQUALS
            KtTokens.LT -> UastBinaryOperator.LESS
            KtTokens.LTEQ -> UastBinaryOperator.LESS_OR_EQUALS
            KtTokens.PLUSEQ -> UastBinaryOperator.PLUS_ASSIGN
            KtTokens.MINUSEQ -> UastBinaryOperator.MINUS_ASSIGN
            KtTokens.MULTEQ -> UastBinaryOperator.MULTIPLY_ASSIGN
            KtTokens.DIVEQ -> UastBinaryOperator.DIVIDE_ASSIGN
            KtTokens.PERCEQ -> UastBinaryOperator.REMAINDER_ASSIGN
            KtTokens.IN_KEYWORD -> KotlinBinaryOperators.IN
            KtTokens.NOT_IN -> KotlinBinaryOperators.NOT_IN
            KtTokens.RANGE -> KotlinBinaryOperators.RANGE_TO
            else -> baseResolveProviderService.resolveBitwiseOperators(sourcePsi)
        }

}