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