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