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.intellij.psi.PsiElement;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.org.objectweb.asm.Type;
022 import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter;
023 import org.jetbrains.jet.codegen.ExpressionCodegen;
024 import org.jetbrains.jet.codegen.StackValue;
025 import org.jetbrains.jet.lang.psi.JetBinaryExpression;
026 import org.jetbrains.jet.lang.psi.JetExpression;
027
028 import java.util.List;
029
030 import static org.jetbrains.org.objectweb.asm.Type.*;
031
032 public class RangeTo extends IntrinsicMethod {
033 @NotNull
034 @Override
035 public Type generateImpl(
036 @NotNull ExpressionCodegen codegen,
037 @NotNull InstructionAdapter v,
038 @NotNull Type returnType,
039 PsiElement element,
040 List<JetExpression> arguments,
041 StackValue receiver
042 ) {
043 v.anew(returnType);
044 v.dup();
045
046 Type type;
047 if (arguments.size() == 1) {
048 assert receiver instanceof StackValue.CallReceiver :
049 "Receiver in an intrinsic qualified expression should be CallReceiver: " + receiver + " on " + element.getText();
050 type = parameterType(receiver.type, codegen.expressionType(arguments.get(0)));
051 receiver.put(type, v);
052 codegen.gen(arguments.get(0), type);
053 }
054 else {
055 JetBinaryExpression expression = (JetBinaryExpression) element;
056 type = parameterType(codegen.expressionType(expression.getLeft()), codegen.expressionType(expression.getRight()));
057 codegen.gen(expression.getLeft(), type);
058 codegen.gen(expression.getRight(), type);
059 }
060
061 v.invokespecial(returnType.getInternalName(), "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, type, type));
062
063 return returnType;
064 }
065
066 @NotNull
067 private static Type parameterType(@NotNull Type leftType, @NotNull Type rightType) {
068 int left = leftType.getSort();
069 int right = rightType.getSort();
070 if (left == DOUBLE || right == DOUBLE) {
071 return DOUBLE_TYPE;
072 }
073 else if (left == FLOAT || right == FLOAT) {
074 return FLOAT_TYPE;
075 }
076 else if (left == LONG || right == LONG) {
077 return LONG_TYPE;
078 }
079 else if (left == INT || right == INT) {
080 return INT_TYPE;
081 }
082 else if (left == SHORT || right == SHORT) {
083 return SHORT_TYPE;
084 }
085 else if (left == CHAR || right == CHAR) {
086 return CHAR_TYPE;
087 }
088 else if (left == BYTE || right == BYTE) {
089 return BYTE_TYPE;
090 }
091 else {
092 throw new IllegalStateException("RangeTo intrinsic can only work for primitive types: " + leftType + ", " + rightType);
093 }
094 }
095 }