All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.types.expressions.OperatorConventions Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/*
 * Copyright 2010-2015 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 org.jetbrains.kotlin.types.expressions;

import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.lexer.JetSingleValueToken;
import org.jetbrains.kotlin.lexer.JetToken;
import org.jetbrains.kotlin.lexer.JetTokens;
import org.jetbrains.kotlin.name.Name;

public class OperatorConventions {

    public static final Name EQUALS = Name.identifier("equals");
    public static final Name IDENTITY_EQUALS = Name.identifier("identityEquals");
    public static final Name COMPARE_TO = Name.identifier("compareTo");
    public static final Name CONTAINS = Name.identifier("contains");
    public static final Name INVOKE = Name.identifier("invoke");
    public static final Name ITERATOR = Name.identifier("iterator");
    public static final Name GET = Name.identifier("get");
    public static final Name SET = Name.identifier("set");
    public static final Name NEXT = Name.identifier("next");
    public static final Name HAS_NEXT = Name.identifier("hasNext");

    private OperatorConventions() {}

    // Names for primitive type conversion properties
    public static final Name DOUBLE = Name.identifier("toDouble");
    public static final Name FLOAT = Name.identifier("toFloat");
    public static final Name LONG = Name.identifier("toLong");
    public static final Name INT = Name.identifier("toInt");
    public static final Name CHAR = Name.identifier("toChar");
    public static final Name SHORT = Name.identifier("toShort");
    public static final Name BYTE = Name.identifier("toByte");


    public static final ImmutableSet NUMBER_CONVERSIONS = ImmutableSet.of(
            DOUBLE, FLOAT, LONG, INT, SHORT, BYTE, CHAR
    );

    public static final ImmutableBiMap UNARY_OPERATION_NAMES = ImmutableBiMap.builder()
            .put(JetTokens.PLUSPLUS, Name.identifier("inc"))
            .put(JetTokens.MINUSMINUS, Name.identifier("dec"))
            .put(JetTokens.PLUS, Name.identifier("plus"))
            .put(JetTokens.MINUS, Name.identifier("minus"))
            .put(JetTokens.EXCL, Name.identifier("not"))
            .build();

    public static final ImmutableBiMap BINARY_OPERATION_NAMES = ImmutableBiMap.builder()
            .put(JetTokens.MUL, Name.identifier("times"))
            .put(JetTokens.PLUS, Name.identifier("plus"))
            .put(JetTokens.MINUS, Name.identifier("minus"))
            .put(JetTokens.DIV, Name.identifier("div"))
            .put(JetTokens.PERC, Name.identifier("mod"))
            .put(JetTokens.RANGE, Name.identifier("rangeTo"))
            .build();

    public static final ImmutableSet NOT_OVERLOADABLE =
            ImmutableSet.of(JetTokens.ANDAND, JetTokens.OROR, JetTokens.ELVIS, JetTokens.EQEQEQ, JetTokens.EXCLEQEQEQ);
    
    public static final ImmutableSet INCREMENT_OPERATIONS =
            ImmutableSet.of(JetTokens.PLUSPLUS, JetTokens.MINUSMINUS);

    public static final ImmutableSet COMPARISON_OPERATIONS =
            ImmutableSet.of(JetTokens.LT, JetTokens.GT, JetTokens.LTEQ, JetTokens.GTEQ);

    public static final ImmutableSet EQUALS_OPERATIONS =
            ImmutableSet.of(JetTokens.EQEQ, JetTokens.EXCLEQ);

    public static final ImmutableSet IDENTITY_EQUALS_OPERATIONS =
            ImmutableSet.of(JetTokens.EQEQEQ, JetTokens.EXCLEQEQEQ);

    public static final ImmutableSet IN_OPERATIONS =
            ImmutableSet.of(JetTokens.IN_KEYWORD, JetTokens.NOT_IN);

    public static final ImmutableBiMap ASSIGNMENT_OPERATIONS = ImmutableBiMap.builder()
            .put(JetTokens.MULTEQ, Name.identifier("timesAssign"))
            .put(JetTokens.DIVEQ, Name.identifier("divAssign"))
            .put(JetTokens.PERCEQ, Name.identifier("modAssign"))
            .put(JetTokens.PLUSEQ, Name.identifier("plusAssign"))
            .put(JetTokens.MINUSEQ, Name.identifier("minusAssign"))
            .build();

    public static final ImmutableBiMap ASSIGNMENT_OPERATION_COUNTERPARTS = ImmutableBiMap.builder()
            .put(JetTokens.MULTEQ, JetTokens.MUL)
            .put(JetTokens.DIVEQ, JetTokens.DIV)
            .put(JetTokens.PERCEQ, JetTokens.PERC)
            .put(JetTokens.PLUSEQ, JetTokens.PLUS)
            .put(JetTokens.MINUSEQ, JetTokens.MINUS)
            .build();

    public static final ImmutableBiMap BOOLEAN_OPERATIONS = ImmutableBiMap.builder()
             .put(JetTokens.ANDAND, Name.identifier("and"))
             .put(JetTokens.OROR, Name.identifier("or"))
             .build();

    @Nullable
    public static Name getNameForOperationSymbol(@NotNull JetToken token) {
        Name name = UNARY_OPERATION_NAMES.get(token);
        if (name != null) return name;
        name = BINARY_OPERATION_NAMES.get(token);
        if (name != null) return name;
        name = ASSIGNMENT_OPERATIONS.get(token);
        if (name != null) return name;
        if (COMPARISON_OPERATIONS.contains(token)) return COMPARE_TO;
        if (EQUALS_OPERATIONS.contains(token)) return EQUALS;
        if (IN_OPERATIONS.contains(token)) return CONTAINS;
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy