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.codegen.intrinsics; 018 019 import com.google.common.collect.ImmutableList; 020 import org.jetbrains.annotations.NotNull; 021 import org.jetbrains.annotations.Nullable; 022 import org.jetbrains.jet.lang.descriptors.CallableMemberDescriptor; 023 import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor; 024 import org.jetbrains.jet.lang.descriptors.SimpleFunctionDescriptor; 025 import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor; 026 import org.jetbrains.jet.lang.resolve.java.JvmPrimitiveType; 027 import org.jetbrains.jet.lang.resolve.name.FqName; 028 import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe; 029 import org.jetbrains.jet.lang.resolve.name.Name; 030 import org.jetbrains.jet.lang.types.expressions.OperatorConventions; 031 import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; 032 import org.jetbrains.jet.lang.types.lang.PrimitiveType; 033 034 import javax.annotation.PostConstruct; 035 import java.util.HashMap; 036 import java.util.List; 037 import java.util.Map; 038 039 import static org.jetbrains.asm4.Opcodes.*; 040 import static org.jetbrains.jet.lang.resolve.DescriptorUtils.*; 041 042 public class IntrinsicMethods { 043 private static final IntrinsicMethod UNARY_MINUS = new UnaryMinus(); 044 private static final IntrinsicMethod UNARY_PLUS = new UnaryPlus(); 045 private static final IntrinsicMethod NUMBER_CAST = new NumberCast(); 046 private static final IntrinsicMethod INV = new Inv(); 047 private static final IntrinsicMethod RANGE_TO = new RangeTo(); 048 private static final IntrinsicMethod INC = new Increment(1); 049 private static final IntrinsicMethod DEC = new Increment(-1); 050 private static final IntrinsicMethod HASH_CODE = new HashCode(); 051 052 private static final IntrinsicMethod ARRAY_SIZE = new ArraySize(); 053 private static final IntrinsicMethod ARRAY_INDICES = new ArrayIndices(); 054 private static final Equals EQUALS = new Equals(); 055 private static final IdentityEquals IDENTITY_EQUALS = new IdentityEquals(); 056 private static final IteratorNext ITERATOR_NEXT = new IteratorNext(); 057 private static final ArraySet ARRAY_SET = new ArraySet(); 058 private static final ArrayGet ARRAY_GET = new ArrayGet(); 059 private static final StringPlus STRING_PLUS = new StringPlus(); 060 public static final String KOTLIN_JAVA_CLASS_FUNCTION = "kotlin.javaClass.function"; 061 private static final String KOTLIN_JAVA_CLASS_PROPERTY = "kotlin.javaClass.property"; 062 public static final String KOTLIN_ARRAYS_ARRAY = "kotlin.arrays.array"; 063 public static final String KOTLIN_COPY_TO_ARRAY = "kotlin.collections.copyToArray"; 064 private static final String KOTLIN_TO_STRING = "kotlin.toString"; 065 private static final String KOTLIN_HASH_CODE = "kotlin.hashCode"; 066 private static final EnumValues ENUM_VALUES = new EnumValues(); 067 private static final EnumValueOf ENUM_VALUE_OF = new EnumValueOf(); 068 private static final ToString TO_STRING = new ToString(); 069 070 private final Map<String, IntrinsicMethod> namedMethods = new HashMap<String, IntrinsicMethod>(); 071 private static final IntrinsicMethod ARRAY_ITERATOR = new ArrayIterator(); 072 private final IntrinsicsMap intrinsicsMap = new IntrinsicsMap(); 073 074 075 @PostConstruct 076 public void init() { 077 namedMethods.put(KOTLIN_JAVA_CLASS_FUNCTION, new JavaClassFunction()); 078 namedMethods.put(KOTLIN_JAVA_CLASS_PROPERTY, new JavaClassProperty()); 079 namedMethods.put(KOTLIN_ARRAYS_ARRAY, new JavaClassArray()); 080 namedMethods.put(KOTLIN_COPY_TO_ARRAY, new CopyToArray()); 081 namedMethods.put(KOTLIN_HASH_CODE, HASH_CODE); 082 namedMethods.put(KOTLIN_TO_STRING, TO_STRING); 083 084 ImmutableList<Name> primitiveCastMethods = OperatorConventions.NUMBER_CONVERSIONS.asList(); 085 for (Name method : primitiveCastMethods) { 086 declareIntrinsicFunction(Name.identifier("Number"), method, 0, NUMBER_CAST); 087 for (PrimitiveType type : PrimitiveType.NUMBER_TYPES) { 088 declareIntrinsicFunction(type.getTypeName(), method, 0, NUMBER_CAST); 089 } 090 } 091 092 for (PrimitiveType type : PrimitiveType.NUMBER_TYPES) { 093 Name typeName = type.getTypeName(); 094 declareIntrinsicFunction(typeName, Name.identifier("plus"), 0, UNARY_PLUS); 095 declareIntrinsicFunction(typeName, Name.identifier("minus"), 0, UNARY_MINUS); 096 declareIntrinsicFunction(typeName, Name.identifier("inv"), 0, INV); 097 declareIntrinsicFunction(typeName, Name.identifier("rangeTo"), 1, RANGE_TO); 098 declareIntrinsicFunction(typeName, Name.identifier("inc"), 0, INC); 099 declareIntrinsicFunction(typeName, Name.identifier("dec"), 0, DEC); 100 declareIntrinsicFunction(typeName, Name.identifier("hashCode"), 0, HASH_CODE); 101 declareIntrinsicFunction(typeName, Name.identifier("equals"), 1, EQUALS); 102 } 103 104 declareBinaryOp(Name.identifier("plus"), IADD); 105 declareBinaryOp(Name.identifier("minus"), ISUB); 106 declareBinaryOp(Name.identifier("times"), IMUL); 107 declareBinaryOp(Name.identifier("div"), IDIV); 108 declareBinaryOp(Name.identifier("mod"), IREM); 109 declareBinaryOp(Name.identifier("shl"), ISHL); 110 declareBinaryOp(Name.identifier("shr"), ISHR); 111 declareBinaryOp(Name.identifier("ushr"), IUSHR); 112 declareBinaryOp(Name.identifier("and"), IAND); 113 declareBinaryOp(Name.identifier("or"), IOR); 114 declareBinaryOp(Name.identifier("xor"), IXOR); 115 116 declareIntrinsicFunction(Name.identifier("Boolean"), Name.identifier("not"), 0, new Not()); 117 118 declareIntrinsicFunction(Name.identifier("String"), Name.identifier("plus"), 1, new Concat()); 119 declareIntrinsicFunction(Name.identifier("CharSequence"), Name.identifier("get"), 1, new StringGetChar()); 120 declareIntrinsicFunction(Name.identifier("String"), Name.identifier("get"), 1, new StringGetChar()); 121 122 FqName builtInsPackageFqName = KotlinBuiltIns.getInstance().getBuiltInsPackageFqName(); 123 intrinsicsMap.registerIntrinsic(builtInsPackageFqName, Name.identifier("name"), 0, new EnumName()); 124 intrinsicsMap.registerIntrinsic(builtInsPackageFqName, Name.identifier("ordinal"), 0, new EnumOrdinal()); 125 126 intrinsicsMap.registerIntrinsic(builtInsPackageFqName, Name.identifier("toString"), 0, TO_STRING); 127 intrinsicsMap.registerIntrinsic(builtInsPackageFqName, Name.identifier("equals"), 1, EQUALS); 128 intrinsicsMap.registerIntrinsic(builtInsPackageFqName, Name.identifier("identityEquals"), 1, IDENTITY_EQUALS); 129 intrinsicsMap.registerIntrinsic(builtInsPackageFqName, Name.identifier("plus"), 1, STRING_PLUS); 130 intrinsicsMap.registerIntrinsic(builtInsPackageFqName, Name.identifier("arrayOfNulls"), 1, new NewArray()); 131 intrinsicsMap.registerIntrinsic(builtInsPackageFqName, Name.identifier("synchronized"), 2, new StupidSync()); 132 intrinsicsMap.registerIntrinsic(builtInsPackageFqName, Name.identifier("iterator"), 0, new IteratorIterator()); 133 134 135 declareIntrinsicFunction(Name.identifier("ByteIterator"), Name.identifier("next"), 0, ITERATOR_NEXT); 136 declareIntrinsicFunction(Name.identifier("ShortIterator"), Name.identifier("next"), 0, ITERATOR_NEXT); 137 declareIntrinsicFunction(Name.identifier("IntIterator"), Name.identifier("next"), 0, ITERATOR_NEXT); 138 declareIntrinsicFunction(Name.identifier("LongIterator"), Name.identifier("next"), 0, ITERATOR_NEXT); 139 declareIntrinsicFunction(Name.identifier("CharIterator"), Name.identifier("next"), 0, ITERATOR_NEXT); 140 declareIntrinsicFunction(Name.identifier("BooleanIterator"), Name.identifier("next"), 0, ITERATOR_NEXT); 141 declareIntrinsicFunction(Name.identifier("FloatIterator"), Name.identifier("next"), 0, ITERATOR_NEXT); 142 declareIntrinsicFunction(Name.identifier("DoubleIterator"), Name.identifier("next"), 0, ITERATOR_NEXT); 143 144 for (PrimitiveType type : PrimitiveType.values()) { 145 declareIntrinsicFunction(type.getTypeName(), Name.identifier("compareTo"), 1, new CompareTo()); 146 } 147 // declareIntrinsicFunction("Any", "equals", 1, new Equals()); 148 // 149 declareIntrinsicProperty(Name.identifier("CharSequence"), Name.identifier("length"), new StringLength()); 150 declareIntrinsicProperty(Name.identifier("String"), Name.identifier("length"), new StringLength()); 151 152 registerStaticField(getFQName(KotlinBuiltIns.getInstance().getUnit()).toSafe(), Name.identifier("VALUE")); 153 154 for (PrimitiveType type : PrimitiveType.NUMBER_TYPES) { 155 registerStaticField(type.getRangeClassName(), Name.identifier("EMPTY")); 156 } 157 158 for (PrimitiveType type : PrimitiveType.NUMBER_TYPES) { 159 registerRangeOrProgressionProperty(type.getRangeClassName(), Name.identifier("start")); 160 registerRangeOrProgressionProperty(type.getRangeClassName(), Name.identifier("end")); 161 162 registerRangeOrProgressionProperty(type.getProgressionClassName(), Name.identifier("start")); 163 registerRangeOrProgressionProperty(type.getProgressionClassName(), Name.identifier("end")); 164 registerRangeOrProgressionProperty(type.getProgressionClassName(), Name.identifier("increment")); 165 } 166 167 declareArrayMethods(); 168 } 169 170 private void registerStaticField(@NotNull FqName classFqName, @NotNull Name propertyName) { 171 FqNameUnsafe classObjectFqName = classFqName.toUnsafe().child(getClassObjectName(classFqName.shortName())); 172 intrinsicsMap.registerIntrinsic(classObjectFqName, propertyName, -1, new StaticField(classFqName, propertyName)); 173 } 174 175 private void registerRangeOrProgressionProperty(@NotNull FqName ownerClass, @NotNull Name propertyName) { 176 intrinsicsMap.registerIntrinsic(ownerClass, propertyName, -1, new PropertyOfProgressionOrRange(ownerClass, propertyName)); 177 } 178 179 private void declareArrayMethods() { 180 181 for (JvmPrimitiveType jvmPrimitiveType : JvmPrimitiveType.values()) { 182 declareArrayMethodsForPrimitive(jvmPrimitiveType); 183 } 184 185 declareIntrinsicProperty(Name.identifier("Array"), Name.identifier("size"), ARRAY_SIZE); 186 declareIntrinsicProperty(Name.identifier("Array"), Name.identifier("indices"), ARRAY_INDICES); 187 declareIntrinsicFunction(Name.identifier("Array"), Name.identifier("set"), 2, ARRAY_SET); 188 declareIntrinsicFunction(Name.identifier("Array"), Name.identifier("get"), 1, ARRAY_GET); 189 declareIterator(Name.identifier("Array")); 190 } 191 192 private void declareArrayMethodsForPrimitive(JvmPrimitiveType jvmPrimitiveType) { 193 PrimitiveType primitiveType = jvmPrimitiveType.getPrimitiveType(); 194 declareIntrinsicProperty(primitiveType.getArrayTypeName(), Name.identifier("size"), ARRAY_SIZE); 195 declareIntrinsicProperty(primitiveType.getArrayTypeName(), Name.identifier("indices"), ARRAY_INDICES); 196 declareIntrinsicFunction(primitiveType.getArrayTypeName(), Name.identifier("set"), 2, ARRAY_SET); 197 declareIntrinsicFunction(primitiveType.getArrayTypeName(), Name.identifier("get"), 1, ARRAY_GET); 198 declareIterator(primitiveType.getArrayTypeName()); 199 } 200 201 private void declareIterator(@NotNull Name arrayClassName) { 202 declareIntrinsicFunction(arrayClassName, Name.identifier("iterator"), 0, ARRAY_ITERATOR); 203 } 204 205 private void declareBinaryOp(Name methodName, int opcode) { 206 BinaryOp op = new BinaryOp(opcode); 207 for (PrimitiveType type : PrimitiveType.values()) { 208 declareIntrinsicFunction(type.getTypeName(), methodName, 1, op); 209 } 210 } 211 212 private void declareIntrinsicProperty(Name className, Name methodName, IntrinsicMethod implementation) { 213 intrinsicsMap.registerIntrinsic(KotlinBuiltIns.getInstance().getBuiltInsPackageFqName().child(className), methodName, -1, implementation); 214 } 215 216 private void declareIntrinsicFunction(Name className, Name functionName, int arity, IntrinsicMethod implementation) { 217 intrinsicsMap.registerIntrinsic(KotlinBuiltIns.getInstance().getBuiltInsPackageFqName().child(className), functionName, arity, implementation); 218 } 219 220 @Nullable 221 public IntrinsicMethod getIntrinsic(@NotNull CallableMemberDescriptor descriptor) { 222 IntrinsicMethod intrinsicMethod = intrinsicsMap.getIntrinsic(descriptor); 223 if (intrinsicMethod != null) { 224 return intrinsicMethod; 225 } 226 227 if (descriptor instanceof SimpleFunctionDescriptor) { 228 SimpleFunctionDescriptor functionDescriptor = (SimpleFunctionDescriptor) descriptor; 229 230 if (isEnumClassObject(functionDescriptor.getContainingDeclaration())) { 231 if (isEnumValuesMethod(functionDescriptor)) { 232 return ENUM_VALUES; 233 } 234 235 if (isEnumValueOfMethod(functionDescriptor)) { 236 return ENUM_VALUE_OF; 237 } 238 } 239 } 240 241 List<AnnotationDescriptor> annotations = descriptor.getAnnotations(); 242 if (annotations != null) { 243 for (AnnotationDescriptor annotation : annotations) { 244 ClassifierDescriptor classifierDescriptor = annotation.getType().getConstructor().getDeclarationDescriptor(); 245 assert classifierDescriptor != null; 246 if ("Intrinsic".equals(classifierDescriptor.getName().asString())) { 247 String value = (String) annotation.getAllValueArguments().values().iterator().next().getValue(); 248 intrinsicMethod = namedMethods.get(value); 249 if (intrinsicMethod != null) { 250 break; 251 } 252 } 253 } 254 } 255 return intrinsicMethod; 256 } 257 }