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.types.expressions;
018
019 import com.google.common.collect.ImmutableBiMap;
020 import com.google.common.collect.ImmutableSet;
021 import org.jetbrains.annotations.NotNull;
022 import org.jetbrains.annotations.Nullable;
023 import org.jetbrains.kotlin.lexer.JetSingleValueToken;
024 import org.jetbrains.kotlin.lexer.JetToken;
025 import org.jetbrains.kotlin.lexer.JetTokens;
026 import org.jetbrains.kotlin.name.Name;
027
028 public class OperatorConventions {
029
030 public static final Name EQUALS = Name.identifier("equals");
031 public static final Name IDENTITY_EQUALS = Name.identifier("identityEquals");
032 public static final Name COMPARE_TO = Name.identifier("compareTo");
033 public static final Name CONTAINS = Name.identifier("contains");
034 public static final Name INVOKE = Name.identifier("invoke");
035
036 private OperatorConventions() {}
037
038 // Names for primitive type conversion properties
039 public static final Name DOUBLE = Name.identifier("toDouble");
040 public static final Name FLOAT = Name.identifier("toFloat");
041 public static final Name LONG = Name.identifier("toLong");
042 public static final Name INT = Name.identifier("toInt");
043 public static final Name CHAR = Name.identifier("toChar");
044 public static final Name SHORT = Name.identifier("toShort");
045 public static final Name BYTE = Name.identifier("toByte");
046
047
048 public static final ImmutableSet<Name> NUMBER_CONVERSIONS = ImmutableSet.of(
049 DOUBLE, FLOAT, LONG, INT, SHORT, BYTE, CHAR
050 );
051
052 public static final ImmutableBiMap<JetSingleValueToken, Name> UNARY_OPERATION_NAMES = ImmutableBiMap.<JetSingleValueToken, Name>builder()
053 .put(JetTokens.PLUSPLUS, Name.identifier("inc"))
054 .put(JetTokens.MINUSMINUS, Name.identifier("dec"))
055 .put(JetTokens.PLUS, Name.identifier("plus"))
056 .put(JetTokens.MINUS, Name.identifier("minus"))
057 .put(JetTokens.EXCL, Name.identifier("not"))
058 .build();
059
060 public static final ImmutableBiMap<JetSingleValueToken, Name> BINARY_OPERATION_NAMES = ImmutableBiMap.<JetSingleValueToken, Name>builder()
061 .put(JetTokens.MUL, Name.identifier("times"))
062 .put(JetTokens.PLUS, Name.identifier("plus"))
063 .put(JetTokens.MINUS, Name.identifier("minus"))
064 .put(JetTokens.DIV, Name.identifier("div"))
065 .put(JetTokens.PERC, Name.identifier("mod"))
066 .put(JetTokens.RANGE, Name.identifier("rangeTo"))
067 .build();
068
069 public static final ImmutableSet<JetSingleValueToken> NOT_OVERLOADABLE =
070 ImmutableSet.of(JetTokens.ANDAND, JetTokens.OROR, JetTokens.ELVIS, JetTokens.EQEQEQ, JetTokens.EXCLEQEQEQ);
071
072 public static final ImmutableSet<JetSingleValueToken> INCREMENT_OPERATIONS =
073 ImmutableSet.of(JetTokens.PLUSPLUS, JetTokens.MINUSMINUS);
074
075 public static final ImmutableSet<JetSingleValueToken> COMPARISON_OPERATIONS =
076 ImmutableSet.of(JetTokens.LT, JetTokens.GT, JetTokens.LTEQ, JetTokens.GTEQ);
077
078 public static final ImmutableSet<JetSingleValueToken> EQUALS_OPERATIONS =
079 ImmutableSet.of(JetTokens.EQEQ, JetTokens.EXCLEQ);
080
081 public static final ImmutableSet<JetSingleValueToken> IDENTITY_EQUALS_OPERATIONS =
082 ImmutableSet.of(JetTokens.EQEQEQ, JetTokens.EXCLEQEQEQ);
083
084 public static final ImmutableSet<JetSingleValueToken> IN_OPERATIONS =
085 ImmutableSet.<JetSingleValueToken>of(JetTokens.IN_KEYWORD, JetTokens.NOT_IN);
086
087 public static final ImmutableBiMap<JetSingleValueToken, Name> ASSIGNMENT_OPERATIONS = ImmutableBiMap.<JetSingleValueToken, Name>builder()
088 .put(JetTokens.MULTEQ, Name.identifier("timesAssign"))
089 .put(JetTokens.DIVEQ, Name.identifier("divAssign"))
090 .put(JetTokens.PERCEQ, Name.identifier("modAssign"))
091 .put(JetTokens.PLUSEQ, Name.identifier("plusAssign"))
092 .put(JetTokens.MINUSEQ, Name.identifier("minusAssign"))
093 .build();
094
095 public static final ImmutableBiMap<JetSingleValueToken, JetSingleValueToken> ASSIGNMENT_OPERATION_COUNTERPARTS = ImmutableBiMap.<JetSingleValueToken, JetSingleValueToken>builder()
096 .put(JetTokens.MULTEQ, JetTokens.MUL)
097 .put(JetTokens.DIVEQ, JetTokens.DIV)
098 .put(JetTokens.PERCEQ, JetTokens.PERC)
099 .put(JetTokens.PLUSEQ, JetTokens.PLUS)
100 .put(JetTokens.MINUSEQ, JetTokens.MINUS)
101 .build();
102
103 public static final ImmutableBiMap<JetSingleValueToken, Name> BOOLEAN_OPERATIONS = ImmutableBiMap.<JetSingleValueToken, Name>builder()
104 .put(JetTokens.ANDAND, Name.identifier("and"))
105 .put(JetTokens.OROR, Name.identifier("or"))
106 .build();
107
108 @Nullable
109 public static Name getNameForOperationSymbol(@NotNull JetToken token) {
110 Name name = UNARY_OPERATION_NAMES.get(token);
111 if (name != null) return name;
112 name = BINARY_OPERATION_NAMES.get(token);
113 if (name != null) return name;
114 name = ASSIGNMENT_OPERATIONS.get(token);
115 if (name != null) return name;
116 if (COMPARISON_OPERATIONS.contains(token)) return COMPARE_TO;
117 if (EQUALS_OPERATIONS.contains(token)) return EQUALS;
118 if (IN_OPERATIONS.contains(token)) return CONTAINS;
119 return null;
120 }
121 }